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
Comments
The execfile fixer of the 2to3 script replaces the 2.x code execfile("a.py") by
The new code does not explicitly close the file. This is not usually a problem in CPython, but
(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. |
Sounds reasonable to me. Is it easy to generate unique identifier names in 2to3 fixers? |
Be aware that execfile() is a simple function call and can be used in any expression. |
Getting the general case right seems a bit too difficult. Examples like
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? |
Or you could just explicitly write out the exec(compile()) in your 2.x code. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: