Update trace.py from 3.14.3 (#7327)

* Update `trace.py` from 3.14.3

* Mark failing tests
This commit is contained in:
Shahar Naveh
2026-03-03 21:38:58 +01:00
committed by GitHub
parent 1d106c3411
commit bc108dfe86
2 changed files with 11 additions and 14 deletions

View File

@@ -1,7 +1,7 @@
import os
from pickle import dump
import sys
from test.support import captured_stdout, requires_resource, requires_gil_enabled
from test.support import captured_stdout, requires_resource
from test.support.os_helper import (TESTFN, rmtree, unlink)
from test.support.script_helper import assert_python_ok, assert_python_failure
import textwrap
@@ -307,9 +307,9 @@ class TestFuncs(unittest.TestCase):
}
self.assertEqual(self.tracer.results().calledfuncs, expected)
@unittest.expectedFailure # TODO: RUSTPYTHON
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
'pre-existing trace function throws off measurements')
@requires_gil_enabled("gh-117783: immortalization of types affects traced method names")
def test_inst_method_calling(self):
obj = TracedClass(20)
self.tracer.runfunc(obj.inst_method_calling, 1)
@@ -341,9 +341,9 @@ class TestCallers(unittest.TestCase):
self.tracer = Trace(count=0, trace=0, countcallers=1)
self.filemod = my_file_and_modname()
@unittest.expectedFailure # TODO: RUSTPYTHON
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
'pre-existing trace function throws off measurements')
@requires_gil_enabled("gh-117783: immortalization of types affects traced method names")
def test_loop_caller_importing(self):
self.tracer.runfunc(traced_func_importing_caller, 1)
@@ -424,7 +424,7 @@ class TestCoverage(unittest.TestCase):
coverage = {}
for line in stdout:
lines, cov, module = line.split()[:3]
coverage[module] = (int(lines), int(cov[:-1]))
coverage[module] = (float(lines), float(cov[:-1]))
# XXX This is needed to run regrtest.py as a script
modname = trace._fullmodname(sys.modules[modname].__file__)
self.assertIn(modname, coverage)
@@ -568,7 +568,7 @@ class TestCommandLine(unittest.TestCase):
stdout = stdout.decode()
self.assertEqual(status, 0)
self.assertIn('lines cov% module (path)', stdout)
self.assertIn(f'6 100% {modulename} ({filename})', stdout)
self.assertIn(f'6 100.0% {modulename} ({filename})', stdout)
def test_run_as_module(self):
assert_python_ok('-m', 'trace', '-l', '--module', 'timeit', '-n', '1')

15
Lib/trace.py vendored Executable file → Normal file
View File

@@ -1,5 +1,3 @@
#!/usr/bin/env python3
# portions copyright 2001, Autonomous Zones Industries, Inc., all rights...
# err... reserved and offered to the public under the terms of the
# Python 2.2 license.
@@ -281,14 +279,13 @@ class CoverageResults:
n_hits, n_lines = self.write_results_file(coverpath, source,
lnotab, count, encoding)
if summary and n_lines:
percent = int(100 * n_hits / n_lines)
sums[modulename] = n_lines, percent, modulename, filename
sums[modulename] = n_lines, n_hits, modulename, filename
if summary and sums:
print("lines cov% module (path)")
for m in sorted(sums):
n_lines, percent, modulename, filename = sums[m]
print("%5d %3d%% %s (%s)" % sums[m])
n_lines, n_hits, modulename, filename = sums[m]
print(f"{n_lines:5d} {n_hits/n_lines:.1%} {modulename} ({filename})")
if self.outfile:
# try and store counts and module info into self.outfile
@@ -402,7 +399,7 @@ class Trace:
@param countfuncs true iff it should just output a list of
(filename, modulename, funcname,) for functions
that were called at least once; This overrides
`count' and `trace'
'count' and 'trace'
@param ignoremods a list of the names of modules to ignore
@param ignoredirs a list of the names of directories to ignore
all of the (recursive) contents of
@@ -534,7 +531,7 @@ class Trace:
def globaltrace_lt(self, frame, why, arg):
"""Handler for call events.
If the code block being entered is to be ignored, returns `None',
If the code block being entered is to be ignored, returns 'None',
else returns self.localtrace.
"""
if why == 'call':
@@ -607,7 +604,7 @@ class Trace:
def main():
import argparse
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(color=True)
parser.add_argument('--version', action='version', version='trace 2.0')
grp = parser.add_argument_group('Main options',