| Victor Stinner | 46496f9 | 2021-02-20 15:17:18 +0100 | [diff] [blame] | 1 | import textwrap |
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 2 | import types |
| Jelle Zijlstra | 3fadd7d | 2023-05-18 16:45:37 -0700 | [diff] [blame] | 3 | import typing |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 4 | import unittest |
| Irit Katriel | 2f9cb7e | 2023-11-07 18:54:36 +0000 | [diff] [blame] | 5 | import warnings |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 6 | |
| Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 7 | |
| 8 | def global_function(): |
| 9 | def inner_function(): |
| 10 | class LocalClass: |
| 11 | pass |
| Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 12 | global inner_global_function |
| 13 | def inner_global_function(): |
| Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 14 | def inner_function2(): |
| 15 | pass |
| 16 | return inner_function2 |
| Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 17 | return LocalClass |
| 18 | return lambda: inner_function |
| 19 | |
| 20 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 21 | class FuncAttrsTest(unittest.TestCase): |
| 22 | def setUp(self): |
| 23 | class F: |
| 24 | def a(self): |
| 25 | pass |
| 26 | def b(): |
| 27 | return 3 |
| 28 | self.fi = F() |
| 29 | self.F = F |
| 30 | self.b = b |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 31 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 32 | def cannot_set_attr(self, obj, name, value, exceptions): |
| 33 | try: |
| 34 | setattr(obj, name, value) |
| 35 | except exceptions: |
| 36 | pass |
| 37 | else: |
| 38 | self.fail("shouldn't be able to set %s to %r" % (name, value)) |
| 39 | try: |
| 40 | delattr(obj, name) |
| 41 | except exceptions: |
| 42 | pass |
| 43 | else: |
| 44 | self.fail("shouldn't be able to del %s" % name) |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 45 | |
| Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 46 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 47 | class FunctionPropertiesTest(FuncAttrsTest): |
| 48 | # Include the external setUp method that is common to all tests |
| 49 | def test_module(self): |
| 50 | self.assertEqual(self.b.__module__, __name__) |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 51 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 52 | def test_dir_includes_correct_attrs(self): |
| 53 | self.b.known_attr = 7 |
| Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 54 | self.assertIn('known_attr', dir(self.b), |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 55 | "set attributes not in dir listing of method") |
| 56 | # Test on underlying function object of method |
| 57 | self.F.a.known_attr = 7 |
| Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 58 | self.assertIn('known_attr', dir(self.fi.a), "set attribute on function " |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 59 | "implementations, should show up in next dir") |
| Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 60 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 61 | def test_duplicate_function_equality(self): |
| 62 | # Body of `duplicate' is the exact same as self.b |
| 63 | def duplicate(): |
| 64 | 'my docstring' |
| 65 | return 3 |
| 66 | self.assertNotEqual(self.b, duplicate) |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 67 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 68 | def test_copying___code__(self): |
| 69 | def test(): pass |
| 70 | self.assertEqual(test(), None) |
| 71 | test.__code__ = self.b.__code__ |
| 72 | self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 73 | |
| Irit Katriel | 2f9cb7e | 2023-11-07 18:54:36 +0000 | [diff] [blame] | 74 | def test_invalid___code___assignment(self): |
| 75 | def A(): pass |
| 76 | def B(): yield |
| 77 | async def C(): yield |
| 78 | async def D(x): await x |
| 79 | |
| 80 | for src in [A, B, C, D]: |
| 81 | for dst in [A, B, C, D]: |
| 82 | if src == dst: |
| 83 | continue |
| 84 | |
| 85 | assert src.__code__.co_flags != dst.__code__.co_flags |
| 86 | prev = dst.__code__ |
| 87 | try: |
| 88 | with self.assertWarnsRegex(DeprecationWarning, 'code object of non-matching type'): |
| 89 | dst.__code__ = src.__code__ |
| 90 | finally: |
| 91 | with warnings.catch_warnings(): |
| 92 | warnings.filterwarnings('ignore', '', DeprecationWarning) |
| 93 | dst.__code__ = prev |
| 94 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 95 | def test___globals__(self): |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 96 | self.assertIs(self.b.__globals__, globals()) |
| 97 | self.cannot_set_attr(self.b, '__globals__', 2, |
| 98 | (AttributeError, TypeError)) |
| 99 | |
| Victor Stinner | a3c3ffa | 2021-02-18 12:35:37 +0100 | [diff] [blame] | 100 | def test___builtins__(self): |
| 101 | self.assertIs(self.b.__builtins__, __builtins__) |
| 102 | self.cannot_set_attr(self.b, '__builtins__', 2, |
| 103 | (AttributeError, TypeError)) |
| 104 | |
| Victor Stinner | 46496f9 | 2021-02-20 15:17:18 +0100 | [diff] [blame] | 105 | # bpo-42990: If globals is specified and has no "__builtins__" key, |
| 106 | # a function inherits the current builtins namespace. |
| 107 | def func(s): return len(s) |
| 108 | ns = {} |
| 109 | func2 = type(func)(func.__code__, ns) |
| 110 | self.assertIs(func2.__globals__, ns) |
| 111 | self.assertIs(func2.__builtins__, __builtins__) |
| 112 | |
| 113 | # Make sure that the function actually works. |
| 114 | self.assertEqual(func2("abc"), 3) |
| 115 | self.assertEqual(ns, {}) |
| 116 | |
| 117 | # Define functions using exec() with different builtins, |
| 118 | # and test inheritance when globals has no "__builtins__" key |
| 119 | code = textwrap.dedent(""" |
| 120 | def func3(s): pass |
| 121 | func4 = type(func3)(func3.__code__, {}) |
| 122 | """) |
| 123 | safe_builtins = {'None': None} |
| 124 | ns = {'type': type, '__builtins__': safe_builtins} |
| 125 | exec(code, ns) |
| 126 | self.assertIs(ns['func3'].__builtins__, safe_builtins) |
| 127 | self.assertIs(ns['func4'].__builtins__, safe_builtins) |
| 128 | self.assertIs(ns['func3'].__globals__['__builtins__'], safe_builtins) |
| 129 | self.assertNotIn('__builtins__', ns['func4'].__globals__) |
| 130 | |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 131 | def test___closure__(self): |
| 132 | a = 12 |
| 133 | def f(): print(a) |
| 134 | c = f.__closure__ |
| Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 135 | self.assertIsInstance(c, tuple) |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 136 | self.assertEqual(len(c), 1) |
| 137 | # don't have a type object handy |
| 138 | self.assertEqual(c[0].__class__.__name__, "cell") |
| 139 | self.cannot_set_attr(f, "__closure__", c, AttributeError) |
| 140 | |
| Pierre Glaser | df8d2cd | 2019-02-07 20:36:48 +0100 | [diff] [blame] | 141 | def test_cell_new(self): |
| 142 | cell_obj = types.CellType(1) |
| 143 | self.assertEqual(cell_obj.cell_contents, 1) |
| 144 | |
| 145 | cell_obj = types.CellType() |
| 146 | msg = "shouldn't be able to read an empty cell" |
| 147 | with self.assertRaises(ValueError, msg=msg): |
| 148 | cell_obj.cell_contents |
| 149 | |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 150 | def test_empty_cell(self): |
| 151 | def f(): print(a) |
| 152 | try: |
| 153 | f.__closure__[0].cell_contents |
| 154 | except ValueError: |
| 155 | pass |
| 156 | else: |
| 157 | self.fail("shouldn't be able to read an empty cell") |
| 158 | a = 12 |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 159 | |
| Lisa Roach | 64505a1 | 2017-06-08 04:43:26 -0700 | [diff] [blame] | 160 | def test_set_cell(self): |
| 161 | a = 12 |
| 162 | def f(): return a |
| 163 | c = f.__closure__ |
| 164 | c[0].cell_contents = 9 |
| 165 | self.assertEqual(c[0].cell_contents, 9) |
| 166 | self.assertEqual(f(), 9) |
| 167 | self.assertEqual(a, 9) |
| 168 | del c[0].cell_contents |
| 169 | try: |
| 170 | c[0].cell_contents |
| 171 | except ValueError: |
| 172 | pass |
| 173 | else: |
| 174 | self.fail("shouldn't be able to read an empty cell") |
| 175 | with self.assertRaises(NameError): |
| 176 | f() |
| 177 | with self.assertRaises(UnboundLocalError): |
| 178 | print(a) |
| 179 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 180 | def test___name__(self): |
| 181 | self.assertEqual(self.b.__name__, 'b') |
| 182 | self.b.__name__ = 'c' |
| 183 | self.assertEqual(self.b.__name__, 'c') |
| 184 | self.b.__name__ = 'd' |
| 185 | self.assertEqual(self.b.__name__, 'd') |
| 186 | # __name__ and __name__ must be a string |
| 187 | self.cannot_set_attr(self.b, '__name__', 7, TypeError) |
| 188 | # __name__ must be available when in restricted mode. Exec will raise |
| 189 | # AttributeError if __name__ is not available on f. |
| 190 | s = """def f(): pass\nf.__name__""" |
| 191 | exec(s, {'__builtins__': {}}) |
| 192 | # Test on methods, too |
| 193 | self.assertEqual(self.fi.a.__name__, 'a') |
| 194 | self.cannot_set_attr(self.fi.a, "__name__", 'a', AttributeError) |
| Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 195 | |
| Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 196 | def test___qualname__(self): |
| 197 | # PEP 3155 |
| 198 | self.assertEqual(self.b.__qualname__, 'FuncAttrsTest.setUp.<locals>.b') |
| 199 | self.assertEqual(FuncAttrsTest.setUp.__qualname__, 'FuncAttrsTest.setUp') |
| 200 | self.assertEqual(global_function.__qualname__, 'global_function') |
| 201 | self.assertEqual(global_function().__qualname__, |
| 202 | 'global_function.<locals>.<lambda>') |
| 203 | self.assertEqual(global_function()().__qualname__, |
| 204 | 'global_function.<locals>.inner_function') |
| 205 | self.assertEqual(global_function()()().__qualname__, |
| 206 | 'global_function.<locals>.inner_function.<locals>.LocalClass') |
| Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 207 | self.assertEqual(inner_global_function.__qualname__, 'inner_global_function') |
| Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 208 | self.assertEqual(inner_global_function().__qualname__, 'inner_global_function.<locals>.inner_function2') |
| Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 209 | self.b.__qualname__ = 'c' |
| 210 | self.assertEqual(self.b.__qualname__, 'c') |
| 211 | self.b.__qualname__ = 'd' |
| 212 | self.assertEqual(self.b.__qualname__, 'd') |
| 213 | # __qualname__ must be a string |
| 214 | self.cannot_set_attr(self.b, '__qualname__', 7, TypeError) |
| 215 | |
| Jelle Zijlstra | 3fadd7d | 2023-05-18 16:45:37 -0700 | [diff] [blame] | 216 | def test___type_params__(self): |
| 217 | def generic[T](): pass |
| 218 | def not_generic(): pass |
| Nikita Sobolev | a8d440b | 2023-08-16 16:22:18 +0300 | [diff] [blame] | 219 | lambda_ = lambda: ... |
| Jelle Zijlstra | 3fadd7d | 2023-05-18 16:45:37 -0700 | [diff] [blame] | 220 | T, = generic.__type_params__ |
| 221 | self.assertIsInstance(T, typing.TypeVar) |
| 222 | self.assertEqual(generic.__type_params__, (T,)) |
| Nikita Sobolev | a8d440b | 2023-08-16 16:22:18 +0300 | [diff] [blame] | 223 | for func in (not_generic, lambda_): |
| 224 | with self.subTest(func=func): |
| 225 | self.assertEqual(func.__type_params__, ()) |
| 226 | with self.assertRaises(TypeError): |
| 227 | del func.__type_params__ |
| 228 | with self.assertRaises(TypeError): |
| 229 | func.__type_params__ = 42 |
| 230 | func.__type_params__ = (T,) |
| 231 | self.assertEqual(func.__type_params__, (T,)) |
| Jelle Zijlstra | 3fadd7d | 2023-05-18 16:45:37 -0700 | [diff] [blame] | 232 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 233 | def test___code__(self): |
| 234 | num_one, num_two = 7, 8 |
| 235 | def a(): pass |
| 236 | def b(): return 12 |
| 237 | def c(): return num_one |
| 238 | def d(): return num_two |
| 239 | def e(): return num_one, num_two |
| 240 | for func in [a, b, c, d, e]: |
| 241 | self.assertEqual(type(func.__code__), types.CodeType) |
| 242 | self.assertEqual(c(), 7) |
| 243 | self.assertEqual(d(), 8) |
| 244 | d.__code__ = c.__code__ |
| 245 | self.assertEqual(c.__code__, d.__code__) |
| 246 | self.assertEqual(c(), 7) |
| 247 | # self.assertEqual(d(), 7) |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 248 | try: |
| 249 | b.__code__ = c.__code__ |
| 250 | except ValueError: |
| 251 | pass |
| 252 | else: |
| 253 | self.fail("__code__ with different numbers of free vars should " |
| 254 | "not be possible") |
| 255 | try: |
| 256 | e.__code__ = d.__code__ |
| 257 | except ValueError: |
| 258 | pass |
| 259 | else: |
| 260 | self.fail("__code__ with different numbers of free vars should " |
| 261 | "not be possible") |
| Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 262 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 263 | def test_blank_func_defaults(self): |
| 264 | self.assertEqual(self.b.__defaults__, None) |
| 265 | del self.b.__defaults__ |
| 266 | self.assertEqual(self.b.__defaults__, None) |
| Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 267 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 268 | def test_func_default_args(self): |
| 269 | def first_func(a, b): |
| 270 | return a+b |
| 271 | def second_func(a=1, b=2): |
| 272 | return a+b |
| 273 | self.assertEqual(first_func.__defaults__, None) |
| 274 | self.assertEqual(second_func.__defaults__, (1, 2)) |
| 275 | first_func.__defaults__ = (1, 2) |
| 276 | self.assertEqual(first_func.__defaults__, (1, 2)) |
| 277 | self.assertEqual(first_func(), 3) |
| 278 | self.assertEqual(first_func(3), 5) |
| 279 | self.assertEqual(first_func(3, 5), 8) |
| 280 | del second_func.__defaults__ |
| 281 | self.assertEqual(second_func.__defaults__, None) |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 282 | try: |
| 283 | second_func() |
| 284 | except TypeError: |
| 285 | pass |
| 286 | else: |
| 287 | self.fail("__defaults__ does not update; deleting it does not " |
| 288 | "remove requirement") |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 289 | |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 290 | |
| 291 | class InstancemethodAttrTest(FuncAttrsTest): |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 292 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 293 | def test___class__(self): |
| 294 | self.assertEqual(self.fi.a.__self__.__class__, self.F) |
| 295 | self.cannot_set_attr(self.fi.a, "__class__", self.F, TypeError) |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 296 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 297 | def test___func__(self): |
| 298 | self.assertEqual(self.fi.a.__func__, self.F.a) |
| 299 | self.cannot_set_attr(self.fi.a, "__func__", self.F.a, AttributeError) |
| Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 300 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 301 | def test___self__(self): |
| 302 | self.assertEqual(self.fi.a.__self__, self.fi) |
| 303 | self.cannot_set_attr(self.fi.a, "__self__", self.fi, AttributeError) |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 304 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 305 | def test___func___non_method(self): |
| 306 | # Behavior should be the same when a method is added via an attr |
| 307 | # assignment |
| 308 | self.fi.id = types.MethodType(id, self.fi) |
| 309 | self.assertEqual(self.fi.id(), id(self.fi)) |
| 310 | # Test usage |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 311 | try: |
| 312 | self.fi.id.unknown_attr |
| 313 | except AttributeError: |
| 314 | pass |
| 315 | else: |
| 316 | self.fail("using unknown attributes should raise AttributeError") |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 317 | # Test assignment and deletion |
| 318 | self.cannot_set_attr(self.fi.id, 'unknown_attr', 2, AttributeError) |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 319 | |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 320 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 321 | class ArbitraryFunctionAttrTest(FuncAttrsTest): |
| 322 | def test_set_attr(self): |
| 323 | self.b.known_attr = 7 |
| 324 | self.assertEqual(self.b.known_attr, 7) |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 325 | try: |
| 326 | self.fi.a.known_attr = 7 |
| 327 | except AttributeError: |
| 328 | pass |
| 329 | else: |
| 330 | self.fail("setting attributes on methods should raise error") |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 331 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 332 | def test_delete_unknown_attr(self): |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 333 | try: |
| 334 | del self.b.unknown_attr |
| 335 | except AttributeError: |
| 336 | pass |
| 337 | else: |
| 338 | self.fail("deleting unknown attribute should raise TypeError") |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 339 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 340 | def test_unset_attr(self): |
| 341 | for func in [self.b, self.fi.a]: |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 342 | try: |
| 343 | func.non_existent_attr |
| 344 | except AttributeError: |
| 345 | pass |
| 346 | else: |
| 347 | self.fail("using unknown attributes should raise " |
| 348 | "AttributeError") |
| 349 | |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 350 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 351 | class FunctionDictsTest(FuncAttrsTest): |
| 352 | def test_setting_dict_to_invalid(self): |
| 353 | self.cannot_set_attr(self.b, '__dict__', None, TypeError) |
| Raymond Hettinger | f80680d | 2008-02-06 00:07:11 +0000 | [diff] [blame] | 354 | from collections import UserDict |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 355 | d = UserDict({'known_attr': 7}) |
| 356 | self.cannot_set_attr(self.fi.a.__func__, '__dict__', d, TypeError) |
| Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 357 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 358 | def test_setting_dict_to_valid(self): |
| 359 | d = {'known_attr': 7} |
| 360 | self.b.__dict__ = d |
| 361 | # Test assignment |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 362 | self.assertIs(d, self.b.__dict__) |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 363 | # ... and on all the different ways of referencing the method's func |
| 364 | self.F.a.__dict__ = d |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 365 | self.assertIs(d, self.fi.a.__func__.__dict__) |
| 366 | self.assertIs(d, self.fi.a.__dict__) |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 367 | # Test value |
| 368 | self.assertEqual(self.b.known_attr, 7) |
| 369 | self.assertEqual(self.b.__dict__['known_attr'], 7) |
| 370 | # ... and again, on all the different method's names |
| 371 | self.assertEqual(self.fi.a.__func__.known_attr, 7) |
| 372 | self.assertEqual(self.fi.a.known_attr, 7) |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 373 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 374 | def test_delete___dict__(self): |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 375 | try: |
| 376 | del self.b.__dict__ |
| 377 | except TypeError: |
| 378 | pass |
| 379 | else: |
| 380 | self.fail("deleting function dictionary should raise TypeError") |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 381 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 382 | def test_unassigned_dict(self): |
| 383 | self.assertEqual(self.b.__dict__, {}) |
| Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 384 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 385 | def test_func_as_dict_key(self): |
| 386 | value = "Some string" |
| 387 | d = {} |
| 388 | d[self.b] = value |
| 389 | self.assertEqual(d[self.b], value) |
| Barry Warsaw | 534c60f | 2001-01-15 21:00:02 +0000 | [diff] [blame] | 390 | |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 391 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 392 | class FunctionDocstringTest(FuncAttrsTest): |
| 393 | def test_set_docstring_attr(self): |
| 394 | self.assertEqual(self.b.__doc__, None) |
| 395 | docstr = "A test method that does nothing" |
| 396 | self.b.__doc__ = docstr |
| 397 | self.F.a.__doc__ = docstr |
| 398 | self.assertEqual(self.b.__doc__, docstr) |
| 399 | self.assertEqual(self.fi.a.__doc__, docstr) |
| 400 | self.cannot_set_attr(self.fi.a, "__doc__", docstr, AttributeError) |
| Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 401 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 402 | def test_delete_docstring(self): |
| 403 | self.b.__doc__ = "The docstring" |
| 404 | del self.b.__doc__ |
| 405 | self.assertEqual(self.b.__doc__, None) |
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 406 | |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 407 | |
| Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 408 | def cell(value): |
| 409 | """Create a cell containing the given value.""" |
| 410 | def f(): |
| 411 | print(a) |
| 412 | a = value |
| 413 | return f.__closure__[0] |
| 414 | |
| 415 | def empty_cell(empty=True): |
| 416 | """Create an empty cell.""" |
| 417 | def f(): |
| 418 | print(a) |
| 419 | # the intent of the following line is simply "if False:"; it's |
| 420 | # spelt this way to avoid the danger that a future optimization |
| 421 | # might simply remove an "if False:" code block. |
| 422 | if not empty: |
| 423 | a = 1729 |
| 424 | return f.__closure__[0] |
| 425 | |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 426 | |
| Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 427 | class CellTest(unittest.TestCase): |
| 428 | def test_comparison(self): |
| 429 | # These tests are here simply to exercise the comparison code; |
| 430 | # their presence should not be interpreted as providing any |
| 431 | # guarantees about the semantics (or even existence) of cell |
| 432 | # comparisons in future versions of CPython. |
| Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 433 | self.assertTrue(cell(2) < cell(3)) |
| 434 | self.assertTrue(empty_cell() < cell('saturday')) |
| 435 | self.assertTrue(empty_cell() == empty_cell()) |
| 436 | self.assertTrue(cell(-36) == cell(-36.0)) |
| 437 | self.assertTrue(cell(True) > empty_cell()) |
| Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 438 | |
| Georg Brandl | 4cb97d0 | 2009-09-04 11:20:54 +0000 | [diff] [blame] | 439 | |
| Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 440 | class StaticMethodAttrsTest(unittest.TestCase): |
| 441 | def test_func_attribute(self): |
| 442 | def f(): |
| 443 | pass |
| 444 | |
| 445 | c = classmethod(f) |
| Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 446 | self.assertTrue(c.__func__ is f) |
| Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 447 | |
| 448 | s = staticmethod(f) |
| Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 449 | self.assertTrue(s.__func__ is f) |
| Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 450 | |
| Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 451 | |
| Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 452 | class BuiltinFunctionPropertiesTest(unittest.TestCase): |
| 453 | # XXX Not sure where this should really go since I can't find a |
| 454 | # test module specifically for builtin_function_or_method. |
| 455 | |
| 456 | def test_builtin__qualname__(self): |
| 457 | import time |
| 458 | |
| 459 | # builtin function: |
| 460 | self.assertEqual(len.__qualname__, 'len') |
| 461 | self.assertEqual(time.time.__qualname__, 'time') |
| 462 | |
| 463 | # builtin classmethod: |
| 464 | self.assertEqual(dict.fromkeys.__qualname__, 'dict.fromkeys') |
| 465 | self.assertEqual(float.__getformat__.__qualname__, |
| 466 | 'float.__getformat__') |
| 467 | |
| 468 | # builtin staticmethod: |
| 469 | self.assertEqual(str.maketrans.__qualname__, 'str.maketrans') |
| 470 | self.assertEqual(bytes.maketrans.__qualname__, 'bytes.maketrans') |
| 471 | |
| 472 | # builtin bound instance method: |
| 473 | self.assertEqual([1, 2, 3].append.__qualname__, 'list.append') |
| 474 | self.assertEqual({'foo': 'bar'}.pop.__qualname__, 'dict.pop') |
| 475 | |
| 476 | |
| Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 477 | if __name__ == "__main__": |
| Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 478 | unittest.main() |