Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Lib/test/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,33 @@ def test_loop_caller_importing(self):
}
self.assertEqual(self.tracer.results().calledfuncs, expected)

def test_ignore_mods(self):
tracer = trace.Trace(ignoremods=['testmod'],
count=0, trace=0, countfuncs=1)
tracer.runfunc(traced_func_importing_caller, 1)

expected = {
self.filemod + ('traced_func_simple_caller',): 1,
self.filemod + ('traced_func_linear',): 1,
self.filemod + ('traced_func_importing_caller',): 1,
self.filemod + ('traced_func_importing',): 1,
}
self.assertEqual(tracer.results().calledfuncs, expected)

def test_ignore_dirs(self):
testmod_dir = os.path.dirname(testmod.__file__)
tracer = trace.Trace(ignoredirs=[testmod_dir],
count=0, trace=0, countfuncs=1)
tracer.runfunc(traced_func_importing_caller, 1)

expected = {
self.filemod + ('traced_func_simple_caller',): 1,
self.filemod + ('traced_func_linear',): 1,
self.filemod + ('traced_func_importing_caller',): 1,
self.filemod + ('traced_func_importing',): 1,
}
self.assertEqual(tracer.results().calledfuncs, expected)

@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
'pre-existing trace function throws off measurements')
def test_inst_method_calling(self):
Expand Down Expand Up @@ -357,6 +384,45 @@ def test_loop_caller_importing(self):
}
self.assertEqual(self.tracer.results().callers, expected)

@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
'pre-existing trace function throws off measurements')
def test_ignore_mods(self):
tracer = trace.Trace(ignoremods=['testmod'],
count=0, trace=0, countcallers=1)
tracer.runfunc(traced_func_importing_caller, 1)

expected = {
((os.path.splitext(trace.__file__)[0] + '.py', 'trace', 'Trace.runfunc'),
(self.filemod + ('traced_func_importing_caller',))): 1,
((self.filemod + ('traced_func_simple_caller',)),
(self.filemod + ('traced_func_linear',))): 1,
((self.filemod + ('traced_func_importing_caller',)),
(self.filemod + ('traced_func_simple_caller',))): 1,
((self.filemod + ('traced_func_importing_caller',)),
(self.filemod + ('traced_func_importing',))): 1,
}
self.assertEqual(tracer.results().callers, expected)

@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
'pre-existing trace function throws off measurements')
def test_ignore_dirs(self):
testmod_dir = os.path.dirname(testmod.__file__)
tracer = trace.Trace(ignoredirs=[testmod_dir],
count=0, trace=0, countcallers=1)
tracer.runfunc(traced_func_importing_caller, 1)

expected = {
((os.path.splitext(trace.__file__)[0] + '.py', 'trace', 'Trace.runfunc'),
(self.filemod + ('traced_func_importing_caller',))): 1,
((self.filemod + ('traced_func_simple_caller',)),
(self.filemod + ('traced_func_linear',))): 1,
((self.filemod + ('traced_func_importing_caller',)),
(self.filemod + ('traced_func_simple_caller',))): 1,
((self.filemod + ('traced_func_importing_caller',)),
(self.filemod + ('traced_func_importing',))): 1,
}
self.assertEqual(tracer.results().callers, expected)


# Created separately for issue #3821
class TestCoverage(unittest.TestCase):
Expand Down
10 changes: 10 additions & 0 deletions Lib/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,11 @@ def globaltrace_trackcallers(self, frame, why, arg):
Adds information about who called who to the self._callers dict.
"""
if why == 'call':
filename = frame.f_globals.get('__file__')
if filename:
modulename = _modname(filename)
if self.ignore.names(filename, modulename):
return
# XXX Should do a better job of identifying methods
this_func = self.file_module_function_of(frame)
parent_func = self.file_module_function_of(frame.f_back)
Expand All @@ -528,6 +533,11 @@ def globaltrace_countfuncs(self, frame, why, arg):
Adds (filename, modulename, funcname) to the self._calledfuncs dict.
"""
if why == 'call':
filename = frame.f_globals.get('__file__')
if filename:
modulename = _modname(filename)
if self.ignore.names(filename, modulename):
return
this_func = self.file_module_function_of(frame)
self._calledfuncs[this_func] = 1

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix the :mod:`trace` command-line tool not applying ``--ignore-module`` and
``--ignore-dir`` in the ``--listfuncs`` and ``--trackcalls`` modes. Original
patch by Tom Hines.
Loading