gh-122864: Fix a ``test_funcattrs.test___builtins__`` when executing directly (#124845)

Previously when executing ``test_functattrs.test___builtins__`` directly, it failed because the fact, that ``__builtins__`` is refers to the built-in module ``builtins`` while it's expects a ``__builtins__.__dict__``. But when this test is being run from another module, then ``__builtins__`` is refers to ``builtins.__dict__``. Now this part of the behaviour is covered.
---------

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Mark Byrne 2024-10-02 11:58:39 +02:00 committed by GitHub
parent 6737333ac5
commit 8fbf10d6cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 2 deletions

View File

@ -98,7 +98,12 @@ class FunctionPropertiesTest(FuncAttrsTest):
(AttributeError, TypeError)) (AttributeError, TypeError))
def test___builtins__(self): def test___builtins__(self):
self.assertIs(self.b.__builtins__, __builtins__) if __name__ == "__main__":
builtins_dict = __builtins__.__dict__
else:
builtins_dict = __builtins__
self.assertIs(self.b.__builtins__, builtins_dict)
self.cannot_set_attr(self.b, '__builtins__', 2, self.cannot_set_attr(self.b, '__builtins__', 2,
(AttributeError, TypeError)) (AttributeError, TypeError))
@ -108,7 +113,7 @@ class FunctionPropertiesTest(FuncAttrsTest):
ns = {} ns = {}
func2 = type(func)(func.__code__, ns) func2 = type(func)(func.__code__, ns)
self.assertIs(func2.__globals__, ns) self.assertIs(func2.__globals__, ns)
self.assertIs(func2.__builtins__, __builtins__) self.assertIs(func2.__builtins__, builtins_dict)
# Make sure that the function actually works. # Make sure that the function actually works.
self.assertEqual(func2("abc"), 3) self.assertEqual(func2("abc"), 3)