From 589f22ff252104cf27c5741d5174e5dccac55e80 Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Fri, 28 Apr 2023 00:18:36 +0300 Subject: [PATCH 1/4] Fix `Trace.localtrace_trace*` output in case of missing source line check if call to `linecache.getline` returns non-empty string otherwise print only filename with lineno --- Lib/trace.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Lib/trace.py b/Lib/trace.py index 213e46517d683d..b1c44397e5650c 100755 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -559,8 +559,11 @@ 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) + if not line: + print("%s(%d)" % (bname, lineno)) + else: + print("%s(%d): %s" % (bname, lineno, line), end='') return self.localtrace def localtrace_trace(self, frame, why, arg): @@ -572,8 +575,11 @@ 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) + if not line: + print("%s(%d)" % (bname, lineno)) + else: + print("%s(%d): %s" % (bname, lineno, line), end='') return self.localtrace def localtrace_count(self, frame, why, arg): From 83bc542096ca2e5d20a4a57e2ce3155e778092d0 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 28 Apr 2023 09:54:17 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-04-28-09-54-15.gh-issue-103956.EyLDPS.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-04-28-09-54-15.gh-issue-103956.EyLDPS.rst 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. From e840e5fc69a2704c12d47cb4735b4be31f2eb613 Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Sat, 10 Feb 2024 21:23:41 +0300 Subject: [PATCH 3/4] refactor --- Lib/trace.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Lib/trace.py b/Lib/trace.py index b1c44397e5650c..1e3da322b577b0 100755 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -560,10 +560,11 @@ def localtrace_trace_and_count(self, frame, why, arg): print('%.2f' % (_time() - self.start_time), end=' ') bname = os.path.basename(filename) line = linecache.getline(filename, lineno) - if not line: - print("%s(%d)" % (bname, lineno)) + print("%s(%d)" % (bname, lineno), end='') + if line: + print(": ", line, end='') else: - print("%s(%d): %s" % (bname, lineno, line), end='') + print() return self.localtrace def localtrace_trace(self, frame, why, arg): @@ -576,10 +577,11 @@ def localtrace_trace(self, frame, why, arg): print('%.2f' % (_time() - self.start_time), end=' ') bname = os.path.basename(filename) line = linecache.getline(filename, lineno) - if not line: - print("%s(%d)" % (bname, lineno)) + print("%s(%d)" % (bname, lineno), end='') + if line: + print(": ", line, end='') else: - print("%s(%d): %s" % (bname, lineno, line), end='') + print() return self.localtrace def localtrace_count(self, frame, why, arg): From f9fffa8c2471f215b8178f7eea5d40178336df05 Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Sun, 11 Feb 2024 01:46:50 +0300 Subject: [PATCH 4/4] added test --- Lib/test/test_trace.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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 = "" + 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()