Skip to content

Commit

Permalink
bpo-38731: Fix NameError in command-line interface of py_compile (GH-…
Browse files Browse the repository at this point in the history
…21617)

(cherry picked from commit 2024d7a)

Co-authored-by: Berker Peksag <berker.peksag@gmail.com>
  • Loading branch information
miss-islington and berkerpeksag committed Jul 25, 2020
1 parent a667e1c commit 949cf93
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
9 changes: 3 additions & 6 deletions Lib/py_compile.py
Expand Up @@ -197,21 +197,18 @@ def main(args=None):
compile(filename, doraise=True)
except PyCompileError as error:
rv = 1
if quiet < 2:
sys.stderr.write("%s\n" % error.msg)
sys.stderr.write("%s\n" % error.msg)
except OSError as error:
rv = 1
if quiet < 2:
sys.stderr.write("%s\n" % error)
sys.stderr.write("%s\n" % error)
else:
for filename in args:
try:
compile(filename, doraise=True)
except PyCompileError as error:
# return value to indicate at least one failure
rv = 1
if quiet < 2:
sys.stderr.write("%s\n" % error.msg)
sys.stderr.write("%s\n" % error.msg)
return rv

if __name__ == "__main__":
Expand Down
56 changes: 56 additions & 0 deletions Lib/test/test_py_compile.py
Expand Up @@ -4,11 +4,13 @@
import py_compile
import shutil
import stat
import subprocess
import sys
import tempfile
import unittest

from test import support
from test.support import script_helper


def without_source_date_epoch(fxn):
Expand Down Expand Up @@ -216,5 +218,59 @@ class PyCompileTestsWithoutSourceEpoch(PyCompileTestsBase,
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__":
unittest.main()
@@ -0,0 +1 @@
Fix :exc:`NameError` in command-line interface of :mod:`py_compile`.

0 comments on commit 949cf93

Please sign in to comment.