1 from test_support
import verbose
, TestFailed
11 # setting attributes on functions
14 except AttributeError:
17 raise TestFailed
, 'expected AttributeError'
19 if b
.__dict
__ <> None:
20 raise TestFailed
, 'expected unassigned func.__dict__ to be None'
24 raise TestFailed
, 'function attribute not set to expected value'
26 docstring
= 'its docstring'
28 if b
.__doc
__ <> docstring
:
29 raise TestFailed
, 'problem with setting __doc__ attribute'
31 if 'publish' not in dir(b
):
32 raise TestFailed
, 'attribute not in dir()'
35 if b
.__dict
__ <> None:
36 raise TestFailed
, 'del func.__dict__ did not result in __dict__ == None'
40 if b
.__dict
__ <> None:
41 raise TestFailed
, 'func.__dict__ = None did not result in __dict__ == None'
49 except AttributeError:
52 raise TestFailed
, 'expected AttributeError'
56 except AttributeError:
59 raise TestFailed
, 'expected AttributeError'
61 # In Python 2.1 beta 1, we disallowed setting attributes on unbound methods
62 # (it was already disallowed on bound methods). See the PEP for details.
68 raise TestFailed
, 'expected TypeError'
70 # But setting it explicitly on the underlying function object is okay.
71 F
.a
.im_func
.publish
= 1
74 raise TestFailed
, 'unbound method attribute not set to expected value'
77 raise TestFailed
, 'bound method attribute access did not work'
80 raise TestFailed
, 'bound method attribute access did not work'
82 if 'publish' not in dir(F
.a
):
83 raise TestFailed
, 'attribute not in dir()'
90 raise TestFailed
, 'expected TypeError'
92 # See the comment above about the change in semantics for Python 2.1b1
98 raise TestFailed
, 'expected TypeError'
100 F
.a
.im_func
.myclass
= F
107 if f1
.a
.myclass
is not f2
.a
.myclass
or \
108 f1
.a
.myclass
is not F
.a
.myclass
:
109 raise TestFailed
, 'attributes were not the same'
111 # try setting __dict__
113 F
.a
.__dict
__ = (1, 2, 3)
117 raise TestFailed
, 'expected TypeError'
119 F
.a
.im_func
.__dict
__ = {'one': 11, 'two': 22, 'three': 33}
122 raise TestFailed
, 'setting __dict__'
124 from UserDict
import UserDict
125 d
= UserDict({'four': 44, 'five': 55})
134 if f2
.a
.one
<> f1
.a
.one
<> F
.a
.one
<> 11:
137 # im_func may not be a Python method!
139 F
.id = new
.instancemethod(id, None, F
)
142 if eff
.id() <> id(eff
):
147 except AttributeError: pass
148 else: raise TestFailed
152 except TypeError: pass
153 else: raise TestFailed
157 except AttributeError: pass
158 else: raise TestFailed
162 except AttributeError: pass
163 else: raise TestFailed
167 except TypeError: pass
168 else: raise TestFailed
172 except AttributeError: pass
173 else: raise TestFailed
175 # Regression test for a crash in pre-2.1a1
179 del another
.func_dict
180 another
.func_dict
= None
184 except AttributeError: pass
185 else: raise TestFailed
187 # This isn't specifically related to function attributes, but it does test a
188 # core dump regression in funcobject.c
189 del another
.func_defaults
200 if foo
==bar
: raise TestFailed
205 foo
.func_code
= temp
.func_code