This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PYTHONPYCACHEPREFIX fails when importing a module from the root ("/")
Type: Stage: patch review
Components: Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: nedbat, petr.viktorin, vstinner
Priority: normal Keywords: patch

Created on 2019-11-07 12:06 by nedbat, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 30456 open petr.viktorin, 2022-01-07 15:53
Messages (3)
msg356188 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2019-11-07 12:06
On the manylinux docker images, the current directory is "/".

If I create a file there and try to import it with PYTHONPYCACHEPREFIX set, I get a stack trace:

$ docker run -it --init --rm -v `pwd`:/io quay.io/pypa/manylinux1_x86_64 bash
[29d75403d7d1">root@29d75403d7d1 /]# cat > foo.py
a = 1
[29d75403d7d1">root@29d75403d7d1 /]# /opt/python/cp38-cp38/bin/python
Python 3.8.0 (default, Oct 30 2019, 22:13:22)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
>>> foo.a
1
>>>
[29d75403d7d1">root@29d75403d7d1 /]# PYTHONPYCACHEPREFIX=/opt/pyc /opt/python/cp38-cp38/bin/python
Python 3.8.0 (default, Oct 30 2019, 22:13:22)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 657, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 562, in module_from_spec
  File "<frozen importlib._bootstrap>", line 541, in _init_module_attrs
  File "<frozen importlib._bootstrap>", line 382, in cached
  File "<frozen importlib._bootstrap_external>", line 427, in _get_cached
  File "<frozen importlib._bootstrap_external>", line 352, in cache_from_source
IndexError: string index out of range
>>>
msg356189 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-11-07 12:11
The bug occurs on this line:

    if sys.pycache_prefix is not None:
        if not _path_isabs(head):
            head = _path_join(_os.getcwd(), head)
        if head[1] == ':' and head[0] not in path_separators: # <==== HERE
            head = head[2:]
        ...

I guess that head="/" in your case: so head[1] raises an IndexError.
msg409977 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2022-01-07 15:59
Here's a patch, but testing it would require putting a file in the root directory. I don't dare do that from the test suite. Is one-time manual testing OK here?
History
Date User Action Args
2022-04-11 14:59:22adminsetgithub: 82916
2022-01-07 15:59:18petr.viktorinsetmessages: + msg409977
2022-01-07 15:53:55petr.viktorinsetkeywords: + patch
nosy: + petr.viktorin

pull_requests: + pull_request28660
stage: patch review
2019-11-07 12:11:41vstinnersetnosy: + vstinner
messages: + msg356189
2019-11-07 12:06:03nedbatcreate