Skip to content

Commit 6ce06c7

Browse files
committed
Remove absolute paths from compiled modules
.pyc files store the absolute paths of their original .py files for displaying on tracebacks. See: https://stackoverflow.com/questions/11191680/why-do-python-pyc-files-contain-the-absolute-path-of-their-source-code https://bugs.python.org/issue1051638 The `ddir` option, documented on the link below, can override the prepended path: http://docs.python.org/library/compileall#cmdoption-compileall-d Avoids exposing the username on the compiling machine in tracebacks in case an exception occurs. For record, here's how PyInstaller dealt with this problem: pyinstaller/pyinstaller#1059
1 parent e666d2c commit 6ce06c7

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

datafiles/Data/Python/package.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
LIB_PATH = Path(ENV_PATH, "Lib", "site-packages")
2727
OUT_PATH = Path("Lib", "site-packages")
2828
ZIP_PATH = Path("Lib", "site-packages.zip")
29+
REL_PATH = Path("Data", "Python", OUT_PATH)
2930

3031

3132
def pack_filter(path):
@@ -74,6 +75,18 @@ def main():
7475
for path in os.listdir(LIB_PATH):
7576
lib_name = os.path.basename(path)
7677
lib_path = Path(LIB_PATH, lib_name)
78+
79+
# Pre-compile all modules without absolute paths
80+
path_prefix = REL_PATH
81+
if os.path.isdir(lib_path):
82+
compileall.compile_dir(
83+
lib_path, ddir=path_prefix, force=True, quiet=2, legacy=True
84+
)
85+
else:
86+
compileall.compile_file(
87+
str(lib_path), ddir=path_prefix, force=True, quiet=2, legacy=True
88+
)
89+
7790
try:
7891
zip_module.writepy(lib_path, filterfunc=pack_filter)
7992
except RuntimeError: # only directories or .py files accepted
@@ -90,7 +103,9 @@ def main():
90103
inpath = Path(LIB_PATH, lib_name)
91104
outpath = Path(OUT_PATH, lib_name)
92105
shutil.copytree(inpath, outpath, ignore=copy_filter)
93-
compileall.compile_dir(outpath, force=True, quiet=True, legacy=True)
106+
compileall.compile_dir(
107+
outpath, ddir=REL_PATH, force=True, quiet=True, legacy=True
108+
)
94109
# Remove corresponding .py files
95110
for root, _, files in os.walk(outpath):
96111
for filename in files:

0 commit comments

Comments
 (0)