Skip to content

Commit 2024d7a

Browse files
authored
bpo-38731: Fix NameError in command-line interface of py_compile (GH-21617)
1 parent 15fdbb7 commit 2024d7a

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

Lib/py_compile.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,18 @@ def main(args=None):
197197
compile(filename, doraise=True)
198198
except PyCompileError as error:
199199
rv = 1
200-
if quiet < 2:
201-
sys.stderr.write("%s\n" % error.msg)
200+
sys.stderr.write("%s\n" % error.msg)
202201
except OSError as error:
203202
rv = 1
204-
if quiet < 2:
205-
sys.stderr.write("%s\n" % error)
203+
sys.stderr.write("%s\n" % error)
206204
else:
207205
for filename in args:
208206
try:
209207
compile(filename, doraise=True)
210208
except PyCompileError as error:
211209
# return value to indicate at least one failure
212210
rv = 1
213-
if quiet < 2:
214-
sys.stderr.write("%s\n" % error.msg)
211+
sys.stderr.write("%s\n" % error.msg)
215212
return rv
216213

217214
if __name__ == "__main__":

Lib/test/test_py_compile.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import py_compile
55
import shutil
66
import stat
7+
import subprocess
78
import sys
89
import tempfile
910
import unittest
1011

1112
from test import support
13+
from test.support import script_helper
1214

1315

1416
def without_source_date_epoch(fxn):
@@ -216,5 +218,59 @@ class PyCompileTestsWithoutSourceEpoch(PyCompileTestsBase,
216218
pass
217219

218220

221+
class PyCompileCLITestCase(unittest.TestCase):
222+
223+
def setUp(self):
224+
self.directory = tempfile.mkdtemp()
225+
self.source_path = os.path.join(self.directory, '_test.py')
226+
self.cache_path = importlib.util.cache_from_source(self.source_path)
227+
with open(self.source_path, 'w') as file:
228+
file.write('x = 123\n')
229+
230+
def tearDown(self):
231+
support.rmtree(self.directory)
232+
233+
def pycompilecmd(self, *args, **kwargs):
234+
# assert_python_* helpers don't return proc object. We'll just use
235+
# subprocess.run() instead of spawn_python() and its friends to test
236+
# stdin support of the CLI.
237+
if args and args[0] == '-' and 'input' in kwargs:
238+
return subprocess.run([sys.executable, '-m', 'py_compile', '-'],
239+
input=kwargs['input'].encode(),
240+
capture_output=True)
241+
return script_helper.assert_python_ok('-m', 'py_compile', *args, **kwargs)
242+
243+
def pycompilecmd_failure(self, *args):
244+
return script_helper.assert_python_failure('-m', 'py_compile', *args)
245+
246+
def test_stdin(self):
247+
result = self.pycompilecmd('-', input=self.source_path)
248+
self.assertEqual(result.returncode, 0)
249+
self.assertEqual(result.stdout, b'')
250+
self.assertEqual(result.stderr, b'')
251+
self.assertTrue(os.path.exists(self.cache_path))
252+
253+
def test_with_files(self):
254+
rc, stdout, stderr = self.pycompilecmd(self.source_path, self.source_path)
255+
self.assertEqual(rc, 0)
256+
self.assertEqual(stdout, b'')
257+
self.assertEqual(stderr, b'')
258+
self.assertTrue(os.path.exists(self.cache_path))
259+
260+
def test_bad_syntax(self):
261+
bad_syntax = os.path.join(os.path.dirname(__file__), 'badsyntax_3131.py')
262+
rc, stdout, stderr = self.pycompilecmd_failure(bad_syntax)
263+
self.assertEqual(rc, 1)
264+
self.assertEqual(stdout, b'')
265+
self.assertIn(b'SyntaxError', stderr)
266+
267+
def test_file_not_exists(self):
268+
should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py')
269+
rc, stdout, stderr = self.pycompilecmd_failure(self.source_path, should_not_exists)
270+
self.assertEqual(rc, 1)
271+
self.assertEqual(stdout, b'')
272+
self.assertIn(b'No such file or directory', stderr)
273+
274+
219275
if __name__ == "__main__":
220276
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :exc:`NameError` in command-line interface of :mod:`py_compile`.

0 commit comments

Comments
 (0)