From 8b9ffe03ce628a4112f8f9311a2309f95f536570 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 16 Mar 2022 14:13:42 +0000 Subject: [PATCH 1/6] drop support for bytes on sys.path --- Doc/library/sys.rst | 2 +- Doc/reference/import.rst | 6 ++---- Lib/importlib/_bootstrap_external.py | 2 +- Lib/zipimport.py | 3 --- 4 files changed, 4 insertions(+), 9 deletions(-) diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index b83b1167e8aad5..54665418f56697 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -1125,7 +1125,7 @@ always available. the entries inserted as a result of :envvar:`PYTHONPATH`. A program is free to modify this list for its own purposes. Only strings - and bytes should be added to :data:`sys.path`; all other data types are + and should be added to :data:`sys.path`; all other data types are ignored during import. diff --git a/Doc/reference/import.rst b/Doc/reference/import.rst index 66737c698ae90d..71fefc9cecb2e8 100644 --- a/Doc/reference/import.rst +++ b/Doc/reference/import.rst @@ -801,10 +801,8 @@ environment variable and various other installation- and implementation-specific defaults. Entries in :data:`sys.path` can name directories on the file system, zip files, and potentially other "locations" (see the :mod:`site` module) that should be searched for modules, such as -URLs, or database queries. Only strings and bytes should be present on -:data:`sys.path`; all other data types are ignored. The encoding of bytes -entries is determined by the individual :term:`path entry finders `. +URLs, or database queries. Only strings should be present on +:data:`sys.path`; all other data types are ignored. The :term:`path based finder` is a :term:`meta path finder`, so the import machinery begins the :term:`import path` search by calling the path diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index a6f0a1b3c4c7d8..487bac443c1718 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -1457,7 +1457,7 @@ def _get_spec(cls, fullname, path, target=None): # the list of paths that will become its __path__ namespace_path = [] for entry in path: - if not isinstance(entry, (str, bytes)): + if not isinstance(entry, str): continue finder = cls._path_importer_cache(entry) if finder is not None: diff --git a/Lib/zipimport.py b/Lib/zipimport.py index 25eaee9c0f291b..dfd1a2366b2af7 100644 --- a/Lib/zipimport.py +++ b/Lib/zipimport.py @@ -62,9 +62,6 @@ class zipimporter(_bootstrap_external._LoaderBasics): # entry in sys.path_importer_cache, fetch the file directory from there # if found, or else read it from the archive. def __init__(self, path): - if not isinstance(path, str): - import os - path = os.fsdecode(path) if not path: raise ZipImportError('archive path is empty', path=path) if alt_path_sep: From fc48cef1f128a2b7d505f59d6c3da0452f314edf Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 16 Mar 2022 14:24:16 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2022-03-16-14-24-14.bpo-47025.qtT3CE.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-03-16-14-24-14.bpo-47025.qtT3CE.rst diff --git a/Misc/NEWS.d/next/Library/2022-03-16-14-24-14.bpo-47025.qtT3CE.rst b/Misc/NEWS.d/next/Library/2022-03-16-14-24-14.bpo-47025.qtT3CE.rst new file mode 100644 index 00000000000000..51bd11a4c92f06 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-03-16-14-24-14.bpo-47025.qtT3CE.rst @@ -0,0 +1 @@ +drop support for bytes on sys.path From 5aad672cfba73bd753dc5ee657fd897481ac2547 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 16 Mar 2022 14:43:04 +0000 Subject: [PATCH 3/6] Update Doc/library/sys.rst Co-authored-by: Eryk Sun --- Doc/library/sys.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index 54665418f56697..ead75771a8b361 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -1125,7 +1125,7 @@ always available. the entries inserted as a result of :envvar:`PYTHONPATH`. A program is free to modify this list for its own purposes. Only strings - and should be added to :data:`sys.path`; all other data types are + should be added to :data:`sys.path`; all other data types are ignored during import. From 0f024c5f4d06376ade8cec5aebe6867b9a914186 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 16 Mar 2022 15:06:11 +0000 Subject: [PATCH 4/6] test that zipimport fails with bytes paths --- Lib/test/test_zipimport.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index 85dbf4d8f68eb6..784dae628ea9cd 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -755,7 +755,8 @@ def testBytesPath(self): z.writestr(zinfo, test_src) zipimport.zipimporter(filename) - zipimport.zipimporter(os.fsencode(filename)) + with self.assertRaises(TypeError): + zipimport.zipimporter(os.fsencode(filename)) with self.assertRaises(TypeError): zipimport.zipimporter(bytearray(os.fsencode(filename))) with self.assertRaises(TypeError): From 8a76d18ae4d472fb87c2ec9e5ef1e0628f95e4ee Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 16 Mar 2022 17:18:44 +0000 Subject: [PATCH 5/6] raise a TypeError if zipimport is called with a non-str --- Lib/zipimport.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/zipimport.py b/Lib/zipimport.py index dfd1a2366b2af7..225bd214d627c5 100644 --- a/Lib/zipimport.py +++ b/Lib/zipimport.py @@ -62,6 +62,8 @@ class zipimporter(_bootstrap_external._LoaderBasics): # entry in sys.path_importer_cache, fetch the file directory from there # if found, or else read it from the archive. def __init__(self, path): + if not isinstance(path, str): + raise TypeError(f"expected str, not {type(path)!r}") if not path: raise ZipImportError('archive path is empty', path=path) if alt_path_sep: From 60168b2df0a1ee858699422e1e43d067f4b9aaea Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sat, 16 Jul 2022 17:12:05 -0700 Subject: [PATCH 6/6] Update Misc/NEWS.d/next/Library/2022-03-16-14-24-14.bpo-47025.qtT3CE.rst --- .../next/Library/2022-03-16-14-24-14.bpo-47025.qtT3CE.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-03-16-14-24-14.bpo-47025.qtT3CE.rst b/Misc/NEWS.d/next/Library/2022-03-16-14-24-14.bpo-47025.qtT3CE.rst index 51bd11a4c92f06..1c7c7ace9706d7 100644 --- a/Misc/NEWS.d/next/Library/2022-03-16-14-24-14.bpo-47025.qtT3CE.rst +++ b/Misc/NEWS.d/next/Library/2022-03-16-14-24-14.bpo-47025.qtT3CE.rst @@ -1 +1 @@ -drop support for bytes on sys.path +Drop support for :class:`bytes` on :attr:`sys.path`.