Skip to content

Commit a3c3ffa

Browse files
authored
bpo-42990: Add __builtins__ attribute to functions (GH-24559)
Expose the new PyFunctionObject.func_builtins member in Python as a new __builtins__ attribute on functions. Document also the behavior change in What's New in Python 3.10.
1 parent 366dc3a commit a3c3ffa

File tree

6 files changed

+24
-3
lines changed

6 files changed

+24
-3
lines changed

Doc/library/inspect.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ attributes:
9595
| | __globals__ | global namespace in which |
9696
| | | this function was defined |
9797
+-----------+-------------------+---------------------------+
98+
| | __builtins__ | builtins namespace |
99+
+-----------+-------------------+---------------------------+
98100
| | __annotations__ | mapping of parameters |
99101
| | | names to annotations; |
100102
| | | ``"return"`` key is |
@@ -251,6 +253,10 @@ attributes:
251253

252254
Add ``cr_origin`` attribute to coroutines.
253255

256+
.. versionchanged:: 3.10
257+
258+
Add ``__builtins__`` attribute to functions.
259+
254260
.. function:: getmembers(object[, predicate])
255261

256262
Return all the members of an object in a list of ``(name, value)``

Doc/whatsnew/3.10.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ Other Language Changes
280280
* Assignment expressions can now be used unparenthesized within set literals
281281
and set comprehensions, as well as in sequence indexes (but not slices).
282282
283+
* Functions have a new ``__builtins__`` attribute which is used to look for
284+
builtin symbols when a function is executed, instead of looking into
285+
``__globals__['__builtins__']``.
286+
(Contributed by Mark Shannon in :issue:`42990`.)
287+
283288
284289
New Modules
285290
===========

Lib/test/test_collections.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,10 @@ class NewPoint(tuple):
682682
self.assertEqual(np.y, 2)
683683

684684
def test_new_builtins_issue_43102(self):
685-
self.assertEqual(
686-
namedtuple('C', ()).__new__.__globals__['__builtins__'],
687-
{})
685+
obj = namedtuple('C', ())
686+
new_func = obj.__new__
687+
self.assertEqual(new_func.__globals__['__builtins__'], {})
688+
self.assertEqual(new_func.__builtins__, {})
688689

689690

690691
################################################################################

Lib/test/test_funcattrs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ def test___globals__(self):
7373
self.cannot_set_attr(self.b, '__globals__', 2,
7474
(AttributeError, TypeError))
7575

76+
def test___builtins__(self):
77+
self.assertIs(self.b.__builtins__, __builtins__)
78+
self.cannot_set_attr(self.b, '__builtins__', 2,
79+
(AttributeError, TypeError))
80+
7681
def test___closure__(self):
7782
a = 12
7883
def f(): print(a)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Functions have a new ``__builtins__`` attribute which is used to look for
2+
builtin symbols when a function is executed, instead of looking into
3+
``__globals__['__builtins__']``. Patch by Mark Shannon and Victor Stinner.

Objects/funcobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ static PyMemberDef func_memberlist[] = {
250250
{"__doc__", T_OBJECT, OFF(func_doc), 0},
251251
{"__globals__", T_OBJECT, OFF(func_globals), READONLY},
252252
{"__module__", T_OBJECT, OFF(func_module), 0},
253+
{"__builtins__", T_OBJECT, OFF(func_builtins), READONLY},
253254
{NULL} /* Sentinel */
254255
};
255256

0 commit comments

Comments
 (0)