blob: 77977d0ae966f8a1682b6fd8110bf0a6b495831e [file] [log] [blame]
Victor Stinner46496f92021-02-20 15:17:18 +01001import textwrap
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +00002import types
Georg Brandl60d14562008-02-05 18:31:41 +00003import unittest
Barry Warsaw4a420a02001-01-15 20:30:15 +00004
Antoine Pitrou86a36b52011-11-25 18:56:07 +01005
6def global_function():
7 def inner_function():
8 class LocalClass:
9 pass
Benjamin Peterson3d9e4812013-10-19 16:01:13 -040010 global inner_global_function
11 def inner_global_function():
Benjamin Peterson6b4f7802013-10-20 17:50:28 -040012 def inner_function2():
13 pass
14 return inner_function2
Antoine Pitrou86a36b52011-11-25 18:56:07 +010015 return LocalClass
16 return lambda: inner_function
17
18
Georg Brandl60d14562008-02-05 18:31:41 +000019class FuncAttrsTest(unittest.TestCase):
20 def setUp(self):
21 class F:
22 def a(self):
23 pass
24 def b():
25 return 3
26 self.fi = F()
27 self.F = F
28 self.b = b
Barry Warsaw4a420a02001-01-15 20:30:15 +000029
Georg Brandl60d14562008-02-05 18:31:41 +000030 def cannot_set_attr(self, obj, name, value, exceptions):
31 try:
32 setattr(obj, name, value)
33 except exceptions:
34 pass
35 else:
36 self.fail("shouldn't be able to set %s to %r" % (name, value))
37 try:
38 delattr(obj, name)
39 except exceptions:
40 pass
41 else:
42 self.fail("shouldn't be able to del %s" % name)
Barry Warsaw4a420a02001-01-15 20:30:15 +000043
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000044
Georg Brandl60d14562008-02-05 18:31:41 +000045class FunctionPropertiesTest(FuncAttrsTest):
46 # Include the external setUp method that is common to all tests
47 def test_module(self):
48 self.assertEqual(self.b.__module__, __name__)
Barry Warsaw4a420a02001-01-15 20:30:15 +000049
Georg Brandl60d14562008-02-05 18:31:41 +000050 def test_dir_includes_correct_attrs(self):
51 self.b.known_attr = 7
Benjamin Peterson577473f2010-01-19 00:09:57 +000052 self.assertIn('known_attr', dir(self.b),
Georg Brandl60d14562008-02-05 18:31:41 +000053 "set attributes not in dir listing of method")
54 # Test on underlying function object of method
55 self.F.a.known_attr = 7
Benjamin Peterson577473f2010-01-19 00:09:57 +000056 self.assertIn('known_attr', dir(self.fi.a), "set attribute on function "
Georg Brandl60d14562008-02-05 18:31:41 +000057 "implementations, should show up in next dir")
Barry Warsawc1e100f2001-02-26 18:07:26 +000058
Georg Brandl60d14562008-02-05 18:31:41 +000059 def test_duplicate_function_equality(self):
60 # Body of `duplicate' is the exact same as self.b
61 def duplicate():
62 'my docstring'
63 return 3
64 self.assertNotEqual(self.b, duplicate)
Barry Warsaw4a420a02001-01-15 20:30:15 +000065
Georg Brandl60d14562008-02-05 18:31:41 +000066 def test_copying___code__(self):
67 def test(): pass
68 self.assertEqual(test(), None)
69 test.__code__ = self.b.__code__
70 self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily
Barry Warsaw4a420a02001-01-15 20:30:15 +000071
Georg Brandl60d14562008-02-05 18:31:41 +000072 def test___globals__(self):
Georg Brandl4cb97d02009-09-04 11:20:54 +000073 self.assertIs(self.b.__globals__, globals())
74 self.cannot_set_attr(self.b, '__globals__', 2,
75 (AttributeError, TypeError))
76
Victor Stinnera3c3ffa2021-02-18 12:35:37 +010077 def test___builtins__(self):
78 self.assertIs(self.b.__builtins__, __builtins__)
79 self.cannot_set_attr(self.b, '__builtins__', 2,
80 (AttributeError, TypeError))
81
Victor Stinner46496f92021-02-20 15:17:18 +010082 # bpo-42990: If globals is specified and has no "__builtins__" key,
83 # a function inherits the current builtins namespace.
84 def func(s): return len(s)
85 ns = {}
86 func2 = type(func)(func.__code__, ns)
87 self.assertIs(func2.__globals__, ns)
88 self.assertIs(func2.__builtins__, __builtins__)
89
90 # Make sure that the function actually works.
91 self.assertEqual(func2("abc"), 3)
92 self.assertEqual(ns, {})
93
94 # Define functions using exec() with different builtins,
95 # and test inheritance when globals has no "__builtins__" key
96 code = textwrap.dedent("""
97 def func3(s): pass
98 func4 = type(func3)(func3.__code__, {})
99 """)
100 safe_builtins = {'None': None}
101 ns = {'type': type, '__builtins__': safe_builtins}
102 exec(code, ns)
103 self.assertIs(ns['func3'].__builtins__, safe_builtins)
104 self.assertIs(ns['func4'].__builtins__, safe_builtins)
105 self.assertIs(ns['func3'].__globals__['__builtins__'], safe_builtins)
106 self.assertNotIn('__builtins__', ns['func4'].__globals__)
107
Georg Brandl4cb97d02009-09-04 11:20:54 +0000108 def test___closure__(self):
109 a = 12
110 def f(): print(a)
111 c = f.__closure__
Ezio Melottie9615932010-01-24 19:26:24 +0000112 self.assertIsInstance(c, tuple)
Georg Brandl4cb97d02009-09-04 11:20:54 +0000113 self.assertEqual(len(c), 1)
114 # don't have a type object handy
115 self.assertEqual(c[0].__class__.__name__, "cell")
116 self.cannot_set_attr(f, "__closure__", c, AttributeError)
117
Pierre Glaserdf8d2cd2019-02-07 20:36:48 +0100118 def test_cell_new(self):
119 cell_obj = types.CellType(1)
120 self.assertEqual(cell_obj.cell_contents, 1)
121
122 cell_obj = types.CellType()
123 msg = "shouldn't be able to read an empty cell"
124 with self.assertRaises(ValueError, msg=msg):
125 cell_obj.cell_contents
126
Georg Brandl4cb97d02009-09-04 11:20:54 +0000127 def test_empty_cell(self):
128 def f(): print(a)
129 try:
130 f.__closure__[0].cell_contents
131 except ValueError:
132 pass
133 else:
134 self.fail("shouldn't be able to read an empty cell")
135 a = 12
Barry Warsaw4a420a02001-01-15 20:30:15 +0000136
Lisa Roach64505a12017-06-08 04:43:26 -0700137 def test_set_cell(self):
138 a = 12
139 def f(): return a
140 c = f.__closure__
141 c[0].cell_contents = 9
142 self.assertEqual(c[0].cell_contents, 9)
143 self.assertEqual(f(), 9)
144 self.assertEqual(a, 9)
145 del c[0].cell_contents
146 try:
147 c[0].cell_contents
148 except ValueError:
149 pass
150 else:
151 self.fail("shouldn't be able to read an empty cell")
152 with self.assertRaises(NameError):
153 f()
154 with self.assertRaises(UnboundLocalError):
155 print(a)
156
Georg Brandl60d14562008-02-05 18:31:41 +0000157 def test___name__(self):
158 self.assertEqual(self.b.__name__, 'b')
159 self.b.__name__ = 'c'
160 self.assertEqual(self.b.__name__, 'c')
161 self.b.__name__ = 'd'
162 self.assertEqual(self.b.__name__, 'd')
163 # __name__ and __name__ must be a string
164 self.cannot_set_attr(self.b, '__name__', 7, TypeError)
165 # __name__ must be available when in restricted mode. Exec will raise
166 # AttributeError if __name__ is not available on f.
167 s = """def f(): pass\nf.__name__"""
168 exec(s, {'__builtins__': {}})
169 # Test on methods, too
170 self.assertEqual(self.fi.a.__name__, 'a')
171 self.cannot_set_attr(self.fi.a, "__name__", 'a', AttributeError)
Barry Warsawc1e100f2001-02-26 18:07:26 +0000172
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100173 def test___qualname__(self):
174 # PEP 3155
175 self.assertEqual(self.b.__qualname__, 'FuncAttrsTest.setUp.<locals>.b')
176 self.assertEqual(FuncAttrsTest.setUp.__qualname__, 'FuncAttrsTest.setUp')
177 self.assertEqual(global_function.__qualname__, 'global_function')
178 self.assertEqual(global_function().__qualname__,
179 'global_function.<locals>.<lambda>')
180 self.assertEqual(global_function()().__qualname__,
181 'global_function.<locals>.inner_function')
182 self.assertEqual(global_function()()().__qualname__,
183 'global_function.<locals>.inner_function.<locals>.LocalClass')
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400184 self.assertEqual(inner_global_function.__qualname__, 'inner_global_function')
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400185 self.assertEqual(inner_global_function().__qualname__, 'inner_global_function.<locals>.inner_function2')
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100186 self.b.__qualname__ = 'c'
187 self.assertEqual(self.b.__qualname__, 'c')
188 self.b.__qualname__ = 'd'
189 self.assertEqual(self.b.__qualname__, 'd')
190 # __qualname__ must be a string
191 self.cannot_set_attr(self.b, '__qualname__', 7, TypeError)
192
Georg Brandl60d14562008-02-05 18:31:41 +0000193 def test___code__(self):
194 num_one, num_two = 7, 8
195 def a(): pass
196 def b(): return 12
197 def c(): return num_one
198 def d(): return num_two
199 def e(): return num_one, num_two
200 for func in [a, b, c, d, e]:
201 self.assertEqual(type(func.__code__), types.CodeType)
202 self.assertEqual(c(), 7)
203 self.assertEqual(d(), 8)
204 d.__code__ = c.__code__
205 self.assertEqual(c.__code__, d.__code__)
206 self.assertEqual(c(), 7)
207 # self.assertEqual(d(), 7)
Georg Brandl4cb97d02009-09-04 11:20:54 +0000208 try:
209 b.__code__ = c.__code__
210 except ValueError:
211 pass
212 else:
213 self.fail("__code__ with different numbers of free vars should "
214 "not be possible")
215 try:
216 e.__code__ = d.__code__
217 except ValueError:
218 pass
219 else:
220 self.fail("__code__ with different numbers of free vars should "
221 "not be possible")
Barry Warsawc1e100f2001-02-26 18:07:26 +0000222
Georg Brandl60d14562008-02-05 18:31:41 +0000223 def test_blank_func_defaults(self):
224 self.assertEqual(self.b.__defaults__, None)
225 del self.b.__defaults__
226 self.assertEqual(self.b.__defaults__, None)
Barry Warsawc1e100f2001-02-26 18:07:26 +0000227
Georg Brandl60d14562008-02-05 18:31:41 +0000228 def test_func_default_args(self):
229 def first_func(a, b):
230 return a+b
231 def second_func(a=1, b=2):
232 return a+b
233 self.assertEqual(first_func.__defaults__, None)
234 self.assertEqual(second_func.__defaults__, (1, 2))
235 first_func.__defaults__ = (1, 2)
236 self.assertEqual(first_func.__defaults__, (1, 2))
237 self.assertEqual(first_func(), 3)
238 self.assertEqual(first_func(3), 5)
239 self.assertEqual(first_func(3, 5), 8)
240 del second_func.__defaults__
241 self.assertEqual(second_func.__defaults__, None)
Georg Brandl4cb97d02009-09-04 11:20:54 +0000242 try:
243 second_func()
244 except TypeError:
245 pass
246 else:
247 self.fail("__defaults__ does not update; deleting it does not "
248 "remove requirement")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000249
Georg Brandl4cb97d02009-09-04 11:20:54 +0000250
251class InstancemethodAttrTest(FuncAttrsTest):
Barry Warsaw4a420a02001-01-15 20:30:15 +0000252
Georg Brandl60d14562008-02-05 18:31:41 +0000253 def test___class__(self):
254 self.assertEqual(self.fi.a.__self__.__class__, self.F)
255 self.cannot_set_attr(self.fi.a, "__class__", self.F, TypeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000256
Georg Brandl60d14562008-02-05 18:31:41 +0000257 def test___func__(self):
258 self.assertEqual(self.fi.a.__func__, self.F.a)
259 self.cannot_set_attr(self.fi.a, "__func__", self.F.a, AttributeError)
Barry Warsawc1e100f2001-02-26 18:07:26 +0000260
Georg Brandl60d14562008-02-05 18:31:41 +0000261 def test___self__(self):
262 self.assertEqual(self.fi.a.__self__, self.fi)
263 self.cannot_set_attr(self.fi.a, "__self__", self.fi, AttributeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000264
Georg Brandl60d14562008-02-05 18:31:41 +0000265 def test___func___non_method(self):
266 # Behavior should be the same when a method is added via an attr
267 # assignment
268 self.fi.id = types.MethodType(id, self.fi)
269 self.assertEqual(self.fi.id(), id(self.fi))
270 # Test usage
Georg Brandl4cb97d02009-09-04 11:20:54 +0000271 try:
272 self.fi.id.unknown_attr
273 except AttributeError:
274 pass
275 else:
276 self.fail("using unknown attributes should raise AttributeError")
Georg Brandl60d14562008-02-05 18:31:41 +0000277 # Test assignment and deletion
278 self.cannot_set_attr(self.fi.id, 'unknown_attr', 2, AttributeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000279
Georg Brandl4cb97d02009-09-04 11:20:54 +0000280
Georg Brandl60d14562008-02-05 18:31:41 +0000281class ArbitraryFunctionAttrTest(FuncAttrsTest):
282 def test_set_attr(self):
283 self.b.known_attr = 7
284 self.assertEqual(self.b.known_attr, 7)
Georg Brandl4cb97d02009-09-04 11:20:54 +0000285 try:
286 self.fi.a.known_attr = 7
287 except AttributeError:
288 pass
289 else:
290 self.fail("setting attributes on methods should raise error")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000291
Georg Brandl60d14562008-02-05 18:31:41 +0000292 def test_delete_unknown_attr(self):
Georg Brandl4cb97d02009-09-04 11:20:54 +0000293 try:
294 del self.b.unknown_attr
295 except AttributeError:
296 pass
297 else:
298 self.fail("deleting unknown attribute should raise TypeError")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000299
Georg Brandl60d14562008-02-05 18:31:41 +0000300 def test_unset_attr(self):
301 for func in [self.b, self.fi.a]:
Georg Brandl4cb97d02009-09-04 11:20:54 +0000302 try:
303 func.non_existent_attr
304 except AttributeError:
305 pass
306 else:
307 self.fail("using unknown attributes should raise "
308 "AttributeError")
309
Barry Warsaw4a420a02001-01-15 20:30:15 +0000310
Georg Brandl60d14562008-02-05 18:31:41 +0000311class FunctionDictsTest(FuncAttrsTest):
312 def test_setting_dict_to_invalid(self):
313 self.cannot_set_attr(self.b, '__dict__', None, TypeError)
Raymond Hettingerf80680d2008-02-06 00:07:11 +0000314 from collections import UserDict
Georg Brandl60d14562008-02-05 18:31:41 +0000315 d = UserDict({'known_attr': 7})
316 self.cannot_set_attr(self.fi.a.__func__, '__dict__', d, TypeError)
Barry Warsawc1e100f2001-02-26 18:07:26 +0000317
Georg Brandl60d14562008-02-05 18:31:41 +0000318 def test_setting_dict_to_valid(self):
319 d = {'known_attr': 7}
320 self.b.__dict__ = d
321 # Test assignment
Georg Brandl4cb97d02009-09-04 11:20:54 +0000322 self.assertIs(d, self.b.__dict__)
Georg Brandl60d14562008-02-05 18:31:41 +0000323 # ... and on all the different ways of referencing the method's func
324 self.F.a.__dict__ = d
Georg Brandl4cb97d02009-09-04 11:20:54 +0000325 self.assertIs(d, self.fi.a.__func__.__dict__)
326 self.assertIs(d, self.fi.a.__dict__)
Georg Brandl60d14562008-02-05 18:31:41 +0000327 # Test value
328 self.assertEqual(self.b.known_attr, 7)
329 self.assertEqual(self.b.__dict__['known_attr'], 7)
330 # ... and again, on all the different method's names
331 self.assertEqual(self.fi.a.__func__.known_attr, 7)
332 self.assertEqual(self.fi.a.known_attr, 7)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000333
Georg Brandl60d14562008-02-05 18:31:41 +0000334 def test_delete___dict__(self):
Georg Brandl4cb97d02009-09-04 11:20:54 +0000335 try:
336 del self.b.__dict__
337 except TypeError:
338 pass
339 else:
340 self.fail("deleting function dictionary should raise TypeError")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000341
Georg Brandl60d14562008-02-05 18:31:41 +0000342 def test_unassigned_dict(self):
343 self.assertEqual(self.b.__dict__, {})
Barry Warsaw4a420a02001-01-15 20:30:15 +0000344
Georg Brandl60d14562008-02-05 18:31:41 +0000345 def test_func_as_dict_key(self):
346 value = "Some string"
347 d = {}
348 d[self.b] = value
349 self.assertEqual(d[self.b], value)
Barry Warsaw534c60f2001-01-15 21:00:02 +0000350
Georg Brandl4cb97d02009-09-04 11:20:54 +0000351
Georg Brandl60d14562008-02-05 18:31:41 +0000352class FunctionDocstringTest(FuncAttrsTest):
353 def test_set_docstring_attr(self):
354 self.assertEqual(self.b.__doc__, None)
355 docstr = "A test method that does nothing"
356 self.b.__doc__ = docstr
357 self.F.a.__doc__ = docstr
358 self.assertEqual(self.b.__doc__, docstr)
359 self.assertEqual(self.fi.a.__doc__, docstr)
360 self.cannot_set_attr(self.fi.a, "__doc__", docstr, AttributeError)
Guido van Rossumbd131492001-09-18 03:28:54 +0000361
Georg Brandl60d14562008-02-05 18:31:41 +0000362 def test_delete_docstring(self):
363 self.b.__doc__ = "The docstring"
364 del self.b.__doc__
365 self.assertEqual(self.b.__doc__, None)
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000366
Georg Brandl4cb97d02009-09-04 11:20:54 +0000367
Mark Dickinson211c6252009-02-01 10:28:51 +0000368def cell(value):
369 """Create a cell containing the given value."""
370 def f():
371 print(a)
372 a = value
373 return f.__closure__[0]
374
375def empty_cell(empty=True):
376 """Create an empty cell."""
377 def f():
378 print(a)
379 # the intent of the following line is simply "if False:"; it's
380 # spelt this way to avoid the danger that a future optimization
381 # might simply remove an "if False:" code block.
382 if not empty:
383 a = 1729
384 return f.__closure__[0]
385
Georg Brandl4cb97d02009-09-04 11:20:54 +0000386
Mark Dickinson211c6252009-02-01 10:28:51 +0000387class CellTest(unittest.TestCase):
388 def test_comparison(self):
389 # These tests are here simply to exercise the comparison code;
390 # their presence should not be interpreted as providing any
391 # guarantees about the semantics (or even existence) of cell
392 # comparisons in future versions of CPython.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000393 self.assertTrue(cell(2) < cell(3))
394 self.assertTrue(empty_cell() < cell('saturday'))
395 self.assertTrue(empty_cell() == empty_cell())
396 self.assertTrue(cell(-36) == cell(-36.0))
397 self.assertTrue(cell(True) > empty_cell())
Mark Dickinson211c6252009-02-01 10:28:51 +0000398
Georg Brandl4cb97d02009-09-04 11:20:54 +0000399
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000400class StaticMethodAttrsTest(unittest.TestCase):
401 def test_func_attribute(self):
402 def f():
403 pass
404
405 c = classmethod(f)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000406 self.assertTrue(c.__func__ is f)
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000407
408 s = staticmethod(f)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000409 self.assertTrue(s.__func__ is f)
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000410
Mark Dickinson211c6252009-02-01 10:28:51 +0000411
Antoine Pitrou5b629422011-12-23 12:40:16 +0100412class BuiltinFunctionPropertiesTest(unittest.TestCase):
413 # XXX Not sure where this should really go since I can't find a
414 # test module specifically for builtin_function_or_method.
415
416 def test_builtin__qualname__(self):
417 import time
418
419 # builtin function:
420 self.assertEqual(len.__qualname__, 'len')
421 self.assertEqual(time.time.__qualname__, 'time')
422
423 # builtin classmethod:
424 self.assertEqual(dict.fromkeys.__qualname__, 'dict.fromkeys')
425 self.assertEqual(float.__getformat__.__qualname__,
426 'float.__getformat__')
427
428 # builtin staticmethod:
429 self.assertEqual(str.maketrans.__qualname__, 'str.maketrans')
430 self.assertEqual(bytes.maketrans.__qualname__, 'bytes.maketrans')
431
432 # builtin bound instance method:
433 self.assertEqual([1, 2, 3].append.__qualname__, 'list.append')
434 self.assertEqual({'foo': 'bar'}.pop.__qualname__, 'dict.pop')
435
436
Georg Brandl60d14562008-02-05 18:31:41 +0000437if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500438 unittest.main()