From cac0dda4a9c1a2ab4d2cc306b0c98dfc67c9b756 Mon Sep 17 00:00:00 2001 From: Vasiliy Kiryanov Date: Sun, 22 Nov 2026 20:06:53 -0500 Subject: [PATCH] gh-54894: Apply trace ignore options to --listfuncs and --trackcalls --- Lib/test/test_trace.py | 66 +++++++++++++++++++ Lib/trace.py | 10 +++ ...6-11-22-20-02-57.gh-issue-54894.pkUwBV.rst | 3 + 3 files changed, 79 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-22-20-02-57.gh-issue-54894.pkUwBV.rst diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py index 241e841d732f5e..57041760846e13 100644 --- a/Lib/test/test_trace.py +++ b/Lib/test/test_trace.py @@ -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): @@ -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): diff --git a/Lib/trace.py b/Lib/trace.py index 66471f45e1c004..9cfd89aff417eb 100644 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -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) @@ -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 diff --git a/Misc/NEWS.d/next/Library/2026-11-22-20-02-57.gh-issue-54894.pkUwBV.rst b/Misc/NEWS.d/next/Library/2026-11-22-20-02-57.gh-issue-54894.pkUwBV.rst new file mode 100644 index 00000000000000..1cd4ac4cef5180 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-22-20-02-57.gh-issue-54894.pkUwBV.rst @@ -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.