Skip to content

py: clean up lexer and reader if an import fails - #12369

Merged
dpgeorge merged 3 commits into
micropython:masterfrom
dpgeorge:py-import-close-on-failure
Sep 14, 2023
Merged

dpgeorge merged 3 commits into
micropython:masterfrom
dpgeorge:py-import-close-on-failure

Conversation

@dpgeorge

@dpgeorge dpgeorge commented Sep 5, 2023

Copy link
Copy Markdown
Member

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.

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Sep 5, 2023
@dpgeorge

dpgeorge commented Sep 5, 2023

Copy link
Copy Markdown
Member Author

This will fix #3843.

@github-actions

github-actions Bot commented Sep 5, 2023 •

Copy link
Copy Markdown

Code size report:

   bare-arm:   +28 +0.049% 
minimal x86:  +128 +0.069% 
   unix x64:  +176 +0.022% standard
      stm32:   +64 +0.016% PYBV10
     mimxrt:   +56 +0.016% TEENSY40
        rp2:   +88 +0.027% RPI_PICO
       samd:   +64 +0.025% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@codecov

codecov Bot commented Sep 5, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #12369 (53d4788) into master (dc99840) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 53d4788 differs from pull request most recent head 397697a. Consider uploading reports for the commit 397697a to get more accurate results

@@           Coverage Diff           @@
##           master   #12369   +/-   ##
=======================================
  Coverage   98.38%   98.38%           
=======================================
  Files         158      158           
  Lines       20933    20945   +12     
=======================================
+ Hits        20595    20607   +12     
  Misses        338      338           
Files Changed Coverage Δ
py/runtime.h 100.00% <ø> (ø)
py/lexer.c 98.92% <100.00%> (+<0.01%) ⬆️
py/parse.c 99.25% <100.00%> (+<0.01%) ⬆️
py/persistentcode.c 96.61% <100.00%> (+0.07%) ⬆️
py/runtime.c 98.90% <100.00%> (+<0.01%) ⬆️

Comment thread py/parse.c Outdated
Comment on lines 1027 to 1029

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@dlech dlech Sep 6, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this change.

@dpgeorge
dpgeorge force-pushed the py-import-close-on-failure branch from 53d4788 to 397697a Compare September 14, 2023 03:04
@dpgeorge
dpgeorge merged commit 397697a into micropython:master Sep 14, 2023
@dpgeorge
dpgeorge deleted the py-import-close-on-failure branch September 14, 2023 03:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py-core Relates to py/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants