Skip to content

Commit

Permalink
Merge pull request #23 from fedora-python/backport_fixes
Browse files Browse the repository at this point in the history
Backport fixes from bpo-39769
  • Loading branch information
frenzymadness authored Mar 3, 2020
2 parents c316b78 + e8099f2 commit 475919c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions compileall2.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ def compile_dir(dir, maxlevels=None, ddir=None, force=False,
hardlink_dupes: hardlink duplicated pyc files
"""
ProcessPoolExecutor = None
if ddir is not None and (stripdir is not None or prependdir is not None):
raise ValueError(("Destination dir (ddir) cannot be used "
"in combination with stripdir or prependdir"))
if ddir is not None:
stripdir = dir
prependdir = ddir
ddir = None
if workers is not None:
if workers < 0:
raise ValueError('workers must be greater or equal to 0')
Expand Down
55 changes: 55 additions & 0 deletions test_compileall2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import compileall2 as compileall
import importlib.util
import test.test_importlib.util
import marshal
import os
import pathlib
import py_compile
Expand Down Expand Up @@ -39,6 +40,19 @@ def _optim_args_from_interpreter_flags():
args.append('-' + 'O' * value)
return args

# Backported from Lib/test/test_importlib/util.py
# Added in: https://github.com/python/cpython/pull/18676/files
def get_code_from_pyc(pyc_path):
"""Reads a pyc file and returns the unmarshalled code object within.
No header validation is performed.
"""
with open(pyc_path, 'rb') as pyc_f:
if compileall.PY37:
pyc_f.seek(16)
else:
pyc_f.seek(12)
return marshal.load(pyc_f)

# Use the original one for Python > 3.5 and backported function otherwise
if compileall.PY36:
optim_args_from_interpreter_flags = support.optim_args_from_interpreter_flags
Expand Down Expand Up @@ -274,6 +288,47 @@ def test_compile_dir_maxlevels(self):
compileall.compile_dir(self.directory, quiet=True, maxlevels=depth)
self.assertTrue(os.path.isfile(pyc_filename))

def _test_ddir_only(self, *, ddir, parallel=True):
"""Recursive compile_dir ddir must contain package paths; bpo39769."""
fullpath = ["test", "foo"]
path = self.directory
mods = []
for subdir in fullpath:
path = os.path.join(path, subdir)
os.mkdir(path)
script_helper.make_script(path, "__init__", "")
mods.append(script_helper.make_script(path, "mod",
"def fn(): 1/0\nfn()\n"))
compileall.compile_dir(
self.directory, quiet=True, ddir=ddir,
workers=2 if parallel else 1)
self.assertTrue(mods)
for mod in mods:
self.assertTrue(mod.startswith(self.directory), mod)
modcode = importlib.util.cache_from_source(mod)
modpath = mod[len(self.directory+os.sep):]
_, _, err = script_helper.assert_python_failure(modcode)
expected_in = os.path.join(ddir, modpath)
mod_code_obj = get_code_from_pyc(modcode)
self.assertEqual(mod_code_obj.co_filename, expected_in)
self.assertIn('"{}"'.format(expected_in), os.fsdecode(err))

def test_ddir_only_one_worker(self):
"""Recursive compile_dir ddir= contains package paths; bpo39769."""
return self._test_ddir_only(ddir="<a prefix>", parallel=False)

def test_ddir_multiple_workers(self):
"""Recursive compile_dir ddir= contains package paths; bpo39769."""
return self._test_ddir_only(ddir="<a prefix>", parallel=True)

def test_ddir_empty_only_one_worker(self):
"""Recursive compile_dir ddir='' contains package paths; bpo39769."""
return self._test_ddir_only(ddir="", parallel=False)

def test_ddir_empty_multiple_workers(self):
"""Recursive compile_dir ddir='' contains package paths; bpo39769."""
return self._test_ddir_only(ddir="", parallel=True)

def test_strip_only(self):
fullpath = ["test", "build", "real", "path"]
path = os.path.join(self.directory, *fullpath)
Expand Down

0 comments on commit 475919c

Please sign in to comment.