1. Ned Batchelder
  2. coverage.py

Commits

Ned Batchelder  committed 0adc21b

No need for paren-less exec any more.

  • Participants
  • Parent commits 7f012cf
  • Branches default

Comments (0)

Files changed (4)

File TODO.txt

View file
     - .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

File coverage/backward.py

View file
         """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.

File coverage/execfile.py

View file
 
 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
 
 
 
         # 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.
         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

File tests/test_coverage.py

View file
             """,
             [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("""\