Simplify pythonexc2js and add more careful error handling #1131
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I simplified the logic in
pythonexc2js
and implemented better error handling. I'm pretty sure that the original version actually always leaks the exception state becausePyErr_Fetch
takes ownership oftype
,value
, andtraceback
but it never decref'd them.Another issue with the original code is that it calls
PyErr_Print()
when the error indicator is unset. The docs say thatPyErr_Print()
may cause a fatal system error if called when the error indicator is unset:https://docs.python.org/3/c-api/exceptions.html?highlight=pyerr_print#c.PyErr_PrintEx
This is not apparently true, looking at the source code of
PyErr_Print
, there is a guard check whether the error indicator is set, if not just does nothing. (This is really what it should do, not like it costs much to be reasonable.) Anyways, because the documented behavior ofPyErr_Print
says it triggers a fatal error in this case, I figure we ought to try to avoid calling it in that condition.In future PRs I am hoping to update the error formatting to alternate between Python and javascript stack traces as the code crosses between languages and to implement fatal error trapping.