Skip to content

Commit

Permalink
Provide user hint when trying to use c-style type names in pythran spec
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-sans-paille committed Nov 25, 2024
1 parent bd488d2 commit 1b6ffc1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
15 changes: 13 additions & 2 deletions pythran/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,23 @@ def PythranSpecError(self, msg, lexpos=None):

def p_error(self, p):
if p.type == 'IDENTIFIER':
alt = {'double': 'float64',
'void': 'None',
'char': 'int8',
'short': 'int16',
'long': 'int64',
}.get(p.value)
if alt:
hint = " Did you mean `{}`?".format(alt)
else:
hint = ''
raise self.PythranSpecError(
"Unexpected identifier `{}` at that point".format(p.value),
"Unsupported identifier `{}` at that point.{}".format(p.value,
hint),
p.lexpos)
else:
raise self.PythranSpecError(
"Unexpected token `{}` at that point".format(p.value),
"Unexpected token `{}` at that point.".format(p.value),
p.lexpos)

def __init__(self):
Expand Down
2 changes: 1 addition & 1 deletion pythran/tests/notebooks/export.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"<unknown>:2:27 error: Unexpected token `[` at that point\n"
"<unknown>:2:27 error: Unexpected token `[` at that point.\n"
]
}
],
Expand Down
5 changes: 5 additions & 0 deletions pythran/tests/test_spec_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def test_invalid_specs5(self):
with self.assertRaises(pythran.syntax.PythranSyntaxError):
pythran.compile_pythrancode("dumber", code)

def test_invalid_specs_with_hint(self):
code = '#pythran export bar(double)\ndef bar(x):pass'
with self.assertRaises(pythran.syntax.PythranSyntaxError) as e:
pythran.compile_pythrancode("dumber", code)
self.assertTrue('float64' in e.exception.msg)

def test_multiline_spec0(self):
code = '''
Expand Down

0 comments on commit 1b6ffc1

Please sign in to comment.