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').
This commit is contained in:
hetmankp 2023-10-24 00:55:39 +11:00 committed by GitHub
parent 52e902ccf0
commit 3726cb0f14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 6 deletions

View file

@ -362,6 +362,29 @@ class CompileallTestsBase:
str(err, encoding=sys.getdefaultencoding())
)
def test_strip_only_invalid(self):
fullpath = ["test", "build", "real", "path"]
path = os.path.join(self.directory, *fullpath)
os.makedirs(path)
script = script_helper.make_script(path, "test", "1 / 0")
bc = importlib.util.cache_from_source(script)
stripdir = os.path.join(self.directory, *(fullpath[:2] + ['fake']))
compileall.compile_dir(path, quiet=True, stripdir=stripdir)
rc, out, err = script_helper.assert_python_failure(bc)
expected_not_in = os.path.join(self.directory, *fullpath[2:])
self.assertIn(
path,
str(err, encoding=sys.getdefaultencoding())
)
self.assertNotIn(
expected_not_in,
str(err, encoding=sys.getdefaultencoding())
)
self.assertNotIn(
stripdir,
str(err, encoding=sys.getdefaultencoding())
)
def test_prepend_only(self):
fullpath = ["test", "build", "real", "path"]
path = os.path.join(self.directory, *fullpath)