Skip to content

Commit 7c87ce7

Browse files
authored
gh-103956: Fix trace output in case of missing source line (GH-103958)
Print only filename with lineno if linecache.getline() returns an empty string.
1 parent da090f1 commit 7c87ce7

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

Lib/test/test_trace.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from test.support.script_helper import assert_python_ok, assert_python_failure
77
import textwrap
88
import unittest
9+
from types import FunctionType
910

1011
import trace
1112
from trace import Trace
@@ -559,5 +560,29 @@ def test_run_as_module(self):
559560
assert_python_failure('-m', 'trace', '-l', '--module', 'not_a_module_zzz')
560561

561562

563+
class TestTrace(unittest.TestCase):
564+
def setUp(self):
565+
self.addCleanup(sys.settrace, sys.gettrace())
566+
self.tracer = Trace(count=0, trace=1)
567+
self.filemod = my_file_and_modname()
568+
569+
def test_no_source_file(self):
570+
filename = "<unknown>"
571+
co = traced_func_linear.__code__
572+
co = co.replace(co_filename=filename)
573+
f = FunctionType(co, globals())
574+
575+
with captured_stdout() as out:
576+
self.tracer.runfunc(f, 2, 3)
577+
578+
out = out.getvalue().splitlines()
579+
firstlineno = get_firstlineno(f)
580+
self.assertIn(f" --- modulename: {self.filemod[1]}, funcname: {f.__code__.co_name}", out[0])
581+
self.assertIn(f"{filename}({firstlineno + 1})", out[1])
582+
self.assertIn(f"{filename}({firstlineno + 2})", out[2])
583+
self.assertIn(f"{filename}({firstlineno + 3})", out[3])
584+
self.assertIn(f"{filename}({firstlineno + 4})", out[4])
585+
586+
562587
if __name__ == '__main__':
563588
unittest.main()

Lib/trace.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,12 @@ def localtrace_trace_and_count(self, frame, why, arg):
565565
if self.start_time:
566566
print('%.2f' % (_time() - self.start_time), end=' ')
567567
bname = os.path.basename(filename)
568-
print("%s(%d): %s" % (bname, lineno,
569-
linecache.getline(filename, lineno)), end='')
568+
line = linecache.getline(filename, lineno)
569+
print("%s(%d)" % (bname, lineno), end='')
570+
if line:
571+
print(": ", line, end='')
572+
else:
573+
print()
570574
return self.localtrace
571575

572576
def localtrace_trace(self, frame, why, arg):
@@ -578,8 +582,12 @@ def localtrace_trace(self, frame, why, arg):
578582
if self.start_time:
579583
print('%.2f' % (_time() - self.start_time), end=' ')
580584
bname = os.path.basename(filename)
581-
print("%s(%d): %s" % (bname, lineno,
582-
linecache.getline(filename, lineno)), end='')
585+
line = linecache.getline(filename, lineno)
586+
print("%s(%d)" % (bname, lineno), end='')
587+
if line:
588+
print(": ", line, end='')
589+
else:
590+
print()
583591
return self.localtrace
584592

585593
def localtrace_count(self, frame, why, arg):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix lack of newline characters in :mod:`trace` module output when line tracing is enabled but source code line for current frame is not available.

0 commit comments

Comments
 (0)