Updated for 2.1a3
[python/dscho.git] / Lib / test / test_funcattrs.py
blob34895338adce4ebdce8fcdb013d194d2c2cb5ed9
1 from test_support import verbose, TestFailed
3 class F:
4 def a(self):
5 pass
7 def b():
8 'my docstring'
9 pass
11 # setting attributes on functions
12 try:
13 b.publish
14 except AttributeError:
15 pass
16 else:
17 raise TestFailed, 'expected AttributeError'
19 if b.__dict__ <> None:
20 raise TestFailed, 'expected unassigned func.__dict__ to be None'
22 b.publish = 1
23 if b.publish <> 1:
24 raise TestFailed, 'function attribute not set to expected value'
26 docstring = 'its docstring'
27 b.__doc__ = 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()'
34 del b.__dict__
35 if b.__dict__ <> None:
36 raise TestFailed, 'del func.__dict__ did not result in __dict__ == None'
38 b.publish = 1
39 b.__dict__ = None
40 if b.__dict__ <> None:
41 raise TestFailed, 'func.__dict__ = None did not result in __dict__ == None'
44 f1 = F()
45 f2 = F()
47 try:
48 F.a.publish
49 except AttributeError:
50 pass
51 else:
52 raise TestFailed, 'expected AttributeError'
54 try:
55 f1.a.publish
56 except AttributeError:
57 pass
58 else:
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.
63 try:
64 F.a.publish = 1
65 except TypeError:
66 pass
67 else:
68 raise TestFailed, 'expected TypeError'
70 # But setting it explicitly on the underlying function object is okay.
71 F.a.im_func.publish = 1
73 if F.a.publish <> 1:
74 raise TestFailed, 'unbound method attribute not set to expected value'
76 if f1.a.publish <> 1:
77 raise TestFailed, 'bound method attribute access did not work'
79 if f2.a.publish <> 1:
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()'
85 try:
86 f1.a.publish = 0
87 except TypeError:
88 pass
89 else:
90 raise TestFailed, 'expected TypeError'
92 # See the comment above about the change in semantics for Python 2.1b1
93 try:
94 F.a.myclass = F
95 except TypeError:
96 pass
97 else:
98 raise TestFailed, 'expected TypeError'
100 F.a.im_func.myclass = F
102 f1.a.myclass
103 f2.a.myclass
104 f1.a.myclass
105 F.a.myclass
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__
112 try:
113 F.a.__dict__ = (1, 2, 3)
114 except TypeError:
115 pass
116 else:
117 raise TestFailed, 'expected TypeError'
119 F.a.im_func.__dict__ = {'one': 11, 'two': 22, 'three': 33}
121 if f1.a.two <> 22:
122 raise TestFailed, 'setting __dict__'
124 from UserDict import UserDict
125 d = UserDict({'four': 44, 'five': 55})
127 try:
128 F.a.__dict__ = d
129 except TypeError:
130 pass
131 else:
132 raise TestFailed
134 if f2.a.one <> f1.a.one <> F.a.one <> 11:
135 raise TestFailed
137 # im_func may not be a Python method!
138 import new
139 F.id = new.instancemethod(id, None, F)
141 eff = F()
142 if eff.id() <> id(eff):
143 raise TestFailed
145 try:
146 F.id.foo
147 except AttributeError: pass
148 else: raise TestFailed
150 try:
151 F.id.foo = 12
152 except TypeError: pass
153 else: raise TestFailed
155 try:
156 F.id.foo
157 except AttributeError: pass
158 else: raise TestFailed
160 try:
161 eff.id.foo
162 except AttributeError: pass
163 else: raise TestFailed
165 try:
166 eff.id.foo = 12
167 except TypeError: pass
168 else: raise TestFailed
170 try:
171 eff.id.foo
172 except AttributeError: pass
173 else: raise TestFailed
175 # Regression test for a crash in pre-2.1a1
176 def another():
177 pass
178 del another.__dict__
179 del another.func_dict
180 another.func_dict = None
182 try:
183 del another.bar
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
191 def foo():
192 pass
194 def bar():
195 pass
197 def temp():
198 print 1
200 if foo==bar: raise TestFailed
202 d={}
203 d[foo] = 1
205 foo.func_code = temp.func_code
207 d[foo]