summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2014-08-19 21:13:26 (GMT)
committerBenjamin Peterson <benjamin@python.org>2014-08-19 21:13:26 (GMT)
commit344ff4ab2b68dee327fb36ee4fe01124466e49d6 (patch)
treeb7f5bedd0568b5eef6812cac94d0d56ecc2c1258
parent54b3b3fb2cb54e25901710b98b8d7b13d5f3b01e (diff)
downloadcpython-344ff4ab2b68dee327fb36ee4fe01124466e49d6.zip
cpython-344ff4ab2b68dee327fb36ee4fe01124466e49d6.tar.gz
cpython-344ff4ab2b68dee327fb36ee4fe01124466e49d6.tar.bz2
allow recursion depth to be specified (closes #19628)
Patch from Claudiu Popa.
-rw-r--r--Doc/library/compileall.rst11
-rw-r--r--Lib/compileall.py12
-rw-r--r--Lib/test/test_compileall.py34
-rw-r--r--Misc/NEWS3
4 files changed, 59 insertions, 1 deletions
diff --git a/Doc/library/compileall.rst b/Doc/library/compileall.rst
index 41e9e1b..104f33a 100644
--- a/Doc/library/compileall.rst
+++ b/Doc/library/compileall.rst
@@ -66,9 +66,20 @@ compile Python sources.
is to write files to their :pep:`3147` locations and names, which allows
byte-code files from multiple versions of Python to coexist.
+.. cmdoption:: -r
+
+ Control the maximum recursion level for subdirectories.
+ If this is given, then ``-l`` option will not be taken into account.
+ :program:`python -m compileall <directory> -r 0` is equivalent to
+ :program:`python -m compileall <directory> -l`.
+
+
.. versionchanged:: 3.2
Added the ``-i``, ``-b`` and ``-h`` options.
+.. versionchanged:: 3.5
+ Added the ``-r`` option.
+
There is no command-line option to control the optimization level used by the
:func:`compile` function, because the Python interpreter itself already
provides the option: :program:`python -O -m compileall`.
diff --git a/Lib/compileall.py b/Lib/compileall.py
index d957ee5..513d899 100644
--- a/Lib/compileall.py
+++ b/Lib/compileall.py
@@ -169,6 +169,10 @@ def main():
parser.add_argument('-l', action='store_const', const=0,
default=10, dest='maxlevels',
help="don't recurse into subdirectories")
+ parser.add_argument('-r', type=int, dest='recursion',
+ help=('control the maximum recursion level. '
+ 'if `-l` and `-r` options are specified, '
+ 'then `-r` takes precedence.'))
parser.add_argument('-f', action='store_true', dest='force',
help='force rebuild even if timestamps are up to date')
parser.add_argument('-q', action='store_true', dest='quiet',
@@ -203,6 +207,12 @@ def main():
import re
args.rx = re.compile(args.rx)
+
+ if args.recursion is not None:
+ maxlevels = args.recursion
+ else:
+ maxlevels = args.maxlevels
+
# if flist is provided then load it
if args.flist:
try:
@@ -222,7 +232,7 @@ def main():
args.quiet, args.legacy):
success = False
else:
- if not compile_dir(dest, args.maxlevels, args.ddir,
+ if not compile_dir(dest, maxlevels, args.ddir,
args.force, args.rx, args.quiet,
args.legacy):
success = False
diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py
index 2a42238..ba1765e 100644
--- a/Lib/test/test_compileall.py
+++ b/Lib/test/test_compileall.py
@@ -273,6 +273,40 @@ class CommandLineTests(unittest.TestCase):
self.assertCompiled(subinitfn)
self.assertCompiled(hamfn)
+ def test_recursion_limit(self):
+ subpackage = os.path.join(self.pkgdir, 'spam')
+ subpackage2 = os.path.join(subpackage, 'ham')
+ subpackage3 = os.path.join(subpackage2, 'eggs')
+ for pkg in (subpackage, subpackage2, subpackage3):
+ script_helper.make_pkg(pkg)
+
+ subinitfn = os.path.join(subpackage, '__init__.py')
+ hamfn = script_helper.make_script(subpackage, 'ham', '')
+ spamfn = script_helper.make_script(subpackage2, 'spam', '')
+ eggfn = script_helper.make_script(subpackage3, 'egg', '')
+
+ self.assertRunOK('-q', '-r 0', self.pkgdir)
+ self.assertNotCompiled(subinitfn)
+ self.assertFalse(
+ os.path.exists(os.path.join(subpackage, '__pycache__')))
+
+ self.assertRunOK('-q', '-r 1', self.pkgdir)
+ self.assertCompiled(subinitfn)
+ self.assertCompiled(hamfn)
+ self.assertNotCompiled(spamfn)
+
+ self.assertRunOK('-q', '-r 2', self.pkgdir)
+ self.assertCompiled(subinitfn)
+ self.assertCompiled(hamfn)
+ self.assertCompiled(spamfn)
+ self.assertNotCompiled(eggfn)
+
+ self.assertRunOK('-q', '-r 5', self.pkgdir)
+ self.assertCompiled(subinitfn)
+ self.assertCompiled(hamfn)
+ self.assertCompiled(spamfn)
+ self.assertCompiled(eggfn)
+
def test_quiet(self):
noisy = self.assertRunOK(self.pkgdir)
quiet = self.assertRunOK('-q', self.pkgdir)
diff --git a/Misc/NEWS b/Misc/NEWS
index 9458a2a..9e2b198 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -121,6 +121,9 @@ Core and Builtins
Library
-------
+- Issue #19628: Allow compileall recursion depth to be specified with a -r
+ option.
+
- Issue #15696: Add a __sizeof__ implementation for mmap objects on Windows.
- Issue #22068: Avoided reference loops with Variables and Fonts in Tkinter.