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