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

Created on 2019-11-07 12:06 by nedbat, last changed 2019-11-07 12:11 by vstinner.

Messages (2)
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.
History
Date User Action Args
2019-11-07 12:11:41vstinnersetnosy: + vstinner
messages: + msg356189
2019-11-07 12:06:03nedbatcreate