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

Document that IDLE -python difference for del __builtins__ #69750

Closed
pppery mannequin opened this issue Nov 5, 2015 · 10 comments
Closed

Document that IDLE -python difference for del __builtins__ #69750

pppery mannequin opened this issue Nov 5, 2015 · 10 comments
Assignees
Labels
docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-IDLE type-bug An unexpected behavior, bug, or error

Comments

@pppery
Copy link
Mannequin

pppery mannequin commented Nov 5, 2015

BPO 25564
Nosy @terryjreedy, @stevendaprano, @pppery

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/terryjreedy'
closed_at = <Date 2016-08-25.05:29:29.052>
created_at = <Date 2015-11-05.23:26:04.761>
labels = ['interpreter-core', 'expert-IDLE', 'type-bug', 'docs']
title = 'Document that IDLE -python difference for `del __builtins__`'
updated_at = <Date 2016-08-25.06:18:19.663>
user = 'https://github.com/pppery'

bugs.python.org fields:

activity = <Date 2016-08-25.06:18:19.663>
actor = 'terry.reedy'
assignee = 'terry.reedy'
closed = True
closed_date = <Date 2016-08-25.05:29:29.052>
closer = 'terry.reedy'
components = ['Documentation', 'IDLE', 'Interpreter Core']
creation = <Date 2015-11-05.23:26:04.761>
creator = 'ppperry'
dependencies = []
files = []
hgrepos = []
issue_num = 25564
keywords = []
message_count = 10.0
messages = ['254149', '254150', '254151', '254153', '254173', '273594', '273617', '273625', '273627', '273629']
nosy_count = 4.0
nosy_names = ['terry.reedy', 'steven.daprano', 'python-dev', 'ppperry']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue25564'
versions = ['Python 2.7', 'Python 3.5', 'Python 3.6']

@pppery
Copy link
Mannequin Author

pppery mannequin commented Nov 5, 2015

In IDLE the following code silently works:
>>> del __builtins__
>>> min
<built-in function min>

In the standard interpreter, it produces an error:
>>> del __builtins__
>>> min
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'min' is not defined

Note that saying __builtins__ = 7 fails in idle as well.

@pppery pppery mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-IDLE labels Nov 5, 2015
@pppery
Copy link
Mannequin Author

pppery mannequin commented Nov 5, 2015

If you type del __builtins__;min an error is raise in both IDLE and the standard interpreter.

@pppery
Copy link
Mannequin Author

pppery mannequin commented Nov 5, 2015

`del __builtins__;min` only fails in IDLE if someone has previously set `__builtins__ to something else.
>>>__builtins__ = 7
>>> min
Traceback (most recent call last):
  File "<pyshell#352>", line 1, in <module>
    min
NameError: name 'min' is not defined
>>>del __builtins__;min
Traceback (most recent call last):
  File "<pyshell#353>", line 1, in <module>
    del __builtins__;min
NameError: name 'min' is not defined
>>del __builtins__;min
<built-in function min>

@stevendaprano
Copy link
Member

__builtins__ is a private implementation detail in CPython. There is no guarantees made about whether it exists or not. E.g. it doesn't exist in Jython.

steve@orac:~/python$ jython
Jython 2.5.1+ (Release_2_5_1, Aug 4 2010, 07:18:19)
[OpenJDK Server VM (Sun Microsystems Inc.)] on java1.6.0_31
Type "help", "copyright", "credits" or "license" for more information.
>>> __builtins__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__builtins__' is not defined

You should use __builtin__ in Python 2 and builtins in Python 3. Anything you do to __builtins__ with an S is implementation-dependent.

I don't think it is a bug that CPython behaves differently regarding __builtins__ depending on whether IDLE is running or not.

@terryjreedy
Copy link
Member

We try to have IDLE work the same as Python (and CPython, where relevant), except where differences are intentional or unavoidable. I am trying to eliminate some unintentional avoidable differences in other issues. However, this one is unavoidable given IDLE's basic design. Also, Steven is correct; see
https://docs.python.org/3/library/builtins.html#module-builtins

IDLE executes user code with "exec(code, self.locals)" (run.py, l.351 in 3.5).
https://docs.python.org/3/library/functions.html#exec
says "If the globals dictionary does not contain a value for the key __builtins__, a reference to the dictionary of the built-in module builtins is inserted under that key."

(Doc issus: From the builtins doc and the Jython example, this seems implementation dependent. Steven, does
  >>> d = {}; exec('', d); d.keys()
  dict_keys(['__builtins__'])
have this result in Jython?)

In the IDLE shell, each statement is exec'ed separately. With two statements, __builtins__ is added back before 'min' , while with one statement, it is not.

Editor: Run Module execs the entire file at once. I expected print(min) to fail either way, but it works either way. I verified that globals().keys() lost '__builtins__', so I don't know how __builtins__.min is found.

I left this open to consider adding a line to
https://docs.python.org/3/library/idle.html#idle-console-differences

@terryjreedy terryjreedy added the docs Documentation in the Doc dir label Nov 6, 2015
@terryjreedy terryjreedy changed the title IDLE behaves differently that the standard interpreter when someone types del __builtins__ IDLE behaves differently than python on del __builtins__ Nov 6, 2015
@terryjreedy terryjreedy self-assigned this Nov 6, 2015
@terryjreedy terryjreedy added the type-bug An unexpected behavior, bug, or error label Nov 6, 2015
@pppery pppery mannequin changed the title IDLE behaves differently than python on del __builtins__ Document that IDLE behaves differently than python on del __builtins__ Aug 24, 2016
@terryjreedy
Copy link
Member

Steven: "You should use __builtin__ in Python 2 and builtins in Python 3." I presume this is for import statements.

ppperry: Titles should fit in the box, so they fit in search listing results.

I am thinking of something like "Since Python inserts '__builtins__' into the exec global namespace when not present and IDLE uses exec, '__builtins__' is defined at the start of each statement or file even when it otherwise would not be."

@terryjreedy terryjreedy changed the title Document that IDLE behaves differently than python on del __builtins__ Document that IDLE -python difference for del __builtins__ Aug 24, 2016
@pppery pppery mannequin changed the title Document that IDLE -python difference for del __builtins__ Document IDLE -python difference for del __builtins__ Aug 24, 2016
@pppery pppery mannequin changed the title Document IDLE -python difference for del __builtins__ Document IDLE -python difference for del __builtins__ Aug 24, 2016
@stevendaprano
Copy link
Member

Terry J. Reedy added the comment:

Steven: "You should use __builtin__ in Python 2 and builtins in
Python 3." I presume this is for import statements.

My understanding is that __builtins__ is intended to be for the private
use of the CPython interpreter only. It may not be available in other
Pythons, or in the future, and code that needs access to the built-ins
should treat it as a regular module and import it via __builtin__ in
Python 2 and builtins in Python 3.

In other words, unless you're the CPython interpreter, don't touch
__builtins__.

I don't know whether IDLE is considered sufficiently closely integrated
to CPython that it is allowed to rely on __builtins__, but for an
ordinary module (even one in the stdlib) I wouldn't touch it at all.

@stevendaprano stevendaprano changed the title Document IDLE -python difference for del __builtins__ Document that IDLE -python difference for del __builtins__ Aug 25, 2016
@python-dev
Copy link
Mannequin

python-dev mannequin commented Aug 25, 2016

New changeset 862761e4376e by Terry Jan Reedy in branch '2.7':
Issue bpo-25564: Mention exec and __builtins__ in IDLE-console difference section.
https://hg.python.org/cpython/rev/862761e4376e

New changeset 641852513b8e by Terry Jan Reedy in branch '3.5':
Issue bpo-25564: Mention exec and __builtins__ in IDLE-console difference section.
https://hg.python.org/cpython/rev/641852513b8e

@terryjreedy
Copy link
Member

I added what I think is an improved version of what I posted.

For 2.7 only, a change in the generated html outside of the text proper from using — and » to using &bpo-8212; and &#187' resulted in the extraneous characters being shown. The displayed text was preceded by '—»»»» ' and followed by the closers. Adding the missing guard 'if self.show' to the charref function prevents this.

@terryjreedy
Copy link
Member

What I meant is that one cannot use __builtin__ or builtins until one has done the import, which is why people tend not to bother and instead do things like ppperry was doing.

To the extent that tkinter is limited to CPython, so is IDLE. Still, the only reference to __builtins__ is in
F:\Python\dev\36\lib\idlelib\autocomplete.py: 188: (fetch completions)
namespace.update(main.__builtins__.__dict__)

I don't know why not just use __builtins__.__dict__, but this follows
import __main__
namespace = __main__.__dict__.copy()

To be cross-platform, you are saying that this should be on 3.x
import builtins; namespace.update(builtins.__dict__)

@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
docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-IDLE type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants