1 from test_support
import verbose
, TestFailed
, verify
12 # setting attributes on functions
15 except AttributeError: pass
16 else: raise TestFailed
, 'expected AttributeError'
19 raise TestFailed
, 'expected unassigned func.__dict__ to be {}'
23 raise TestFailed
, 'function attribute not set to expected value'
25 docstring
= 'its docstring'
27 if b
.__doc
__ <> docstring
:
28 raise TestFailed
, 'problem with setting __doc__ attribute'
30 if 'publish' not in dir(b
):
31 raise TestFailed
, 'attribute not in dir()'
35 except TypeError: pass
36 else: raise TestFailed
, 'del func.__dict__ expected TypeError'
41 except TypeError: pass
42 else: raise TestFailed
, 'func.__dict__ = None expected TypeError'
44 d
= {'hello': 'world'}
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'
56 except AttributeError: pass
57 else: raise TestFailed
, 'expected AttributeError'
61 except AttributeError: pass
62 else: raise TestFailed
, 'expected AttributeError'
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.
68 except (AttributeError, TypeError): pass
69 else: raise TestFailed
, 'expected AttributeError or TypeError'
71 # But setting it explicitly on the underlying function object is okay.
72 F
.a
.im_func
.publish
= 1
75 raise TestFailed
, 'unbound method attribute not set to expected value'
78 raise TestFailed
, 'bound method attribute access did not work'
81 raise TestFailed
, 'bound method attribute access did not work'
83 if 'publish' not in dir(F
.a
):
84 raise TestFailed
, 'attribute not in dir()'
88 except (AttributeError, TypeError): pass
89 else: raise TestFailed
, 'expected AttributeError or TypeError'
91 # See the comment above about the change in semantics for Python 2.1b1
94 except (AttributeError, TypeError): pass
95 else: raise TestFailed
, 'expected AttributeError or TypeError'
97 F
.a
.im_func
.myclass
= F
104 if f1
.a
.myclass
is not f2
.a
.myclass
or \
105 f1
.a
.myclass
is not F
.a
.myclass
:
106 raise TestFailed
, 'attributes were not the same'
108 # try setting __dict__
110 F
.a
.__dict
__ = (1, 2, 3)
111 except (AttributeError, TypeError): pass
112 else: raise TestFailed
, 'expected TypeError or AttributeError'
114 F
.a
.im_func
.__dict
__ = {'one': 11, 'two': 22, 'three': 33}
117 raise TestFailed
, 'setting __dict__'
119 from UserDict
import UserDict
120 d
= UserDict({'four': 44, 'five': 55})
124 except (AttributeError, TypeError): pass
125 else: raise TestFailed
127 if f2
.a
.one
<> f1
.a
.one
<> F
.a
.one
<> 11:
130 # im_func may not be a Python method!
132 F
.id = new
.instancemethod(id, None, F
)
135 if eff
.id() <> id(eff
):
140 except AttributeError: pass
141 else: raise TestFailed
145 except (AttributeError, TypeError): pass
146 else: raise TestFailed
150 except AttributeError: pass
151 else: raise TestFailed
155 except AttributeError: pass
156 else: raise TestFailed
160 except (AttributeError, TypeError): pass
161 else: raise TestFailed
165 except AttributeError: pass
166 else: raise TestFailed
168 # Regression test for a crash in pre-2.1a1
174 except TypeError: pass
175 else: raise TestFailed
178 del another
.func_dict
179 except TypeError: pass
180 else: raise TestFailed
183 another
.func_dict
= None
184 except TypeError: pass
185 else: raise TestFailed
189 except AttributeError: pass
190 else: raise TestFailed
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
211 foo
.func_code
= temp
.func_code
215 # Test all predefined function attributes systematically
217 def cantset(obj
, name
, value
):
218 verify(hasattr(obj
, name
)) # Otherwise it's probably a typo
220 setattr(obj
, name
, value
)
221 except (AttributeError, TypeError):
224 raise TestFailed
, "shouldn't be able to set %s to %r" % (name
, value
)
227 except (AttributeError, TypeError):
230 raise TestFailed
, "shouldn't be able to del %s" % name
232 def test_func_closure():
236 verify(isinstance(c
, tuple))
238 verify(c
[0].__class
__.__name
__ == "cell") # don't have a type object handy
239 cantset(f
, "func_closure", c
)
243 verify(f
.__doc
__ is None)
244 verify(f
.func_doc
is None)
246 verify(f
.__doc
__ == "hello")
247 verify(f
.func_doc
== "hello")
249 verify(f
.__doc
__ is None)
250 verify(f
.func_doc
is None)
252 verify(f
.__doc
__ == "world")
253 verify(f
.func_doc
== "world")
255 verify(f
.func_doc
is None)
256 verify(f
.__doc
__ is None)
258 def test_func_globals():
260 verify(f
.func_globals
is globals())
261 cantset(f
, "func_globals", globals())
263 def test_func_name():
265 verify(f
.__name
__ == "f")
266 verify(f
.func_name
== "f")
267 cantset(f
, "func_name", "f")
268 cantset(f
, "__name__", "f")
270 def test_func_code():
273 verify(type(f
.func_code
) is types
.CodeType
)
274 f
.func_code
= g
.func_code
275 cantset(f
, "func_code", None)
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))
286 verify(g
.func_defaults
is None)
292 raise TestFailed
, "shouldn't be allowed to call g() w/o defaults"
294 def test_func_dict():
301 verify(a
== {'hello': 'world'})
302 verify(f
.func_dict
is a
is f
.__dict
__)
304 verify(not hasattr(f
, "hello"))
305 f
.__dict
__ = {'world': 'hello'}
306 verify(f
.world
== "hello")
307 verify(f
.__dict
__ is f
.func_dict
== {'world': 'hello'})
308 cantset(f
, "func_dict", None)
309 cantset(f
, "__dict__", None)
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
)
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
)
332 verify(C
.foo
.im_self
is None)
334 verify(c
.foo
.im_self
is c
)
335 cantset(C
.foo
, "im_self", None)
336 cantset(c
.foo
, "im_self", c
)
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
__)
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")
358 verify(C
.foo
.__name
__ == "foo")
359 verify(C().foo
.__name
__ == "foo")
360 cantset(C
.foo
, "__name__", "foo")
361 cantset(C().foo
, "__name__", "foo")
371 # Tests for instance method attributes