[Python-checkins] bpo-43102: Set namedtuple __new__'s internal builtins to a dict. (GH-24439) (GH-24452)
rhettinger
webhook-mailer at python.org
Thu Feb 4 19:12:46 EST 2021
https://github.com/python/cpython/commit/29584aa6acbc70091dc23636db51ee1696e65072
commit: 29584aa6acbc70091dc23636db51ee1696e65072
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2021-02-04T16:12:34-08:00
summary:
bpo-43102: Set namedtuple __new__'s internal builtins to a dict. (GH-24439) (GH-24452)
files:
A Misc/NEWS.d/next/Library/2021-02-03-22-55-27.bpo-43102.TSlZ6J.rst
M Lib/collections/__init__.py
M Lib/test/test_collections.py
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index bc69a6757f294..5bdd3b3516d3b 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -424,7 +424,7 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non
namespace = {
'_tuple_new': tuple_new,
- '__builtins__': None,
+ '__builtins__': {},
'__name__': f'namedtuple_{typename}',
}
code = f'lambda _cls, {arg_list}: _tuple_new(_cls, ({arg_list}))'
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 057ec92015cf4..3ff660cf7a37b 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -680,6 +680,11 @@ class NewPoint(tuple):
self.assertEqual(np.x, 1)
self.assertEqual(np.y, 2)
+ def test_new_builtins_issue_43102(self):
+ self.assertEqual(
+ namedtuple('C', ()).__new__.__globals__['__builtins__'],
+ {})
+
################################################################################
### Abstract Base Classes
diff --git a/Misc/NEWS.d/next/Library/2021-02-03-22-55-27.bpo-43102.TSlZ6J.rst b/Misc/NEWS.d/next/Library/2021-02-03-22-55-27.bpo-43102.TSlZ6J.rst
new file mode 100644
index 0000000000000..985fd68a03a93
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-02-03-22-55-27.bpo-43102.TSlZ6J.rst
@@ -0,0 +1,2 @@
+The namedtuple __new__ method had its __builtins__ set to None instead
+of an actual dictionary. This created problems for introspection tools.
More information about the Python-checkins
mailing list