diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2009-05-05 23:13:17 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2009-05-05 23:13:17 -0400 |
commit | a5c57934960bd5e762b9a8138dc6c575a20d41fd (patch) | |
tree | 5406235e907eaecbe6556b565f740b2fef5e66ed /coverage | |
parent | 6799d42aa4fc14d5f0fe9de2ca1c8af1b9042173 (diff) | |
download | python-coveragepy-git-a5c57934960bd5e762b9a8138dc6c575a20d41fd.tar.gz |
.pyc files can be moved, so use __file__ to correctly find the source file.
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/collector.py | 2 | ||||
-rw-r--r-- | coverage/control.py | 19 | ||||
-rw-r--r-- | coverage/tracer.c | 2 |
3 files changed, 19 insertions, 4 deletions
diff --git a/coverage/collector.py b/coverage/collector.py index b6ca19bd..f290b85b 100644 --- a/coverage/collector.py +++ b/coverage/collector.py @@ -24,7 +24,7 @@ except ImportError: filename = frame.f_code.co_filename tracename = self.should_trace_cache.get(filename) if tracename is None: - tracename = self.should_trace(filename) + tracename = self.should_trace(filename, frame) self.should_trace_cache[filename] = tracename if tracename: # We need to trace. Push the current filename on the stack diff --git a/coverage/control.py b/coverage/control.py index 529f5252..ab9112ef 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -33,7 +33,7 @@ class coverage: import atexit atexit.register(self.save) - def _should_trace(self, filename): + def _should_trace(self, filename, frame): """Decide whether to trace execution in `filename` Returns a canonicalized filename if it should be traced, False if it @@ -44,8 +44,23 @@ class coverage: # There's no point in ever tracing string executions, we can't do # anything with the data later anyway. return False - + + # Compiled Python files have two filenames: frame.f_code.co_filename is + # the filename where the .pyc was originally compiled. The second name + # is __file__, which is where the .pyc was actually loaded from. Since + # .pyc files can be moved after compilation (for example, by being + # installed), we look for __file__ in the frame and prefer it to the + # co_filename value. + dunder_file = frame.f_globals.get('__file__') + if dunder_file: + filename = dunder_file + if not filename.endswith(".py"): + filename = filename[:-1] + canonical = self.file_locator.canonical_filename(filename) + + # If we aren't supposed to trace the stdlib, then check if this is in + # the stdlib and skip it if so. if not self.cover_stdlib: if canonical.startswith(self.sysprefix): return False diff --git a/coverage/tracer.c b/coverage/tracer.c index 2831264a..bfb27614 100644 --- a/coverage/tracer.c +++ b/coverage/tracer.c @@ -80,7 +80,7 @@ Tracer_trace(Tracer *self, PyFrameObject *frame, int what, PyObject *arg) tracename = PyDict_GetItem(self->should_trace_cache, filename);
if (tracename == NULL) {
// We've never considered this file before. Ask should_trace about it.
- PyObject * args = Py_BuildValue("(O)", filename);
+ PyObject * args = Py_BuildValue("(OO)", filename, frame);
tracename = PyObject_Call(self->should_trace, args, NULL);
Py_DECREF(args);
if (tracename == NULL) {
|