1 | n/a | import types |
---|
2 | n/a | import unittest |
---|
3 | n/a | |
---|
4 | n/a | |
---|
5 | n/a | def global_function(): |
---|
6 | n/a | def inner_function(): |
---|
7 | n/a | class LocalClass: |
---|
8 | n/a | pass |
---|
9 | n/a | global inner_global_function |
---|
10 | n/a | def inner_global_function(): |
---|
11 | n/a | def inner_function2(): |
---|
12 | n/a | pass |
---|
13 | n/a | return inner_function2 |
---|
14 | n/a | return LocalClass |
---|
15 | n/a | return lambda: inner_function |
---|
16 | n/a | |
---|
17 | n/a | |
---|
18 | n/a | class FuncAttrsTest(unittest.TestCase): |
---|
19 | n/a | def setUp(self): |
---|
20 | n/a | class F: |
---|
21 | n/a | def a(self): |
---|
22 | n/a | pass |
---|
23 | n/a | def b(): |
---|
24 | n/a | return 3 |
---|
25 | n/a | self.fi = F() |
---|
26 | n/a | self.F = F |
---|
27 | n/a | self.b = b |
---|
28 | n/a | |
---|
29 | n/a | def cannot_set_attr(self, obj, name, value, exceptions): |
---|
30 | n/a | try: |
---|
31 | n/a | setattr(obj, name, value) |
---|
32 | n/a | except exceptions: |
---|
33 | n/a | pass |
---|
34 | n/a | else: |
---|
35 | n/a | self.fail("shouldn't be able to set %s to %r" % (name, value)) |
---|
36 | n/a | try: |
---|
37 | n/a | delattr(obj, name) |
---|
38 | n/a | except exceptions: |
---|
39 | n/a | pass |
---|
40 | n/a | else: |
---|
41 | n/a | self.fail("shouldn't be able to del %s" % name) |
---|
42 | n/a | |
---|
43 | n/a | |
---|
44 | n/a | class FunctionPropertiesTest(FuncAttrsTest): |
---|
45 | n/a | # Include the external setUp method that is common to all tests |
---|
46 | n/a | def test_module(self): |
---|
47 | n/a | self.assertEqual(self.b.__module__, __name__) |
---|
48 | n/a | |
---|
49 | n/a | def test_dir_includes_correct_attrs(self): |
---|
50 | n/a | self.b.known_attr = 7 |
---|
51 | n/a | self.assertIn('known_attr', dir(self.b), |
---|
52 | n/a | "set attributes not in dir listing of method") |
---|
53 | n/a | # Test on underlying function object of method |
---|
54 | n/a | self.F.a.known_attr = 7 |
---|
55 | n/a | self.assertIn('known_attr', dir(self.fi.a), "set attribute on function " |
---|
56 | n/a | "implementations, should show up in next dir") |
---|
57 | n/a | |
---|
58 | n/a | def test_duplicate_function_equality(self): |
---|
59 | n/a | # Body of `duplicate' is the exact same as self.b |
---|
60 | n/a | def duplicate(): |
---|
61 | n/a | 'my docstring' |
---|
62 | n/a | return 3 |
---|
63 | n/a | self.assertNotEqual(self.b, duplicate) |
---|
64 | n/a | |
---|
65 | n/a | def test_copying___code__(self): |
---|
66 | n/a | def test(): pass |
---|
67 | n/a | self.assertEqual(test(), None) |
---|
68 | n/a | test.__code__ = self.b.__code__ |
---|
69 | n/a | self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily |
---|
70 | n/a | |
---|
71 | n/a | def test___globals__(self): |
---|
72 | n/a | self.assertIs(self.b.__globals__, globals()) |
---|
73 | n/a | self.cannot_set_attr(self.b, '__globals__', 2, |
---|
74 | n/a | (AttributeError, TypeError)) |
---|
75 | n/a | |
---|
76 | n/a | def test___closure__(self): |
---|
77 | n/a | a = 12 |
---|
78 | n/a | def f(): print(a) |
---|
79 | n/a | c = f.__closure__ |
---|
80 | n/a | self.assertIsInstance(c, tuple) |
---|
81 | n/a | self.assertEqual(len(c), 1) |
---|
82 | n/a | # don't have a type object handy |
---|
83 | n/a | self.assertEqual(c[0].__class__.__name__, "cell") |
---|
84 | n/a | self.cannot_set_attr(f, "__closure__", c, AttributeError) |
---|
85 | n/a | |
---|
86 | n/a | def test_empty_cell(self): |
---|
87 | n/a | def f(): print(a) |
---|
88 | n/a | try: |
---|
89 | n/a | f.__closure__[0].cell_contents |
---|
90 | n/a | except ValueError: |
---|
91 | n/a | pass |
---|
92 | n/a | else: |
---|
93 | n/a | self.fail("shouldn't be able to read an empty cell") |
---|
94 | n/a | a = 12 |
---|
95 | n/a | |
---|
96 | n/a | def test___name__(self): |
---|
97 | n/a | self.assertEqual(self.b.__name__, 'b') |
---|
98 | n/a | self.b.__name__ = 'c' |
---|
99 | n/a | self.assertEqual(self.b.__name__, 'c') |
---|
100 | n/a | self.b.__name__ = 'd' |
---|
101 | n/a | self.assertEqual(self.b.__name__, 'd') |
---|
102 | n/a | # __name__ and __name__ must be a string |
---|
103 | n/a | self.cannot_set_attr(self.b, '__name__', 7, TypeError) |
---|
104 | n/a | # __name__ must be available when in restricted mode. Exec will raise |
---|
105 | n/a | # AttributeError if __name__ is not available on f. |
---|
106 | n/a | s = """def f(): pass\nf.__name__""" |
---|
107 | n/a | exec(s, {'__builtins__': {}}) |
---|
108 | n/a | # Test on methods, too |
---|
109 | n/a | self.assertEqual(self.fi.a.__name__, 'a') |
---|
110 | n/a | self.cannot_set_attr(self.fi.a, "__name__", 'a', AttributeError) |
---|
111 | n/a | |
---|
112 | n/a | def test___qualname__(self): |
---|
113 | n/a | # PEP 3155 |
---|
114 | n/a | self.assertEqual(self.b.__qualname__, 'FuncAttrsTest.setUp.<locals>.b') |
---|
115 | n/a | self.assertEqual(FuncAttrsTest.setUp.__qualname__, 'FuncAttrsTest.setUp') |
---|
116 | n/a | self.assertEqual(global_function.__qualname__, 'global_function') |
---|
117 | n/a | self.assertEqual(global_function().__qualname__, |
---|
118 | n/a | 'global_function.<locals>.<lambda>') |
---|
119 | n/a | self.assertEqual(global_function()().__qualname__, |
---|
120 | n/a | 'global_function.<locals>.inner_function') |
---|
121 | n/a | self.assertEqual(global_function()()().__qualname__, |
---|
122 | n/a | 'global_function.<locals>.inner_function.<locals>.LocalClass') |
---|
123 | n/a | self.assertEqual(inner_global_function.__qualname__, 'inner_global_function') |
---|
124 | n/a | self.assertEqual(inner_global_function().__qualname__, 'inner_global_function.<locals>.inner_function2') |
---|
125 | n/a | self.b.__qualname__ = 'c' |
---|
126 | n/a | self.assertEqual(self.b.__qualname__, 'c') |
---|
127 | n/a | self.b.__qualname__ = 'd' |
---|
128 | n/a | self.assertEqual(self.b.__qualname__, 'd') |
---|
129 | n/a | # __qualname__ must be a string |
---|
130 | n/a | self.cannot_set_attr(self.b, '__qualname__', 7, TypeError) |
---|
131 | n/a | |
---|
132 | n/a | def test___code__(self): |
---|
133 | n/a | num_one, num_two = 7, 8 |
---|
134 | n/a | def a(): pass |
---|
135 | n/a | def b(): return 12 |
---|
136 | n/a | def c(): return num_one |
---|
137 | n/a | def d(): return num_two |
---|
138 | n/a | def e(): return num_one, num_two |
---|
139 | n/a | for func in [a, b, c, d, e]: |
---|
140 | n/a | self.assertEqual(type(func.__code__), types.CodeType) |
---|
141 | n/a | self.assertEqual(c(), 7) |
---|
142 | n/a | self.assertEqual(d(), 8) |
---|
143 | n/a | d.__code__ = c.__code__ |
---|
144 | n/a | self.assertEqual(c.__code__, d.__code__) |
---|
145 | n/a | self.assertEqual(c(), 7) |
---|
146 | n/a | # self.assertEqual(d(), 7) |
---|
147 | n/a | try: |
---|
148 | n/a | b.__code__ = c.__code__ |
---|
149 | n/a | except ValueError: |
---|
150 | n/a | pass |
---|
151 | n/a | else: |
---|
152 | n/a | self.fail("__code__ with different numbers of free vars should " |
---|
153 | n/a | "not be possible") |
---|
154 | n/a | try: |
---|
155 | n/a | e.__code__ = d.__code__ |
---|
156 | n/a | except ValueError: |
---|
157 | n/a | pass |
---|
158 | n/a | else: |
---|
159 | n/a | self.fail("__code__ with different numbers of free vars should " |
---|
160 | n/a | "not be possible") |
---|
161 | n/a | |
---|
162 | n/a | def test_blank_func_defaults(self): |
---|
163 | n/a | self.assertEqual(self.b.__defaults__, None) |
---|
164 | n/a | del self.b.__defaults__ |
---|
165 | n/a | self.assertEqual(self.b.__defaults__, None) |
---|
166 | n/a | |
---|
167 | n/a | def test_func_default_args(self): |
---|
168 | n/a | def first_func(a, b): |
---|
169 | n/a | return a+b |
---|
170 | n/a | def second_func(a=1, b=2): |
---|
171 | n/a | return a+b |
---|
172 | n/a | self.assertEqual(first_func.__defaults__, None) |
---|
173 | n/a | self.assertEqual(second_func.__defaults__, (1, 2)) |
---|
174 | n/a | first_func.__defaults__ = (1, 2) |
---|
175 | n/a | self.assertEqual(first_func.__defaults__, (1, 2)) |
---|
176 | n/a | self.assertEqual(first_func(), 3) |
---|
177 | n/a | self.assertEqual(first_func(3), 5) |
---|
178 | n/a | self.assertEqual(first_func(3, 5), 8) |
---|
179 | n/a | del second_func.__defaults__ |
---|
180 | n/a | self.assertEqual(second_func.__defaults__, None) |
---|
181 | n/a | try: |
---|
182 | n/a | second_func() |
---|
183 | n/a | except TypeError: |
---|
184 | n/a | pass |
---|
185 | n/a | else: |
---|
186 | n/a | self.fail("__defaults__ does not update; deleting it does not " |
---|
187 | n/a | "remove requirement") |
---|
188 | n/a | |
---|
189 | n/a | |
---|
190 | n/a | class InstancemethodAttrTest(FuncAttrsTest): |
---|
191 | n/a | |
---|
192 | n/a | def test___class__(self): |
---|
193 | n/a | self.assertEqual(self.fi.a.__self__.__class__, self.F) |
---|
194 | n/a | self.cannot_set_attr(self.fi.a, "__class__", self.F, TypeError) |
---|
195 | n/a | |
---|
196 | n/a | def test___func__(self): |
---|
197 | n/a | self.assertEqual(self.fi.a.__func__, self.F.a) |
---|
198 | n/a | self.cannot_set_attr(self.fi.a, "__func__", self.F.a, AttributeError) |
---|
199 | n/a | |
---|
200 | n/a | def test___self__(self): |
---|
201 | n/a | self.assertEqual(self.fi.a.__self__, self.fi) |
---|
202 | n/a | self.cannot_set_attr(self.fi.a, "__self__", self.fi, AttributeError) |
---|
203 | n/a | |
---|
204 | n/a | def test___func___non_method(self): |
---|
205 | n/a | # Behavior should be the same when a method is added via an attr |
---|
206 | n/a | # assignment |
---|
207 | n/a | self.fi.id = types.MethodType(id, self.fi) |
---|
208 | n/a | self.assertEqual(self.fi.id(), id(self.fi)) |
---|
209 | n/a | # Test usage |
---|
210 | n/a | try: |
---|
211 | n/a | self.fi.id.unknown_attr |
---|
212 | n/a | except AttributeError: |
---|
213 | n/a | pass |
---|
214 | n/a | else: |
---|
215 | n/a | self.fail("using unknown attributes should raise AttributeError") |
---|
216 | n/a | # Test assignment and deletion |
---|
217 | n/a | self.cannot_set_attr(self.fi.id, 'unknown_attr', 2, AttributeError) |
---|
218 | n/a | |
---|
219 | n/a | |
---|
220 | n/a | class ArbitraryFunctionAttrTest(FuncAttrsTest): |
---|
221 | n/a | def test_set_attr(self): |
---|
222 | n/a | self.b.known_attr = 7 |
---|
223 | n/a | self.assertEqual(self.b.known_attr, 7) |
---|
224 | n/a | try: |
---|
225 | n/a | self.fi.a.known_attr = 7 |
---|
226 | n/a | except AttributeError: |
---|
227 | n/a | pass |
---|
228 | n/a | else: |
---|
229 | n/a | self.fail("setting attributes on methods should raise error") |
---|
230 | n/a | |
---|
231 | n/a | def test_delete_unknown_attr(self): |
---|
232 | n/a | try: |
---|
233 | n/a | del self.b.unknown_attr |
---|
234 | n/a | except AttributeError: |
---|
235 | n/a | pass |
---|
236 | n/a | else: |
---|
237 | n/a | self.fail("deleting unknown attribute should raise TypeError") |
---|
238 | n/a | |
---|
239 | n/a | def test_unset_attr(self): |
---|
240 | n/a | for func in [self.b, self.fi.a]: |
---|
241 | n/a | try: |
---|
242 | n/a | func.non_existent_attr |
---|
243 | n/a | except AttributeError: |
---|
244 | n/a | pass |
---|
245 | n/a | else: |
---|
246 | n/a | self.fail("using unknown attributes should raise " |
---|
247 | n/a | "AttributeError") |
---|
248 | n/a | |
---|
249 | n/a | |
---|
250 | n/a | class FunctionDictsTest(FuncAttrsTest): |
---|
251 | n/a | def test_setting_dict_to_invalid(self): |
---|
252 | n/a | self.cannot_set_attr(self.b, '__dict__', None, TypeError) |
---|
253 | n/a | from collections import UserDict |
---|
254 | n/a | d = UserDict({'known_attr': 7}) |
---|
255 | n/a | self.cannot_set_attr(self.fi.a.__func__, '__dict__', d, TypeError) |
---|
256 | n/a | |
---|
257 | n/a | def test_setting_dict_to_valid(self): |
---|
258 | n/a | d = {'known_attr': 7} |
---|
259 | n/a | self.b.__dict__ = d |
---|
260 | n/a | # Test assignment |
---|
261 | n/a | self.assertIs(d, self.b.__dict__) |
---|
262 | n/a | # ... and on all the different ways of referencing the method's func |
---|
263 | n/a | self.F.a.__dict__ = d |
---|
264 | n/a | self.assertIs(d, self.fi.a.__func__.__dict__) |
---|
265 | n/a | self.assertIs(d, self.fi.a.__dict__) |
---|
266 | n/a | # Test value |
---|
267 | n/a | self.assertEqual(self.b.known_attr, 7) |
---|
268 | n/a | self.assertEqual(self.b.__dict__['known_attr'], 7) |
---|
269 | n/a | # ... and again, on all the different method's names |
---|
270 | n/a | self.assertEqual(self.fi.a.__func__.known_attr, 7) |
---|
271 | n/a | self.assertEqual(self.fi.a.known_attr, 7) |
---|
272 | n/a | |
---|
273 | n/a | def test_delete___dict__(self): |
---|
274 | n/a | try: |
---|
275 | n/a | del self.b.__dict__ |
---|
276 | n/a | except TypeError: |
---|
277 | n/a | pass |
---|
278 | n/a | else: |
---|
279 | n/a | self.fail("deleting function dictionary should raise TypeError") |
---|
280 | n/a | |
---|
281 | n/a | def test_unassigned_dict(self): |
---|
282 | n/a | self.assertEqual(self.b.__dict__, {}) |
---|
283 | n/a | |
---|
284 | n/a | def test_func_as_dict_key(self): |
---|
285 | n/a | value = "Some string" |
---|
286 | n/a | d = {} |
---|
287 | n/a | d[self.b] = value |
---|
288 | n/a | self.assertEqual(d[self.b], value) |
---|
289 | n/a | |
---|
290 | n/a | |
---|
291 | n/a | class FunctionDocstringTest(FuncAttrsTest): |
---|
292 | n/a | def test_set_docstring_attr(self): |
---|
293 | n/a | self.assertEqual(self.b.__doc__, None) |
---|
294 | n/a | docstr = "A test method that does nothing" |
---|
295 | n/a | self.b.__doc__ = docstr |
---|
296 | n/a | self.F.a.__doc__ = docstr |
---|
297 | n/a | self.assertEqual(self.b.__doc__, docstr) |
---|
298 | n/a | self.assertEqual(self.fi.a.__doc__, docstr) |
---|
299 | n/a | self.cannot_set_attr(self.fi.a, "__doc__", docstr, AttributeError) |
---|
300 | n/a | |
---|
301 | n/a | def test_delete_docstring(self): |
---|
302 | n/a | self.b.__doc__ = "The docstring" |
---|
303 | n/a | del self.b.__doc__ |
---|
304 | n/a | self.assertEqual(self.b.__doc__, None) |
---|
305 | n/a | |
---|
306 | n/a | |
---|
307 | n/a | def cell(value): |
---|
308 | n/a | """Create a cell containing the given value.""" |
---|
309 | n/a | def f(): |
---|
310 | n/a | print(a) |
---|
311 | n/a | a = value |
---|
312 | n/a | return f.__closure__[0] |
---|
313 | n/a | |
---|
314 | n/a | def empty_cell(empty=True): |
---|
315 | n/a | """Create an empty cell.""" |
---|
316 | n/a | def f(): |
---|
317 | n/a | print(a) |
---|
318 | n/a | # the intent of the following line is simply "if False:"; it's |
---|
319 | n/a | # spelt this way to avoid the danger that a future optimization |
---|
320 | n/a | # might simply remove an "if False:" code block. |
---|
321 | n/a | if not empty: |
---|
322 | n/a | a = 1729 |
---|
323 | n/a | return f.__closure__[0] |
---|
324 | n/a | |
---|
325 | n/a | |
---|
326 | n/a | class CellTest(unittest.TestCase): |
---|
327 | n/a | def test_comparison(self): |
---|
328 | n/a | # These tests are here simply to exercise the comparison code; |
---|
329 | n/a | # their presence should not be interpreted as providing any |
---|
330 | n/a | # guarantees about the semantics (or even existence) of cell |
---|
331 | n/a | # comparisons in future versions of CPython. |
---|
332 | n/a | self.assertTrue(cell(2) < cell(3)) |
---|
333 | n/a | self.assertTrue(empty_cell() < cell('saturday')) |
---|
334 | n/a | self.assertTrue(empty_cell() == empty_cell()) |
---|
335 | n/a | self.assertTrue(cell(-36) == cell(-36.0)) |
---|
336 | n/a | self.assertTrue(cell(True) > empty_cell()) |
---|
337 | n/a | |
---|
338 | n/a | |
---|
339 | n/a | class StaticMethodAttrsTest(unittest.TestCase): |
---|
340 | n/a | def test_func_attribute(self): |
---|
341 | n/a | def f(): |
---|
342 | n/a | pass |
---|
343 | n/a | |
---|
344 | n/a | c = classmethod(f) |
---|
345 | n/a | self.assertTrue(c.__func__ is f) |
---|
346 | n/a | |
---|
347 | n/a | s = staticmethod(f) |
---|
348 | n/a | self.assertTrue(s.__func__ is f) |
---|
349 | n/a | |
---|
350 | n/a | |
---|
351 | n/a | class BuiltinFunctionPropertiesTest(unittest.TestCase): |
---|
352 | n/a | # XXX Not sure where this should really go since I can't find a |
---|
353 | n/a | # test module specifically for builtin_function_or_method. |
---|
354 | n/a | |
---|
355 | n/a | def test_builtin__qualname__(self): |
---|
356 | n/a | import time |
---|
357 | n/a | |
---|
358 | n/a | # builtin function: |
---|
359 | n/a | self.assertEqual(len.__qualname__, 'len') |
---|
360 | n/a | self.assertEqual(time.time.__qualname__, 'time') |
---|
361 | n/a | |
---|
362 | n/a | # builtin classmethod: |
---|
363 | n/a | self.assertEqual(dict.fromkeys.__qualname__, 'dict.fromkeys') |
---|
364 | n/a | self.assertEqual(float.__getformat__.__qualname__, |
---|
365 | n/a | 'float.__getformat__') |
---|
366 | n/a | |
---|
367 | n/a | # builtin staticmethod: |
---|
368 | n/a | self.assertEqual(str.maketrans.__qualname__, 'str.maketrans') |
---|
369 | n/a | self.assertEqual(bytes.maketrans.__qualname__, 'bytes.maketrans') |
---|
370 | n/a | |
---|
371 | n/a | # builtin bound instance method: |
---|
372 | n/a | self.assertEqual([1, 2, 3].append.__qualname__, 'list.append') |
---|
373 | n/a | self.assertEqual({'foo': 'bar'}.pop.__qualname__, 'dict.pop') |
---|
374 | n/a | |
---|
375 | n/a | |
---|
376 | n/a | if __name__ == "__main__": |
---|
377 | n/a | unittest.main() |
---|