From dfbfe736cee70efd27cc019be2fcc2d972cdf654 Mon Sep 17 00:00:00 2001 From: Jeremy Hylton Date: Mon, 21 Apr 2003 22:49:17 +0000 Subject: [PATCH] Add helper function to get module name taking packages into account. --- Lib/trace.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Lib/trace.py b/Lib/trace.py index 6661627c8e9..0a063c7638d 100644 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -157,10 +157,29 @@ class Ignore: def modname(path): """Return a plausible module name for the patch.""" + base = os.path.basename(path) filename, ext = os.path.splitext(base) return filename +def fullmodname(path): + """Return a plausible module name for the patch.""" + + # If the file 'path' is part of a package, then the filename isn't + # enough to uniquely identify it. Try to do the right thing by + # looking in sys.path for the longest matching prefix. We'll + # assume that the rest is the package name. + + longest = "" + for dir in sys.path: + if path.startswith(dir) and path[len(dir)] == os.path.sep: + if len(dir) > len(longest): + longest = dir + + base = path[len(longest) + 1:].replace("/", ".") + filename, ext = os.path.splitext(base) + return filename + class CoverageResults: def __init__(self, counts=None, calledfuncs=None, infile=None, outfile=None): @@ -225,7 +244,7 @@ class CoverageResults: # skip some "files" we don't care about... if filename == "": continue - modulename = modname(filename) + modulename = fullmodname(filename) if filename.endswith(".pyc") or filename.endswith(".pyo"): filename = filename[:-1] @@ -470,6 +489,8 @@ class Trace: code = frame.f_code filename = code.co_filename if filename: + # XXX modname() doesn't work right for packages, so + # the ignore support won't work right for packages modulename = modname(filename) if modulename is not None: ignore_it = self.ignore.names(filename, modulename)