(core) Raise syntax errors that Python can format nicely to show the location

Summary: Update _create_syntax_error_code to raise an error with similar arguments to the real arguments it already has, with our modifications.

Test Plan: Updated python unit tests

Reviewers: jarek, dsagal

Reviewed By: dsagal

Subscribers: dsagal

Differential Revision: https://phab.getgrist.com/D3040
This commit is contained in:
Alex Hall
2021-09-24 15:06:39 +02:00
parent fb583f303a
commit 52fd28815e
6 changed files with 64 additions and 16 deletions

View File

@@ -118,10 +118,11 @@ def _create_syntax_error_code(builder, input_text, err):
output_offset = output_ln.line_to_offset(err.lineno, err.offset - 1 if err.offset else 0)
input_offset = builder.map_back_offset(output_offset)
line, col = input_ln.offset_to_line(input_offset)
message = '%s on line %d col %d' % (err.args[0], line, col + 1)
return "%s\nraise %s(%r)" % (
message = err.args[0]
input_text_line = input_text.splitlines()[line - 1]
return "%s\nraise %s(%r, ('usercode', %r, %r, %r))" % (
textbuilder.line_start_re.sub('# ', input_text.rstrip()),
type(err).__name__, message)
type(err).__name__, message, line, col + 1, input_text_line)
#----------------------------------------------------------------------