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

Skip to content

Commit 2e33ecd

Browse files
nanjekyejoannahberkerpeksag
authored andcommitted
bpo-22640: Add silent mode to py_compile.compile() (GH-12976)
1 parent 3c8724f commit 2e33ecd

5 files changed

Lines changed: 43 additions & 9 deletions

File tree

Doc/library/py_compile.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ byte-code cache files in the directory containing the source code.
4242
is raised. This function returns the path to byte-compiled file, i.e.
4343
whatever *cfile* value was used.
4444

45+
The *doraise* and *quiet* arguments determine how errors are handled while
46+
compiling file. If *quiet* is 0 or 1, and *doraise* is false, the default
47+
behaviour is enabled: an error string is written to ``sys.stderr``, and the
48+
function returns ``None`` instead of a path. If *doraise* is true,
49+
a :exc:`PyCompileError` is raised instead. However if *quiet* is 2,
50+
no message is written, and *doraise* has no effect.
51+
4552
If the path that *cfile* becomes (either explicitly specified or computed)
4653
is a symlink or non-regular file, :exc:`FileExistsError` will be raised.
4754
This is to act as a warning that import will turn those paths into regular
@@ -82,6 +89,9 @@ byte-code cache files in the directory containing the source code.
8289
overrides the value of the *invalidation_mode* argument, and determines
8390
its default value instead.
8491

92+
.. versionchanged:: 3.8
93+
The *quiet* parameter was added.
94+
8595

8696
.. class:: PycInvalidationMode
8797

Doc/whatsnew/3.8.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,13 @@ NSKeyedArchiver-encoded binary plists.
537537
(Contributed by Jon Janzen in :issue:`26707`.)
538538

539539

540+
py_compile
541+
----------
542+
543+
:func:`py_compile.compile` now supports silent mode.
544+
(Contributed by Joannah Nanjekye in :issue:`22640`.)
545+
546+
540547
socket
541548
------
542549

Lib/py_compile.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def _get_default_invalidation_mode():
7777

7878

7979
def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1,
80-
invalidation_mode=None):
80+
invalidation_mode=None, quiet=0):
8181
"""Byte-compile one Python source file to Python bytecode.
8282
8383
:param file: The source file name.
@@ -95,6 +95,8 @@ def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1,
9595
are -1, 0, 1 and 2. A value of -1 means to use the optimization
9696
level of the current interpreter, as given by -O command line options.
9797
:param invalidation_mode:
98+
:param quiet: Return full output with False or 0, errors only with 1,
99+
and no output with 2.
98100
99101
:return: Path to the resulting byte compiled file.
100102
@@ -143,11 +145,12 @@ def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1,
143145
_optimize=optimize)
144146
except Exception as err:
145147
py_exc = PyCompileError(err.__class__, err, dfile or file)
146-
if doraise:
147-
raise py_exc
148-
else:
149-
sys.stderr.write(py_exc.msg + '\n')
150-
return
148+
if quiet < 2:
149+
if doraise:
150+
raise py_exc
151+
else:
152+
sys.stderr.write(py_exc.msg + '\n')
153+
return
151154
try:
152155
dirname = os.path.dirname(cfile)
153156
if dirname:
@@ -194,18 +197,21 @@ def main(args=None):
194197
compile(filename, doraise=True)
195198
except PyCompileError as error:
196199
rv = 1
197-
sys.stderr.write("%s\n" % error.msg)
200+
if quiet < 2:
201+
sys.stderr.write("%s\n" % error.msg)
198202
except OSError as error:
199203
rv = 1
200-
sys.stderr.write("%s\n" % error)
204+
if quiet < 2:
205+
sys.stderr.write("%s\n" % error)
201206
else:
202207
for filename in args:
203208
try:
204209
compile(filename, doraise=True)
205210
except PyCompileError as error:
206211
# return value to indicate at least one failure
207212
rv = 1
208-
sys.stderr.write("%s\n" % error.msg)
213+
if quiet < 2:
214+
sys.stderr.write("%s\n" % error.msg)
209215
return rv
210216

211217
if __name__ == "__main__":

Lib/test/test_py_compile.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ def test_invalidation_mode(self):
192192
fp.read(), 'test', {})
193193
self.assertEqual(flags, 0b1)
194194

195+
def test_quiet(self):
196+
bad_coding = os.path.join(os.path.dirname(__file__), 'bad_coding2.py')
197+
with support.captured_stderr() as stderr:
198+
self.assertIsNone(py_compile.compile(bad_coding, doraise=False, quiet=2))
199+
self.assertIsNone(py_compile.compile(bad_coding, doraise=True, quiet=2))
200+
self.assertEqual(stderr.getvalue(), '')
201+
with self.assertRaises(py_compile.PyCompileError):
202+
py_compile.compile(bad_coding, doraise=True, quiet=1)
203+
195204

196205
class PyCompileTestsWithSourceEpoch(PyCompileTestsBase,
197206
unittest.TestCase,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`py_compile.compile` now supports silent mode.
2+
Patch by Joannah Nanjekye

0 commit comments

Comments
 (0)