msg254149 - (view) |
Author: (ppperry) |
Date: 2015-11-05 23:26 |
In IDLE the following code silently works: >>> del __builtins__ >>> min In the standard interpreter, it produces an error: >>> del __builtins__ >>> min Traceback (most recent call last): File "", line 1, in 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 min NameError: name 'min' is not defined >>>del __builtins__;min Traceback (most recent call last): File "<pyshell#353>", line 1, in del __builtins__;min NameError: name 'min' is not defined >>del __builtins__;min |
|
|
msg254153 - (view) |
Author: Steven D'Aprano (steven.daprano) *  |
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🔞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 "", line 1, in 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) *  |
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) *  |
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) *  |
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)  |
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) *  |
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 — and » to using — 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. |
|
|
msg273629 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
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\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__) |
|
|