I’m using exec to execute Python code the user typed in. As part of this, I’d like to display meaningful error messages when they have syntax errors in their code.
On a normal Python installation on my desktop, printing out the args attribute of the SyntaxError gives me something like:
('invalid syntax', ('<string>', 1, 4, 'x 10\n'))
It’s a tuple that includes information like the line number and the character offset of the error.
On the client side in Anvil, the args attribute of the SyntaxError gives me:
('bad input',)
That seems to be the generic syntax error message and there’s no indication of line number or offset.
Is this a limitation in Skulpt? Is there some way to invoke exec that will provide the missing line and offset information?
Both of those SyntaxErrors were generated from the same code run in the different environments:
try:
exec("x 10\nprint(x)", {})
except SyntaxError as e:
print("Syntax error: " + str(e.args))