Repository navigation
py: clean up lexer and reader if an import fails - #12369
Conversation
|
This will fix #3843. |
|
Code size report: |
Codecov Report
@@ Coverage Diff @@
## master #12369 +/- ##
=======================================
Coverage 98.38% 98.38%
=======================================
Files 158 158
Lines 20933 20945 +12
=======================================
+ Hits 20595 20607 +12
Misses 338 338
|
There was a problem hiding this comment.
Instead of changing the signature of mp_lexer_free how about creating a macro...
#define MP_DEFINE_NLR_CALLBACK_FUNCTION_1(ctx, f, d) \
nlr_jump_callback_node_call_function_1_t ctx = { \
.func = (void (*)(void *))(f), \
.arg = (void *)(d), \
}Then usage is also less verbose.
| nlr_jump_callback_node_call_function_1_t ctx; | |
| ctx.func = mp_lexer_free; | |
| ctx.arg = lex; | |
| MP_DEFINE_NLR_CALLBACK_FUNCTION_1(ctx, eader->close, reader->data); |
There was a problem hiding this comment.
Yeah, happy to make this change.
The reason I changed the signature was because I didn't want to risk any edge-case C incompatibility when casting a function to a different function pointer. I'm actually not sure of the rules here, do you know what is legal/allowed by the C standard with respect to casting function pointers?
There was a problem hiding this comment.
I'm not aware of any compiler restrictions on casting functions pointers (e.g. https://stackoverflow.com/a/66206802/1976323). And I've seen plenty of code (including CPython extensions) that make casts like this. So I don't think there is anything to worry about here. We do lose some type safety, but it doesn't seem like something that would be easy to mess up in the first place.
Signed-off-by: Damien George <[email protected]>
Fixes issue micropython#3843. Signed-off-by: Damien George <[email protected]>
Fixes issue micropython#3874. Signed-off-by: Damien George <[email protected]>
53d4788 to
397697a
Compare
This is an alternative to #3874 that uses the new nlr-jump-callback feature to make sure lexers are freed, and readers closed, if an exception is raised during the loading of a file.