Skip to content

Commit 344ff4a

Browse files
committed
allow recursion depth to be specified (closes python#19628)
Patch from Claudiu Popa.
1 parent 54b3b3f commit 344ff4a

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

Doc/library/compileall.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,20 @@ compile Python sources.
6666
is to write files to their :pep:`3147` locations and names, which allows
6767
byte-code files from multiple versions of Python to coexist.
6868

69+
.. cmdoption:: -r
70+
71+
Control the maximum recursion level for subdirectories.
72+
If this is given, then ``-l`` option will not be taken into account.
73+
:program:`python -m compileall <directory> -r 0` is equivalent to
74+
:program:`python -m compileall <directory> -l`.
75+
76+
6977
.. versionchanged:: 3.2
7078
Added the ``-i``, ``-b`` and ``-h`` options.
7179

80+
.. versionchanged:: 3.5
81+
Added the ``-r`` option.
82+
7283
There is no command-line option to control the optimization level used by the
7384
:func:`compile` function, because the Python interpreter itself already
7485
provides the option: :program:`python -O -m compileall`.

Lib/compileall.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ def main():
169169
parser.add_argument('-l', action='store_const', const=0,
170170
default=10, dest='maxlevels',
171171
help="don't recurse into subdirectories")
172+
parser.add_argument('-r', type=int, dest='recursion',
173+
help=('control the maximum recursion level. '
174+
'if `-l` and `-r` options are specified, '
175+
'then `-r` takes precedence.'))
172176
parser.add_argument('-f', action='store_true', dest='force',
173177
help='force rebuild even if timestamps are up to date')
174178
parser.add_argument('-q', action='store_true', dest='quiet',
@@ -203,6 +207,12 @@ def main():
203207
import re
204208
args.rx = re.compile(args.rx)
205209

210+
211+
if args.recursion is not None:
212+
maxlevels = args.recursion
213+
else:
214+
maxlevels = args.maxlevels
215+
206216
# if flist is provided then load it
207217
if args.flist:
208218
try:
@@ -222,7 +232,7 @@ def main():
222232
args.quiet, args.legacy):
223233
success = False
224234
else:
225-
if not compile_dir(dest, args.maxlevels, args.ddir,
235+
if not compile_dir(dest, maxlevels, args.ddir,
226236
args.force, args.rx, args.quiet,
227237
args.legacy):
228238
success = False

Lib/test/test_compileall.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,40 @@ def test_recursion_control(self):
273273
self.assertCompiled(subinitfn)
274274
self.assertCompiled(hamfn)
275275

276+
def test_recursion_limit(self):
277+
subpackage = os.path.join(self.pkgdir, 'spam')
278+
subpackage2 = os.path.join(subpackage, 'ham')
279+
subpackage3 = os.path.join(subpackage2, 'eggs')
280+
for pkg in (subpackage, subpackage2, subpackage3):
281+
script_helper.make_pkg(pkg)
282+
283+
subinitfn = os.path.join(subpackage, '__init__.py')
284+
hamfn = script_helper.make_script(subpackage, 'ham', '')
285+
spamfn = script_helper.make_script(subpackage2, 'spam', '')
286+
eggfn = script_helper.make_script(subpackage3, 'egg', '')
287+
288+
self.assertRunOK('-q', '-r 0', self.pkgdir)
289+
self.assertNotCompiled(subinitfn)
290+
self.assertFalse(
291+
os.path.exists(os.path.join(subpackage, '__pycache__')))
292+
293+
self.assertRunOK('-q', '-r 1', self.pkgdir)
294+
self.assertCompiled(subinitfn)
295+
self.assertCompiled(hamfn)
296+
self.assertNotCompiled(spamfn)
297+
298+
self.assertRunOK('-q', '-r 2', self.pkgdir)
299+
self.assertCompiled(subinitfn)
300+
self.assertCompiled(hamfn)
301+
self.assertCompiled(spamfn)
302+
self.assertNotCompiled(eggfn)
303+
304+
self.assertRunOK('-q', '-r 5', self.pkgdir)
305+
self.assertCompiled(subinitfn)
306+
self.assertCompiled(hamfn)
307+
self.assertCompiled(spamfn)
308+
self.assertCompiled(eggfn)
309+
276310
def test_quiet(self):
277311
noisy = self.assertRunOK(self.pkgdir)
278312
quiet = self.assertRunOK('-q', self.pkgdir)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ Core and Builtins
121121
Library
122122
-------
123123

124+
- Issue #19628: Allow compileall recursion depth to be specified with a -r
125+
option.
126+
124127
- Issue #15696: Add a __sizeof__ implementation for mmap objects on Windows.
125128

126129
- Issue #22068: Avoided reference loops with Variables and Fonts in Tkinter.

0 commit comments

Comments
 (0)