homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Document that IDLE -python difference for `del __builtins__`
Type: behavior Stage: resolved
Components: Documentation, IDLE, Interpreter Core Versions: Python 3.6, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: ppperry, python-dev, steven.daprano, terry.reedy
Priority: normal Keywords:

Created on 2015年11月05日 23:26 by ppperry, last changed 2022年04月11日 14:58 by admin. This issue is now closed.

Messages (10)
msg254149 - (view) Author: (ppperry) Date: 2015年11月05日 23:26
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.
msg254150 - (view) Author: (ppperry) Date: 2015年11月05日 23:28
If you type `del __builtins__;min` an error is raise in both IDLE and the standard interpreter.
msg254151 - (view) Author: (ppperry) Date: 2015年11月05日 23:31
`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>
msg254153 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2015年11月06日 00:59
__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.
msg254173 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015年11月06日 08:37
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 
msg273594 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016年08月24日 21:04
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."
msg273617 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016年08月25日 01:50
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.
msg273625 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016年08月25日 05:23
New changeset 862761e4376e by Terry Jan Reedy in branch '2.7':
Issue #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 #25564: Mention exec and __builtins__ in IDLE-console difference section.
https://hg.python.org/cpython/rev/641852513b8e 
msg273627 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016年08月25日 05:29
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 &mdash; and &raquo; to using &#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.
msg273629 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016年08月25日 06:18
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\dev36円\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__)
History
Date User Action Args
2022年04月11日 14:58:23adminsetgithub: 69750
2016年08月25日 06:18:19terry.reedysetmessages: + msg273629
2016年08月25日 05:29:29terry.reedysetstatus: open -> closed
resolution: fixed
messages: + msg273627

stage: needs patch -> resolved
2016年08月25日 05:23:21python-devsetnosy: + python-dev
messages: + msg273625
2016年08月25日 01:50:48steven.dapranosetmessages: + msg273617
title: Document IDLE -python difference for `del __builtins__` -> Document that IDLE -python difference for `del __builtins__`
2016年08月24日 23:21:49ppperrysettitle: Document IDLE -python difference for `del __builtins__` -> Document IDLE -python difference for `del __builtins__`
2016年08月24日 23:21:44ppperrysettitle: Document that IDLE -python difference for `del __builtins__` -> Document IDLE -python difference for `del __builtins__`
2016年08月24日 21:04:51terry.reedysettitle: Document that IDLE behaves differently than python on `del __builtins__` -> Document that IDLE -python difference for `del __builtins__`
messages: + msg273594
versions: - Python 3.4
2016年08月24日 13:22:30ppperrysettitle: IDLE behaves differently than python on `del __builtins__` -> Document that IDLE behaves differently than python on `del __builtins__`
2015年11月06日 08:37:07terry.reedysetassignee: terry.reedy
components: + Documentation
title: IDLE behaves differently that the standard interpreter when someone types `del __builtins__` -> IDLE behaves differently than python on `del __builtins__`
type: behavior
versions: + Python 3.4, Python 3.5, Python 3.6
messages: + msg254173
stage: needs patch
2015年11月06日 01:01:29steven.dapranosetnosy: + terry.reedy
2015年11月06日 00:59:46steven.dapranosetnosy: + steven.daprano
messages: + msg254153
2015年11月05日 23:31:45ppperrysetversions: + Python 2.7
2015年11月05日 23:31:20ppperrysetmessages: + msg254151
2015年11月05日 23:28:14ppperrysetmessages: + msg254150
2015年11月05日 23:26:04ppperrycreate

AltStyle によって変換されたページ (->オリジナル) /