Comment 5 for bug 1882535

Revision history for this message
Dmitrii Shcherbakov (dmitriis) wrote :

source_to_code is used to load the code object which has a source path specified:

https://github.com/python/cpython/blob/0f5a28f834bdac2da8a04597dc0fc5b71e50da9d/Lib/py_compile.py#L144-L145
https://docs.python.org/3/library/importlib.html#importlib.abc.InspectLoader.source_to_code
"The path argument should be the “path” to where the source code originated from, which can be an abstract concept (e.g. location in a zip file)."

Seems to be the reason for adding a path to the .pyc file:
https://docs.python.org/3/library/compileall.html#cmdoption-compileall-d
"This will appear in compilation time tracebacks, and is also compiled in to the byte-code file, where it will be used in tracebacks and other messages in cases where the source file does not exist at the time the byte-code file is executed."

The path specified in the second argument to source_to_code is included into the dump of a code object:

python -m py_compile ./bdist_egg.py
(Pdb) cfile
'./__pycache__/bdist_egg.cpython-38.pyc'
(Pdb) dfile
(Pdb) file
'./bdist_egg.py'

# vs

pip3 install -t ./ setuptools
(Pdb) cfile
'/tmp/pip-unpacked-wheel-79blnlyq/setuptools/command/__pycache__/bdist_egg.cpython-38.pyc'
(Pdb) dfile
(Pdb) file
'/tmp/pip-unpacked-wheel-79blnlyq/setuptools/command/bdist_egg.py'

(Pdb) '/tmp/pip-unpacked' in str(marshal.dumps(loader.source_to_code(source_bytes, dfile or file, _optimize=optimize)))
True

(Pdb) '/tmp/pip-unpacked' in str(marshal.dumps(loader.source_to_code(source_bytes, './setuptools/command/bdist_egg.py', _optimize=optimize)))
False

(Pdb) './setuptools/command/bdist_egg.py' in str(marshal.dumps(loader.source_to_code(source_bytes, './setuptools/command/bdist_egg.py', _optimize=optimize)))
True

The absolute paths are passed in from pip:
https://github.com/pypa/pip/blob/20.0.2/src/pip/_vendor/distlib/util.py#L596