Debian Codegraph @

pypy/objspace/std/test/test_methodcache.py

raw symbols SCIP
1 import pytest
2 from pypy.objspace.std.test import test_typeobject
3
4
5 @pytest.mark.skipif('config.option.runappdirect')
6 class AppTestMethodCaching(test_typeobject.AppTestTypeObject):
7 spaceconfig = {"objspace.std.withmethodcachecounter": True}
8
9 def setup_class(cls):
10 # This is for the following tests, which are a bit fragile and
11 # historically have been failing once in a while. With this hack,
12 # they are run up to 5 times in a row, saving the frame of the
13 # failed attempt. This means occasional collisions should work
14 # differently during the retry.
15 cls.w_retry = cls.space.appexec([], """():
16 def retry(run):
17 keepalive = []
18 for i in range(4):
19 try:
20 return run()
21 except AssertionError:
22 import sys
23 keepalive.append(sys.exc_info())
24 return run()
25 return retry
26 """)
27
28 def test_mix_classes(self):
29 @self.retry
30 def run():
31 import __pypy__
32 class A(object):
33 def f(self):
34 return 42
35 class B(object):
36 def f(self):
37 return 43
38 class C(object):
39 def f(self):
40 return 44
41 l = [A(), B(), C()] * 10
42 __pypy__.reset_method_cache_counter()
43 for i, a in enumerate(l):
44 assert a.f() == 42 + i % 3
45 cache_counter = __pypy__.method_cache_counter("f")
46 assert cache_counter[0] >= 15
47 assert cache_counter[1] >= 3 # should be (27, 3)
48 assert sum(cache_counter) == 30
49
50
51 def test_subclasses(self):
52 @self.retry
53 def run():
54 import __pypy__
55 class A(object):
56 def f(self):
57 return 42
58 class B(object):
59 def f(self):
60 return 43
61 class C(A):
62 pass
63 l = [A(), B(), C()] * 10
64 __pypy__.reset_method_cache_counter()
65 for i, a in enumerate(l):
66 assert a.f() == 42 + (i % 3 == 1)
67 cache_counter = __pypy__.method_cache_counter("f")
68 assert cache_counter[0] >= 15
69 assert cache_counter[1] >= 3 # should be (27, 3)
70 assert sum(cache_counter) == 30
71
72 @pytest.mark.flaky
73 def test_many_names(self):
74 @self.retry
75 def run():
76 import __pypy__
77 laste = None
78 for j in range(20):
79 class A(object):
80 def f(self):
81 return 42
82 class B(object):
83 def f(self):
84 return 43
85 class C(A):
86 pass
87 l = [A(), B(), C()] * 10
88 __pypy__.reset_method_cache_counter()
89 for i, a in enumerate(l):
90 assert a.f() == 42 + (i % 3 == 1)
91 cache_counter = __pypy__.method_cache_counter("f")
92 assert cache_counter[0] >= 15
93 assert cache_counter[1] >= 3 # should be (27, 3)
94 assert sum(cache_counter) == 30
95
96 a = A()
97 names = [name for name in A.__dict__.keys()
98 if not name.startswith('_')]
99 names.sort()
100 names_repeated = names * 10
101 result = []
102 __pypy__.reset_method_cache_counter()
103 for name in names_repeated:
104 result.append(getattr(a, name))
105 append_counter = __pypy__.method_cache_counter("append")
106 names_counters = [__pypy__.method_cache_counter(name)
107 for name in names]
108 try:
109 assert append_counter[0] >= 10 * len(names) - 1
110 for name, count in zip(names, names_counters):
111 assert count == (9, 1), str((name, count))
112 break
113 except AssertionError as e:
114 laste = e
115 else:
116 raise laste
117
118 def test_mutating_bases(self):
119 class C(object):
120 pass
121 class C2(object):
122 foo = 5
123 class D(C):
124 pass
125 class E(D):
126 pass
127 d = D()
128 e = E()
129 D.__bases__ = (C2,)
130 assert e.foo == 5
131
132 class F(object):
133 foo = 3
134 D.__bases__ = (C, F)
135 assert e.foo == 3
136
137 def test_custom_metaclass(self):
138 @self.retry
139 def run():
140 import __pypy__
141 for j in range(20):
142 class MetaA(type):
143 def __getattribute__(self, x):
144 return 1
145 def f(self):
146 return 42
147 A = type.__new__(MetaA, "A", (), {"f": f})
148 l = [type.__getattribute__(A, "__new__")(A)] * 10
149 __pypy__.reset_method_cache_counter()
150 for i, a in enumerate(l):
151 # use getattr to circumvent the mapdict cache
152 assert getattr(a, "f")() == 42
153 cache_counter = __pypy__.method_cache_counter("f")
154 assert sum(cache_counter) == 10
155 if cache_counter == (9, 1):
156 break
157 #else the moon is misaligned, try again
158 else:
159 raise AssertionError("cache_counter = %r" % (cache_counter,))
160
161 def test_mutate_class(self):
162 @self.retry
163 def run():
164 import __pypy__
165 class A(object):
166 x = 1
167 y = 2
168 __pypy__.reset_method_cache_counter()
169 a = A()
170 for i in range(100):
171 assert a.y == 2
172 assert a.x == i + 1
173 A.x += 1
174 cache_counter = __pypy__.method_cache_counter("x")
175 # XXX this is the bad case for the mapdict cache: looking up
176 # non-method attributes from the class
177 assert cache_counter[0] >= 450
178 assert cache_counter[1] >= 1
179 assert sum(cache_counter) == 500
180
181 __pypy__.reset_method_cache_counter()
182 a = A()
183 for i in range(100):
184 assert a.y == 2
185 setattr(a, "a%s" % i, i)
186 cache_counter = __pypy__.method_cache_counter("x")
187 assert cache_counter[0] == 0 # 0 hits, because all the attributes are new