diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-05-09 13:05:37 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-09 13:05:37 (GMT) |
commit | c4b853f2a5a4601806f503f1833dce3a4e8d2b58 (patch) | |
tree | e01b6d92f22cdf811929f93d83c4e2110a80809b /Lib | |
parent | d86b49411753bf2c83291e3a14ae43fefded2f84 (diff) | |
download | cpython-c4b853f2a5a4601806f503f1833dce3a4e8d2b58.zip cpython-c4b853f2a5a4601806f503f1833dce3a4e8d2b58.tar.gz cpython-c4b853f2a5a4601806f503f1833dce3a4e8d2b58.tar.bz2 |
[3.13] gh-103956: Fix `trace` output in case of missing source line (GH-103958) (GH-118834)
Print only filename with lineno if linecache.getline() returns an empty string.
(cherry picked from commit 7c87ce777b3fd9055b118a58ec8614901ecb45e9)
Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_trace.py | 25 | ||||
-rwxr-xr-x | Lib/trace.py | 16 |
2 files changed, 37 insertions, 4 deletions
diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py index c1e289b..93966ee 100644 --- a/Lib/test/test_trace.py +++ b/Lib/test/test_trace.py @@ -6,6 +6,7 @@ from test.support.os_helper import (TESTFN, rmtree, unlink) from test.support.script_helper import assert_python_ok, assert_python_failure import textwrap import unittest +from types import FunctionType import trace from trace import Trace @@ -559,5 +560,29 @@ class TestCommandLine(unittest.TestCase): assert_python_failure('-m', 'trace', '-l', '--module', 'not_a_module_zzz') +class TestTrace(unittest.TestCase): + def setUp(self): + self.addCleanup(sys.settrace, sys.gettrace()) + self.tracer = Trace(count=0, trace=1) + self.filemod = my_file_and_modname() + + def test_no_source_file(self): + filename = "<unknown>" + co = traced_func_linear.__code__ + co = co.replace(co_filename=filename) + f = FunctionType(co, globals()) + + with captured_stdout() as out: + self.tracer.runfunc(f, 2, 3) + + out = out.getvalue().splitlines() + firstlineno = get_firstlineno(f) + self.assertIn(f" --- modulename: {self.filemod[1]}, funcname: {f.__code__.co_name}", out[0]) + self.assertIn(f"{filename}({firstlineno + 1})", out[1]) + self.assertIn(f"{filename}({firstlineno + 2})", out[2]) + self.assertIn(f"{filename}({firstlineno + 3})", out[3]) + self.assertIn(f"{filename}({firstlineno + 4})", out[4]) + + if __name__ == '__main__': unittest.main() diff --git a/Lib/trace.py b/Lib/trace.py index 7886959..64fc803 100755 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -565,8 +565,12 @@ class Trace: if self.start_time: print('%.2f' % (_time() - self.start_time), end=' ') bname = os.path.basename(filename) - print("%s(%d): %s" % (bname, lineno, - linecache.getline(filename, lineno)), end='') + line = linecache.getline(filename, lineno) + print("%s(%d)" % (bname, lineno), end='') + if line: + print(": ", line, end='') + else: + print() return self.localtrace def localtrace_trace(self, frame, why, arg): @@ -578,8 +582,12 @@ class Trace: if self.start_time: print('%.2f' % (_time() - self.start_time), end=' ') bname = os.path.basename(filename) - print("%s(%d): %s" % (bname, lineno, - linecache.getline(filename, lineno)), end='') + line = linecache.getline(filename, lineno) + print("%s(%d)" % (bname, lineno), end='') + if line: + print(": ", line, end='') + else: + print() return self.localtrace def localtrace_count(self, frame, why, arg): |