blob: e41df9c70a2464f78c3eddb3847a6832f7d21f57 [file] [log] [blame]
Barry Warsaw4a420a02001-01-15 20:30:15 +00001from test_support import verbose, TestFailed
2
3class F:
4 def a(self):
5 pass
6
7def b():
8 'my docstring'
9 pass
10
11# setting attributes on functions
12try:
13 b.publish
14except AttributeError:
15 pass
16else:
17 raise TestFailed, 'expected AttributeError'
18
19b.publish = 1
20if b.publish <> 1:
21 raise TestFailed, 'function attribute not set to expected value'
22
23docstring = 'its docstring'
24b.__doc__ = docstring
25if b.__doc__ <> docstring:
26 raise TestFailed, 'problem with setting __doc__ attribute'
27
28if 'publish' not in dir(b):
29 raise TestFailed, 'attribute not in dir()'
30
31f1 = F()
32f2 = F()
33
34try:
35 F.a.publish
36except AttributeError:
37 pass
38else:
39 raise TestFailed, 'expected AttributeError'
40
41try:
42 f1.a.publish
43except AttributeError:
44 pass
45else:
46 raise TestFailed, 'expected AttributeError'
47
48
49F.a.publish = 1
50if F.a.publish <> 1:
51 raise TestFailed, 'unbound method attribute not set to expected value'
52
53if f1.a.publish <> 1:
54 raise TestFailed, 'bound method attribute access did not work'
55
56if f2.a.publish <> 1:
57 raise TestFailed, 'bound method attribute access did not work'
58
59if 'publish' not in dir(F.a):
60 raise TestFailed, 'attribute not in dir()'
61
62try:
63 f1.a.publish = 0
64except TypeError:
65 pass
66else:
67 raise TestFailed, 'expected TypeError'
68
69F.a.myclass = F
70f1.a.myclass
71f2.a.myclass
72f1.a.myclass
73F.a.myclass
74
75if f1.a.myclass is not f2.a.myclass or \
76 f1.a.myclass is not F.a.myclass:
77 raise TestFailed, 'attributes were not the same'
78
79# try setting __dict__
80try:
81 F.a.__dict__ = (1, 2, 3)
82except TypeError:
83 pass
84else:
85 raise TestFailed, 'expected TypeError'
86
87F.a.__dict__ = {'one': 11, 'two': 22, 'three': 33}
88if f1.a.two <> 22:
89 raise TestFailed, 'setting __dict__'
90
91from UserDict import UserDict
92d = UserDict({'four': 44, 'five': 55})
93
94try:
95 F.a.__dict__ = d
96except TypeError:
97 pass
98else:
99 raise TestFailed
100
101if f2.a.one <> f1.a.one <> F.a.one <> 11:
102 raise TestFailed
Barry Warsaw534c60f2001-01-15 21:00:02 +0000103
104# im_func may not be a Python method!
105import new
106F.id = new.instancemethod(id, None, F)
107
108eff = F()
109if eff.id() <> id(eff):
110 raise TestFailed
111
112try:
113 F.id.foo
114except AttributeError: pass
115else: raise TestFailed
116
117try:
118 F.id.foo = 12
119except TypeError: pass
120else: raise TestFailed
121
122try:
123 F.id.foo
124except AttributeError: pass
125else: raise TestFailed
126
127try:
128 eff.id.foo
129except AttributeError: pass
130else: raise TestFailed
131
132try:
133 eff.id.foo = 12
134except TypeError: pass
135else: raise TestFailed
136
137try:
138 eff.id.foo
139except AttributeError: pass
140else: raise TestFailed
Barry Warsaw2e9b3962001-01-19 19:55:12 +0000141
142# Regression test for a crash in pre-2.1a1
143def another():
144 pass
145del another.__dict__
146del another.func_dict
147another.func_dict = None
148
149try:
150 del another.bar
151except AttributeError: pass
152else: raise TestFailed
153
154# This isn't specifically related to function attributes, but it does test a
155# core dump regression in funcobject.c
156del another.func_defaults
Moshe Zadka497671e2001-01-29 06:21:17 +0000157
158def foo():
Tim Peters10fb3862001-02-09 20:17:14 +0000159 pass
Moshe Zadka497671e2001-01-29 06:21:17 +0000160
161def bar():
Tim Peters10fb3862001-02-09 20:17:14 +0000162 pass
Moshe Zadka497671e2001-01-29 06:21:17 +0000163
164def temp():
Tim Peters10fb3862001-02-09 20:17:14 +0000165 print 1
Moshe Zadka497671e2001-01-29 06:21:17 +0000166
167if foo==bar: raise TestFailed
168
169d={}
170d[foo] = 1
171
172foo.func_code = temp.func_code
173
174d[foo]