From 0889e3048d0849a1cf6112557d1b59340053ba9c Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 15 Aug 2022 13:39:06 -0400 Subject: [PATCH] GH-94808: Test __build_class__ inside non-dict __builtins__ (GH-95932) (cherry picked from commit 3adb4d864bb18a51334c922a732e5e3602799ba1) Co-authored-by: Michael Droettboom --- Lib/test/test_builtin.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index efa9459a58629d..64c74ec2c59ae0 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -737,11 +737,6 @@ def test_exec_globals(self): self.assertRaises(TypeError, exec, code, {'__builtins__': 123}) - # no __build_class__ function - code = compile("class A: pass", "", "exec") - self.assertRaisesRegex(NameError, "__build_class__ not found", - exec, code, {'__builtins__': {}}) - class frozendict_error(Exception): pass @@ -758,6 +753,15 @@ def __setitem__(self, key, value): self.assertRaises(frozendict_error, exec, code, {'__builtins__': frozen_builtins}) + # no __build_class__ function + code = compile("class A: pass", "", "exec") + self.assertRaisesRegex(NameError, "__build_class__ not found", + exec, code, {'__builtins__': {}}) + # __build_class__ in a custom __builtins__ + exec(code, {'__builtins__': frozen_builtins}) + self.assertRaisesRegex(NameError, "__build_class__ not found", + exec, code, {'__builtins__': frozendict()}) + # read-only globals namespace = frozendict({}) code = compile("x=1", "test", "exec")