blob: b3fc5ad42e7fde472f6c2768fee8d5d6c739db3f [file] [log] [blame]
Victor Stinner46496f92021-02-20 15:17:18 +01001import textwrap
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +00002import types
Jelle Zijlstra3fadd7d2023-05-18 16:45:37 -07003import typing
Georg Brandl60d14562008-02-05 18:31:41 +00004import unittest
Irit Katriel2f9cb7e2023-11-07 18:54:36 +00005import warnings
Barry Warsaw4a420a02001-01-15 20:30:15 +00006
Antoine Pitrou86a36b52011-11-25 18:56:07 +01007
8def global_function():
9 def inner_function():
10 class LocalClass:
11 pass
Benjamin Peterson3d9e4812013-10-19 16:01:13 -040012 global inner_global_function
13 def inner_global_function():
Benjamin Peterson6b4f7802013-10-20 17:50:28 -040014 def inner_function2():
15 pass
16 return inner_function2
Antoine Pitrou86a36b52011-11-25 18:56:07 +010017 return LocalClass
18 return lambda: inner_function
19
20
Georg Brandl60d14562008-02-05 18:31:41 +000021class 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 Warsaw4a420a02001-01-15 20:30:15 +000031
Georg Brandl60d14562008-02-05 18:31:41 +000032 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 Warsaw4a420a02001-01-15 20:30:15 +000045
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000046
Georg Brandl60d14562008-02-05 18:31:41 +000047class 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 Warsaw4a420a02001-01-15 20:30:15 +000051
Georg Brandl60d14562008-02-05 18:31:41 +000052 def test_dir_includes_correct_attrs(self):
53 self.b.known_attr = 7
Benjamin Peterson577473f2010-01-19 00:09:57 +000054 self.assertIn('known_attr', dir(self.b),
Georg Brandl60d14562008-02-05 18:31:41 +000055 "set attributes not in dir listing of method")
56 # Test on underlying function object of method
57 self.F.a.known_attr = 7
Benjamin Peterson577473f2010-01-19 00:09:57 +000058 self.assertIn('known_attr', dir(self.fi.a), "set attribute on function "
Georg Brandl60d14562008-02-05 18:31:41 +000059 "implementations, should show up in next dir")
Barry Warsawc1e100f2001-02-26 18:07:26 +000060
Georg Brandl60d14562008-02-05 18:31:41 +000061 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 Warsaw4a420a02001-01-15 20:30:15 +000067
Georg Brandl60d14562008-02-05 18:31:41 +000068 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 Warsaw4a420a02001-01-15 20:30:15 +000073
Irit Katriel2f9cb7e2023-11-07 18:54:36 +000074 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 Brandl60d14562008-02-05 18:31:41 +000095 def test___globals__(self):
Georg Brandl4cb97d02009-09-04 11:20:54 +000096 self.assertIs(self.b.__globals__, globals())
97 self.cannot_set_attr(self.b, '__globals__', 2,
98 (AttributeError, TypeError))
99
Victor Stinnera3c3ffa2021-02-18 12:35:37 +0100100 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 Stinner46496f92021-02-20 15:17:18 +0100105 # 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 Brandl4cb97d02009-09-04 11:20:54 +0000131 def test___closure__(self):
132 a = 12
133 def f(): print(a)
134 c = f.__closure__
Ezio Melottie9615932010-01-24 19:26:24 +0000135 self.assertIsInstance(c, tuple)
Georg Brandl4cb97d02009-09-04 11:20:54 +0000136 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 Glaserdf8d2cd2019-02-07 20:36:48 +0100141 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 Brandl4cb97d02009-09-04 11:20:54 +0000150 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 Warsaw4a420a02001-01-15 20:30:15 +0000159
Lisa Roach64505a12017-06-08 04:43:26 -0700160 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 Brandl60d14562008-02-05 18:31:41 +0000180 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 Warsawc1e100f2001-02-26 18:07:26 +0000195
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100196 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 Peterson3d9e4812013-10-19 16:01:13 -0400207 self.assertEqual(inner_global_function.__qualname__, 'inner_global_function')
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400208 self.assertEqual(inner_global_function().__qualname__, 'inner_global_function.<locals>.inner_function2')
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100209 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 Zijlstra3fadd7d2023-05-18 16:45:37 -0700216 def test___type_params__(self):
217 def generic[T](): pass
218 def not_generic(): pass
Nikita Soboleva8d440b2023-08-16 16:22:18 +0300219 lambda_ = lambda: ...
Jelle Zijlstra3fadd7d2023-05-18 16:45:37 -0700220 T, = generic.__type_params__
221 self.assertIsInstance(T, typing.TypeVar)
222 self.assertEqual(generic.__type_params__, (T,))
Nikita Soboleva8d440b2023-08-16 16:22:18 +0300223 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 Zijlstra3fadd7d2023-05-18 16:45:37 -0700232
Georg Brandl60d14562008-02-05 18:31:41 +0000233 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 Brandl4cb97d02009-09-04 11:20:54 +0000248 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 Warsawc1e100f2001-02-26 18:07:26 +0000262
Georg Brandl60d14562008-02-05 18:31:41 +0000263 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 Warsawc1e100f2001-02-26 18:07:26 +0000267
Georg Brandl60d14562008-02-05 18:31:41 +0000268 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 Brandl4cb97d02009-09-04 11:20:54 +0000282 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 Warsaw4a420a02001-01-15 20:30:15 +0000289
Georg Brandl4cb97d02009-09-04 11:20:54 +0000290
291class InstancemethodAttrTest(FuncAttrsTest):
Barry Warsaw4a420a02001-01-15 20:30:15 +0000292
Georg Brandl60d14562008-02-05 18:31:41 +0000293 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 Warsaw4a420a02001-01-15 20:30:15 +0000296
Georg Brandl60d14562008-02-05 18:31:41 +0000297 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 Warsawc1e100f2001-02-26 18:07:26 +0000300
Georg Brandl60d14562008-02-05 18:31:41 +0000301 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 Warsaw4a420a02001-01-15 20:30:15 +0000304
Georg Brandl60d14562008-02-05 18:31:41 +0000305 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 Brandl4cb97d02009-09-04 11:20:54 +0000311 try:
312 self.fi.id.unknown_attr
313 except AttributeError:
314 pass
315 else:
316 self.fail("using unknown attributes should raise AttributeError")
Georg Brandl60d14562008-02-05 18:31:41 +0000317 # Test assignment and deletion
318 self.cannot_set_attr(self.fi.id, 'unknown_attr', 2, AttributeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000319
Georg Brandl4cb97d02009-09-04 11:20:54 +0000320
Georg Brandl60d14562008-02-05 18:31:41 +0000321class ArbitraryFunctionAttrTest(FuncAttrsTest):
322 def test_set_attr(self):
323 self.b.known_attr = 7
324 self.assertEqual(self.b.known_attr, 7)
Georg Brandl4cb97d02009-09-04 11:20:54 +0000325 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 Warsaw4a420a02001-01-15 20:30:15 +0000331
Georg Brandl60d14562008-02-05 18:31:41 +0000332 def test_delete_unknown_attr(self):
Georg Brandl4cb97d02009-09-04 11:20:54 +0000333 try:
334 del self.b.unknown_attr
335 except AttributeError:
336 pass
337 else:
338 self.fail("deleting unknown attribute should raise TypeError")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000339
Georg Brandl60d14562008-02-05 18:31:41 +0000340 def test_unset_attr(self):
341 for func in [self.b, self.fi.a]:
Georg Brandl4cb97d02009-09-04 11:20:54 +0000342 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 Warsaw4a420a02001-01-15 20:30:15 +0000350
Georg Brandl60d14562008-02-05 18:31:41 +0000351class FunctionDictsTest(FuncAttrsTest):
352 def test_setting_dict_to_invalid(self):
353 self.cannot_set_attr(self.b, '__dict__', None, TypeError)
Raymond Hettingerf80680d2008-02-06 00:07:11 +0000354 from collections import UserDict
Georg Brandl60d14562008-02-05 18:31:41 +0000355 d = UserDict({'known_attr': 7})
356 self.cannot_set_attr(self.fi.a.__func__, '__dict__', d, TypeError)
Barry Warsawc1e100f2001-02-26 18:07:26 +0000357
Georg Brandl60d14562008-02-05 18:31:41 +0000358 def test_setting_dict_to_valid(self):
359 d = {'known_attr': 7}
360 self.b.__dict__ = d
361 # Test assignment
Georg Brandl4cb97d02009-09-04 11:20:54 +0000362 self.assertIs(d, self.b.__dict__)
Georg Brandl60d14562008-02-05 18:31:41 +0000363 # ... and on all the different ways of referencing the method's func
364 self.F.a.__dict__ = d
Georg Brandl4cb97d02009-09-04 11:20:54 +0000365 self.assertIs(d, self.fi.a.__func__.__dict__)
366 self.assertIs(d, self.fi.a.__dict__)
Georg Brandl60d14562008-02-05 18:31:41 +0000367 # 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 Warsaw4a420a02001-01-15 20:30:15 +0000373
Georg Brandl60d14562008-02-05 18:31:41 +0000374 def test_delete___dict__(self):
Georg Brandl4cb97d02009-09-04 11:20:54 +0000375 try:
376 del self.b.__dict__
377 except TypeError:
378 pass
379 else:
380 self.fail("deleting function dictionary should raise TypeError")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000381
Georg Brandl60d14562008-02-05 18:31:41 +0000382 def test_unassigned_dict(self):
383 self.assertEqual(self.b.__dict__, {})
Barry Warsaw4a420a02001-01-15 20:30:15 +0000384
Georg Brandl60d14562008-02-05 18:31:41 +0000385 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 Warsaw534c60f2001-01-15 21:00:02 +0000390
Georg Brandl4cb97d02009-09-04 11:20:54 +0000391
Georg Brandl60d14562008-02-05 18:31:41 +0000392class 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 Rossumbd131492001-09-18 03:28:54 +0000401
Georg Brandl60d14562008-02-05 18:31:41 +0000402 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 Rossumd9d1d4a2001-09-17 23:46:56 +0000406
Georg Brandl4cb97d02009-09-04 11:20:54 +0000407
Mark Dickinson211c6252009-02-01 10:28:51 +0000408def 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
415def 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 Brandl4cb97d02009-09-04 11:20:54 +0000426
Mark Dickinson211c6252009-02-01 10:28:51 +0000427class 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 Petersonc9c0f202009-06-30 23:06:06 +0000433 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 Dickinson211c6252009-02-01 10:28:51 +0000438
Georg Brandl4cb97d02009-09-04 11:20:54 +0000439
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000440class StaticMethodAttrsTest(unittest.TestCase):
441 def test_func_attribute(self):
442 def f():
443 pass
444
445 c = classmethod(f)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000446 self.assertTrue(c.__func__ is f)
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000447
448 s = staticmethod(f)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000449 self.assertTrue(s.__func__ is f)
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000450
Mark Dickinson211c6252009-02-01 10:28:51 +0000451
Antoine Pitrou5b629422011-12-23 12:40:16 +0100452class 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 Brandl60d14562008-02-05 18:31:41 +0000477if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500478 unittest.main()