Anvil Community Forum

Skulpt limitations on SyntaxError?

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))

Moved to bug reports.

In short - yes this is a limitation of skulpt.

It’s on my back burner list of features to add to skulpt.

Internally this is implemented - else you’d never see tracebacks in anvil - but it’s not exposed in the same way that python exposes its traceback :disappointed: .

Edit: as a hugely inconvenient workaround you could re exec the code on the server to get the correct traceback details. You get the benefit of client side execution when it works and only do the slow thing for SyntaxErrors.

1 Like

The workaround works, thanks, and the amount of code I’ll be dealing with will be small, so it doesn’t introduce that much of a delay. It’ll definitely work until Skulpt returns the same information.

Edit: Any chance of getting traceback support in general in Skulpt? It’d be great if, in a function, I could know the line number of the code currently being executed.

I faced the same problems with my project as well. Skulpt is very limiting when it comes to this. But here is a work around.

Don’t ask me the logic but when you do something like:

except SyntaxError as e:
        self.label_1.text=e

It will display you the line of error

Now if you wish to extract this string, you will have to do this

import anvil.js

dom_node=anvil.js.get_dom_node(self.label_1)
error=dom_node.innerText
1 Like

That’s bizarre! str(e) does not return the same thing that e does when it’s assigned to the label text. Skulpt must be doing something different with the two cases.

Thanks, I’ll try that and see if I can avoid the server call for syntax errors.

My guess is that javascript treats Error types different than Python which causes this conflict.

The hack tickles the underlying Skulpt architecture in just the right way.
(Its string representation in Javascript happens to be different to the string representation in Python).

Worth noting that it’s somewhat fragile and is likely to stop working at some point in the future.
Though that would likely coincide with supporting a python way to access the formatted traceback.

I suppose implementing the Traceback module in Skulpt will solve the problem fully. But till then, this hack can do the job

How shall we report BUGS we find in the New Interface?

Just report bugs by creating a Q&A post.
If it’s specific to the beta IDE it’s helpful to write [Beta] as the prefix to the thread title.

If it’s a genuine bug we’ll move it to bug reports.

1 Like