bpo-38112: Compileall improvements (GH-16012)

* Raise the limit of maximum path depth to actual  recursion limit

* Add posibilities to adjust a path compiled in .pyc  file.

Now, you can:
- Strip a part of path from a beggining of path into compiled file
   example "-s /test /test/build/real/test.py" → "build/real/test.py"
- Append some new path to a beggining of path into compiled file
   example "-p /boo real/test.py" → "/boo/real/test.py"

You can also use both options in the same time. In that case,
striping is done before appending.

* Add a possibility to specify multiple optimization levels

Each optimization level then leads to separated compiled file.
Use `action='append'` instead of `nargs='+'` for the -o option.
Instead of `-o 0 1 2`, specify `-o 0 -o 1 -o 2`. It's more to type,
but much more explicit.

* Add a symlinks limitation feature

This feature allows us to limit byte-compilation of symbolic
links if they are pointing outside specified dir (build root
for example).
This commit is contained in:
Lumír 'Frenzy' Balhar 2019-09-26 08:28:26 +02:00 committed by Petr Viktorin
parent 52b9408038
commit 8e7bb991de
4 changed files with 386 additions and 39 deletions

View File

@ -52,6 +52,13 @@ compile Python sources.
cases where the source file does not exist at the time the byte-code file is cases where the source file does not exist at the time the byte-code file is
executed. executed.
.. cmdoption:: -s strip_prefix
.. cmdoption:: -p prepend_prefix
Remove (``-s``) or append (``-p``) the given prefix of paths
recorded in the ``.pyc`` files.
Cannot be combined with ``-d``.
.. cmdoption:: -x regex .. cmdoption:: -x regex
regex is used to search the full path to each file considered for regex is used to search the full path to each file considered for
@ -96,6 +103,16 @@ compile Python sources.
variable is not set, and ``checked-hash`` if the ``SOURCE_DATE_EPOCH`` variable is not set, and ``checked-hash`` if the ``SOURCE_DATE_EPOCH``
environment variable is set. environment variable is set.
.. cmdoption:: -o level
Compile with the given optimization level. May be used multiple times
to compile for multiple levels at a time (for example,
``compileall -o 1 -o 2``).
.. cmdoption:: -e dir
Ignore symlinks pointing outside the given directory.
.. versionchanged:: 3.2 .. versionchanged:: 3.2
Added the ``-i``, ``-b`` and ``-h`` options. Added the ``-i``, ``-b`` and ``-h`` options.
@ -107,6 +124,12 @@ compile Python sources.
.. versionchanged:: 3.7 .. versionchanged:: 3.7
Added the ``--invalidation-mode`` option. Added the ``--invalidation-mode`` option.
.. versionchanged:: 3.9
Added the ``-s``, ``-p``, ``-e`` options.
Raised the default recursion limit from 10 to
:py:func:`sys.getrecursionlimit()`.
Added the possibility to specify the ``-o`` option multiple times.
There is no command-line option to control the optimization level used by the There is no command-line option to control the optimization level used by the
:func:`compile` function, because the Python interpreter itself already :func:`compile` function, because the Python interpreter itself already
@ -120,7 +143,7 @@ runtime.
Public functions Public functions
---------------- ----------------
.. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1, invalidation_mode=None) .. function:: compile_dir(dir, maxlevels=sys.getrecursionlimit(), ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1, invalidation_mode=None, stripdir=None, prependdir=None, limit_sl_dest=None)
Recursively descend the directory tree named by *dir*, compiling all :file:`.py` Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
files along the way. Return a true value if all the files compiled successfully, files along the way. Return a true value if all the files compiled successfully,
@ -166,6 +189,10 @@ Public functions
:class:`py_compile.PycInvalidationMode` enum and controls how the generated :class:`py_compile.PycInvalidationMode` enum and controls how the generated
pycs are invalidated at runtime. pycs are invalidated at runtime.
The *stripdir*, *prependdir* and *limit_sl_dest* arguments correspond to
the ``-s``, ``-p`` and ``-e`` options described above.
They may be specified as ``str``, ``bytes`` or :py:class:`os.PathLike`.
.. versionchanged:: 3.2 .. versionchanged:: 3.2
Added the *legacy* and *optimize* parameter. Added the *legacy* and *optimize* parameter.
@ -191,6 +218,9 @@ Public functions
.. versionchanged:: 3.8 .. versionchanged:: 3.8
Setting *workers* to 0 now chooses the optimal number of cores. Setting *workers* to 0 now chooses the optimal number of cores.
.. versionchanged:: 3.9
Added *stripdir*, *prependdir* and *limit_sl_dest* arguments.
.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, invalidation_mode=None) .. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, invalidation_mode=None)
Compile the file with path *fullname*. Return a true value if the file Compile the file with path *fullname*. Return a true value if the file
@ -223,6 +253,10 @@ Public functions
:class:`py_compile.PycInvalidationMode` enum and controls how the generated :class:`py_compile.PycInvalidationMode` enum and controls how the generated
pycs are invalidated at runtime. pycs are invalidated at runtime.
The *stripdir*, *prependdir* and *limit_sl_dest* arguments correspond to
the ``-s``, ``-p`` and ``-e`` options described above.
They may be specified as ``str``, ``bytes`` or :py:class:`os.PathLike`.
.. versionadded:: 3.2 .. versionadded:: 3.2
.. versionchanged:: 3.5 .. versionchanged:: 3.5
@ -238,6 +272,9 @@ Public functions
.. versionchanged:: 3.7.2 .. versionchanged:: 3.7.2
The *invalidation_mode* parameter's default value is updated to None. The *invalidation_mode* parameter's default value is updated to None.
.. versionchanged:: 3.9
Added *stripdir*, *prependdir* and *limit_sl_dest* arguments.
.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1, invalidation_mode=None) .. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1, invalidation_mode=None)
Byte-compile all the :file:`.py` files found along ``sys.path``. Return a Byte-compile all the :file:`.py` files found along ``sys.path``. Return a

View File

@ -17,10 +17,13 @@ import py_compile
import struct import struct
from functools import partial from functools import partial
from pathlib import Path
RECURSION_LIMIT = sys.getrecursionlimit()
__all__ = ["compile_dir","compile_file","compile_path"] __all__ = ["compile_dir","compile_file","compile_path"]
def _walk_dir(dir, ddir=None, maxlevels=10, quiet=0): def _walk_dir(dir, maxlevels=RECURSION_LIMIT, quiet=0):
if quiet < 2 and isinstance(dir, os.PathLike): if quiet < 2 and isinstance(dir, os.PathLike):
dir = os.fspath(dir) dir = os.fspath(dir)
if not quiet: if not quiet:
@ -36,35 +39,39 @@ def _walk_dir(dir, ddir=None, maxlevels=10, quiet=0):
if name == '__pycache__': if name == '__pycache__':
continue continue
fullname = os.path.join(dir, name) fullname = os.path.join(dir, name)
if ddir is not None:
dfile = os.path.join(ddir, name)
else:
dfile = None
if not os.path.isdir(fullname): if not os.path.isdir(fullname):
yield fullname yield fullname
elif (maxlevels > 0 and name != os.curdir and name != os.pardir and elif (maxlevels > 0 and name != os.curdir and name != os.pardir and
os.path.isdir(fullname) and not os.path.islink(fullname)): os.path.isdir(fullname) and not os.path.islink(fullname)):
yield from _walk_dir(fullname, ddir=dfile, yield from _walk_dir(fullname, maxlevels=maxlevels - 1,
maxlevels=maxlevels - 1, quiet=quiet) quiet=quiet)
def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, def compile_dir(dir, maxlevels=RECURSION_LIMIT, ddir=None, force=False,
quiet=0, legacy=False, optimize=-1, workers=1, rx=None, quiet=0, legacy=False, optimize=-1, workers=1,
invalidation_mode=None): invalidation_mode=None, stripdir=None,
prependdir=None, limit_sl_dest=None):
"""Byte-compile all modules in the given directory tree. """Byte-compile all modules in the given directory tree.
Arguments (only dir is required): Arguments (only dir is required):
dir: the directory to byte-compile dir: the directory to byte-compile
maxlevels: maximum recursion level (default 10) maxlevels: maximum recursion level (default `sys.getrecursionlimit()`)
ddir: the directory that will be prepended to the path to the ddir: the directory that will be prepended to the path to the
file as it is compiled into each byte-code file. file as it is compiled into each byte-code file.
force: if True, force compilation, even if timestamps are up-to-date force: if True, force compilation, even if timestamps are up-to-date
quiet: full output with False or 0, errors only with 1, quiet: full output with False or 0, errors only with 1,
no output with 2 no output with 2
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
optimize: optimization level or -1 for level of the interpreter optimize: int or list of optimization levels or -1 for level of
the interpreter. Multiple levels leads to multiple compiled
files each with one optimization level.
workers: maximum number of parallel workers workers: maximum number of parallel workers
invalidation_mode: how the up-to-dateness of the pyc will be checked invalidation_mode: how the up-to-dateness of the pyc will be checked
stripdir: part of path to left-strip from source file path
prependdir: path to prepend to beggining of original file path, applied
after stripdir
limit_sl_dest: ignore symlinks if they are pointing outside of
the defined path
""" """
ProcessPoolExecutor = None ProcessPoolExecutor = None
if workers < 0: if workers < 0:
@ -76,8 +83,7 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
from concurrent.futures import ProcessPoolExecutor from concurrent.futures import ProcessPoolExecutor
except ImportError: except ImportError:
workers = 1 workers = 1
files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels, files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels)
ddir=ddir)
success = True success = True
if workers != 1 and ProcessPoolExecutor is not None: if workers != 1 and ProcessPoolExecutor is not None:
# If workers == 0, let ProcessPoolExecutor choose # If workers == 0, let ProcessPoolExecutor choose
@ -88,19 +94,25 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
rx=rx, quiet=quiet, rx=rx, quiet=quiet,
legacy=legacy, legacy=legacy,
optimize=optimize, optimize=optimize,
invalidation_mode=invalidation_mode), invalidation_mode=invalidation_mode,
stripdir=stripdir,
prependdir=prependdir,
limit_sl_dest=limit_sl_dest),
files) files)
success = min(results, default=True) success = min(results, default=True)
else: else:
for file in files: for file in files:
if not compile_file(file, ddir, force, rx, quiet, if not compile_file(file, ddir, force, rx, quiet,
legacy, optimize, invalidation_mode): legacy, optimize, invalidation_mode,
stripdir=stripdir, prependdir=prependdir,
limit_sl_dest=limit_sl_dest):
success = False success = False
return success return success
def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
legacy=False, optimize=-1, legacy=False, optimize=-1,
invalidation_mode=None): invalidation_mode=None, stripdir=None, prependdir=None,
limit_sl_dest=None):
"""Byte-compile one file. """Byte-compile one file.
Arguments (only fullname is required): Arguments (only fullname is required):
@ -112,32 +124,76 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
quiet: full output with False or 0, errors only with 1, quiet: full output with False or 0, errors only with 1,
no output with 2 no output with 2
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
optimize: optimization level or -1 for level of the interpreter optimize: int or list of optimization levels or -1 for level of
the interpreter. Multiple levels leads to multiple compiled
files each with one optimization level.
invalidation_mode: how the up-to-dateness of the pyc will be checked invalidation_mode: how the up-to-dateness of the pyc will be checked
stripdir: part of path to left-strip from source file path
prependdir: path to prepend to beggining of original file path, applied
after stripdir
limit_sl_dest: ignore symlinks if they are pointing outside of
the defined path.
""" """
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"))
success = True success = True
if quiet < 2 and isinstance(fullname, os.PathLike): if quiet < 2 and isinstance(fullname, os.PathLike):
fullname = os.fspath(fullname) fullname = os.fspath(fullname)
name = os.path.basename(fullname) name = os.path.basename(fullname)
dfile = None
if ddir is not None: if ddir is not None:
dfile = os.path.join(ddir, name) dfile = os.path.join(ddir, name)
else:
dfile = None if stripdir is not None:
fullname_parts = fullname.split(os.path.sep)
stripdir_parts = stripdir.split(os.path.sep)
ddir_parts = list(fullname_parts)
for spart, opart in zip(stripdir_parts, fullname_parts):
if spart == opart:
ddir_parts.remove(spart)
dfile = os.path.join(*ddir_parts)
if prependdir is not None:
if dfile is None:
dfile = os.path.join(prependdir, fullname)
else:
dfile = os.path.join(prependdir, dfile)
if isinstance(optimize, int):
optimize = [optimize]
if rx is not None: if rx is not None:
mo = rx.search(fullname) mo = rx.search(fullname)
if mo: if mo:
return success return success
if limit_sl_dest is not None and os.path.islink(fullname):
if Path(limit_sl_dest).resolve() not in Path(fullname).resolve().parents:
return success
opt_cfiles = {}
if os.path.isfile(fullname): if os.path.isfile(fullname):
if legacy: for opt_level in optimize:
cfile = fullname + 'c' if legacy:
else: opt_cfiles[opt_level] = fullname + 'c'
if optimize >= 0:
opt = optimize if optimize >= 1 else ''
cfile = importlib.util.cache_from_source(
fullname, optimization=opt)
else: else:
cfile = importlib.util.cache_from_source(fullname) if opt_level >= 0:
cache_dir = os.path.dirname(cfile) opt = opt_level if opt_level >= 1 else ''
cfile = (importlib.util.cache_from_source(
fullname, optimization=opt))
opt_cfiles[opt_level] = cfile
else:
cfile = importlib.util.cache_from_source(fullname)
opt_cfiles[opt_level] = cfile
head, tail = name[:-3], name[-3:] head, tail = name[:-3], name[-3:]
if tail == '.py': if tail == '.py':
if not force: if not force:
@ -145,18 +201,22 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
mtime = int(os.stat(fullname).st_mtime) mtime = int(os.stat(fullname).st_mtime)
expect = struct.pack('<4sll', importlib.util.MAGIC_NUMBER, expect = struct.pack('<4sll', importlib.util.MAGIC_NUMBER,
0, mtime) 0, mtime)
with open(cfile, 'rb') as chandle: for cfile in opt_cfiles.values():
actual = chandle.read(12) with open(cfile, 'rb') as chandle:
if expect == actual: actual = chandle.read(12)
if expect != actual:
break
else:
return success return success
except OSError: except OSError:
pass pass
if not quiet: if not quiet:
print('Compiling {!r}...'.format(fullname)) print('Compiling {!r}...'.format(fullname))
try: try:
ok = py_compile.compile(fullname, cfile, dfile, True, for opt_level, cfile in opt_cfiles.items():
optimize=optimize, ok = py_compile.compile(fullname, cfile, dfile, True,
invalidation_mode=invalidation_mode) optimize=opt_level,
invalidation_mode=invalidation_mode)
except py_compile.PyCompileError as err: except py_compile.PyCompileError as err:
success = False success = False
if quiet >= 2: if quiet >= 2:
@ -225,7 +285,7 @@ def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Utilities to support installing Python libraries.') description='Utilities to support installing Python libraries.')
parser.add_argument('-l', action='store_const', const=0, parser.add_argument('-l', action='store_const', const=0,
default=10, dest='maxlevels', default=RECURSION_LIMIT, dest='maxlevels',
help="don't recurse into subdirectories") help="don't recurse into subdirectories")
parser.add_argument('-r', type=int, dest='recursion', parser.add_argument('-r', type=int, dest='recursion',
help=('control the maximum recursion level. ' help=('control the maximum recursion level. '
@ -243,6 +303,20 @@ def main():
'compile-time tracebacks and in runtime ' 'compile-time tracebacks and in runtime '
'tracebacks in cases where the source file is ' 'tracebacks in cases where the source file is '
'unavailable')) 'unavailable'))
parser.add_argument('-s', metavar='STRIPDIR', dest='stripdir',
default=None,
help=('part of path to left-strip from path '
'to source file - for example buildroot. '
'`-d` and `-s` options cannot be '
'specified together.'))
parser.add_argument('-p', metavar='PREPENDDIR', dest='prependdir',
default=None,
help=('path to add as prefix to path '
'to source file - for example / to make '
'it absolute when some part is removed '
'by `-s` option. '
'`-d` and `-p` options cannot be '
'specified together.'))
parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None, parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None,
help=('skip files matching the regular expression; ' help=('skip files matching the regular expression; '
'the regexp is searched for in the full path ' 'the regexp is searched for in the full path '
@ -265,6 +339,12 @@ def main():
'"checked-hash" if the SOURCE_DATE_EPOCH ' '"checked-hash" if the SOURCE_DATE_EPOCH '
'environment variable is set, and ' 'environment variable is set, and '
'"timestamp" otherwise.')) '"timestamp" otherwise.'))
parser.add_argument('-o', action='append', type=int, dest='opt_levels',
help=('Optimization levels to run compilation with.'
'Default is -1 which uses optimization level of'
'Python interpreter itself (specified by -O).'))
parser.add_argument('-e', metavar='DIR', dest='limit_sl_dest',
help='Ignore symlinks pointing outsite of the DIR')
args = parser.parse_args() args = parser.parse_args()
compile_dests = args.compile_dest compile_dests = args.compile_dest
@ -273,12 +353,22 @@ def main():
import re import re
args.rx = re.compile(args.rx) args.rx = re.compile(args.rx)
if args.limit_sl_dest == "":
args.limit_sl_dest = None
if args.recursion is not None: if args.recursion is not None:
maxlevels = args.recursion maxlevels = args.recursion
else: else:
maxlevels = args.maxlevels maxlevels = args.maxlevels
if args.opt_levels is None:
args.opt_levels = [-1]
if args.ddir is not None and (
args.stripdir is not None or args.prependdir is not None
):
parser.error("-d cannot be used in combination with -s or -p")
# if flist is provided then load it # if flist is provided then load it
if args.flist: if args.flist:
try: try:
@ -303,13 +393,21 @@ def main():
if os.path.isfile(dest): if os.path.isfile(dest):
if not compile_file(dest, args.ddir, args.force, args.rx, if not compile_file(dest, args.ddir, args.force, args.rx,
args.quiet, args.legacy, args.quiet, args.legacy,
invalidation_mode=invalidation_mode): invalidation_mode=invalidation_mode,
stripdir=args.stripdir,
prependdir=args.prependdir,
optimize=args.opt_levels,
limit_sl_dest=args.limit_sl_dest):
success = False success = False
else: else:
if not compile_dir(dest, maxlevels, args.ddir, if not compile_dir(dest, maxlevels, args.ddir,
args.force, args.rx, args.quiet, args.force, args.rx, args.quiet,
args.legacy, workers=args.workers, args.legacy, workers=args.workers,
invalidation_mode=invalidation_mode): invalidation_mode=invalidation_mode,
stripdir=args.stripdir,
prependdir=args.prependdir,
optimize=args.opt_levels,
limit_sl_dest=args.limit_sl_dest):
success = False success = False
return success return success
else: else:

View File

@ -41,6 +41,16 @@ class CompileallTestsBase:
os.mkdir(self.subdirectory) os.mkdir(self.subdirectory)
self.source_path3 = os.path.join(self.subdirectory, '_test3.py') self.source_path3 = os.path.join(self.subdirectory, '_test3.py')
shutil.copyfile(self.source_path, self.source_path3) shutil.copyfile(self.source_path, self.source_path3)
many_directories = [str(number) for number in range(1, 100)]
self.long_path = os.path.join(self.directory,
"long",
*many_directories)
os.makedirs(self.long_path)
self.source_path_long = os.path.join(self.long_path, '_test4.py')
shutil.copyfile(self.source_path, self.source_path_long)
self.bc_path_long = importlib.util.cache_from_source(
self.source_path_long
)
def tearDown(self): def tearDown(self):
shutil.rmtree(self.directory) shutil.rmtree(self.directory)
@ -194,6 +204,123 @@ class CompileallTestsBase:
compileall.compile_dir(self.directory, quiet=True, workers=5) compileall.compile_dir(self.directory, quiet=True, workers=5)
self.assertTrue(compile_file_mock.called) self.assertTrue(compile_file_mock.called)
def text_compile_dir_maxlevels(self):
# Test the actual impact of maxlevels attr
compileall.compile_dir(os.path.join(self.directory, "long"),
maxlevels=10, quiet=True)
self.assertFalse(os.path.isfile(self.bc_path_long))
compileall.compile_dir(os.path.join(self.directory, "long"),
maxlevels=110, quiet=True)
self.assertTrue(os.path.isfile(self.bc_path_long))
def test_strip_only(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])
compileall.compile_dir(path, quiet=True, stripdir=stripdir)
rc, out, err = script_helper.assert_python_failure(bc)
expected_in = os.path.join(*fullpath[2:])
self.assertIn(
expected_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)
os.makedirs(path)
script = script_helper.make_script(path, "test", "1 / 0")
bc = importlib.util.cache_from_source(script)
prependdir = "/foo"
compileall.compile_dir(path, quiet=True, prependdir=prependdir)
rc, out, err = script_helper.assert_python_failure(bc)
expected_in = os.path.join(prependdir, self.directory, *fullpath)
self.assertIn(
expected_in,
str(err, encoding=sys.getdefaultencoding())
)
def test_strip_and_prepend(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])
prependdir = "/foo"
compileall.compile_dir(path, quiet=True,
stripdir=stripdir, prependdir=prependdir)
rc, out, err = script_helper.assert_python_failure(bc)
expected_in = os.path.join(prependdir, *fullpath[2:])
self.assertIn(
expected_in,
str(err, encoding=sys.getdefaultencoding())
)
self.assertNotIn(
stripdir,
str(err, encoding=sys.getdefaultencoding())
)
def test_strip_prepend_and_ddir(self):
fullpath = ["test", "build", "real", "path", "ddir"]
path = os.path.join(self.directory, *fullpath)
os.makedirs(path)
script_helper.make_script(path, "test", "1 / 0")
with self.assertRaises(ValueError):
compileall.compile_dir(path, quiet=True, ddir="/bar",
stripdir="/foo", prependdir="/bar")
def test_multiple_optimization_levels(self):
script = script_helper.make_script(self.directory,
"test_optimization",
"a = 0")
bc = []
for opt_level in "", 1, 2, 3:
bc.append(importlib.util.cache_from_source(script,
optimization=opt_level))
test_combinations = [[0, 1], [1, 2], [0, 2], [0, 1, 2]]
for opt_combination in test_combinations:
compileall.compile_file(script, quiet=True,
optimize=opt_combination)
for opt_level in opt_combination:
self.assertTrue(os.path.isfile(bc[opt_level]))
try:
os.unlink(bc[opt_level])
except Exception:
pass
@support.skip_unless_symlink
def test_ignore_symlink_destination(self):
# Create folders for allowed files, symlinks and prohibited area
allowed_path = os.path.join(self.directory, "test", "dir", "allowed")
symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks")
prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited")
os.makedirs(allowed_path)
os.makedirs(symlinks_path)
os.makedirs(prohibited_path)
# Create scripts and symlinks and remember their byte-compiled versions
allowed_script = script_helper.make_script(allowed_path, "test_allowed", "a = 0")
prohibited_script = script_helper.make_script(prohibited_path, "test_prohibited", "a = 0")
allowed_symlink = os.path.join(symlinks_path, "test_allowed.py")
prohibited_symlink = os.path.join(symlinks_path, "test_prohibited.py")
os.symlink(allowed_script, allowed_symlink)
os.symlink(prohibited_script, prohibited_symlink)
allowed_bc = importlib.util.cache_from_source(allowed_symlink)
prohibited_bc = importlib.util.cache_from_source(prohibited_symlink)
compileall.compile_dir(symlinks_path, quiet=True, limit_sl_dest=allowed_path)
self.assertTrue(os.path.isfile(allowed_bc))
self.assertFalse(os.path.isfile(prohibited_bc))
class CompileallTestsWithSourceEpoch(CompileallTestsBase, class CompileallTestsWithSourceEpoch(CompileallTestsBase,
unittest.TestCase, unittest.TestCase,
@ -436,6 +563,20 @@ class CommandLineTestsBase:
self.assertCompiled(spamfn) self.assertCompiled(spamfn)
self.assertCompiled(eggfn) self.assertCompiled(eggfn)
@support.skip_unless_symlink
def test_symlink_loop(self):
# Currently, compileall ignores symlinks to directories.
# If that limitation is ever lifted, it should protect against
# recursion in symlink loops.
pkg = os.path.join(self.pkgdir, 'spam')
script_helper.make_pkg(pkg)
os.symlink('.', os.path.join(pkg, 'evil'))
os.symlink('.', os.path.join(pkg, 'evil2'))
self.assertRunOK('-q', self.pkgdir)
self.assertCompiled(os.path.join(
self.pkgdir, 'spam', 'evil', 'evil2', '__init__.py'
))
def test_quiet(self): def test_quiet(self):
noisy = self.assertRunOK(self.pkgdir) noisy = self.assertRunOK(self.pkgdir)
quiet = self.assertRunOK('-q', self.pkgdir) quiet = self.assertRunOK('-q', self.pkgdir)
@ -577,6 +718,74 @@ class CommandLineTestsBase:
self.assertTrue(compile_dir.called) self.assertTrue(compile_dir.called)
self.assertEqual(compile_dir.call_args[-1]['workers'], 0) self.assertEqual(compile_dir.call_args[-1]['workers'], 0)
def test_strip_and_prepend(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])
prependdir = "/foo"
self.assertRunOK("-s", stripdir, "-p", prependdir, path)
rc, out, err = script_helper.assert_python_failure(bc)
expected_in = os.path.join(prependdir, *fullpath[2:])
self.assertIn(
expected_in,
str(err, encoding=sys.getdefaultencoding())
)
self.assertNotIn(
stripdir,
str(err, encoding=sys.getdefaultencoding())
)
def test_multiple_optimization_levels(self):
path = os.path.join(self.directory, "optimizations")
os.makedirs(path)
script = script_helper.make_script(path,
"test_optimization",
"a = 0")
bc = []
for opt_level in "", 1, 2, 3:
bc.append(importlib.util.cache_from_source(script,
optimization=opt_level))
test_combinations = [["0", "1"],
["1", "2"],
["0", "2"],
["0", "1", "2"]]
for opt_combination in test_combinations:
self.assertRunOK(path, *("-o" + str(n) for n in opt_combination))
for opt_level in opt_combination:
self.assertTrue(os.path.isfile(bc[int(opt_level)]))
try:
os.unlink(bc[opt_level])
except Exception:
pass
@support.skip_unless_symlink
def test_ignore_symlink_destination(self):
# Create folders for allowed files, symlinks and prohibited area
allowed_path = os.path.join(self.directory, "test", "dir", "allowed")
symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks")
prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited")
os.makedirs(allowed_path)
os.makedirs(symlinks_path)
os.makedirs(prohibited_path)
# Create scripts and symlinks and remember their byte-compiled versions
allowed_script = script_helper.make_script(allowed_path, "test_allowed", "a = 0")
prohibited_script = script_helper.make_script(prohibited_path, "test_prohibited", "a = 0")
allowed_symlink = os.path.join(symlinks_path, "test_allowed.py")
prohibited_symlink = os.path.join(symlinks_path, "test_prohibited.py")
os.symlink(allowed_script, allowed_symlink)
os.symlink(prohibited_script, prohibited_symlink)
allowed_bc = importlib.util.cache_from_source(allowed_symlink)
prohibited_bc = importlib.util.cache_from_source(prohibited_symlink)
self.assertRunOK(symlinks_path, "-e", allowed_path)
self.assertTrue(os.path.isfile(allowed_bc))
self.assertFalse(os.path.isfile(prohibited_bc))
class CommandLineTestsWithSourceEpoch(CommandLineTestsBase, class CommandLineTestsWithSourceEpoch(CommandLineTestsBase,
unittest.TestCase, unittest.TestCase,

View File

@ -0,0 +1,3 @@
:mod:`compileall` has a higher default recursion limit and new command-line
arguments for path manipulation, symlinks handling, and multiple
optimization levels.