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