https://github.com/python/cpython/commit/7c87ce777b3fd9055b118a58ec8614901ecb45e9
commit: 7c87ce777b3fd9055b118a58ec8614901ecb45e9
branch: main
author: Radislav Chugunov <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-05-09T12:09:44Z
summary:
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.
files:
A Misc/NEWS.d/next/Library/2023-04-28-09-54-15.gh-issue-103956.EyLDPS.rst
M Lib/test/test_trace.py
M Lib/trace.py
diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py
index c1e289bcaff9e5..93966ee31d0a01 100644
--- a/Lib/test/test_trace.py
+++ b/Lib/test/test_trace.py
@@ -6,6 +6,7 @@
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 @@ def test_run_as_module(self):
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 7886959fa64f68..64fc8037e355ee 100755
--- a/Lib/trace.py
+++ b/Lib/trace.py
@@ -565,8 +565,12 @@ def localtrace_trace_and_count(self, frame, why, arg):
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 @@ def localtrace_trace(self, frame, why, arg):
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):
diff --git
a/Misc/NEWS.d/next/Library/2023-04-28-09-54-15.gh-issue-103956.EyLDPS.rst
b/Misc/NEWS.d/next/Library/2023-04-28-09-54-15.gh-issue-103956.EyLDPS.rst
new file mode 100644
index 00000000000000..4ce1491ffa91e2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-04-28-09-54-15.gh-issue-103956.EyLDPS.rst
@@ -0,0 +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.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]