Barry Warsaw | 408b6d3 | 2002-07-30 23:27:12 +0000 | [diff] [blame] | 1 | from test.test_support import verbose, TestFailed, verify |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 2 | import types |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 3 | |
| 4 | class F: |
| 5 | def a(self): |
| 6 | pass |
| 7 | |
| 8 | def b(): |
| 9 | 'my docstring' |
| 10 | pass |
| 11 | |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 12 | # __module__ is a special attribute |
| 13 | verify(b.__module__ == __name__) |
| 14 | verify(verify.__module__ == "test.test_support") |
| 15 | |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 16 | # setting attributes on functions |
| 17 | try: |
| 18 | b.publish |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 19 | except AttributeError: pass |
| 20 | else: raise TestFailed, 'expected AttributeError' |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 21 | |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 22 | if b.__dict__ <> {}: |
| 23 | raise TestFailed, 'expected unassigned func.__dict__ to be {}' |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 24 | |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 25 | b.publish = 1 |
| 26 | if b.publish <> 1: |
| 27 | raise TestFailed, 'function attribute not set to expected value' |
| 28 | |
| 29 | docstring = 'its docstring' |
| 30 | b.__doc__ = docstring |
| 31 | if b.__doc__ <> docstring: |
| 32 | raise TestFailed, 'problem with setting __doc__ attribute' |
| 33 | |
| 34 | if 'publish' not in dir(b): |
| 35 | raise TestFailed, 'attribute not in dir()' |
| 36 | |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 37 | try: |
| 38 | del b.__dict__ |
Guido van Rossum | db2a902 | 2001-09-18 03:55:22 +0000 | [diff] [blame] | 39 | except TypeError: pass |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 40 | else: raise TestFailed, 'del func.__dict__ expected TypeError' |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 41 | |
| 42 | b.publish = 1 |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 43 | try: |
| 44 | b.__dict__ = None |
Guido van Rossum | db2a902 | 2001-09-18 03:55:22 +0000 | [diff] [blame] | 45 | except TypeError: pass |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 46 | else: raise TestFailed, 'func.__dict__ = None expected TypeError' |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 47 | |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 48 | d = {'hello': 'world'} |
| 49 | b.__dict__ = d |
| 50 | if b.func_dict is not d: |
| 51 | raise TestFailed, 'func.__dict__ assignment to dictionary failed' |
| 52 | if b.hello <> 'world': |
| 53 | raise TestFailed, 'attribute after func.__dict__ assignment failed' |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 54 | |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 55 | f1 = F() |
| 56 | f2 = F() |
| 57 | |
| 58 | try: |
| 59 | F.a.publish |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 60 | except AttributeError: pass |
| 61 | else: raise TestFailed, 'expected AttributeError' |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 62 | |
| 63 | try: |
| 64 | f1.a.publish |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 65 | except AttributeError: pass |
| 66 | else: raise TestFailed, 'expected AttributeError' |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 67 | |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 68 | # In Python 2.1 beta 1, we disallowed setting attributes on unbound methods |
| 69 | # (it was already disallowed on bound methods). See the PEP for details. |
| 70 | try: |
| 71 | F.a.publish = 1 |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 72 | except (AttributeError, TypeError): pass |
| 73 | else: raise TestFailed, 'expected AttributeError or TypeError' |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 74 | |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 75 | # But setting it explicitly on the underlying function object is okay. |
| 76 | F.a.im_func.publish = 1 |
| 77 | |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 78 | if F.a.publish <> 1: |
| 79 | raise TestFailed, 'unbound method attribute not set to expected value' |
| 80 | |
| 81 | if f1.a.publish <> 1: |
| 82 | raise TestFailed, 'bound method attribute access did not work' |
| 83 | |
| 84 | if f2.a.publish <> 1: |
| 85 | raise TestFailed, 'bound method attribute access did not work' |
| 86 | |
| 87 | if 'publish' not in dir(F.a): |
| 88 | raise TestFailed, 'attribute not in dir()' |
| 89 | |
| 90 | try: |
| 91 | f1.a.publish = 0 |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 92 | except (AttributeError, TypeError): pass |
| 93 | else: raise TestFailed, 'expected AttributeError or TypeError' |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 94 | |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 95 | # See the comment above about the change in semantics for Python 2.1b1 |
| 96 | try: |
| 97 | F.a.myclass = F |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 98 | except (AttributeError, TypeError): pass |
| 99 | else: raise TestFailed, 'expected AttributeError or TypeError' |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 100 | |
| 101 | F.a.im_func.myclass = F |
| 102 | |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 103 | f1.a.myclass |
| 104 | f2.a.myclass |
| 105 | f1.a.myclass |
| 106 | F.a.myclass |
| 107 | |
| 108 | if f1.a.myclass is not f2.a.myclass or \ |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 109 | f1.a.myclass is not F.a.myclass: |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 110 | raise TestFailed, 'attributes were not the same' |
| 111 | |
| 112 | # try setting __dict__ |
| 113 | try: |
| 114 | F.a.__dict__ = (1, 2, 3) |
Guido van Rossum | 56ff387 | 2001-10-22 02:00:09 +0000 | [diff] [blame] | 115 | except (AttributeError, TypeError): pass |
| 116 | else: raise TestFailed, 'expected TypeError or AttributeError' |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 117 | |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 118 | F.a.im_func.__dict__ = {'one': 11, 'two': 22, 'three': 33} |
| 119 | |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 120 | if f1.a.two <> 22: |
| 121 | raise TestFailed, 'setting __dict__' |
| 122 | |
| 123 | from UserDict import UserDict |
| 124 | d = UserDict({'four': 44, 'five': 55}) |
| 125 | |
| 126 | try: |
| 127 | F.a.__dict__ = d |
Guido van Rossum | 56ff387 | 2001-10-22 02:00:09 +0000 | [diff] [blame] | 128 | except (AttributeError, TypeError): pass |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 129 | else: raise TestFailed |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 130 | |
| 131 | if f2.a.one <> f1.a.one <> F.a.one <> 11: |
| 132 | raise TestFailed |
Barry Warsaw | 534c60f | 2001-01-15 21:00:02 +0000 | [diff] [blame] | 133 | |
| 134 | # im_func may not be a Python method! |
| 135 | import new |
| 136 | F.id = new.instancemethod(id, None, F) |
| 137 | |
| 138 | eff = F() |
| 139 | if eff.id() <> id(eff): |
| 140 | raise TestFailed |
| 141 | |
| 142 | try: |
| 143 | F.id.foo |
| 144 | except AttributeError: pass |
| 145 | else: raise TestFailed |
| 146 | |
| 147 | try: |
| 148 | F.id.foo = 12 |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 149 | except (AttributeError, TypeError): pass |
Barry Warsaw | 534c60f | 2001-01-15 21:00:02 +0000 | [diff] [blame] | 150 | else: raise TestFailed |
| 151 | |
| 152 | try: |
| 153 | F.id.foo |
| 154 | except AttributeError: pass |
| 155 | else: raise TestFailed |
| 156 | |
| 157 | try: |
| 158 | eff.id.foo |
| 159 | except AttributeError: pass |
| 160 | else: raise TestFailed |
| 161 | |
| 162 | try: |
| 163 | eff.id.foo = 12 |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 164 | except (AttributeError, TypeError): pass |
Barry Warsaw | 534c60f | 2001-01-15 21:00:02 +0000 | [diff] [blame] | 165 | else: raise TestFailed |
| 166 | |
| 167 | try: |
| 168 | eff.id.foo |
| 169 | except AttributeError: pass |
| 170 | else: raise TestFailed |
Barry Warsaw | 2e9b396 | 2001-01-19 19:55:12 +0000 | [diff] [blame] | 171 | |
| 172 | # Regression test for a crash in pre-2.1a1 |
| 173 | def another(): |
| 174 | pass |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 175 | |
| 176 | try: |
| 177 | del another.__dict__ |
Guido van Rossum | db2a902 | 2001-09-18 03:55:22 +0000 | [diff] [blame] | 178 | except TypeError: pass |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 179 | else: raise TestFailed |
| 180 | |
| 181 | try: |
| 182 | del another.func_dict |
Guido van Rossum | db2a902 | 2001-09-18 03:55:22 +0000 | [diff] [blame] | 183 | except TypeError: pass |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 184 | else: raise TestFailed |
| 185 | |
| 186 | try: |
| 187 | another.func_dict = None |
Guido van Rossum | db2a902 | 2001-09-18 03:55:22 +0000 | [diff] [blame] | 188 | except TypeError: pass |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 189 | else: raise TestFailed |
Barry Warsaw | 2e9b396 | 2001-01-19 19:55:12 +0000 | [diff] [blame] | 190 | |
| 191 | try: |
| 192 | del another.bar |
| 193 | except AttributeError: pass |
| 194 | else: raise TestFailed |
| 195 | |
| 196 | # This isn't specifically related to function attributes, but it does test a |
| 197 | # core dump regression in funcobject.c |
| 198 | del another.func_defaults |
Moshe Zadka | 497671e | 2001-01-29 06:21:17 +0000 | [diff] [blame] | 199 | |
| 200 | def foo(): |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 201 | pass |
Moshe Zadka | 497671e | 2001-01-29 06:21:17 +0000 | [diff] [blame] | 202 | |
| 203 | def bar(): |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 204 | pass |
Moshe Zadka | 497671e | 2001-01-29 06:21:17 +0000 | [diff] [blame] | 205 | |
| 206 | def temp(): |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 207 | print 1 |
Moshe Zadka | 497671e | 2001-01-29 06:21:17 +0000 | [diff] [blame] | 208 | |
Barry Warsaw | 033daa4 | 2001-08-14 18:28:28 +0000 | [diff] [blame] | 209 | if foo==bar: |
| 210 | raise TestFailed |
Moshe Zadka | 497671e | 2001-01-29 06:21:17 +0000 | [diff] [blame] | 211 | |
| 212 | d={} |
| 213 | d[foo] = 1 |
| 214 | |
| 215 | foo.func_code = temp.func_code |
| 216 | |
| 217 | d[foo] |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 218 | |
| 219 | # Test all predefined function attributes systematically |
| 220 | |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 221 | def cantset(obj, name, value): |
| 222 | verify(hasattr(obj, name)) # Otherwise it's probably a typo |
| 223 | try: |
| 224 | setattr(obj, name, value) |
Guido van Rossum | 56ff387 | 2001-10-22 02:00:09 +0000 | [diff] [blame] | 225 | except (AttributeError, TypeError): |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 226 | pass |
| 227 | else: |
| 228 | raise TestFailed, "shouldn't be able to set %s to %r" % (name, value) |
| 229 | try: |
| 230 | delattr(obj, name) |
Guido van Rossum | 56ff387 | 2001-10-22 02:00:09 +0000 | [diff] [blame] | 231 | except (AttributeError, TypeError): |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 232 | pass |
| 233 | else: |
| 234 | raise TestFailed, "shouldn't be able to del %s" % name |
| 235 | |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 236 | def test_func_closure(): |
| 237 | a = 12 |
| 238 | def f(): print a |
| 239 | c = f.func_closure |
| 240 | verify(isinstance(c, tuple)) |
| 241 | verify(len(c) == 1) |
| 242 | verify(c[0].__class__.__name__ == "cell") # don't have a type object handy |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 243 | cantset(f, "func_closure", c) |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 244 | |
| 245 | def test_func_doc(): |
| 246 | def f(): pass |
| 247 | verify(f.__doc__ is None) |
| 248 | verify(f.func_doc is None) |
| 249 | f.__doc__ = "hello" |
| 250 | verify(f.__doc__ == "hello") |
| 251 | verify(f.func_doc == "hello") |
| 252 | del f.__doc__ |
| 253 | verify(f.__doc__ is None) |
| 254 | verify(f.func_doc is None) |
| 255 | f.func_doc = "world" |
| 256 | verify(f.__doc__ == "world") |
| 257 | verify(f.func_doc == "world") |
| 258 | del f.func_doc |
| 259 | verify(f.func_doc is None) |
| 260 | verify(f.__doc__ is None) |
| 261 | |
| 262 | def test_func_globals(): |
| 263 | def f(): pass |
| 264 | verify(f.func_globals is globals()) |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 265 | cantset(f, "func_globals", globals()) |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 266 | |
| 267 | def test_func_name(): |
| 268 | def f(): pass |
| 269 | verify(f.__name__ == "f") |
| 270 | verify(f.func_name == "f") |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 271 | cantset(f, "func_name", "f") |
| 272 | cantset(f, "__name__", "f") |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 273 | |
| 274 | def test_func_code(): |
| 275 | def f(): pass |
| 276 | def g(): print 12 |
| 277 | verify(type(f.func_code) is types.CodeType) |
| 278 | f.func_code = g.func_code |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 279 | cantset(f, "func_code", None) |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 280 | |
| 281 | def test_func_defaults(): |
| 282 | def f(a, b): return (a, b) |
| 283 | verify(f.func_defaults is None) |
| 284 | f.func_defaults = (1, 2) |
| 285 | verify(f.func_defaults == (1, 2)) |
| 286 | verify(f(10) == (10, 2)) |
| 287 | def g(a=1, b=2): return (a, b) |
| 288 | verify(g.func_defaults == (1, 2)) |
| 289 | del g.func_defaults |
| 290 | verify(g.func_defaults is None) |
| 291 | try: |
| 292 | g() |
| 293 | except TypeError: |
| 294 | pass |
| 295 | else: |
| 296 | raise TestFailed, "shouldn't be allowed to call g() w/o defaults" |
| 297 | |
| 298 | def test_func_dict(): |
| 299 | def f(): pass |
| 300 | a = f.__dict__ |
| 301 | b = f.func_dict |
| 302 | verify(a == {}) |
| 303 | verify(a is b) |
| 304 | f.hello = 'world' |
| 305 | verify(a == {'hello': 'world'}) |
| 306 | verify(f.func_dict is a is f.__dict__) |
| 307 | f.func_dict = {} |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 308 | verify(not hasattr(f, "hello")) |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 309 | f.__dict__ = {'world': 'hello'} |
| 310 | verify(f.world == "hello") |
| 311 | verify(f.__dict__ is f.func_dict == {'world': 'hello'}) |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 312 | cantset(f, "func_dict", None) |
| 313 | cantset(f, "__dict__", None) |
| 314 | |
| 315 | def test_im_class(): |
| 316 | class C: |
| 317 | def foo(self): pass |
| 318 | verify(C.foo.im_class is C) |
| 319 | verify(C().foo.im_class is C) |
| 320 | cantset(C.foo, "im_class", C) |
| 321 | cantset(C().foo, "im_class", C) |
| 322 | |
| 323 | def test_im_func(): |
| 324 | def foo(self): pass |
| 325 | class C: |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 326 | pass |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 327 | C.foo = foo |
| 328 | verify(C.foo.im_func is foo) |
| 329 | verify(C().foo.im_func is foo) |
| 330 | cantset(C.foo, "im_func", foo) |
| 331 | cantset(C().foo, "im_func", foo) |
| 332 | |
| 333 | def test_im_self(): |
| 334 | class C: |
| 335 | def foo(self): pass |
| 336 | verify(C.foo.im_self is None) |
| 337 | c = C() |
| 338 | verify(c.foo.im_self is c) |
| 339 | cantset(C.foo, "im_self", None) |
| 340 | cantset(c.foo, "im_self", c) |
| 341 | |
| 342 | def test_im_dict(): |
| 343 | class C: |
| 344 | def foo(self): pass |
| 345 | foo.bar = 42 |
| 346 | verify(C.foo.__dict__ == {'bar': 42}) |
| 347 | verify(C().foo.__dict__ == {'bar': 42}) |
| 348 | cantset(C.foo, "__dict__", C.foo.__dict__) |
| 349 | cantset(C().foo, "__dict__", C.foo.__dict__) |
| 350 | |
| 351 | def test_im_doc(): |
| 352 | class C: |
| 353 | def foo(self): "hello" |
| 354 | verify(C.foo.__doc__ == "hello") |
| 355 | verify(C().foo.__doc__ == "hello") |
| 356 | cantset(C.foo, "__doc__", "hello") |
| 357 | cantset(C().foo, "__doc__", "hello") |
| 358 | |
| 359 | def test_im_name(): |
| 360 | class C: |
| 361 | def foo(self): pass |
| 362 | verify(C.foo.__name__ == "foo") |
| 363 | verify(C().foo.__name__ == "foo") |
| 364 | cantset(C.foo, "__name__", "foo") |
| 365 | cantset(C().foo, "__name__", "foo") |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 366 | |
| 367 | def testmore(): |
| 368 | test_func_closure() |
| 369 | test_func_doc() |
| 370 | test_func_globals() |
| 371 | test_func_name() |
| 372 | test_func_code() |
| 373 | test_func_defaults() |
| 374 | test_func_dict() |
Guido van Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 375 | # Tests for instance method attributes |
| 376 | test_im_class() |
| 377 | test_im_func() |
| 378 | test_im_self() |
| 379 | test_im_dict() |
| 380 | test_im_doc() |
| 381 | test_im_name() |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 382 | |
| 383 | testmore() |