diff options
-rw-r--r-- | CHANGES.txt | 4 | ||||
-rw-r--r-- | coverage/backward.py | 17 | ||||
-rw-r--r-- | coverage/execfile.py | 10 | ||||
-rw-r--r-- | test/test_execfile.py | 12 |
4 files changed, 33 insertions, 10 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 23f9b355..dd921f8d 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -32,10 +32,14 @@ Version 3.3 an added dot rather than simply appended, so that .coveragerc files will not be confused for data files. +- Python source files that don't end with a newline can now be executed, fixing + `issue 47`. + - Added an AUTHORS.txt file. .. _issue 39: http://bitbucket.org/ned/coveragepy/issue/39 .. _issue 40: http://bitbucket.org/ned/coveragepy/issue/40 +.. _issue 47: http://bitbucket.org/ned/coveragepy/issue/47 Version 3.2, 5 December 2009 diff --git a/coverage/backward.py b/coverage/backward.py index af46e0dd..425bcc6e 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -51,19 +51,20 @@ except NameError: # Exec is a statement in Py2, a function in Py3 if sys.version_info >= (3, 0): - def exec_function(source, filename, global_map): + def exec_code_object(code, global_map): """A wrapper around exec().""" - exec(compile(source, filename, "exec"), global_map) + exec(code, global_map) else: # OK, this is pretty gross. In Py2, exec was a statement, but that will # be a syntax error if we try to put it in a Py3 file, even if it is never # executed. So hide it inside an evaluated string literal instead. - eval(compile("""\ -def exec_function(source, filename, global_map): - exec compile(source, filename, "exec") in global_map -""", - "<exec_function>", "exec" - )) + eval( + compile( + "def exec_code_object(code, global_map):\n" + " exec code in global_map\n", + "<exec_function>", "exec" + ) + ) # ConfigParser was renamed to the more-standard configparser try: diff --git a/coverage/execfile.py b/coverage/execfile.py index f04840b5..1a2ffadd 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -2,7 +2,7 @@ import imp, os, sys -from coverage.backward import exec_function +from coverage.backward import exec_code_object from coverage.misc import NoSource, ExceptionDuringRun @@ -42,9 +42,15 @@ def run_python_file(filename, args): except IOError: raise NoSource("No file to run: %r" % filename) + # We have the source. `compile` still needs the last line to be clean, + # so make sure it is, then compile a code object from it. + if source[-1] != '\n': + source += '\n' + code = compile(source, filename, "exec") + # Execute the source file. try: - exec_function(source, filename, main_mod.__dict__) + exec_code_object(code, main_mod.__dict__) except: # Something went wrong while executing the user code. # Get the exc_info, and pack them into an exception that we can diff --git a/test/test_execfile.py b/test/test_execfile.py index 8c5e9a11..2f28a069 100644 --- a/test/test_execfile.py +++ b/test/test_execfile.py @@ -60,5 +60,17 @@ class RunTest(CoverageTest): run_python_file('nl.py', ['nl.py']) self.assertEqual(self.stdout(), "Hello, world!\n"*3) + def test_missing_final_newline(self): + # Make sure we can deal with a Python file with no final newline. + self.make_file("abrupt.py", """\ + if 1: + a = 1 + print("a is %r" % a) + #""") + abrupt = open("abrupt.py").read() + self.assertEqual(abrupt[-1], '#') + run_python_file("abrupt.py", ["abrupt.py"]) + self.assertEqual(self.stdout(), "a is 1\n") + def test_no_such_file(self): self.assertRaises(NoSource, run_python_file, "xyzzy.py", []) |