From 97d6b3fe4c87ab00f62253a2b19a680da214de4f Mon Sep 17 00:00:00 2001 From: fly1d <309400591+fly1d@users.noreply.github.com> Date: Thu, 5 Nov 2026 13:38:27 +0800 Subject: [PATCH] Handle missing function names in parsed tracebacks --- AUTHORS.rst | 1 + CHANGELOG.rst | 5 +++++ src/tblib/__init__.py | 2 +- tests/test_tblib.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 6d7199a..6aaa5c6 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -17,3 +17,4 @@ Authors * Oldřich Jedlička - https://github.com/oldium * Colin Watson - https://github.com/cjwatson * Jacob Tomlinson - https://github.com/jacobtomlinson +* fly1d - https://github.com/fly1d diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ed95b83..c4af14a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,11 @@ Changelog ========= +Unreleased +~~~~~~~~~~ + +* Fixed traceback reconstruction for parsed frames without a function name, such as locations in formatted ``SyntaxError`` output. + 3.2.2 (2025-11-12) ~~~~~~~~~~~~~~~~~~ diff --git a/src/tblib/__init__.py b/src/tblib/__init__.py index 546c327..c451302 100644 --- a/src/tblib/__init__.py +++ b/src/tblib/__init__.py @@ -127,7 +127,7 @@ def as_traceback(self): co_firstlineno=current.tb_lineno, co_argcount=0, co_filename=f_code.co_filename, - co_name=f_code.co_name, + co_name=f_code.co_name if f_code.co_name is not None else stub.co_name, co_freevars=(), co_cellvars=(), ) diff --git a/tests/test_tblib.py b/tests/test_tblib.py index 6d01ebe..f2203c1 100644 --- a/tests/test_tblib.py +++ b/tests/test_tblib.py @@ -57,6 +57,42 @@ def func(my_arg='2'): assert Traceback.from_dict(value).tb_next.tb_frame.f_locals == {'my_variable': 1} +def test_parse_syntax_error_traceback(): + filename = '' + source = 'first = 1\nsecond = 2\nif True print("invalid")\n' + + try: + compile(source, filename, 'exec') + except SyntaxError as exc: + syntax_location = (exc.filename, exc.lineno) + outer_frames = [(frame.filename, frame.lineno, frame.name) for frame in traceback.extract_tb(exc.__traceback__)] + formatted = traceback.format_exc() + else: + raise AssertionError('The invalid source should raise SyntaxError') + + assert syntax_location == (filename, 3) + assert outer_frames + + parsed = Traceback.from_string(formatted) + parsed_frames = [] + current = parsed + while current is not None: + code = current.tb_frame.f_code + parsed_frames.append((code.co_filename, current.tb_lineno, code.co_name)) + current = current.tb_next + + # The parser already includes the bare compiler location without a function name. + assert parsed_frames == [*outer_frames, (*syntax_location, None)] + serialized = parsed.as_dict() + + reconstructed = traceback.extract_tb(parsed.as_traceback()) + reconstructed_frames = [(frame.filename, frame.lineno, frame.name) for frame in reconstructed] + + # Keep the exec stub's native name only where the input did not supply one. + assert reconstructed_frames == [*outer_frames, (*syntax_location, '')] + assert parsed.as_dict() == serialized + + def test_parse_traceback(): tb1 = Traceback.from_string( """