diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-07-11 08:49:20 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-07-11 08:49:20 -0400 |
commit | 1bbf2cd42d6d86ac71bb429fef320af92f1ef3d6 (patch) | |
tree | 6b20aeffbc31cb07e7427948f9998daa12fa51c1 | |
parent | 72d59223f8fc096576ee4fe9c6b5684ee066123a (diff) | |
download | python-coveragepy-git-1bbf2cd42d6d86ac71bb429fef320af92f1ef3d6.tar.gz |
If __file__ disagrees with the frame, use the frame info. Fixes #380.
-rw-r--r-- | CHANGES.txt | 6 | ||||
-rw-r--r-- | coverage/control.py | 8 | ||||
-rw-r--r-- | coverage/debug.py | 23 | ||||
-rw-r--r-- | coverage/misc.py | 6 | ||||
-rw-r--r-- | tests/test_oddball.py | 31 |
5 files changed, 64 insertions, 10 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index ee32ffcf..136300fb 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -13,8 +13,12 @@ Latest - Plugins are now initialized differently. Instead of looking for a class called ``Plugin``, coverage looks for a function called ``coverage_init``. +- Code that was executed with `exec` would be mis-attributed to the file that + called it. This is now fixed, closing `issue 380`_. -.. 40 issues closed in 4.0 so far +.. 41 issues closed in 4.0 so far + +.. _issue 380: https://bitbucket.org/ned/coveragepy/issues/380/code-executed-by-exec-excluded-from Version 4.0a6 --- 21 June 2015 diff --git a/coverage/control.py b/coverage/control.py index 1dbf0672..91365e11 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -449,10 +449,12 @@ class Coverage(object): if dunder_file: filename = self._source_for_file(dunder_file) if original_filename and not original_filename.startswith('<'): - if os.path.basename(original_filename) != os.path.basename(filename): + orig = os.path.basename(original_filename) + if orig != os.path.basename(filename): # Files shouldn't be renamed when moved. This happens when - # exec'ing code, not sure why yet. - self._warn("File was renamed?: %r became %r" % (original_filename, filename)) + # exec'ing code. If it seems like something is wrong with + # the frame's filename, then just use the original. + filename = original_filename if not filename: # Empty string is pretty useless. diff --git a/coverage/debug.py b/coverage/debug.py index 5b41bc40..fbffc569 100644 --- a/coverage/debug.py +++ b/coverage/debug.py @@ -1,5 +1,6 @@ """Control of and utilities for debugging.""" +import inspect import os @@ -66,3 +67,25 @@ def info_formatter(info): prefix = "" else: yield "%*s: %s" % (label_len, label, data) + + +def short_stack(): # pragma: debugging + """Return a string summarizing the call stack. + + The string is multi-line, with one line per stack frame. Each line shows + the function name, the file name, and the line number: + + ... + start_import_stop : /Users/ned/coverage/trunk/tests/coveragetest.py @95 + import_local_file : /Users/ned/coverage/trunk/tests/coveragetest.py @81 + import_local_file : /Users/ned/coverage/trunk/coverage/backward.py @159 + ... + + """ + stack = inspect.stack()[:0:-1] + return "\n".join("%30s : %s @%d" % (t[3], t[1], t[2]) for t in stack) + + +def dump_stack_frames(): # pragma: debugging + """Print a summary of the stack to stdout.""" + print(short_stack()) diff --git a/coverage/misc.py b/coverage/misc.py index 21b7333c..b99fbb81 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -75,12 +75,6 @@ def format_lines(statements, lines): return ret -def short_stack(): # pragma: debugging - """Return a string summarizing the call stack.""" - stack = inspect.stack()[:0:-1] - return "\n".join("%30s : %s @%d" % (t[3], t[1], t[2]) for t in stack) - - def expensive(fn): """A decorator to cache the result of an expensive operation. diff --git a/tests/test_oddball.py b/tests/test_oddball.py index 9fdc654d..f4410da7 100644 --- a/tests/test_oddball.py +++ b/tests/test_oddball.py @@ -400,3 +400,34 @@ class GettraceTest(CoverageTest): f = 12 ''', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], "") + + +class ExecTest(CoverageTest): + """Tests of exec.""" + def test_correct_filename(self): + # https://bitbucket.org/ned/coveragepy/issues/380/code-executed-by-exec-excluded-from + # Bug was that exec'd files would have their lines attributed to the + # calling file. Make two files, both with ~30 lines, but no lines in + # common. Line 30 in to_exec.py will be recorded as line 30 in main.py. + self.make_file("to_exec.py", """\ + \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n + print("var is {}".format(var)) # line 31 + """) + self.make_file("main.py", """\ + namespace = {'var': 17} + with open("to_exec.py") as to_exec_py: + code = compile(to_exec_py.read(), 'to_exec.py', 'exec') + exec(code, globals(), namespace) + \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n + print("done") # line 35 + """) + + cov = coverage.coverage() + self.start_import_stop(cov, "main") + + _, statements, missing, _ = cov.analysis("main.py") + self.assertEqual(statements, [1, 2, 3, 4, 35]) + self.assertEqual(missing, []) + _, statements, missing, _ = cov.analysis("to_exec.py") + self.assertEqual(statements, [31]) + self.assertEqual(missing, []) |