1 from test
.test_support
import verbose
, TestFailed
, verify
12 # __module__ is a special attribute
13 verify(b
.__module
__ == __name__
)
14 verify(verify
.__module
__ == "test.test_support")
16 # setting attributes on functions
19 except AttributeError: pass
20 else: raise TestFailed
, 'expected AttributeError'
23 raise TestFailed
, 'expected unassigned func.__dict__ to be {}'
27 raise TestFailed
, 'function attribute not set to expected value'
29 docstring
= 'its docstring'
31 if b
.__doc
__ <> docstring
:
32 raise TestFailed
, 'problem with setting __doc__ attribute'
34 if 'publish' not in dir(b
):
35 raise TestFailed
, 'attribute not in dir()'
39 except TypeError: pass
40 else: raise TestFailed
, 'del func.__dict__ expected TypeError'
45 except TypeError: pass
46 else: raise TestFailed
, 'func.__dict__ = None expected TypeError'
48 d
= {'hello': 'world'}
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'
60 except AttributeError: pass
61 else: raise TestFailed
, 'expected AttributeError'
65 except AttributeError: pass
66 else: raise TestFailed
, 'expected AttributeError'
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.
72 except (AttributeError, TypeError): pass
73 else: raise TestFailed
, 'expected AttributeError or TypeError'
75 # But setting it explicitly on the underlying function object is okay.
76 F
.a
.im_func
.publish
= 1
79 raise TestFailed
, 'unbound method attribute not set to expected value'
82 raise TestFailed
, 'bound method attribute access did not work'
85 raise TestFailed
, 'bound method attribute access did not work'
87 if 'publish' not in dir(F
.a
):
88 raise TestFailed
, 'attribute not in dir()'
92 except (AttributeError, TypeError): pass
93 else: raise TestFailed
, 'expected AttributeError or TypeError'
95 # See the comment above about the change in semantics for Python 2.1b1
98 except (AttributeError, TypeError): pass
99 else: raise TestFailed
, 'expected AttributeError or TypeError'
101 F
.a
.im_func
.myclass
= F
108 if f1
.a
.myclass
is not f2
.a
.myclass
or \
109 f1
.a
.myclass
is not F
.a
.myclass
:
110 raise TestFailed
, 'attributes were not the same'
112 # try setting __dict__
114 F
.a
.__dict
__ = (1, 2, 3)
115 except (AttributeError, TypeError): pass
116 else: raise TestFailed
, 'expected TypeError or AttributeError'
118 F
.a
.im_func
.__dict
__ = {'one': 11, 'two': 22, 'three': 33}
121 raise TestFailed
, 'setting __dict__'
123 from UserDict
import UserDict
124 d
= UserDict({'four': 44, 'five': 55})
128 except (AttributeError, TypeError): pass
129 else: raise TestFailed
131 if f2
.a
.one
<> f1
.a
.one
<> F
.a
.one
<> 11:
134 # im_func may not be a Python method!
136 F
.id = new
.instancemethod(id, None, F
)
139 if eff
.id() <> id(eff
):
144 except AttributeError: pass
145 else: raise TestFailed
149 except (AttributeError, TypeError): pass
150 else: raise TestFailed
154 except AttributeError: pass
155 else: raise TestFailed
159 except AttributeError: pass
160 else: raise TestFailed
164 except (AttributeError, TypeError): pass
165 else: raise TestFailed
169 except AttributeError: pass
170 else: raise TestFailed
172 # Regression test for a crash in pre-2.1a1
178 except TypeError: pass
179 else: raise TestFailed
182 del another
.func_dict
183 except TypeError: pass
184 else: raise TestFailed
187 another
.func_dict
= None
188 except TypeError: pass
189 else: raise TestFailed
193 except AttributeError: pass
194 else: raise TestFailed
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
215 foo
.func_code
= temp
.func_code
219 # Test all predefined function attributes systematically
221 def cantset(obj
, name
, value
):
222 verify(hasattr(obj
, name
)) # Otherwise it's probably a typo
224 setattr(obj
, name
, value
)
225 except (AttributeError, TypeError):
228 raise TestFailed
, "shouldn't be able to set %s to %r" % (name
, value
)
231 except (AttributeError, TypeError):
234 raise TestFailed
, "shouldn't be able to del %s" % name
236 def test_func_closure():
240 verify(isinstance(c
, tuple))
242 verify(c
[0].__class
__.__name
__ == "cell") # don't have a type object handy
243 cantset(f
, "func_closure", c
)
247 verify(f
.__doc
__ is None)
248 verify(f
.func_doc
is None)
250 verify(f
.__doc
__ == "hello")
251 verify(f
.func_doc
== "hello")
253 verify(f
.__doc
__ is None)
254 verify(f
.func_doc
is None)
256 verify(f
.__doc
__ == "world")
257 verify(f
.func_doc
== "world")
259 verify(f
.func_doc
is None)
260 verify(f
.__doc
__ is None)
262 def test_func_globals():
264 verify(f
.func_globals
is globals())
265 cantset(f
, "func_globals", globals())
267 def test_func_name():
269 verify(f
.__name
__ == "f")
270 verify(f
.func_name
== "f")
271 cantset(f
, "func_name", "f")
272 cantset(f
, "__name__", "f")
274 def test_func_code():
277 verify(type(f
.func_code
) is types
.CodeType
)
278 f
.func_code
= g
.func_code
279 cantset(f
, "func_code", None)
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))
290 verify(g
.func_defaults
is None)
296 raise TestFailed
, "shouldn't be allowed to call g() w/o defaults"
298 def test_func_dict():
305 verify(a
== {'hello': 'world'})
306 verify(f
.func_dict
is a
is f
.__dict
__)
308 verify(not hasattr(f
, "hello"))
309 f
.__dict
__ = {'world': 'hello'}
310 verify(f
.world
== "hello")
311 verify(f
.__dict
__ is f
.func_dict
== {'world': 'hello'})
312 cantset(f
, "func_dict", None)
313 cantset(f
, "__dict__", None)
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
)
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
)
336 verify(C
.foo
.im_self
is None)
338 verify(c
.foo
.im_self
is c
)
339 cantset(C
.foo
, "im_self", None)
340 cantset(c
.foo
, "im_self", c
)
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
__)
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")
362 verify(C
.foo
.__name
__ == "foo")
363 verify(C().foo
.__name
__ == "foo")
364 cantset(C
.foo
, "__name__", "foo")
365 cantset(C().foo
, "__name__", "foo")
375 # Tests for instance method attributes