Skip to content

bpo-42990: Add __builtins__ attribute to functions #24559

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

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ attributes:
| | __globals__ | global namespace in which |
| | | this function was defined |
+-----------+-------------------+---------------------------+
| | __builtins__ | builtins namespace |
+-----------+-------------------+---------------------------+
| | __annotations__ | mapping of parameters |
| | | names to annotations; |
| | | ``"return"`` key is |
Expand Down Expand Up @@ -251,6 +253,10 @@ attributes:

Add ``cr_origin`` attribute to coroutines.

.. versionchanged:: 3.10

Add ``__builtins__`` attribute to functions.

.. function:: getmembers(object[, predicate])

Return all the members of an object in a list of ``(name, value)``
Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ Other Language Changes
* Assignment expressions can now be used unparenthesized within set literals
and set comprehensions, as well as in sequence indexes (but not slices).

* Functions have a new ``__builtins__`` attribute which is used to look for
builtin symbols when a function is executed, instead of looking into
``__globals__['__builtins__']``.
(Contributed by Mark Shannon in :issue:`42990`.)


New Modules
===========
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,10 @@ class NewPoint(tuple):
self.assertEqual(np.y, 2)

def test_new_builtins_issue_43102(self):
self.assertEqual(
namedtuple('C', ()).__new__.__globals__['__builtins__'],
{})
obj = namedtuple('C', ())
new_func = obj.__new__
self.assertEqual(new_func.__globals__['__builtins__'], {})
self.assertEqual(new_func.__builtins__, {})


################################################################################
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_funcattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def test___globals__(self):
self.cannot_set_attr(self.b, '__globals__', 2,
(AttributeError, TypeError))

def test___builtins__(self):
self.assertIs(self.b.__builtins__, __builtins__)
self.cannot_set_attr(self.b, '__builtins__', 2,
(AttributeError, TypeError))

def test___closure__(self):
a = 12
def f(): print(a)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Functions have a new ``__builtins__`` attribute which is used to look for
builtin symbols when a function is executed, instead of looking into
``__globals__['__builtins__']``. Patch by Mark Shannon and Victor Stinner.
1 change: 1 addition & 0 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ static PyMemberDef func_memberlist[] = {
{"__doc__", T_OBJECT, OFF(func_doc), 0},
{"__globals__", T_OBJECT, OFF(func_globals), READONLY},
{"__module__", T_OBJECT, OFF(func_module), 0},
{"__builtins__", T_OBJECT, OFF(func_builtins), READONLY},
{NULL} /* Sentinel */
};

Expand Down