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