-
Notifications
You must be signed in to change notification settings - Fork 510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stack printing: use JS stacktrace instead of building one by hand. #2476
Comments
I created a stackoverflow question to see if there is a better way to identify the script. |
One issue is that we won't have access to I'm not familiar enough with all of Python internals. The JS->Python stack conversion, in association of Building a python Exception could be done using :
Alternatively, |
Hi David, I don't think using the JS stack trace to generate the complete Python error message is possible. Besides the (difficult) issues you raise, you have to take into account that not all browsers generate the same JS trace. With this JS code function f(){
a
}
f() Firefox gives this stack
while Chrome and Edge give
So you would have to parse the JS stack with different options... and you can't be sure that a browser won't change the format without notice, this is not standardized. |
Hum, I understand. Though, by looking around a bit, I found a really interesting thing: I'll have to test it tomorrow, but it could be interesting to add it to the generated JS, to maybe improve debugging on the generated JS ? Also, for reference, V8 offers a way to format the stacks, cf https://v8.dev/docs/stack-trace-api . Unfortunately, Firefox doesn't use V8, but MonkeySpider. |
Yep, this works quite well (Firefox and Chrome) : new Function(`//# sourceURL=test.js
throw new Error()`)() E.g. in FF:
Instead of :
If click on it on Chrome we can also see the line in the JS code. I'm also wondering if, technically, we could even use the source map directive to show instead the Python code in the Browser. I won't test it but it'll be funny if it were the case xD. |
Hi,
In the past, Brython had some issues related to stack printing, often due to JS errors not properly handled by Brython.
I think that using JS stack and to "convert it", would be more reliable and easier.
1. Get the JS stack:
You can get a stack with :
2. Identifies the Brython script in the stack trace:
A stacktrace line corresponding to Brython code, should looks something like that :
The part
Function:41:10
identifies a position in the generated JS code (line and column).You may need to offset the line and column (in my case, I use line-2, and col-1).
Identifying the Python script is a little harder as different python code could produce the exact same stacktrace and I wonder how it'll behave with python imports.
It seems that building a Function inside a Function:
is reflected in the stacktrace:
Maybe there is a way to add lines as a way to identifies the python script in order to get something like ?
This is kind of a dirty h4ck, but I do not see how to do otherwise.
Maybe putting the generated JS code into a
<script>
tag to execute it would be reflected in the stacktrace and would be cleaner ?3. Translate JS code position to Python code position:
There are several ways of doing so:
With the 2 to 4 ways, I think we could remove all brython position array "[0,0,0]" in the generated JS code.
Maybe, when trace is disabled, some functions call could also be removed.
The 4th solution is, I think, the best one.
The text was updated successfully, but these errors were encountered: