diff options
-rw-r--r-- | TODO.txt | 2 | ||||
-rw-r--r-- | coverage/backward.py | 17 | ||||
-rw-r--r-- | coverage/execfile.py | 8 | ||||
-rw-r--r-- | tests/test_coverage.py | 91 |
4 files changed, 34 insertions, 84 deletions
@@ -22,7 +22,7 @@ Key: - .format() ? + try/except/finally - with assertRaises - - exec statement can look like a function in py2 (since when?) + + exec statement can look like a function in py2 (since when?) + Remove code only run on <2.6 - Change data file to json diff --git a/coverage/backward.py b/coverage/backward.py index 8237d01b..a0dc9027 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -45,23 +45,6 @@ else: """Produce the items from dict `d`.""" return d.iteritems() -# Exec is a statement in Py2, a function in Py3 -if sys.version_info >= (3, 0): - def exec_code_object(code, global_map): - """A wrapper around exec().""" - 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_code_object(code, global_map):\n" - " exec code in global_map\n", - "<exec_function>", "exec" - ) - ) - # Reading Python source and interpreting the coding comment is a big deal. if sys.version_info >= (3, 0): # Python 3.2 provides `tokenize.open`, the best way to open source files. diff --git a/coverage/execfile.py b/coverage/execfile.py index f90096e9..7b90137a 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -2,7 +2,7 @@ import imp, marshal, os, sys -from coverage.backward import exec_code_object, open_source +from coverage.backward import open_source from coverage.misc import ExceptionDuringRun, NoCode, NoSource @@ -99,7 +99,7 @@ def run_python_file(filename, args, package=None): # Execute the code object. try: - exec_code_object(code, main_mod.__dict__) + exec(code, main_mod.__dict__) except SystemExit: # The user called sys.exit(). Just pass it along to the upper # layers, where it will be handled. @@ -107,11 +107,11 @@ def run_python_file(filename, args, package=None): except: # Something went wrong while executing the user code. # Get the exc_info, and pack them into an exception that we can - # throw up to the outer loop. We peel two layers off the traceback + # throw up to the outer loop. We peel one layer off the traceback # so that the coverage.py code doesn't appear in the final printed # traceback. typ, err, tb = sys.exc_info() - raise ExceptionDuringRun(typ, err, tb.tb_next.tb_next) + raise ExceptionDuringRun(typ, err, tb.tb_next) finally: # Restore the old __main__ sys.modules['__main__'] = old_main_mod diff --git a/tests/test_coverage.py b/tests/test_coverage.py index 6eb37e26..0cb33dcd 100644 --- a/tests/test_coverage.py +++ b/tests/test_coverage.py @@ -497,68 +497,35 @@ class SimpleStatementTest(CoverageTest): """, [1,2,3,4,5], "") - if sys.version_info < (3, 0): - # In Python 2.x, exec is a statement. - def test_exec(self): - self.check_coverage("""\ - a = b = c = 1 - exec "a = 2" - exec ("b = " + - "c = " + - "2") - assert a == 2 and b == 2 and c == 2 - """, - [1,2,3,6], "") - self.check_coverage("""\ - vars = {'a': 1, 'b': 1, 'c': 1} - exec "a = 2" in vars - exec ("b = " + - "c = " + - "2") in vars - assert vars['a'] == 2 and vars['b'] == 2 and vars['c'] == 2 - """, - [1,2,3,6], "") - self.check_coverage("""\ - globs = {} - locs = {'a': 1, 'b': 1, 'c': 1} - exec "a = 2" in globs, locs - exec ("b = " + - "c = " + - "2") in globs, locs - assert locs['a'] == 2 and locs['b'] == 2 and locs['c'] == 2 - """, - [1,2,3,4,7], "") - else: - # In Python 3.x, exec is a function. - def test_exec(self): - self.check_coverage("""\ - a = b = c = 1 - exec("a = 2") - exec("b = " + - "c = " + - "2") - assert a == 2 and b == 2 and c == 2 - """, - [1,2,3,6], "") - self.check_coverage("""\ - vars = {'a': 1, 'b': 1, 'c': 1} - exec("a = 2", vars) - exec("b = " + - "c = " + - "2", vars) - assert vars['a'] == 2 and vars['b'] == 2 and vars['c'] == 2 - """, - [1,2,3,6], "") - self.check_coverage("""\ - globs = {} - locs = {'a': 1, 'b': 1, 'c': 1} - exec("a = 2", globs, locs) - exec("b = " + - "c = " + - "2", globs, locs) - assert locs['a'] == 2 and locs['b'] == 2 and locs['c'] == 2 - """, - [1,2,3,4,7], "") + def test_exec(self): + self.check_coverage("""\ + a = b = c = 1 + exec("a = 2") + exec("b = " + + "c = " + + "2") + assert a == 2 and b == 2 and c == 2 + """, + [1,2,3,6], "") + self.check_coverage("""\ + vars = {'a': 1, 'b': 1, 'c': 1} + exec("a = 2", vars) + exec("b = " + + "c = " + + "2", vars) + assert vars['a'] == 2 and vars['b'] == 2 and vars['c'] == 2 + """, + [1,2,3,6], "") + self.check_coverage("""\ + globs = {} + locs = {'a': 1, 'b': 1, 'c': 1} + exec("a = 2", globs, locs) + exec("b = " + + "c = " + + "2", globs, locs) + assert locs['a'] == 2 and locs['b'] == 2 and locs['c'] == 2 + """, + [1,2,3,4,7], "") def test_extra_doc_string(self): self.check_coverage("""\ |