Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6554b86

Browse files
committed
Issue #21338: Add silent mode for compileall.
quiet parameters of compile_{dir, file, path} functions now have a multilevel value. Also, -q option of the CLI now have a multilevel value. Patch by Thomas Kluyver.
1 parent 41d3196 commit 6554b86

4 files changed

Lines changed: 59 additions & 25 deletions

File tree

Doc/library/compileall.rst

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ compile Python sources.
4242

4343
.. cmdoption:: -q
4444

45-
Do not print the list of files compiled, print only error messages.
45+
Do not print the list of files compiled. If passed once, error messages will
46+
still be printed. If passed twice (``-qq``), all output is suppressed.
4647

4748
.. cmdoption:: -d destdir
4849

@@ -89,6 +90,9 @@ compile Python sources.
8990
.. versionchanged:: 3.5
9091
Added the ``-j`` and ``-r`` options.
9192

93+
.. versionchanged:: 3.5
94+
``-q`` option was changed to a multilevel value.
95+
9296

9397
There is no command-line option to control the optimization level used by the
9498
:func:`compile` function, because the Python interpreter itself already
@@ -97,7 +101,7 @@ provides the option: :program:`python -O -m compileall`.
97101
Public functions
98102
----------------
99103

100-
.. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1, workers=1)
104+
.. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1)
101105

102106
Recursively descend the directory tree named by *dir*, compiling all :file:`.py`
103107
files along the way.
@@ -118,8 +122,9 @@ Public functions
118122
file considered for compilation, and if it returns a true value, the file
119123
is skipped.
120124

121-
If *quiet* is true, nothing is printed to the standard output unless errors
122-
occur.
125+
If *quiet* is ``False`` or ``0`` (the default), the filenames and other
126+
information are printed to standard out. Set to ``1``, only errors are
127+
printed. Set to ``2``, all output is suppressed.
123128

124129
If *legacy* is true, byte-code files are written to their legacy locations
125130
and names, which may overwrite byte-code files created by another version of
@@ -142,8 +147,10 @@ Public functions
142147
.. versionchanged:: 3.5
143148
Added the *workers* parameter.
144149

150+
.. versionchanged:: 3.5
151+
*quiet* parameter was changed to a multilevel value.
145152

146-
.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1)
153+
.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1)
147154

148155
Compile the file with path *fullname*.
149156

@@ -157,8 +164,9 @@ Public functions
157164
file being compiled, and if it returns a true value, the file is not
158165
compiled and ``True`` is returned.
159166

160-
If *quiet* is true, nothing is printed to the standard output unless errors
161-
occur.
167+
If *quiet* is ``False`` or ``0`` (the default), the filenames and other
168+
information are printed to standard out. Set to ``1``, only errors are
169+
printed. Set to ``2``, all output is suppressed.
162170

163171
If *legacy* is true, byte-code files are written to their legacy locations
164172
and names, which may overwrite byte-code files created by another version of
@@ -171,8 +179,10 @@ Public functions
171179

172180
.. versionadded:: 3.2
173181

182+
.. versionchanged:: 3.5
183+
*quiet* parameter was changed to a multilevel value.
174184

175-
.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, legacy=False, optimize=-1)
185+
.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1)
176186

177187
Byte-compile all the :file:`.py` files found along ``sys.path``. If
178188
*skip_curdir* is true (the default), the current directory is not included
@@ -183,6 +193,8 @@ Public functions
183193
.. versionchanged:: 3.2
184194
Added the *legacy* and *optimize* parameter.
185195

196+
.. versionchanged:: 3.5
197+
*quiet* parameter was changed to a multilevel value.
186198

187199
To force a recompile of all the :file:`.py` files in the :file:`Lib/`
188200
subdirectory and all its subdirectories::

Lib/compileall.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424

2525
__all__ = ["compile_dir","compile_file","compile_path"]
2626

27-
def _walk_dir(dir, ddir=None, maxlevels=10, quiet=False):
27+
def _walk_dir(dir, ddir=None, maxlevels=10, quiet=0):
2828
if not quiet:
2929
print('Listing {!r}...'.format(dir))
3030
try:
3131
names = os.listdir(dir)
3232
except OSError:
33-
print("Can't list {!r}".format(dir))
33+
if quiet < 2:
34+
print("Can't list {!r}".format(dir))
3435
names = []
3536
names.sort()
3637
for name in names:
@@ -49,7 +50,7 @@ def _walk_dir(dir, ddir=None, maxlevels=10, quiet=False):
4950
maxlevels=maxlevels - 1, quiet=quiet)
5051

5152
def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
52-
quiet=False, legacy=False, optimize=-1, workers=1):
53+
quiet=0, legacy=False, optimize=-1, workers=1):
5354
"""Byte-compile all modules in the given directory tree.
5455
5556
Arguments (only dir is required):
@@ -59,7 +60,8 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
5960
ddir: the directory that will be prepended to the path to the
6061
file as it is compiled into each byte-code file.
6162
force: if True, force compilation, even if timestamps are up-to-date
62-
quiet: if True, be quiet during compilation
63+
quiet: full output with False or 0, errors only with 1,
64+
no output with 2
6365
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
6466
optimize: optimization level or -1 for level of the interpreter
6567
workers: maximum number of parallel workers
@@ -89,7 +91,7 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
8991
success = 0
9092
return success
9193

92-
def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False,
94+
def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
9395
legacy=False, optimize=-1):
9496
"""Byte-compile one file.
9597
@@ -99,7 +101,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False,
99101
ddir: if given, the directory name compiled in to the
100102
byte-code file.
101103
force: if True, force compilation, even if timestamps are up-to-date
102-
quiet: if True, be quiet during compilation
104+
quiet: full output with False or 0, errors only with 1,
105+
no output with 2
103106
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
104107
optimize: optimization level or -1 for level of the interpreter
105108
"""
@@ -142,7 +145,10 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False,
142145
ok = py_compile.compile(fullname, cfile, dfile, True,
143146
optimize=optimize)
144147
except py_compile.PyCompileError as err:
145-
if quiet:
148+
success = 0
149+
if quiet >= 2:
150+
return success
151+
elif quiet:
146152
print('*** Error compiling {!r}...'.format(fullname))
147153
else:
148154
print('*** ', end='')
@@ -151,20 +157,21 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False,
151157
errors='backslashreplace')
152158
msg = msg.decode(sys.stdout.encoding)
153159
print(msg)
154-
success = 0
155160
except (SyntaxError, UnicodeError, OSError) as e:
156-
if quiet:
161+
success = 0
162+
if quiet >= 2:
163+
return success
164+
elif quiet:
157165
print('*** Error compiling {!r}...'.format(fullname))
158166
else:
159167
print('*** ', end='')
160168
print(e.__class__.__name__ + ':', e)
161-
success = 0
162169
else:
163170
if ok == 0:
164171
success = 0
165172
return success
166173

167-
def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=False,
174+
def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0,
168175
legacy=False, optimize=-1):
169176
"""Byte-compile all module on sys.path.
170177
@@ -173,14 +180,15 @@ def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=False,
173180
skip_curdir: if true, skip current directory (default True)
174181
maxlevels: max recursion level (default 0)
175182
force: as for compile_dir() (default False)
176-
quiet: as for compile_dir() (default False)
183+
quiet: as for compile_dir() (default 0)
177184
legacy: as for compile_dir() (default False)
178185
optimize: as for compile_dir() (default -1)
179186
"""
180187
success = 1
181188
for dir in sys.path:
182189
if (not dir or dir == os.curdir) and skip_curdir:
183-
print('Skipping current directory')
190+
if quiet < 2:
191+
print('Skipping current directory')
184192
else:
185193
success = success and compile_dir(dir, maxlevels, None,
186194
force, quiet=quiet,
@@ -203,8 +211,9 @@ def main():
203211
'then `-r` takes precedence.'))
204212
parser.add_argument('-f', action='store_true', dest='force',
205213
help='force rebuild even if timestamps are up to date')
206-
parser.add_argument('-q', action='store_true', dest='quiet',
207-
help='output only error messages')
214+
parser.add_argument('-q', action='count', dest='quiet', default=0,
215+
help='output only error messages; -qq will suppress '
216+
'the error messages as well.')
208217
parser.add_argument('-b', action='store_true', dest='legacy',
209218
help='use legacy (pre-PEP3147) compiled file locations')
210219
parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None,
@@ -250,7 +259,8 @@ def main():
250259
for line in f:
251260
compile_dests.append(line.strip())
252261
except OSError:
253-
print("Error reading file list {}".format(args.flist))
262+
if args.quiet < 2:
263+
print("Error reading file list {}".format(args.flist))
254264
return False
255265

256266
if args.workers is not None:
@@ -274,7 +284,8 @@ def main():
274284
return compile_path(legacy=args.legacy, force=args.force,
275285
quiet=args.quiet)
276286
except KeyboardInterrupt:
277-
print("\n[interrupted]")
287+
if args.quiet < 2:
288+
print("\n[interrupted]")
278289
return False
279290
return True
280291

Lib/test/test_compileall.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ def test_quiet(self):
347347
self.assertNotEqual(b'', noisy)
348348
self.assertEqual(b'', quiet)
349349

350+
def test_silent(self):
351+
script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
352+
_, quiet, _ = self.assertRunNotOK('-q', self.pkgdir)
353+
_, silent, _ = self.assertRunNotOK('-qq', self.pkgdir)
354+
self.assertNotEqual(b'', quiet)
355+
self.assertEqual(b'', silent)
356+
350357
def test_regexp(self):
351358
self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir)
352359
self.assertNotCompiled(self.barfn)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ Core and Builtins
175175
Library
176176
-------
177177

178+
- Issue #21338: Add silent mode for compileall. quiet parameters of
179+
compile_{dir, file, path} functions now have a multilevel value. Also,
180+
-q option of the CLI now have a multilevel value. Patch by Thomas Kluyver.
181+
178182
- Issue #20152: Convert the array and cmath modules to Argument Clinic.
179183

180184
- Issue #18643: Add socket.socketpair() on Windows.

0 commit comments

Comments
 (0)