Skip to content

Commit cf265dd

Browse files
authored
Merge pull request #106 from Ritsuka314/fix-issue-73
fix #73: correct line number in CompilationError message
2 parents 4b707b2 + fa494a0 commit cf265dd

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

src/pydsl/compiler.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,32 @@ class Source:
7070
embeded: bool
7171
filepath: typing.Optional[Path] = None
7272
embedee_filepath: typing.Optional[Path] = None
73+
lineno: typing.Optional[int] = None
7374

74-
def init_embeded(src_str: str, filepath: Path, src_ast=None) -> "Source":
75+
def init_embeded(
76+
src_str: str, filepath: Path, src_ast=None, lineno=None
77+
) -> "Source":
7578
src_ast = src_ast if src_ast is not None else ast.parse(src_str)
7679
return Source(
7780
src_str,
7881
src_ast,
7982
embeded=True,
8083
filepath=None,
8184
embedee_filepath=filepath,
85+
lineno=lineno,
8286
)
8387

84-
def init_file(src_str: str, filepath: Path, src_ast=None) -> "Source":
88+
def init_file(
89+
src_str: str, filepath: Path, src_ast=None, lineno=None
90+
) -> "Source":
8591
src_ast = src_ast if src_ast is not None else ast.parse(src_str)
8692
return Source(
8793
src_str,
8894
src_ast,
8995
embeded=False,
9096
filepath=filepath,
9197
embedee_filepath=None,
98+
lineno=lineno,
9299
)
93100

94101
@property
@@ -167,15 +174,16 @@ def programmer_message(self):
167174
else ""
168175
)
169176

177+
base_lineno = 0
170178
if self.src is not None:
171179
file_descriptor = f"{self.src.path}"
172-
if self.src.embeded:
173-
file_descriptor = f"<embed in {file_descriptor}>"
180+
if self.src.lineno is not None:
181+
base_lineno = self.src.lineno
174182
else:
175183
file_descriptor = "<unknown>"
176184

177185
if hasattr(self.node, "lineno"):
178-
line_descriptor = str(self.node.lineno)
186+
line_descriptor = str(self.node.lineno + base_lineno)
179187
else:
180188
line_descriptor = "<unknown>"
181189

src/pydsl/frontend.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,11 +1307,16 @@ def src_ast(self) -> AST:
13071307
def filepath(self) -> Path:
13081308
return Path(inspect.getsourcefile(self._o))
13091309

1310+
@property
1311+
def lineno(self) -> int:
1312+
return inspect.getsourcelines(self._o)[1]
1313+
13101314
@cache
13111315
def get_src(self) -> Source:
13121316
return Source.init_embeded(
13131317
src_str=self.src_str,
13141318
filepath=self.filepath,
1319+
lineno=self.lineno,
13151320
)
13161321

13171322
def emit_mlir(self) -> str:
@@ -1431,6 +1436,7 @@ def get_src(self) -> Source:
14311436
src_str=self.src_str,
14321437
src_ast=self.src_ast,
14331438
filepath=self.filepath,
1439+
lineno=self.lineno,
14341440
)
14351441

14361442
def finalize(self) -> None:

tests/e2e/test_error_msg.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from pydsl.compiler import CompilationError
2+
from pydsl.frontend import compile
3+
from helper import run
4+
5+
import os
6+
import inspect
7+
8+
9+
def test_error_msg():
10+
try:
11+
12+
@compile()
13+
def f():
14+
return a
15+
16+
except CompilationError as e:
17+
current_line = inspect.currentframe().f_lineno
18+
# the number 2 is the relative position
19+
# between inspect.currentframe() and the line `return a`
20+
error_line = current_line - 3
21+
22+
current_file_path = os.path.abspath(__file__)
23+
24+
expected_msg = f'File "{current_file_path}", line {error_line}'
25+
26+
assert e.programmer_message().startswith(expected_msg), (
27+
f"expect error message:\n"
28+
f"{expected_msg}\n"
29+
f"got\n"
30+
f"{e.programmer_message()}"
31+
)
32+
else:
33+
assert False, f"expected CompilationError, but no error was raised"
34+
35+
36+
if __name__ == "__main__":
37+
run(test_error_msg)

0 commit comments

Comments
 (0)