Add helper function to get module name taking packages into account.

This commit is contained in:
Jeremy Hylton 2003-04-21 22:49:17 +00:00
parent 38732e1065
commit dfbfe736ce
1 changed files with 22 additions and 1 deletions

View File

@ -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 == "<string>":
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)