Skip to content

Commit ccbf853

Browse files
gh-91181: drop support for bytes on sys.path (GH-31934)
Support for bytes broke sometime between Python 3.2 and 3.6 and has been broken ever since. Trying to bring back supports is surprisingly difficult in the face of -b and checking for keys in sys.path_importer_cache. Since the support was broken for so long, trying to overcome the difficulty of bringing back the support has been deemed not worth it. Co-authored-by: Eryk Sun <eryksun@gmail.com> Co-authored-by: Brett Cannon <brett@python.org> (cherry picked from commit 6da988a) Co-authored-by: Thomas Grainger <tagrain@gmail.com>
1 parent e121cb5 commit ccbf853

File tree

6 files changed

+8
-9
lines changed

6 files changed

+8
-9
lines changed

Doc/library/sys.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ always available.
11581158
line option or the :envvar:`PYTHONSAFEPATH` environment variable?
11591159

11601160
A program is free to modify this list for its own purposes. Only strings
1161-
and bytes should be added to :data:`sys.path`; all other data types are
1161+
should be added to :data:`sys.path`; all other data types are
11621162
ignored during import.
11631163

11641164

Doc/reference/import.rst

+2-4
Original file line numberDiff line numberDiff line change
@@ -800,10 +800,8 @@ environment variable and various other installation- and
800800
implementation-specific defaults. Entries in :data:`sys.path` can name
801801
directories on the file system, zip files, and potentially other "locations"
802802
(see the :mod:`site` module) that should be searched for modules, such as
803-
URLs, or database queries. Only strings and bytes should be present on
804-
:data:`sys.path`; all other data types are ignored. The encoding of bytes
805-
entries is determined by the individual :term:`path entry finders <path entry
806-
finder>`.
803+
URLs, or database queries. Only strings should be present on
804+
:data:`sys.path`; all other data types are ignored.
807805

808806
The :term:`path based finder` is a :term:`meta path finder`, so the import
809807
machinery begins the :term:`import path` search by calling the path

Lib/importlib/_bootstrap_external.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ def _get_spec(cls, fullname, path, target=None):
14681468
# the list of paths that will become its __path__
14691469
namespace_path = []
14701470
for entry in path:
1471-
if not isinstance(entry, (str, bytes)):
1471+
if not isinstance(entry, str):
14721472
continue
14731473
finder = cls._path_importer_cache(entry)
14741474
if finder is not None:

Lib/test/test_zipimport.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,8 @@ def testBytesPath(self):
758758
z.writestr(zinfo, test_src)
759759

760760
zipimport.zipimporter(filename)
761-
zipimport.zipimporter(os.fsencode(filename))
761+
with self.assertRaises(TypeError):
762+
zipimport.zipimporter(os.fsencode(filename))
762763
with self.assertRaises(TypeError):
763764
zipimport.zipimporter(bytearray(os.fsencode(filename)))
764765
with self.assertRaises(TypeError):

Lib/zipimport.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ class zipimporter(_bootstrap_external._LoaderBasics):
6363
# if found, or else read it from the archive.
6464
def __init__(self, path):
6565
if not isinstance(path, str):
66-
import os
67-
path = os.fsdecode(path)
66+
raise TypeError(f"expected str, not {type(path)!r}")
6867
if not path:
6968
raise ZipImportError('archive path is empty', path=path)
7069
if alt_path_sep:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Drop support for :class:`bytes` on :attr:`sys.path`.

0 commit comments

Comments
 (0)