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