From 363052adfc672a3af81721ac34c44ca0f4744445 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 3 Feb 2021 22:57:54 -0800 Subject: [PATCH 1/2] bpo-43102: Set namedtuple __new__'s internal builtins to a dict. --- Lib/collections/__init__.py | 2 +- Lib/test/test_collections.py | 6 ++++++ .../next/Library/2021-02-03-22-55-27.bpo-43102.TSlZ6J.rst | 2 ++ 3 files changed, 9 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 7d338131d6740d7..6fe3c4c5aa541d7 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 a1ca958257adf6a..7a6b71a05fe3f5f 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -681,6 +681,12 @@ 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 000000000000000..7319a0272cdd6b5 --- /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 now had its __builtins__ set to None instead +of an actual dictionary. This created problems for introspection tools. From 940a58142f7d44d70284642b44f17072b60ec1c2 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 3 Feb 2021 23:01:47 -0800 Subject: [PATCH 2/2] Fix typo --- Lib/test/test_collections.py | 1 - .../next/Library/2021-02-03-22-55-27.bpo-43102.TSlZ6J.rst | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 7a6b71a05fe3f5f..befb7ab436c40af 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -687,7 +687,6 @@ def test_new_builtins_issue_43102(self): {}) - ################################################################################ ### 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 index 7319a0272cdd6b5..985fd68a03a935d 100644 --- 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 @@ -1,2 +1,2 @@ -The namedtuple __new__ method now had its __builtins__ set to None instead +The namedtuple __new__ method had its __builtins__ set to None instead of an actual dictionary. This created problems for introspection tools.