flexxui / pscript

Python to JavaScript compiler
http://pscript.readthedocs.io
BSD 2-Clause "Simplified" License
256 stars 25 forks source link

Debugging ? #49

Open ed2050 opened 3 years ago

ed2050 commented 3 years ago

Hey guys, great project! I'm impressed by how clean and syntax-complete this is. Several other python-to-js solutions are a pain to use or break python syntax in unexpected ways.

Question (since I don't see a discussion board or mailing list):

How can I debug pscript programs? Running the compiled js gives error messages in the console that don't relate to my python code at all. Is there some magic trick to get python line numbers in error messages? Any other suggestions for how to debug? Running in cpython only goes so far, the browser / DOM data don't exist there.

Thanks, best wishes.

nathanielatom commented 3 years ago

It's been awhile since I've used it, but I think you can use the JavaScript debugger command, just add it to a new line in your pscript similar to how breakpoint() would be called in python. Then if you open the browser developer tools console and reload / execute the relevant code it should trigger the debugger and allow you to step through the transpiled JS and see the values of variables. There's also good old-fashioned print() debugging, which logs to the browser console; gl :)

ed2050 commented 3 years ago

Thanks, I'll give it a try. Been using print debugging (or in this case, log debugging) for too long. :)

Anyone know how to generate source maps with pscript? Some transpilers generate source maps so that errors indicate the line in the original python code instead of a line in the javascript code. Would be very handy if pscript supports that. Thanks.

almarklein commented 3 years ago

Yeah, at the moment you'll have to debug the JavaScript that has been generated and then figure out how to update your Python code. This can be a painful task, I know.

Source maps would be great, but PScript does not produce these. They would not fully solve the problem, because there's still several JS gotcha's to take into account, but at least it would make it easier what Python code is failing.

ed2050 commented 3 years ago

No worries Almar. It's going quite well, actually. Very nice that pscript preserves the python method names. Easy to see the call chain where things went wrong. Having short methods helps too. :)

By contrast, brython reproduces python runtime errors almost exactly - yet can still be very difficult to debug. Because the parser emits pages and pages of unintelligible javascript class errors on bad syntax. It can be a real pain finding that one misplaced ] in a long file. More than once I've had to resort to binary search debugging: comment out half the code, see if the error occurs, comment out half the remaining code, repeat ad nauseum. :)

almarklein commented 3 years ago

This is our punishment for trying to run Python instead of JavaScript in a browser :)