You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Free resource before raise syntax error so we can access python file after syntax error. #3843
@dpgeorge, I was about to open an issue for this problem, so I can provide additional information on why I think it's a problem.
mp_lexer_free() calls lex->reader.close(lex->reader.data);. When parsing a file on disk (e.g., foo.py as a result of import foo), we end up keeping a handle open to that file if throwing an exception in mp_parse() or fold_constants(). I'm not sure if other functions called from mp_parse() can throw an exception or not. Perhaps it's safest if mp_parse() has a wrapper that calls mp_lexer_free() and re-throws the exception? I'd like some feedback on that idea before actually implementing it, though. It seems "right" since mp_parse() already takes responsibility for freeing the lexer (and closing the reader) on success.
I have a similar fix I plan to submit for mp_raw_code_load(), the equivalent import code path for loading .mpy files.
@tomlogic thanks for the info. I agree with your analysis, mp_parse() does have the responsibility to free the lexer. If it didn't catch the exception then the caller would need to.
Activity
dpgeorge commentedon Jun 9, 2018
Thanks for the suggestion. What was it exactly that prompted this change?
tomlogic commentedon Jun 17, 2018
@dpgeorge, I was about to open an issue for this problem, so I can provide additional information on why I think it's a problem.
mp_lexer_free()callslex->reader.close(lex->reader.data);. When parsing a file on disk (e.g.,foo.pyas a result ofimport foo), we end up keeping a handle open to that file if throwing an exception inmp_parse()orfold_constants(). I'm not sure if other functions called frommp_parse()can throw an exception or not. Perhaps it's safest ifmp_parse()has a wrapper that callsmp_lexer_free()and re-throws the exception? I'd like some feedback on that idea before actually implementing it, though. It seems "right" sincemp_parse()already takes responsibility for freeing the lexer (and closing the reader) on success.I have a similar fix I plan to submit for
mp_raw_code_load(), the equivalentimportcode path for loading.mpyfiles.dpgeorge commentedon Jun 18, 2018
@tomlogic thanks for the info. I agree with your analysis,
mp_parse()does have the responsibility to free the lexer. If it didn't catch the exception then the caller would need to.py/parse: Always free lexer even if an exception is raised.
dpgeorge commentedon Sep 14, 2023
Fixed by 5e122b1
py/parse: Always free lexer even if an exception is raised.
py/parse: Always free lexer even if an exception is raised.
py/parse: Always free lexer even if an exception is raised.