8000 gh-105931: Fix surprising compileall stripdir behaviour (GH-108671) · python/cpython@3726cb0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3726cb0

Browse files
authored
gh-105931: Fix surprising compileall stripdir behaviour (GH-108671)
Before, the '-s STRIPDIR' option on compileall lead to some surprising results as it only strips away path components that match, but leaves alone the non-matching ones interspersed in between. For example, with: python -m compileall -s/path/to/another/src /path/to/build/src/file.py The resulting written path will be: build/file.py This fix only strips directories that are a fully matching prefix of the source path. If a stripdir is provided that is not a valid prefix, a warning will be displayed (which can be silenced with '-qq').
1 parent 52e902c commit 3726cb0

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

Lib/compileall.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
172172
if stripdir is not None:
173173
fullname_parts = fullname.split(os.path.sep)
174174
stripdir_parts = stripdir.split(os.path.sep)
175-
ddir_parts = list(fullname_parts)
176175

177-
for spart, opart in zip(stripdir_parts, fullname_parts):
178-
if spart == opart:
179-
ddir_parts.remove(spart)
180-
181-
dfile = os.path.join(*ddir_parts)
176+
if stripdir_parts != fullname_parts[:len(stripdir_parts)]:
177+
if quiet < 2:
178+
print("The stripdir path {!r} is not a valid prefix for "
179+
"source path {!r}; ignoring".format(stripdir, fullname))
180+
else:
181+
dfile = os.path.join(*fullname_parts[len(stripdir_parts):])
182182

183183
if prependdir is not None:
184184
if dfile is None:

Lib/test/test_compileall.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,29 @@ def test_strip_only(self):
362362
str(err, encoding=sys.getdefaultencoding())
363363
)
364364

365+
def test_strip_only_invalid(self):
366+
fullpath = ["test", "build", "real", "path"]
367+
path = os.path.join(self.directory, *fullpath)
368+
os.makedirs(path)
369+
script = script_helper.make_script(path, "test", "1 / 0")
370+
bc = importlib.util.cache_from_source(script)
371+
stripdir = os.path.join(self.directory, *(fullpath[:2] + ['fake']))
372+
compileall.compile_dir(path, quiet=True, stripdir=stripdir)
373+
rc, out, err = script_helper.assert_python_failure(bc)
374+
expected_not_in = os.path.join(self.directory, *fullpath[2:])
375+
self.assertIn(
376+
path,
377+
str(err, encoding=sys.getdefaultencoding())
378+
)
379+
self.assertNotIn(
380+
expected_not_in,
381+
str(err, encoding=sys.getdefaultencoding())
382+
)
383+
self.assertNotIn(
384+
stripdir,
385+
str(err, encoding=sys.getdefaultencoding())
386+
)
387+
365388
def test_prepend_only(self):
366389
fullpath = ["test", "build", "real", "path"]
367390
path = os.path.join(self.directory, *fullpath)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Change :mod:`compileall` to only strip the stripdir prefix from the full path
2+
recorded in the compiled ``.pyc`` file, when the prefix matches the start of
3+
the full path in its entirety. When the prefix does not match, no stripping is
4+
performed and a warning to this effect is displayed.
5+
6+
Previously all path components of the stripdir prefix that matched the full
7+
path were removed, while those that did not match were left alone (including
8+
ones interspersed between matching components).

0 commit comments

Comments
 (0)
0