(self)
| 99 | (AttributeError, TypeError)) |
| 100 | |
| 101 | def test___builtins__(self): |
| 102 | if __name__ == "__main__": |
| 103 | builtins_dict = __builtins__.__dict__ |
| 104 | else: |
| 105 | builtins_dict = __builtins__ |
| 106 | |
| 107 | self.assertIs(self.b.__builtins__, builtins_dict) |
| 108 | self.cannot_set_attr(self.b, '__builtins__', 2, |
| 109 | (AttributeError, TypeError)) |
| 110 | |
| 111 | # bpo-42990: If globals is specified and has no "__builtins__" key, |
| 112 | # a function inherits the current builtins namespace. |
| 113 | def func(s): return len(s) |
| 114 | ns = {} |
| 115 | func2 = type(func)(func.__code__, ns) |
| 116 | self.assertIs(func2.__globals__, ns) |
| 117 | self.assertIs(func2.__builtins__, builtins_dict) |
| 118 | |
| 119 | # Make sure that the function actually works. |
| 120 | self.assertEqual(func2("abc"), 3) |
| 121 | self.assertEqual(ns, {}) |
| 122 | |
| 123 | # Define functions using exec() with different builtins, |
| 124 | # and test inheritance when globals has no "__builtins__" key |
| 125 | code = textwrap.dedent(""" |
| 126 | def func3(s): pass |
| 127 | func4 = type(func3)(func3.__code__, {}) |
| 128 | """) |
| 129 | safe_builtins = {'None': None} |
| 130 | ns = {'type': type, '__builtins__': safe_builtins} |
| 131 | exec(code, ns) |
| 132 | self.assertIs(ns['func3'].__builtins__, safe_builtins) |
| 133 | self.assertIs(ns['func4'].__builtins__, safe_builtins) |
| 134 | self.assertIs(ns['func3'].__globals__['__builtins__'], safe_builtins) |
| 135 | self.assertNotIn('__builtins__', ns['func4'].__globals__) |
| 136 | |
| 137 | def test___closure__(self): |
| 138 | a = 12 |
nothing calls this directly
no test coverage detected