bpo-38731: Fix NameError in command-line interface of py_compile (GH-21617)

(cherry picked from commit 2024d7aca1)

Co-authored-by: Berker Peksag <berker.peksag@gmail.com>
This commit is contained in:
Miss Islington (bot) 2020-07-25 14:03:50 -07:00 committed by GitHub
parent a667e1c66a
commit 949cf93f8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 6 deletions

View File

@ -197,12 +197,10 @@ def main(args=None):
compile(filename, doraise=True) compile(filename, doraise=True)
except PyCompileError as error: except PyCompileError as error:
rv = 1 rv = 1
if quiet < 2: sys.stderr.write("%s\n" % error.msg)
sys.stderr.write("%s\n" % error.msg)
except OSError as error: except OSError as error:
rv = 1 rv = 1
if quiet < 2: sys.stderr.write("%s\n" % error)
sys.stderr.write("%s\n" % error)
else: else:
for filename in args: for filename in args:
try: try:
@ -210,8 +208,7 @@ def main(args=None):
except PyCompileError as error: except PyCompileError as error:
# return value to indicate at least one failure # return value to indicate at least one failure
rv = 1 rv = 1
if quiet < 2: sys.stderr.write("%s\n" % error.msg)
sys.stderr.write("%s\n" % error.msg)
return rv return rv
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -4,11 +4,13 @@ import os
import py_compile import py_compile
import shutil import shutil
import stat import stat
import subprocess
import sys import sys
import tempfile import tempfile
import unittest import unittest
from test import support from test import support
from test.support import script_helper
def without_source_date_epoch(fxn): def without_source_date_epoch(fxn):
@ -216,5 +218,59 @@ class PyCompileTestsWithoutSourceEpoch(PyCompileTestsBase,
pass pass
class PyCompileCLITestCase(unittest.TestCase):
def setUp(self):
self.directory = tempfile.mkdtemp()
self.source_path = os.path.join(self.directory, '_test.py')
self.cache_path = importlib.util.cache_from_source(self.source_path)
with open(self.source_path, 'w') as file:
file.write('x = 123\n')
def tearDown(self):
support.rmtree(self.directory)
def pycompilecmd(self, *args, **kwargs):
# assert_python_* helpers don't return proc object. We'll just use
# subprocess.run() instead of spawn_python() and its friends to test
# stdin support of the CLI.
if args and args[0] == '-' and 'input' in kwargs:
return subprocess.run([sys.executable, '-m', 'py_compile', '-'],
input=kwargs['input'].encode(),
capture_output=True)
return script_helper.assert_python_ok('-m', 'py_compile', *args, **kwargs)
def pycompilecmd_failure(self, *args):
return script_helper.assert_python_failure('-m', 'py_compile', *args)
def test_stdin(self):
result = self.pycompilecmd('-', input=self.source_path)
self.assertEqual(result.returncode, 0)
self.assertEqual(result.stdout, b'')
self.assertEqual(result.stderr, b'')
self.assertTrue(os.path.exists(self.cache_path))
def test_with_files(self):
rc, stdout, stderr = self.pycompilecmd(self.source_path, self.source_path)
self.assertEqual(rc, 0)
self.assertEqual(stdout, b'')
self.assertEqual(stderr, b'')
self.assertTrue(os.path.exists(self.cache_path))
def test_bad_syntax(self):
bad_syntax = os.path.join(os.path.dirname(__file__), 'badsyntax_3131.py')
rc, stdout, stderr = self.pycompilecmd_failure(bad_syntax)
self.assertEqual(rc, 1)
self.assertEqual(stdout, b'')
self.assertIn(b'SyntaxError', stderr)
def test_file_not_exists(self):
should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py')
rc, stdout, stderr = self.pycompilecmd_failure(self.source_path, should_not_exists)
self.assertEqual(rc, 1)
self.assertEqual(stdout, b'')
self.assertIn(b'No such file or directory', stderr)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -0,0 +1 @@
Fix :exc:`NameError` in command-line interface of :mod:`py_compile`.