Skip to content

Commit d3756a1

Browse files
committed
Fix lineno offset across python version
Previously call_site.f_lineno was used but this definition changed by an offset of 1 in Python 3.11 You can verify this, e.g., by running this script: class MyDecorator: def __call__(self, func): frame = inspect.currentframe() call_site = frame.f_back func_def_line = func.__code__.co_firstlineno if sys.version_info < (3, 11): assert call_site.f_lineno - func_def_line == 1 else: assert call_site.f_lineno - func_def_line == 2 return func decorator = MyDecorator() @decorator # Line 26 def test_func(): # Line 27 pass
1 parent cc0bdd7 commit d3756a1

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/kirin/ir/group.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def wrapper(py_func: Callable) -> Method:
216216
f"`{py_func.__name__}` is already defined in the current scope and is not a Method."
217217
)
218218

219-
lineno_offset = call_site_frame.f_lineno - 1
219+
lineno_offset = py_func.__code__.co_firstlineno - 1
220220
file = call_site_frame.f_code.co_filename
221221

222222
code = self.lowering.python_function(py_func, lineno_offset=lineno_offset)

src/kirin/lowering/python/lowering.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def lower_global(self, state: State[ast.AST], node: ast.AST) -> LoweringABC.Resu
138138
def visit(self, state: State[ast.AST], node: ast.AST) -> Result:
139139
if hasattr(node, "lineno"):
140140
state.source = SourceInfo.from_ast(node, state.file)
141-
state.source.offset(state.lineno_offset - 1, state.col_offset)
141+
state.source.offset(state.lineno_offset, state.col_offset)
142142
name = node.__class__.__name__
143143
if name in self.registry.ast_table:
144144
return self.registry.ast_table[name].lower(state, node)
@@ -150,7 +150,7 @@ def generic_visit(self, state: State[ast.AST], node: ast.AST) -> Result:
150150
def visit_Call(self, state: State[ast.AST], node: ast.Call) -> Result:
151151
if hasattr(node.func, "lineno"):
152152
state.source = SourceInfo.from_ast(node, state.file)
153-
state.source.offset(state.lineno_offset - 1, state.col_offset)
153+
state.source.offset(state.lineno_offset, state.col_offset)
154154

155155
global_callee_result = state.get_global(node.func, no_raise=True)
156156
if global_callee_result is None:

0 commit comments

Comments
 (0)