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
Comments
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 |
If you type |
`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> |
__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 I don't think it is a bug that CPython behaves differently regarding __builtins__ depending on whether IDLE is running or not. |
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 IDLE executes user code with "exec(code, self.locals)" (run.py, l.351 in 3.5). (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 |
del __builtins__
del __builtins__
del __builtins__
del __builtins__
Steven: "You should use 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." |
del __builtins__
del __builtins__
del __builtins__
del __builtins__
del __builtins__
del __builtins__
Terry J. Reedy added the comment:
My understanding is that __builtins__ is intended to be for the private In other words, unless you're the CPython interpreter, don't touch I don't know whether IDLE is considered sufficiently closely integrated |
del __builtins__
del __builtins__
New changeset 862761e4376e by Terry Jan Reedy in branch '2.7': New changeset 641852513b8e by Terry Jan Reedy in branch '3.5': |
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 »' 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. |
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 I don't know why not just use __builtins__.__dict__, but this follows To be cross-platform, you are saying that this should be on 3.x |
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: