Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Authors
* Alisa Sireneva - https://github.com/purplesyringa
* Michał Górny - https://github.com/mgorny
* Tim Maxwell - https://github.com/tmaxwell-anthropic
* Haoyu Weng - https://github.com/wengh
36 changes: 13 additions & 23 deletions src/tblib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import re
import sys
from types import CodeType

__version__ = '3.0.0'
__all__ = 'Traceback', 'TracebackParseError', 'Frame', 'Code'
Expand Down Expand Up @@ -117,30 +116,21 @@ def as_traceback(self):
current = self
top_tb = None
tb = None
stub = compile(
'raise __traceback_maker',
'<string>',
'exec',
)
while current:
f_code = current.tb_frame.f_code
code = compile('\n' * (current.tb_lineno - 1) + 'raise __traceback_maker', current.tb_frame.f_code.co_filename, 'exec')
if hasattr(code, 'replace'):
# Python 3.8 and newer
code = code.replace(co_argcount=0, co_filename=f_code.co_filename, co_name=f_code.co_name, co_freevars=(), co_cellvars=())
else:
code = CodeType(
0,
code.co_kwonlyargcount,
code.co_nlocals,
code.co_stacksize,
code.co_flags,
code.co_code,
code.co_consts,
code.co_names,
code.co_varnames,
f_code.co_filename,
f_code.co_name,
code.co_firstlineno,
code.co_lnotab,
(),
(),
)
code = stub.replace(
co_firstlineno=current.tb_lineno,
co_argcount=0,
co_filename=f_code.co_filename,
co_name=f_code.co_name,
co_freevars=(),
co_cellvars=(),
)

# noinspection PyBroadException
try:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_tblib.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ def test_parse_traceback():
assert tb4.as_dict() == tb3.as_dict() == tb2.as_dict() == tb1.as_dict() == expected_dict


def test_large_line_number():
line_number = 2**31 - 1
tb1 = Traceback.from_string(
f"""
Traceback (most recent call last):
File "file1", line {line_number}, in <module>
code1
"""
).as_traceback()
assert tb1.tb_lineno == line_number


def test_pytest_integration(testdir):
test = testdir.makepyfile(
"""
Expand Down
Loading