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

'exec' does not accept what 'open' returns #45253

Closed
brettcannon opened this issue Jul 29, 2007 · 14 comments
Closed

'exec' does not accept what 'open' returns #45253

brettcannon opened this issue Jul 29, 2007 · 14 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@brettcannon
Copy link
Member

BPO 1762972
Nosy @gvanrossum, @brettcannon, @birkenfeld, @tiran

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 = <Date 2008-12-05.09:32:44.069>
created_at = <Date 2007-07-29.03:24:55.000>
labels = ['interpreter-core']
title = "'exec' does not accept what 'open' returns"
updated_at = <Date 2008-12-05.09:32:44.068>
user = 'https://github.com/brettcannon'

bugs.python.org fields:

activity = <Date 2008-12-05.09:32:44.068>
actor = 'georg.brandl'
assignee = 'gvanrossum'
closed = True
closed_date = <Date 2008-12-05.09:32:44.069>
closer = 'georg.brandl'
components = ['Interpreter Core']
creation = <Date 2007-07-29.03:24:55.000>
creator = 'brett.cannon'
dependencies = []
files = []
hgrepos = []
issue_num = 1762972
keywords = []
message_count = 14.0
messages = ['32561', '32562', '32563', '32564', '32565', '32566', '32567', '59465', '59467', '59484', '59485', '63890', '66800', '76993']
nosy_count = 4.0
nosy_names = ['gvanrossum', 'brett.cannon', 'georg.brandl', 'christian.heimes']
pr_nums = []
priority = 'normal'
resolution = 'accepted'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1762972'
versions = ['Python 3.0']

@brettcannon
Copy link
Member Author

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.

@brettcannon brettcannon added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jul 29, 2007
@brettcannon brettcannon added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jul 29, 2007
@gvanrossum
Copy link
Member

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
Copy link
Member Author

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
Copy link
Member

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
Copy link
Member Author

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
Copy link
Member

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
Copy link
Member Author

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
Copy link
Member

tiran commented Jan 7, 2008

both sounds like a good idea.

@tiran
Copy link
Member

tiran commented Jan 7, 2008

I've readded reload() as imp.reload() in r59825. I've also copied two
unit tests from trunk to py3k.

@tiran
Copy link
Member

tiran commented Jan 7, 2008

I've added the __file__ patch in r59841. Is there another location that
needs to be fixed?

@gvanrossum
Copy link
Member

Maybe just a doc update?

@gvanrossum
Copy link
Member

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

@gvanrossum gvanrossum assigned tiran and unassigned gvanrossum Mar 18, 2008
@birkenfeld birkenfeld assigned gvanrossum and unassigned tiran May 13, 2008
@birkenfeld
Copy link
Member

This has apparently been fixed now.

@birkenfeld
Copy link
Member

All open items in this issue seem to be addressed

@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
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

4 participants