From b6d68aa08baebb753534a26d537ac3c0d2c21c79 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Thu, 4 Feb 2021 15:52:16 -0800 Subject: bpo-43102: Set namedtuple __new__'s internal builtins to a dict. (GH-24439) --- Lib/collections/__init__.py | 2 +- Lib/test/test_collections.py | 5 +++++ Misc/NEWS.d/next/Library/2021-02-03-22-55-27.bpo-43102.TSlZ6J.rst | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2021-02-03-22-55-27.bpo-43102.TSlZ6J.rst diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 7d33813..6fe3c4c 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -407,7 +407,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 a1ca958..befb7ab 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -681,6 +681,11 @@ class TestNamedTuple(unittest.TestCase): 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 0000000..985fd68 --- /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. -- cgit v0.12