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
'exec' does not accept what 'open' returns #45253
Comments
Since the move over to io.py 'exec' no longer accepts what 'open' returns. Looks like there is a type check in 'exec' for strings, files, or code object, but 'open' returns a TextIOWrapper that fails that typecheck. |
Arguably we don't need this feature any more. The only reasonable way to implement it would be to slurp the entire contents of the file into a string and then exec that. The caller might as well do this. Alternatively one could use execfile() (which takes a filename instead of a stream). The only advantage of using exec(<stream>) might be that it would deal with encoding declarations transparantly; but we should probably have a way to handle those somewhere in the library anyway, as it's also necessary for other tools (e.g. modulefinder.py, pyclbr.py and linecache.py). Alternatively I'm not so sure that we will need to support encoding declarations forever -- I'm hoping that at some point UTF-8 and BOM+UTF-16 will take over the world. |
I am fine with ditching the feature and forcing people to deal with the encoding issues externally to 'exec', although as you pointed out dealing with any -*- encoding marker might be a pain without some library help (although doesn't the parser pick up on this and handle those nasty details?). As for execfile, PEP-3100 has it listed for removal in favour of moving people over to using 'exec'. The main thing is trying to come up with an easy solution for replacing 'reload' with a reasonable one-liner when debugging at the interpreter. And yes, I hope encoding declarations can go away and the world either lives with just UTF-8 or BOM+UTF-16 (I really wish we could just require this for all Python source files and start towards this, but I know some people would flip out over that). |
OK, let's ditch it (i.e. status quo) and also ditch execfile. The one-liner you're looking for is *almost* exec(open(M.__file__).read(), M.__dict__) (where M stands for any already imported module), except for the nastiness that if m.__file__ ends with .pyc or .pyo you'd have to strip the last character. (And to do this right you'd need help from the imp module because those extensions may vary by platform -- e.g. I know of a company that patches their Pythons to use .pyc24.) |
Hrm. Well then we can either add something like __source_file__ to modules, put 'reload' into the imp module (or on module objects as a method), or have a function in imp (or as a method on modules) that returns the source path (if it exists). But having to do:: M.__file__.rsplit('.', 1) + filter((lambda x : x[2] == imp.PY_SOURCE), imp.get_suffixes())[0] seems like a lot to memorize (let alone type in), especially since there is even no error checking that the path is even to a source or bytecode file to start with or that you end up with. |
Or we can change the semantics of __file__ to always point to the source file. I think that's acceptable behavior -- the current behavior seems more of a pain than necessary. I also think that adding imp.reload() makes sense; I was just saying yesterday to a few fellow-Googler-Python-developers that I was already missing reload(). |
Agreed on both, although what to do when there is no source but only bytecode? Just say that __file__ will point to the source if available, otherwise to the bytecode? That seems reasonable to me. And imp.reload sounds good. This whole bug report started because 'reload' was gone and I was trying to figure out the best way to replace its loss. I know I am short on time and thus have no clue if I can help on these. Should we add these to PEP-3100 so we don't forget, or do you think you might get to this? |
both sounds like a good idea. |
I've readded reload() as imp.reload() in r59825. I've also copied two |
I've added the __file__ patch in r59841. Is there another location that |
Maybe just a doc update? |
Christian, I still see __file__ point to a .pyc file: guido-macbookpro:~/p3 guido$ ./python.exe
Python 3.0a3+ (py3k:61464M, Mar 17 2008, 16:36:53)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.__file__
'/Users/guido/p3/Lib/string.pyc'
>>> Please assign back to me if you've fixed this... |
This has apparently been fixed now. |
All open items in this issue seem to be addressed |
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: