1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
import py
from pypy.interpreter import executioncontext
from pypy.interpreter.error import OperationError
class Finished(Exception):
pass
class TestExecutionContext:
def test_action(self):
class DemoAction(executioncontext.AsyncAction):
counter = 0
def perform(self, ec, frame):
self.counter += 1
if self.counter == 10:
raise Finished
space = self.space
a1 = DemoAction(space)
for i in range(20):
# assert does not raise:
space.appexec([], """():
n = 5
return n + 2
""")
try:
for i in range(20):
a1.fire()
space.appexec([], """():
n = 5
return n + 2
""")
assert a1.counter == i + 1
except Finished:
pass
assert i == 9
def test_action_queue(self):
events = []
class Action1(executioncontext.AsyncAction):
def perform(self, ec, frame):
events.append('one')
class Action2(executioncontext.AsyncAction):
def perform(self, ec, frame):
events.append('two')
space = self.space
a1 = Action1(space)
a2 = Action2(space)
a1.fire()
a2.fire()
space.appexec([], """():
n = 5
return n + 2
""")
assert events == ['one', 'two']
#
events[:] = []
a1.fire()
space.appexec([], """():
n = 5
return n + 2
""")
assert events == ['one']
def test_fire_inside_perform(self):
# test what happens if we call AsyncAction.fire() while we are in the
# middle of an AsyncAction.perform(). In particular, this happens when
# PyObjectDeallocAction.fire() is called by rawrefcount: see issue
# 2805
events = []
class Action1(executioncontext.AsyncAction):
_count = 0
def perform(self, ec, frame):
events.append('one')
if self._count == 0:
# a1 is no longer in the queue, so it will be enqueued
a1.fire()
#
# a2 is still in the queue, so the fire() is ignored and
# it's performed in its normal order, i.e. BEFORE a3
a2.fire()
self._count += 1
class Action2(executioncontext.AsyncAction):
def perform(self, ec, frame):
events.append('two')
class Action3(executioncontext.AsyncAction):
def perform(self, ec, frame):
events.append('three')
space = self.space
a1 = Action1(space)
a2 = Action2(space)
a3 = Action3(space)
a1.fire()
a2.fire()
a3.fire()
space.appexec([], """():
pass
""")
assert events == ['one', 'two', 'three', 'one']
def test_periodic_action(self):
from pypy.interpreter.executioncontext import ActionFlag
class DemoAction(executioncontext.PeriodicAsyncAction):
counter = 0
def perform(self, ec, frame):
self.counter += 1
print '->', self.counter
if self.counter == 3:
raise Finished
space = self.space
a2 = DemoAction(space)
try:
space.actionflag.setcheckinterval(100)
space.actionflag.register_periodic_action(a2, True)
try:
for i in range(500):
space.appexec([], """():
n = 5
return n + 2
""")
except Finished:
pass
finally:
space.actionflag = ActionFlag() # reset to default
assert 10 < i < 110
def test_llprofile(self):
l = []
def profile_func(space, w_arg, frame, event, w_aarg):
assert w_arg is space.w_None
l.append(event)
space = self.space
space.getexecutioncontext().setllprofile(profile_func, space.w_None)
space.appexec([], """():
pass
""")
space.getexecutioncontext().setllprofile(None, None)
assert l[-4:] == ['call', 'return', 'call', 'return']
def test_llprofile_c_call(self):
from pypy.interpreter.function import Function, Method
l = []
seen = []
space = self.space
def profile_func(space, w_arg, frame, event, w_func):
assert w_arg is space.w_None
l.append(event)
if event == 'c_call':
seen.append(w_func)
def check_snippet(snippet, expected_c_call):
del l[:]
del seen[:]
space.getexecutioncontext().setllprofile(profile_func,
space.w_None)
space.appexec([], """():
%s
return
""" % snippet)
space.getexecutioncontext().setllprofile(None, None)
assert l[-6:] == ['call', 'return', 'call', 'c_call', 'c_return', 'return']
if isinstance(seen[-1], Method):
w_class = space.type(seen[-1].w_instance)
found = 'method %s of %s' % (
seen[-1].w_function.name,
w_class.getname(space).encode('utf-8'))
else:
assert isinstance(seen[-1], Function)
found = 'builtin %s' % seen[-1].name
assert found == expected_c_call
check_snippet('l = []; l.append(42)', 'method append of list')
check_snippet('max(1, 2)', 'builtin max')
check_snippet('args = (1, 2); max(*args)', 'builtin max')
check_snippet('max(1, 2, **{})', 'builtin max')
check_snippet('args = (1, 2); max(*args, **{})', 'builtin max')
check_snippet('abs(val=0)', 'builtin abs')
def test_llprofile_c_exception(self):
l = []
def profile_func(space, w_arg, frame, event, w_aarg):
assert w_arg is space.w_None
l.append(event)
space = self.space
space.getexecutioncontext().setllprofile(profile_func, space.w_None)
def check_snippet(snippet):
space.appexec([], """():
try:
%s
except:
pass
return
""" % snippet)
space.getexecutioncontext().setllprofile(None, None)
assert l[-6:] == ['call', 'return', 'call', 'c_call', 'c_exception', 'return']
check_snippet('d = {}; d.__getitem__(42)')
def test_c_call_setprofile_outer_frame(self):
space = self.space
w_events = space.appexec([], """():
import sys
l = []
def profile(frame, event, arg):
l.append(event)
def foo():
sys.setprofile(profile)
def bar():
foo()
max(1, 2)
bar()
sys.setprofile(None)
return l
""")
events = space.unwrap(w_events)
assert events == ['return', 'c_call', 'c_return', 'return', 'c_call']
def test_c_call_setprofile_kwargs(self):
space = self.space
w_events = space.appexec([], """():
import sys
l = []
def profile(frame, event, arg):
l.append(event)
def bar():
sys.setprofile(profile)
[].sort(reverse=True)
sys.setprofile(None)
bar()
return l
""")
events = space.unwrap(w_events)
assert events == ['c_call', 'c_return', 'c_call']
def test_c_call_setprofile_strange_method(self):
space = self.space
w_events = space.appexec([], """():
import sys
class A(object):
def __init__(self, value):
self.value = value
def meth(self):
pass
MethodType = type(A(0).meth)
strangemeth = MethodType(A, 42)
l = []
def profile(frame, event, arg):
l.append(event)
def foo():
sys.setprofile(profile)
def bar():
foo()
strangemeth()
bar()
sys.setprofile(None)
return l
""")
events = space.unwrap(w_events)
assert events == ['return', 'call', 'return', 'return', 'c_call']
def test_c_call_profiles_immediately(self):
space = self.space
w_events = space.appexec([], """():
import sys
l = []
def profile(frame, event, arg):
l.append((event, arg))
def bar():
sys.setprofile(profile)
max(3, 4)
bar()
sys.setprofile(None)
return l
""")
events = space.unwrap(w_events)
assert [i[0] for i in events] == ['c_call', 'c_return', 'return', 'c_call']
assert events[0][1] == events[1][1]
def test_profile_and_exception(self):
space = self.space
w_res = space.appexec([], """():
l = []
def profile(*args):
l.append(sys.exc_info()[0])
import sys
try:
sys.setprofile(profile)
try:
x
except:
expected = sys.exc_info()[0]
assert expected is NameError
for i in l:
assert expected is l[0]
finally:
sys.setprofile(None)
""")
class AppTestProfile:
def test_return(self):
import sys
l = []
def profile(frame, event, arg):
l.append((event, arg))
def bar(x):
return 40 + x
sys.setprofile(profile)
bar(2)
sys.setprofile(None)
assert l == [('call', None),
('return', 42),
('c_call', sys.setprofile)], repr(l)
def test_c_return(self):
import sys
l = []
def profile(frame, event, arg):
l.append((event, arg))
sys.setprofile(profile)
max(2, 42)
sys.setprofile(None)
assert l == [('c_call', max),
('c_return', max),
('c_call', sys.setprofile)], repr(l)
def test_exception(self):
import sys
l = []
def profile(frame, event, arg):
l.append((event, arg))
def f():
raise ValueError("foo")
sys.setprofile(profile)
try:
f()
except ValueError:
pass
sys.setprofile(None)
assert l == [('call', None),
('return', None),
('c_call', sys.setprofile)], repr(l)
def test_c_exception(self):
import sys
l = []
def profile(frame, event, arg):
l.append((event, arg))
sys.setprofile(profile)
try:
divmod(5, 0)
except ZeroDivisionError:
pass
sys.setprofile(None)
assert l == [('c_call', divmod),
('c_exception', divmod),
('c_call', sys.setprofile)], repr(l)
|