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
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='https://github.com/gvanrossum'closed_at=<Date2008-12-05.09:32:44.069>created_at=<Date2007-07-29.03:24:55.000>labels= ['interpreter-core']
title="'exec' does not accept what 'open' returns"updated_at=<Date2008-12-05.09:32:44.068>user='https://github.com/brettcannon'
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?
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'
>>>
Activity
brettcannon commentedon Jul 29, 2007
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.
gvanrossum commentedon Aug 3, 2007
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.
brettcannon commentedon Aug 3, 2007
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).
gvanrossum commentedon Aug 3, 2007
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.)
brettcannon commentedon Aug 3, 2007
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.
gvanrossum commentedon Aug 3, 2007
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().
brettcannon commentedon Aug 3, 2007
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?
tiran commentedon Jan 7, 2008
both sounds like a good idea.
tiran commentedon Jan 7, 2008
I've readded reload() as imp.reload() in r59825. I've also copied two
unit tests from trunk to py3k.
tiran commentedon Jan 7, 2008
I've added the __file__ patch in r59841. Is there another location that
needs to be fixed?
gvanrossum commentedon Jan 7, 2008
Maybe just a doc update?
gvanrossum commentedon Mar 18, 2008
Christian, I still see __file__ point to a .pyc file:
Please assign back to me if you've fixed this...
birkenfeld commentedon May 13, 2008
This has apparently been fixed now.
birkenfeld commentedon Dec 5, 2008
All open items in this issue seem to be addressed