https://github.com/python/cpython/commit/7de5d02b3052f47f3efdc712ab097f680fe8f232
commit: 7de5d02b3052f47f3efdc712ab097f680fe8f232
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: encukou <[email protected]>
date: 2025-09-18T15:37:39+02:00
summary:

[3.13] gh-82916: Don't fail when importing from / with sys.pycache_prefix set 
(GH-30456) (GH-137905)


(cherry picked from commit d8a9466e29f59f81aacf1959b835862bcd47b731)

Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>

files:
A Misc/NEWS.d/next/Library/2022-01-07-16-56-57.bpo-38735.NFfJX6.rst
M Lib/importlib/_bootstrap_external.py
M Lib/test/test_importlib/test_util.py

diff --git a/Lib/importlib/_bootstrap_external.py 
b/Lib/importlib/_bootstrap_external.py
index eb8fea1aad0919..c5e641773e6c5e 100644
--- a/Lib/importlib/_bootstrap_external.py
+++ b/Lib/importlib/_bootstrap_external.py
@@ -567,7 +567,8 @@ def cache_from_source(path, debug_override=None, *, 
optimization=None):
         # Strip initial drive from a Windows path. We know we have an absolute
         # path here, so the second part of the check rules out a POSIX path 
that
         # happens to contain a colon at the second character.
-        if head[1] == ':' and head[0] not in path_separators:
+        # Slicing avoids issues with an empty (or short) `head`.
+        if head[1:2] == ':' and head[0:1] not in path_separators:
             head = head[2:]
 
         # Strip initial path separator from `head` to complete the conversion
diff --git a/Lib/test/test_importlib/test_util.py 
b/Lib/test/test_importlib/test_util.py
index f3d042b21a7f8f..258b7acc7ce861 100644
--- a/Lib/test/test_importlib/test_util.py
+++ b/Lib/test/test_importlib/test_util.py
@@ -581,6 +581,18 @@ def 
test_cache_from_source_respects_pycache_prefix_relative(self):
                 self.util.cache_from_source(path, optimization=''),
                 os.path.normpath(expect))
 
+    @unittest.skipIf(sys.implementation.cache_tag is None,
+                     'requires sys.implementation.cache_tag to not be None')
+    def test_cache_from_source_in_root_with_pycache_prefix(self):
+        # Regression test for gh-82916
+        pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
+        path = 'qux.py'
+        expect = os.path.join(os.path.sep, 'tmp', 'bytecode',
+                              f'qux.{self.tag}.pyc')
+        with util.temporary_pycache_prefix(pycache_prefix):
+            with os_helper.change_cwd('/'):
+                self.assertEqual(self.util.cache_from_source(path), expect)
+
     @unittest.skipIf(sys.implementation.cache_tag is None,
                      'requires sys.implementation.cache_tag to not be None')
     def test_source_from_cache_inside_pycache_prefix(self):
diff --git a/Misc/NEWS.d/next/Library/2022-01-07-16-56-57.bpo-38735.NFfJX6.rst 
b/Misc/NEWS.d/next/Library/2022-01-07-16-56-57.bpo-38735.NFfJX6.rst
new file mode 100644
index 00000000000000..7f4ea04284e10b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-01-07-16-56-57.bpo-38735.NFfJX6.rst
@@ -0,0 +1,2 @@
+Fix failure when importing a module from the root directory on unix-like
+platforms with sys.pycache_prefix set.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to