Skip to content

Commit

Permalink
Fix get_stack_info for python3.12 (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
parthchadha authored Dec 3, 2024
1 parent e179c82 commit 913afcd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions tripy/tests/frontend/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_stack_info_is_populated(self, build_func, expected_line_number):
function=tp.Tensor.raw_init.__name__,
code=None,
_dispatch_target="",
column_range=None,
column_range=(25, 30) if sys.version_info >= (3, 11) else None,
)
assert a.stack_info[a.stack_info.get_first_user_frame_index()] == SourceInfo(
__name__,
Expand All @@ -136,7 +136,7 @@ def test_stack_info_is_populated(self, build_func, expected_line_number):
function=build_func.__name__,
code=inspect.getsource(build_func).rstrip(),
_dispatch_target="",
column_range=None,
column_range=(0, 0) if sys.version_info >= (3, 11) else None,
)

def test_eval_of_storage_tensor_is_nop(self):
Expand Down
10 changes: 6 additions & 4 deletions tripy/tripy/utils/stack_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,16 @@ def get_stack_info(include_code_index: int = None) -> StackInfo:
source_info._dispatch_target = frame.f_locals["key"]

try:
# In Python 3.11, frames contain column offset information.
frame.f_code.co_positions
except AttributeError:
pass
else:
# There are twice as many instructions as co_positions()
_, _, start, end = frame.f_code.co_positions()[frame.f_lasti // 2]
source_info.column_range = (start, end)
# For 3.11+, positions is an iterator of (line, end_line, column, end_column)
for pos in frame.f_code.co_positions():
if pos[0] == frame.f_lineno:
_, _, start, end = pos
source_info.column_range = (start, end)
break

stack_info.append(source_info)
frame = frame.f_back
Expand Down

0 comments on commit 913afcd

Please sign in to comment.