
This issue trackerhas been migrated toGitHub, and is currentlyread-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.
Created on2015-11-05 23:26 byppperry, last changed2022-04-11 14:58 byadmin. This issue is nowclosed.
| 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__>>> minTraceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'min' is not definedNote 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>>> minTraceback (most recent call last): File "<pyshell#352>", line 1, in <module> minNameError: name 'min' is not defined>>>del __builtins__;minTraceback (most recent call last): File "<pyshell#353>", line 1, in <module> del __builtins__;minNameError: name 'min' is not defined>>del __builtins__;min<built-in function 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$ jythonJython 2.5.1+ (Release_2_5_1, Aug 4 2010, 07:18:19)[OpenJDK Server VM (Sun Microsystems Inc.)] on java1.6.0_31Type "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 definedYou 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; seehttps://docs.python.org/3/library/builtins.html#module-builtinsIDLE executes user code with "exec(code, self.locals)" (run.py, l.351 in 3.5).https://docs.python.org/3/library/functions.html#execsays "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 tohttps://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 changeset862761e4376e 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/862761e4376eNew changeset641852513b8e 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 inF:\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__) | |||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:58:23 | admin | set | github: 69750 |
| 2016-08-25 06:18:19 | terry.reedy | set | messages: +msg273629 |
| 2016-08-25 05:29:29 | terry.reedy | set | status: open -> closed resolution: fixed messages: +msg273627 stage: needs patch -> resolved |
| 2016-08-25 05:23:21 | python-dev | set | nosy: +python-dev messages: +msg273625 |
| 2016-08-25 01:50:48 | steven.daprano | set | messages: +msg273617 title: Document IDLE -python difference for `del __builtins__` -> Document that IDLE -python difference for `del __builtins__` |
| 2016-08-24 23:21:49 | ppperry | set | title: Document IDLE -python difference for `del __builtins__` -> Document IDLE -python difference for `del __builtins__` |
| 2016-08-24 23:21:44 | ppperry | set | title: Document that IDLE -python difference for `del __builtins__` -> Document IDLE -python difference for `del __builtins__` |
| 2016-08-24 21:04:51 | terry.reedy | set | title: 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:30 | ppperry | set | title: IDLE behaves differently than python on `del __builtins__` -> Document that IDLE behaves differently than python on `del __builtins__` |
| 2015-11-06 08:37:07 | terry.reedy | set | assignee: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:29 | steven.daprano | set | nosy: +terry.reedy |
| 2015-11-06 00:59:46 | steven.daprano | set | nosy: +steven.daprano messages: +msg254153 |
| 2015-11-05 23:31:45 | ppperry | set | versions: + Python 2.7 |
| 2015-11-05 23:31:20 | ppperry | set | messages: +msg254151 |
| 2015-11-05 23:28:14 | ppperry | set | messages: +msg254150 |
| 2015-11-05 23:26:04 | ppperry | create | |