classification
Title: The doc say all globals are copied on eval(), but only __builtins__ is copied
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: amishne, berker.peksag, docs@python, martin.panter, miss-islington, rhettinger, ztane
Priority: normal Keywords: patch

Created on 2014-07-24 13:59 by amishne, last changed 2018-08-19 10:53 by berker.peksag. This issue is now closed.

Files
File name Uploaded Description Edit
evalglobals2.patch amishne, 2014-07-27 09:55 review
evalglobals3.patch amishne, 2014-07-27 09:57 review
Pull Requests
URL Status Linked Edit
PR 8812 merged berker.peksag, 2018-08-18 22:18
PR 8818 merged miss-islington, 2018-08-19 10:25
PR 8819 merged miss-islington, 2018-08-19 10:25
Messages (12)
msg223837 - (view) Author: Alon Mishne (amishne) * Date: 2014-07-24 13:59
According to the documentation of eval():

> If the globals dictionary is present and lacks '__builtins__', the current globals are copied into globals before expression is parsed.

However in practice only the __builtins__ items are copied, see:

http://hg.python.org/cpython/file/2.7/Python/bltinmodule.c#l655

See http://stackoverflow.com/q/24934908/242762
msg223971 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-07-25 17:08
Thanks for noticing this.  Do you want to propose a patch?
msg224112 - (view) Author: Alon Mishne (amishne) * Date: 2014-07-27 09:55
Patch for 2.7.8.

The patch is just changing "globals are" to "mapping of ‘__builtins__’ is", and subsequent paragraph reformatting.
msg224113 - (view) Author: Alon Mishne (amishne) * Date: 2014-07-27 09:57
Patch for 3.5.x

The patch is just changing "globals are" to "mapping of ‘__builtins__’ is", and subsequent paragraph reformatting.

Nearly identical to 2.7.8 patch.
msg242083 - (view) Author: Antti Haapala (ztane) * Date: 2015-04-26 21:54
+1 for this patch, the current documentation states it very wrong.
msg242105 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-04-27 07:41
The patch looks good (though it would have been easier to check the diff if the text had not be reflowed).

I will apply it when I get a chance.  Or if anyone else wants to grab it, go ahead.
msg282262 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-12-03 02:32
“the current mapping of '__builtins__' is copied into *globals* ”

That sounds like we insert each individual builtin name, i.e. globals.update(builtins_mapping). But my understanding is that it is the __builtins__ global variable that is affected:

globals["__builtins__"] = builtins_mapping

What about borrowing the wording from exec():

If the *globals* dictionary is present and lacks ‘__builtins__’, a reference to the current mapping of ‘__builtins__’ is inserted under that key . . .

See also Issue 26363, which also covers this problem.
msg323736 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-08-18 22:19
I like Martin's suggestion in msg282262 and I've just created PR 8812 to implement it.
msg323748 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-08-19 10:25
New changeset 225b05548027d55aafb11b65f6a4a2bef2f5196f by Berker Peksag in branch 'master':
bpo-22057: Clarify eval() documentation (GH-8812)
https://github.com/python/cpython/commit/225b05548027d55aafb11b65f6a4a2bef2f5196f
msg323749 - (view) Author: miss-islington (miss-islington) Date: 2018-08-19 10:29
New changeset 0e1e8dbb0b99ec78f4567382428fdd46e2244d40 by Miss Islington (bot) in branch '3.7':
bpo-22057: Clarify eval() documentation (GH-8812)
https://github.com/python/cpython/commit/0e1e8dbb0b99ec78f4567382428fdd46e2244d40
msg323751 - (view) Author: miss-islington (miss-islington) Date: 2018-08-19 10:32
New changeset f19579b8f1fcd2dd8ffaf3e443b09930057c01d1 by Miss Islington (bot) in branch '3.6':
bpo-22057: Clarify eval() documentation (GH-8812)
https://github.com/python/cpython/commit/f19579b8f1fcd2dd8ffaf3e443b09930057c01d1
msg323752 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-08-19 10:53
Thanks for the review, Martin, and thanks for the report, Alon!
History
Date User Action Args
2018-08-19 10:53:32berker.peksagsetstatus: open -> closed
resolution: fixed
messages: + msg323752

stage: patch review -> resolved
2018-08-19 10:32:15miss-islingtonsetmessages: + msg323751
2018-08-19 10:29:53miss-islingtonsetnosy: + miss-islington
messages: + msg323749
2018-08-19 10:25:54miss-islingtonsetpull_requests: + pull_request8297
2018-08-19 10:25:45miss-islingtonsetpull_requests: + pull_request8296
2018-08-19 10:25:40berker.peksagsetmessages: + msg323748
2018-08-19 00:07:47berker.peksaglinkissue26363 superseder
2018-08-18 22:19:40berker.peksagsetversions: + Python 3.8, - Python 3.5
nosy: + berker.peksag

messages: + msg323736

type: behavior
2018-08-18 22:18:08berker.peksagsetpull_requests: + pull_request8290
2016-12-03 02:32:54martin.pantersetversions: + Python 3.6, Python 3.7, - Python 3.4
nosy: + martin.panter

messages: + msg282262

stage: needs patch -> patch review
2015-04-27 07:41:20rhettingersetmessages: + msg242105
2015-04-26 21:54:24ztanesetnosy: + ztane
messages: + msg242083
2014-07-27 09:57:43amishnesetfiles: + evalglobals3.patch

messages: + msg224113
2014-07-27 09:55:55amishnesetfiles: + evalglobals2.patch
keywords: + patch
messages: + msg224112
2014-07-25 17:08:55rhettingersetversions: - Python 3.1, Python 3.2, Python 3.3
nosy: + rhettinger

messages: + msg223971

assignee: docs@python -> rhettinger
stage: needs patch
2014-07-24 13:59:49amishnecreate