Skip to content

Free resource before raise syntax error so we can access python file after syntax error. #3843

Description

@KrisJe

nlr_raise(exc);

    mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTR_NULL);
    mp_lexer_free(lex); //<-- add this
    nlr_raise(exc);

Activity

  1. dpgeorge commented on Jun 9, 2018

    @dpgeorge
    Member

    Thanks for the suggestion. What was it exactly that prompted this change?

  2. tomlogic commented on Jun 17, 2018

    @tomlogic
    Contributor

    @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.

  3. dpgeorge commented on Jun 18, 2018

    @dpgeorge
    Member

    @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.

  4. dpgeorge commented on Sep 14, 2023

    @dpgeorge
    Member

    Fixed by 5e122b1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions