Debian Codegraph @

pypy/objspace/std/test/apptest_userobject.py

raw symbols SCIP
1 import sys
2 import gc
3 from _io import StringIO
4
5 def test_emptyclass():
6 class empty(object): pass
7 inst = empty()
8 assert isinstance(inst, empty)
9 inst.attr = 23
10 assert inst.attr == 23
11
12 def test_method():
13 class A(object):
14 def f(self, v):
15 return v*42
16 a = A()
17 assert a.f('?') == '??????????????????????????????????????????'
18
19 def test_unboundmethod():
20 class A(object):
21 def f(self, v):
22 return v*17
23 a = A()
24 assert A.f(a, '!') == '!!!!!!!!!!!!!!!!!'
25
26 def test_subclassing():
27 for base in tuple, list, dict, str, int, float:
28 class subclass(base): pass
29 stuff = subclass()
30 assert isinstance(stuff, base)
31 assert subclass.__base__ is base
32
33 def test_subclasstuple():
34 class subclass(tuple): pass
35 stuff = subclass()
36 assert isinstance(stuff, tuple)
37 stuff.attr = 23
38 assert stuff.attr ==23
39 assert len(stuff) ==0
40 result = stuff + (1,2,3)
41 assert len(result) ==3
42
43 def test_subsubclass():
44 class base(object):
45 baseattr = 12
46 class derived(base):
47 derivedattr = 34
48 inst = derived()
49 assert isinstance(inst, base)
50 assert inst.baseattr ==12
51 assert inst.derivedattr ==34
52
53 def test_descr_get():
54 class C(object):
55 class desc(object):
56 def __get__(self, ob, cls=None):
57 return 42
58 prop = desc()
59 assert C().prop == 42
60
61 def test_descr_set():
62 class C(object):
63 class desc(object):
64 def __set__(self, ob, val):
65 ob.wibble = val
66 prop = desc()
67 c = C()
68 c.prop = 32
69 assert c.wibble == 32
70
71 def test_descr_delete():
72 class C(object):
73 class desc(object):
74 def __set__(self, ob, val):
75 oogabooga
76 def __delete__(self, ob):
77 ob.wibble = 22
78 prop = desc()
79 c = C()
80 del c.prop
81 assert c.wibble == 22
82
83 def test_class_setattr():
84 class C(object):
85 pass
86 C.a = 1
87 assert hasattr(C, 'a')
88 assert C.a == 1
89
90 def test_add():
91 class C(object):
92 def __add__(self, other):
93 return self, other
94 c1 = C()
95 assert c1+3 == (c1, 3)
96
97 def test_call():
98 class C(object):
99 def __call__(self, *args):
100 return args
101 c1 = C()
102 assert c1() == ()
103 assert c1(5) == (5,)
104 assert c1("hello", "world") == ("hello", "world")
105
106 def test_getattribute():
107 class C(object):
108 def __getattribute__(self, name):
109 return '->' + name
110 c1 = C()
111 assert c1.a == '->a'
112 c1.a = 5
113 assert c1.a == '->a'
114
115 def test_getattr():
116 class C(object):
117 def __getattr__(self, name):
118 return '->' + name
119 c1 = C()
120 assert c1.a == '->a'
121 c1.a = 5
122 assert c1.a == 5
123
124 def test_dict():
125 class A(object):
126 pass
127 class B(A):
128 pass
129 assert not '__dict__' in object.__dict__
130 assert '__dict__' in A.__dict__
131 assert not '__dict__' in B.__dict__
132 a = A()
133 a.x = 5
134 assert a.__dict__ == {'x': 5}
135 a.__dict__ = {'y': 6}
136 assert a.y == 6
137 assert not hasattr(a, 'x')
138
139 def test_del():
140 lst = []
141 class A(object):
142 def __del__(self):
143 lst.append(42)
144 A()
145 gc.collect()
146 assert lst == [42]
147
148 def test_del_exception():
149 class A(object):
150 def __del__(self):
151 raise ValueError('foo bar')
152 unraisables = []
153 def ownhook(hookargs):
154 unraisables.append(hookargs)
155 oldhook = sys.unraisablehook
156 sys.unraisablehook = ownhook
157 try:
158 A()
159 gc.collect()
160 A()
161 gc.collect()
162 A.__del__ = lambda a, b, c: None # will get "not enough arguments"
163 A()
164 gc.collect()
165 finally:
166 sys.unraisablehook = oldhook
167 def check_tb(x, traceback=True):
168 if traceback:
169 assert x.exc_traceback.tb_frame.f_code.co_name == "__del__"
170 assert repr(x.exc_value) == "ValueError('foo bar')"
171 else:
172 assert x.exc_type is TypeError
173 assert "<lambda>() missing 2 required positional arguments: 'b' and 'c'" in str(x.exc_value)
174 assert len(unraisables) == 3
175 check_tb(unraisables[0])
176 check_tb(unraisables[1])
177 check_tb(unraisables[2], traceback=False)
178
179 def test_instance_overrides_meth():
180 class C(object):
181 def m(self):
182 return "class"
183 assert C().m() == 'class'
184 c = C()
185 c.m = lambda: "instance"
186 res = c.m()
187 assert res == "instance"
188
189 def test_override_builtin_methods():
190 class myint(int):
191 def __add__(self, other):
192 return 'add'
193 def __rsub__(self, other):
194 return 'rsub'
195 assert myint(3) + 5 == 'add'
196 assert 5 + myint(3) == 8
197 assert myint(3) - 5 == -2
198 assert 5 - myint(3) == 'rsub'
199
200 def test_repr():
201 class Foo(object):
202 pass
203 Foo.__module__ = 'a.b.c'
204 Foo.__qualname__ = 'd.Foo'
205 s = repr(Foo())
206 assert s.startswith('<a.b.c.d.Foo object at ')
207
208 def test_repr_nonascii():
209 Japan = type('日本', (), dict(__module__='日本国'))
210 s = repr(Japan())
211 assert s.startswith('<日本国.日本 object at ')
212
213 def test_del_attr():
214 class Foo(object):
215 def __init__(self):
216 self.x = 3
217
218 foo = Foo()
219 del foo.x
220
221 with raises(AttributeError):
222 del foo.x
223
224 class Foo:
225 def __init__(self):
226 self.x = 3
227
228 foo = Foo()
229 del foo.x
230 with raises(AttributeError):
231 del foo.x
232
233 def test_del_attr_class():
234 class Foo:
235 pass
236
237 with raises(AttributeError):
238 del Foo.x
239
240 def test_getstate():
241 class Foo:
242 pass
243
244 class Base:
245 __slots__ = {"name": "foo"}
246 def __init__(self, name):
247 self.name = name
248
249 class Bar(Base):
250 pass
251
252 a = Foo()
253 a.abc = 42
254 state = a.__getstate__()
255 assert state == {'abc': 42}
256
257 b = Bar('world')
258 b.abc = 'hello'
259 state = b.__getstate__()
260 assert state == ({'abc': 'hello'}, {'name': 'world'})
261
262 b = Base('world')
263 state = b.__getstate__()
264 assert state == (None, {'name': 'world'})
265