Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

execfile fixer produces code that does not close the file #57541

Closed
smarnach mannequin opened this issue Nov 3, 2011 · 6 comments
Closed

execfile fixer produces code that does not close the file #57541

smarnach mannequin opened this issue Nov 3, 2011 · 6 comments
Labels
topic-2to3 type-bug An unexpected behavior, bug, or error

Comments

@smarnach
Copy link
Mannequin

smarnach mannequin commented Nov 3, 2011

BPO 13332
Nosy @pitrou, @benjaminp, @asmeurer, @akheron, @smarnach
Superseder
  • bpo-45544: Close 2to3 issues and list them here
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-10-20.22:36:26.926>
    created_at = <Date 2011-11-03.12:11:31.896>
    labels = ['type-bug', 'expert-2to3']
    title = 'execfile fixer produces code that does not close the file'
    updated_at = <Date 2021-10-20.22:41:03.719>
    user = 'https://github.com/smarnach'

    bugs.python.org fields:

    activity = <Date 2021-10-20.22:41:03.719>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-10-20.22:36:26.926>
    closer = 'iritkatriel'
    components = ['2to3 (2.x to 3.x conversion tool)']
    creation = <Date 2011-11-03.12:11:31.896>
    creator = 'smarnach'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 13332
    keywords = []
    message_count = 6.0
    messages = ['146918', '146924', '146925', '146931', '146953', '146977']
    nosy_count = 6.0
    nosy_names = ['pitrou', 'benjamin.peterson', 'VPeric', 'Aaron.Meurer', 'petri.lehtinen', 'smarnach']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = '45544'
    type = 'behavior'
    url = 'https://bugs.python.org/issue13332'
    versions = ['Python 3.2', 'Python 3.3']

    @smarnach
    Copy link
    Mannequin Author

    smarnach mannequin commented Nov 3, 2011

    The execfile fixer of the 2to3 script replaces the 2.x code

        execfile("a.py")

    by

    exec(compile(open("a.py").read(), "a.py", 'exec'))
    

    The new code does not explicitly close the file. This is not usually a problem in CPython, but

    1. the code will throw a RessourceWarnings if enabled and

    2. other Python implementation don't close the file immediately.

    (I think the 2to3 script should be as implementation-independent as possible.)

    The obvious fix would be to use a with-statement:

        with open("a.py") as new_name:
            exec(compile(new_name.read(), "a.py", 'exec'))

    If this is to be changed, I'd be happy to prepare a patch.

    @smarnach smarnach mannequin added topic-2to3 type-bug An unexpected behavior, bug, or error labels Nov 3, 2011
    @akheron
    Copy link
    Member

    akheron commented Nov 3, 2011

    Sounds reasonable to me. Is it easy to generate unique identifier names in 2to3 fixers?

    @pitrou
    Copy link
    Member

    pitrou commented Nov 3, 2011

    Be aware that execfile() is a simple function call and can be used in any expression.

    @smarnach
    Copy link
    Mannequin Author

    smarnach mannequin commented Nov 3, 2011

    @petri: Yes, that's what the BaseFix.new_name() method is for.

    @antoine: I thought about this, though I don't think it is very common to call execfile() as part of an expression. The whole statement containing the function call would need to be wrapped.

    @smarnach
    Copy link
    Mannequin Author

    smarnach mannequin commented Nov 3, 2011

    Getting the general case right seems a bit too difficult. Examples like

    [execfile(n) for n in names if condition(n)]
    
    execfile(execfile(n1) or n2)
    
    try: 1 / 0
    except execfile(n) or ZeroDivisionError: pass
    

    would require rather complex transformations to get exactly matching behaviour, and obviously we don't want to explode the fixer code to support such nonsense.

    I think it is enough to cover the case of an execfile() call that forms a statement of its own. Browsing through the first ten pages of a Google code search for "execfile" didn't reveal any other uses, except for a few that aren't covered by the current version of the fixer either. 1

    I'd suggest to simply throw a "could not convert" warning for any other uses of execfile(). Opinions?

    @benjaminp
    Copy link
    Contributor

    Or you could just explicitly write out the exec(compile()) in your 2.x code.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-2to3 type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants