| 1 |
import pytest |
| 2 |
import os |
| 3 |
from rpython.rlib.rvmprof.test.support import fakevmprof |
| 4 |
from pypy.interpreter.gateway import interp2app |
| 5 |
from pypy.module._continuation.test.support import BaseAppTest |
| 6 |
|
| 7 |
@pytest.mark.usefixtures('app_fakevmprof') |
| 8 |
class AppTestStacklet(BaseAppTest): |
| 9 |
def setup_class(cls): |
| 10 |
BaseAppTest.setup_class.im_func(cls) |
| 11 |
cls.w_translated = cls.space.wrap( |
| 12 |
os.path.join(os.path.dirname(__file__), |
| 13 |
'test_translated.py')) |
| 14 |
cls.w_stack = cls.space.appexec([], """(): |
| 15 |
import sys |
| 16 |
def stack(f=None): |
| 17 |
''' |
| 18 |
get the call-stack of the caller or the specified frame |
| 19 |
''' |
| 20 |
if f is None: |
| 21 |
f = sys._getframe(1) |
| 22 |
res = [] |
| 23 |
seen = set() |
| 24 |
while f: |
| 25 |
if f in seen: |
| 26 |
# frame cycle |
| 27 |
res.append('...') |
| 28 |
break |
| 29 |
if f.f_code.co_name == '<module>': |
| 30 |
# if we are running with -A, cut all the stack above |
| 31 |
# the test function |
| 32 |
break |
| 33 |
seen.add(f) |
| 34 |
res.append(f.f_code.co_name) |
| 35 |
f = f.f_back |
| 36 |
#print res |
| 37 |
return res |
| 38 |
return stack |
| 39 |
""") |
| 40 |
cls.w_appdirect = cls.space.wrap(cls.runappdirect) |
| 41 |
|
| 42 |
|
| 43 |
@pytest.fixture |
| 44 |
def app_fakevmprof(self, fakevmprof): |
| 45 |
""" |
| 46 |
This is automaticaly re-initialized for every method: thanks to |
| 47 |
fakevmprof's finalizer, it checks that we called {start,stop}_sampling |
| 48 |
the in pairs |
| 49 |
""" |
| 50 |
w = self.space.wrap |
| 51 |
i2a = interp2app |
| 52 |
def is_sampling_enabled(space): |
| 53 |
return space.wrap(fakevmprof.is_sampling_enabled) |
| 54 |
self.w_is_sampling_enabled = w(i2a(is_sampling_enabled)) |
| 55 |
|
| 56 |
def start_sampling(space): |
| 57 |
fakevmprof.start_sampling() |
| 58 |
self.w_start_sampling = w(i2a(start_sampling)) |
| 59 |
|
| 60 |
def stop_sampling(space): |
| 61 |
fakevmprof.stop_sampling() |
| 62 |
self.w_stop_sampling = w(i2a(stop_sampling)) |
| 63 |
|
| 64 |
|
| 65 |
def test_new_empty(self): |
| 66 |
from _continuation import continulet |
| 67 |
|
| 68 |
def empty_callback(c): |
| 69 |
never_called |
| 70 |
|
| 71 |
c = continulet(empty_callback) |
| 72 |
assert type(c) is continulet |
| 73 |
|
| 74 |
def test_call_empty(self): |
| 75 |
from _continuation import continulet |
| 76 |
|
| 77 |
def empty_callback(c1): |
| 78 |
assert c1 is c |
| 79 |
seen.append(1) |
| 80 |
return 42 |
| 81 |
|
| 82 |
seen = [] |
| 83 |
c = continulet(empty_callback) |
| 84 |
res = c.switch() |
| 85 |
assert res == 42 |
| 86 |
assert seen == [1] |
| 87 |
|
| 88 |
def test_no_double_init(self): |
| 89 |
from _continuation import continulet, error |
| 90 |
|
| 91 |
def empty_callback(c1): |
| 92 |
never_called |
| 93 |
|
| 94 |
c = continulet(empty_callback) |
| 95 |
raises(error, c.__init__, empty_callback) |
| 96 |
|
| 97 |
def test_no_init_after_started(self): |
| 98 |
from _continuation import continulet, error |
| 99 |
|
| 100 |
def empty_callback(c1): |
| 101 |
raises(error, c1.__init__, empty_callback) |
| 102 |
return 42 |
| 103 |
|
| 104 |
c = continulet(empty_callback) |
| 105 |
res = c.switch() |
| 106 |
assert res == 42 |
| 107 |
|
| 108 |
def test_no_init_after_finished(self): |
| 109 |
from _continuation import continulet, error |
| 110 |
|
| 111 |
def empty_callback(c1): |
| 112 |
return 42 |
| 113 |
|
| 114 |
c = continulet(empty_callback) |
| 115 |
res = c.switch() |
| 116 |
assert res == 42 |
| 117 |
raises(error, c.__init__, empty_callback) |
| 118 |
|
| 119 |
def test_propagate_exception(self): |
| 120 |
from _continuation import continulet |
| 121 |
|
| 122 |
def empty_callback(c1): |
| 123 |
assert c1 is c |
| 124 |
seen.append(42) |
| 125 |
raise ValueError |
| 126 |
|
| 127 |
seen = [] |
| 128 |
c = continulet(empty_callback) |
| 129 |
raises(ValueError, c.switch) |
| 130 |
assert seen == [42] |
| 131 |
|
| 132 |
def test_callback_with_arguments(self): |
| 133 |
from _continuation import continulet |
| 134 |
|
| 135 |
def empty_callback(c1, *args, **kwds): |
| 136 |
seen.append(c1) |
| 137 |
seen.append(args) |
| 138 |
seen.append(kwds) |
| 139 |
return 42 |
| 140 |
|
| 141 |
seen = [] |
| 142 |
c = continulet(empty_callback, 42, 43, foo=44, bar=45) |
| 143 |
res = c.switch() |
| 144 |
assert res == 42 |
| 145 |
assert seen == [c, (42, 43), {'foo': 44, 'bar': 45}] |
| 146 |
|
| 147 |
def test_switch(self): |
| 148 |
from _continuation import continulet |
| 149 |
|
| 150 |
def switchbackonce_callback(c): |
| 151 |
seen.append(1) |
| 152 |
res = c.switch('a') |
| 153 |
assert res == 'b' |
| 154 |
seen.append(3) |
| 155 |
return 'c' |
| 156 |
|
| 157 |
seen = [] |
| 158 |
c = continulet(switchbackonce_callback) |
| 159 |
seen.append(0) |
| 160 |
res = c.switch() |
| 161 |
assert res == 'a' |
| 162 |
seen.append(2) |
| 163 |
res = c.switch('b') |
| 164 |
assert res == 'c' |
| 165 |
assert seen == [0, 1, 2, 3] |
| 166 |
|
| 167 |
def test_initial_switch_must_give_None(self): |
| 168 |
from _continuation import continulet |
| 169 |
|
| 170 |
def empty_callback(c): |
| 171 |
return 'ok' |
| 172 |
|
| 173 |
c = continulet(empty_callback) |
| 174 |
res = c.switch(None) |
| 175 |
assert res == 'ok' |
| 176 |
|
| 177 |
c = continulet(empty_callback) |
| 178 |
raises(TypeError, c.switch, 'foo') |
| 179 |
|
| 180 |
def test_continuation_error(self): |
| 181 |
from _continuation import continulet, error |
| 182 |
|
| 183 |
def empty_callback(c): |
| 184 |
return 42 |
| 185 |
|
| 186 |
c = continulet(empty_callback) |
| 187 |
c.switch() |
| 188 |
e = raises(error, c.switch) |
| 189 |
assert str(e.value) == "continulet already finished" |
| 190 |
|
| 191 |
def test_go_depth2(self): |
| 192 |
from _continuation import continulet |
| 193 |
|
| 194 |
def depth2(c): |
| 195 |
seen.append(3) |
| 196 |
return 4 |
| 197 |
|
| 198 |
def depth1(c): |
| 199 |
seen.append(1) |
| 200 |
c2 = continulet(depth2) |
| 201 |
seen.append(2) |
| 202 |
res = c2.switch() |
| 203 |
seen.append(res) |
| 204 |
return 5 |
| 205 |
|
| 206 |
seen = [] |
| 207 |
c = continulet(depth1) |
| 208 |
seen.append(0) |
| 209 |
res = c.switch() |
| 210 |
seen.append(res) |
| 211 |
assert seen == [0, 1, 2, 3, 4, 5] |
| 212 |
|
| 213 |
def test_exception_depth2(self): |
| 214 |
from _continuation import continulet |
| 215 |
|
| 216 |
def depth2(c): |
| 217 |
seen.append(2) |
| 218 |
raise ValueError |
| 219 |
|
| 220 |
def depth1(c): |
| 221 |
seen.append(1) |
| 222 |
try: |
| 223 |
continulet(depth2).switch() |
| 224 |
except ValueError: |
| 225 |
seen.append(3) |
| 226 |
return 4 |
| 227 |
|
| 228 |
seen = [] |
| 229 |
c = continulet(depth1) |
| 230 |
res = c.switch() |
| 231 |
seen.append(res) |
| 232 |
assert seen == [1, 2, 3, 4] |
| 233 |
|
| 234 |
def test_exception_with_switch(self): |
| 235 |
from _continuation import continulet |
| 236 |
|
| 237 |
def depth1(c): |
| 238 |
seen.append(1) |
| 239 |
c.switch() |
| 240 |
seen.append(3) |
| 241 |
raise ValueError |
| 242 |
|
| 243 |
seen = [] |
| 244 |
c = continulet(depth1) |
| 245 |
seen.append(0) |
| 246 |
c.switch() |
| 247 |
seen.append(2) |
| 248 |
raises(ValueError, c.switch) |
| 249 |
assert seen == [0, 1, 2, 3] |
| 250 |
|
| 251 |
def test_is_pending(self): |
| 252 |
from _continuation import continulet |
| 253 |
|
| 254 |
def switchbackonce_callback(c): |
| 255 |
assert c.is_pending() |
| 256 |
res = c.switch('a') |
| 257 |
assert res == 'b' |
| 258 |
assert c.is_pending() |
| 259 |
return 'c' |
| 260 |
|
| 261 |
c = continulet.__new__(continulet) |
| 262 |
assert not c.is_pending() |
| 263 |
c.__init__(switchbackonce_callback) |
| 264 |
assert c.is_pending() |
| 265 |
res = c.switch() |
| 266 |
assert res == 'a' |
| 267 |
assert c.is_pending() |
| 268 |
res = c.switch('b') |
| 269 |
assert res == 'c' |
| 270 |
assert not c.is_pending() |
| 271 |
|
| 272 |
def test_switch_alternate(self): |
| 273 |
from _continuation import continulet |
| 274 |
|
| 275 |
def func_lower(c): |
| 276 |
res = c.switch('a') |
| 277 |
assert res == 'b' |
| 278 |
res = c.switch('c') |
| 279 |
assert res == 'd' |
| 280 |
return 'e' |
| 281 |
|
| 282 |
def func_upper(c): |
| 283 |
res = c.switch('A') |
| 284 |
assert res == 'B' |
| 285 |
res = c.switch('C') |
| 286 |
assert res == 'D' |
| 287 |
return 'E' |
| 288 |
|
| 289 |
c_lower = continulet(func_lower) |
| 290 |
c_upper = continulet(func_upper) |
| 291 |
res = c_lower.switch() |
| 292 |
assert res == 'a' |
| 293 |
res = c_upper.switch() |
| 294 |
assert res == 'A' |
| 295 |
res = c_lower.switch('b') |
| 296 |
assert res == 'c' |
| 297 |
res = c_upper.switch('B') |
| 298 |
assert res == 'C' |
| 299 |
res = c_lower.switch('d') |
| 300 |
assert res == 'e' |
| 301 |
res = c_upper.switch('D') |
| 302 |
assert res == 'E' |
| 303 |
|
| 304 |
def test_switch_not_initialized(self): |
| 305 |
from _continuation import continulet |
| 306 |
c0 = continulet.__new__(continulet) |
| 307 |
res = c0.switch() |
| 308 |
assert res is None |
| 309 |
res = c0.switch(123) |
| 310 |
assert res == 123 |
| 311 |
raises(ValueError, c0.throw, ValueError) |
| 312 |
|
| 313 |
def test_exception_with_switch_depth2(self): |
| 314 |
from _continuation import continulet |
| 315 |
|
| 316 |
def depth2(c): |
| 317 |
seen.append(4) |
| 318 |
c.switch() |
| 319 |
seen.append(6) |
| 320 |
raise ValueError |
| 321 |
|
| 322 |
def depth1(c): |
| 323 |
seen.append(1) |
| 324 |
c.switch() |
| 325 |
seen.append(3) |
| 326 |
c2 = continulet(depth2) |
| 327 |
c2.switch() |
| 328 |
seen.append(5) |
| 329 |
raises(ValueError, c2.switch) |
| 330 |
assert not c2.is_pending() |
| 331 |
seen.append(7) |
| 332 |
assert c.is_pending() |
| 333 |
raise KeyError |
| 334 |
|
| 335 |
seen = [] |
| 336 |
c = continulet(depth1) |
| 337 |
c.switch() |
| 338 |
seen.append(2) |
| 339 |
raises(KeyError, c.switch) |
| 340 |
assert not c.is_pending() |
| 341 |
assert seen == [1, 2, 3, 4, 5, 6, 7] |
| 342 |
|
| 343 |
def test_random_switching(self): |
| 344 |
from _continuation import continulet |
| 345 |
|
| 346 |
seen = [] |
| 347 |
|
| 348 |
def t1(c1): |
| 349 |
seen.append(3) |
| 350 |
res = c1.switch() |
| 351 |
seen.append(6) |
| 352 |
return res |
| 353 |
|
| 354 |
def s1(c1, n): |
| 355 |
seen.append(2) |
| 356 |
assert n == 123 |
| 357 |
c2 = t1(c1) |
| 358 |
seen.append(7) |
| 359 |
res = c1.switch('a') + 1 |
| 360 |
seen.append(10) |
| 361 |
return res |
| 362 |
|
| 363 |
def s2(c2, c1): |
| 364 |
seen.append(5) |
| 365 |
res = c1.switch(c2) |
| 366 |
seen.append(8) |
| 367 |
assert res == 'a' |
| 368 |
res = c2.switch('b') + 2 |
| 369 |
seen.append(12) |
| 370 |
return res |
| 371 |
|
| 372 |
def f(): |
| 373 |
seen.append(1) |
| 374 |
c1 = continulet(s1, 123) |
| 375 |
c2 = continulet(s2, c1) |
| 376 |
c1.switch() |
| 377 |
seen.append(4) |
| 378 |
res = c2.switch() |
| 379 |
seen.append(9) |
| 380 |
assert res == 'b' |
| 381 |
res = c1.switch(1000) |
| 382 |
seen.append(11) |
| 383 |
assert res == 1001 |
| 384 |
res = c2.switch(2000) |
| 385 |
seen.append(13) |
| 386 |
return res |
| 387 |
|
| 388 |
res = f() |
| 389 |
assert res == 2002 |
| 390 |
assert seen == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] |
| 391 |
|
| 392 |
def test_f_back(self): |
| 393 |
import sys |
| 394 |
from _continuation import continulet |
| 395 |
stack = self.stack |
| 396 |
|
| 397 |
def bar(c): |
| 398 |
assert stack() == ['bar', 'foo', 'test_f_back'] |
| 399 |
c.switch(sys._getframe(0)) |
| 400 |
c.switch(sys._getframe(0).f_back) |
| 401 |
c.switch(sys._getframe(1)) |
| 402 |
|
| 403 |
assert stack() == ['bar', 'foo', 'main', 'test_f_back'] |
| 404 |
c.switch(sys._getframe(1).f_back) |
| 405 |
|
| 406 |
assert stack() == ['bar', 'foo', 'main2', 'test_f_back'] |
| 407 |
assert sys._getframe(2) is f3_foo.f_back |
| 408 |
c.switch(sys._getframe(2)) |
| 409 |
def foo(c): |
| 410 |
bar(c) |
| 411 |
|
| 412 |
assert stack() == ['test_f_back'] |
| 413 |
c = continulet(foo) |
| 414 |
f1_bar = c.switch() |
| 415 |
assert f1_bar.f_code.co_name == 'bar' |
| 416 |
f2_foo = c.switch() |
| 417 |
assert f2_foo.f_code.co_name == 'foo' |
| 418 |
f3_foo = c.switch() |
| 419 |
assert f3_foo is f2_foo |
| 420 |
assert f1_bar.f_back is f3_foo |
| 421 |
|
| 422 |
def main(): |
| 423 |
f4_main = c.switch() |
| 424 |
assert f4_main.f_code.co_name == 'main' |
| 425 |
assert f3_foo.f_back is f1_bar |
| 426 |
assert stack() == ['main', 'test_f_back'] |
| 427 |
assert stack(f1_bar) == ['bar', 'foo', '...'] |
| 428 |
|
| 429 |
def main2(): |
| 430 |
f5_main2 = c.switch() |
| 431 |
assert f5_main2.f_code.co_name == 'main2' |
| 432 |
assert f3_foo.f_back is f1_bar |
| 433 |
assert stack(f1_bar) == ['bar', 'foo', '...'] |
| 434 |
|
| 435 |
main() |
| 436 |
main2() |
| 437 |
res = c.switch() |
| 438 |
assert res is None |
| 439 |
assert f3_foo.f_back is None |
| 440 |
|
| 441 |
def test_traceback_is_complete(self): |
| 442 |
import sys |
| 443 |
from _continuation import continulet |
| 444 |
|
| 445 |
def g(): |
| 446 |
raise KeyError |
| 447 |
def f(c): |
| 448 |
g() |
| 449 |
|
| 450 |
def do(c): |
| 451 |
c.switch() |
| 452 |
|
| 453 |
c = continulet(f) |
| 454 |
try: |
| 455 |
do(c) |
| 456 |
except KeyError: |
| 457 |
tb = sys.exc_info()[2] |
| 458 |
else: |
| 459 |
raise AssertionError("should have raised!") |
| 460 |
|
| 461 |
assert tb.tb_next.tb_frame.f_code.co_name == 'do' |
| 462 |
assert tb.tb_next.tb_next.tb_frame.f_code.co_name == 'f' |
| 463 |
assert tb.tb_next.tb_next.tb_next.tb_frame.f_code.co_name == 'g' |
| 464 |
assert tb.tb_next.tb_next.tb_next.tb_next is None |
| 465 |
|
| 466 |
def test_switch2_simple(self): |
| 467 |
from _continuation import continulet |
| 468 |
|
| 469 |
def f1(c1): |
| 470 |
res = c1.switch('started 1') |
| 471 |
assert res == 'a' |
| 472 |
res = c1.switch('b', to=c2) |
| 473 |
assert res == 'c' |
| 474 |
return 42 |
| 475 |
def f2(c2): |
| 476 |
res = c2.switch('started 2') |
| 477 |
assert res == 'b' |
| 478 |
res = c2.switch('c', to=c1) |
| 479 |
not_reachable |
| 480 |
|
| 481 |
c1 = continulet(f1) |
| 482 |
c2 = continulet(f2) |
| 483 |
res = c1.switch() |
| 484 |
assert res == 'started 1' |
| 485 |
res = c2.switch() |
| 486 |
assert res == 'started 2' |
| 487 |
res = c1.switch('a') |
| 488 |
assert res == 42 |
| 489 |
|
| 490 |
def test_switch2_pingpong(self): |
| 491 |
from _continuation import continulet |
| 492 |
|
| 493 |
def f1(c1): |
| 494 |
res = c1.switch('started 1') |
| 495 |
assert res == 'go' |
| 496 |
for i in range(10): |
| 497 |
res = c1.switch(i, to=c2) |
| 498 |
assert res == 100 + i |
| 499 |
return 42 |
| 500 |
def f2(c2): |
| 501 |
res = c2.switch('started 2') |
| 502 |
for i in range(10): |
| 503 |
assert res == i |
| 504 |
res = c2.switch(100 + i, to=c1) |
| 505 |
not_reachable |
| 506 |
|
| 507 |
c1 = continulet(f1) |
| 508 |
c2 = continulet(f2) |
| 509 |
res = c1.switch() |
| 510 |
assert res == 'started 1' |
| 511 |
res = c2.switch() |
| 512 |
assert res == 'started 2' |
| 513 |
res = c1.switch('go') |
| 514 |
assert res == 42 |
| 515 |
|
| 516 |
def test_switch2_more_complex(self): |
| 517 |
from _continuation import continulet |
| 518 |
|
| 519 |
def f1(c1): |
| 520 |
res = c1.switch(to=c2) |
| 521 |
assert res == 'a' |
| 522 |
res = c1.switch('b', to=c2) |
| 523 |
assert res == 'c' |
| 524 |
return 41 |
| 525 |
def f2(c2): |
| 526 |
res = c2.switch('a', to=c1) |
| 527 |
assert res == 'b' |
| 528 |
return 42 |
| 529 |
|
| 530 |
c1 = continulet(f1) |
| 531 |
c2 = continulet(f2) |
| 532 |
res = c1.switch() |
| 533 |
assert res == 42 |
| 534 |
assert not c2.is_pending() |
| 535 |
res = c1.switch('c') |
| 536 |
assert res == 41 |
| 537 |
|
| 538 |
def test_switch2_no_op(self): |
| 539 |
from _continuation import continulet |
| 540 |
|
| 541 |
def f1(c1): |
| 542 |
res = c1.switch('a', to=c1) |
| 543 |
assert res == 'a' |
| 544 |
return 42 |
| 545 |
|
| 546 |
c1 = continulet(f1) |
| 547 |
res = c1.switch() |
| 548 |
assert res == 42 |
| 549 |
|
| 550 |
def test_switch2_immediately_away(self): |
| 551 |
from _continuation import continulet |
| 552 |
|
| 553 |
def f1(c1): |
| 554 |
print('in f1') |
| 555 |
return 'm' |
| 556 |
|
| 557 |
def f2(c2): |
| 558 |
res = c2.switch('z') |
| 559 |
print('got there!') |
| 560 |
assert res == 'a' |
| 561 |
return None |
| 562 |
|
| 563 |
c1 = continulet(f1) |
| 564 |
c2 = continulet(f2) |
| 565 |
res = c2.switch() |
| 566 |
assert res == 'z' |
| 567 |
assert c1.is_pending() |
| 568 |
assert c2.is_pending() |
| 569 |
print('calling!') |
| 570 |
res = c1.switch('a', to=c2) |
| 571 |
print('back') |
| 572 |
assert res == 'm' |
| 573 |
|
| 574 |
def test_switch2_immediately_away_corner_case(self): |
| 575 |
from _continuation import continulet |
| 576 |
|
| 577 |
def f1(c1): |
| 578 |
this_is_never_seen |
| 579 |
|
| 580 |
def f2(c2): |
| 581 |
res = c2.switch('z') |
| 582 |
assert res is None |
| 583 |
return 'b' |
| 584 |
|
| 585 |
|
| 586 |
c1 = continulet(f1) |
| 587 |
c2 = continulet(f2) |
| 588 |
res = c2.switch() |
| 589 |
assert res == 'z' |
| 590 |
raises(TypeError, c1.switch, to=c2) |
| 591 |
|
| 592 |
def test_switch2_not_initialized(self): |
| 593 |
from _continuation import continulet |
| 594 |
c0 = continulet.__new__(continulet) |
| 595 |
c0bis = continulet.__new__(continulet) |
| 596 |
res = c0.switch(123, to=c0) |
| 597 |
assert res == 123 |
| 598 |
res = c0.switch(123, to=c0bis) |
| 599 |
assert res == 123 |
| 600 |
raises(ValueError, c0.throw, ValueError, to=c0) |
| 601 |
raises(ValueError, c0.throw, ValueError, to=c0bis) |
| 602 |
|
| 603 |
def f1(c1): |
| 604 |
c1.switch('a') |
| 605 |
raises(ValueError, c1.switch, 'b') |
| 606 |
raises(KeyError, c1.switch, 'c') |
| 607 |
return 'd' |
| 608 |
c1 = continulet(f1) |
| 609 |
res = c0.switch(to=c1) |
| 610 |
assert res == 'a' |
| 611 |
res = c1.switch(to=c0) |
| 612 |
assert res == 'b' |
| 613 |
res = c1.throw(ValueError, to=c0) |
| 614 |
assert res == 'c' |
| 615 |
res = c0.throw(KeyError, to=c1) |
| 616 |
assert res == 'd' |
| 617 |
|
| 618 |
def test_switch2_already_finished(self): |
| 619 |
from _continuation import continulet, error |
| 620 |
|
| 621 |
def f1(c1): |
| 622 |
not_reachable |
| 623 |
def empty_callback(c): |
| 624 |
return 42 |
| 625 |
|
| 626 |
c1 = continulet(f1) |
| 627 |
c2 = continulet(empty_callback) |
| 628 |
c2.switch() |
| 629 |
e = raises(error, c1.switch, to=c2) |
| 630 |
assert str(e.value) == "continulet already finished" |
| 631 |
|
| 632 |
def test_throw(self): |
| 633 |
import sys |
| 634 |
from _continuation import continulet |
| 635 |
|
| 636 |
def f1(c1): |
| 637 |
try: |
| 638 |
c1.switch() |
| 639 |
except KeyError: |
| 640 |
res = "got keyerror" |
| 641 |
try: |
| 642 |
c1.switch(res) |
| 643 |
except IndexError as exc: |
| 644 |
e = exc |
| 645 |
try: |
| 646 |
c1.switch(e) |
| 647 |
except IndexError as exc: |
| 648 |
e2 = exc |
| 649 |
try: |
| 650 |
c1.switch(e2) |
| 651 |
except IndexError: |
| 652 |
c1.throw(*sys.exc_info()) |
| 653 |
should_never_reach_here |
| 654 |
|
| 655 |
c1 = continulet(f1) |
| 656 |
c1.switch() |
| 657 |
res = c1.throw(KeyError) |
| 658 |
assert res == "got keyerror" |
| 659 |
class FooError(IndexError): |
| 660 |
pass |
| 661 |
foo = FooError() |
| 662 |
res = c1.throw(foo) |
| 663 |
assert res is foo |
| 664 |
res = c1.throw(IndexError, foo) |
| 665 |
assert res is foo |
| 666 |
|
| 667 |
def main(): |
| 668 |
def do_raise(): |
| 669 |
raise foo |
| 670 |
try: |
| 671 |
do_raise() |
| 672 |
except IndexError: |
| 673 |
tb = sys.exc_info()[2] |
| 674 |
try: |
| 675 |
c1.throw(IndexError, foo, tb) |
| 676 |
except IndexError: |
| 677 |
tb = sys.exc_info()[2] |
| 678 |
return tb |
| 679 |
|
| 680 |
tb = main() |
| 681 |
assert tb.tb_frame.f_code.co_name == 'main' |
| 682 |
assert tb.tb_next.tb_frame.f_code.co_name == 'f1' |
| 683 |
assert tb.tb_next.tb_next.tb_frame.f_code.co_name == 'main' |
| 684 |
assert tb.tb_next.tb_next.tb_next.tb_frame.f_code.co_name == 'do_raise' |
| 685 |
assert tb.tb_next.tb_next.tb_next.tb_next.tb_frame.f_code.co_name == 'f1' |
| 686 |
assert tb.tb_next.tb_next.tb_next.tb_next.tb_next is None |
| 687 |
|
| 688 |
def test_throw_to_starting(self): |
| 689 |
from _continuation import continulet |
| 690 |
|
| 691 |
def f1(c1): |
| 692 |
not_reached |
| 693 |
|
| 694 |
c1 = continulet(f1) |
| 695 |
raises(IndexError, c1.throw, IndexError) |
| 696 |
|
| 697 |
def test_throw2_simple(self): |
| 698 |
from _continuation import continulet |
| 699 |
|
| 700 |
def f1(c1): |
| 701 |
not_reached |
| 702 |
def f2(c2): |
| 703 |
try: |
| 704 |
c2.switch("ready") |
| 705 |
except IndexError: |
| 706 |
raise ValueError |
| 707 |
|
| 708 |
c1 = continulet(f1) |
| 709 |
c2 = continulet(f2) |
| 710 |
res = c2.switch() |
| 711 |
assert res == "ready" |
| 712 |
assert c1.is_pending() |
| 713 |
assert c2.is_pending() |
| 714 |
raises(ValueError, c1.throw, IndexError, to=c2) |
| 715 |
assert not c1.is_pending() |
| 716 |
assert not c2.is_pending() |
| 717 |
|
| 718 |
def test_throw2_no_op(self): |
| 719 |
from _continuation import continulet |
| 720 |
|
| 721 |
def f1(c1): |
| 722 |
raises(ValueError, c1.throw, ValueError, to=c1) |
| 723 |
return "ok" |
| 724 |
|
| 725 |
c1 = continulet(f1) |
| 726 |
res = c1.switch() |
| 727 |
assert res == "ok" |
| 728 |
|
| 729 |
def test_permute(self): |
| 730 |
import sys |
| 731 |
from _continuation import continulet, permute |
| 732 |
|
| 733 |
def f1(c1): |
| 734 |
res = c1.switch() |
| 735 |
assert res == "ok" |
| 736 |
return "done" |
| 737 |
|
| 738 |
def f2(c2): |
| 739 |
assert sys._getframe(1).f_code.co_name == 'main' |
| 740 |
permute(c1, c2) |
| 741 |
assert sys._getframe(1).f_code.co_name == 'f1' |
| 742 |
return "ok" |
| 743 |
|
| 744 |
c1 = continulet(f1) |
| 745 |
c2 = continulet(f2) |
| 746 |
def main(): |
| 747 |
c1.switch() |
| 748 |
res = c2.switch() |
| 749 |
assert res == "done" |
| 750 |
main() |
| 751 |
|
| 752 |
def test_permute_noninitialized(self): |
| 753 |
from _continuation import continulet, permute |
| 754 |
permute(continulet.__new__(continulet)) |
| 755 |
permute(continulet.__new__(continulet), |
| 756 |
continulet.__new__(continulet)) |
| 757 |
|
| 758 |
def test_bug_finish_with_already_finished_stacklet(self): |
| 759 |
from _continuation import continulet, error |
| 760 |
|
| 761 |
c1 = continulet(lambda x: x) |
| 762 |
c1.switch() |
| 763 |
|
| 764 |
c2 = continulet(lambda x: x) |
| 765 |
|
| 766 |
raises(error, c1.switch, to=c2) |
| 767 |
|
| 768 |
def test_various_depths(self): |
| 769 |
skip("may fail on top of CPython") |
| 770 |
|
| 771 |
d = {} |
| 772 |
execfile(self.translated, d) |
| 773 |
d['set_fast_mode']() |
| 774 |
d['test_various_depths']() |
| 775 |
|
| 776 |
def test_exc_info_doesnt_follow_continuations(self): |
| 777 |
import sys |
| 778 |
from _continuation import continulet |
| 779 |
|
| 780 |
def f1(c1): |
| 781 |
return sys.exc_info() |
| 782 |
|
| 783 |
c1 = continulet(f1) |
| 784 |
try: |
| 785 |
1 // 0 |
| 786 |
except ZeroDivisionError: |
| 787 |
got = c1.switch() |
| 788 |
assert got == (None, None, None) |
| 789 |
|
| 790 |
def test_bug_issue1984(self): |
| 791 |
from _continuation import continulet, error |
| 792 |
|
| 793 |
c1 = continulet.__new__(continulet) |
| 794 |
c2 = continulet(lambda g: None) |
| 795 |
|
| 796 |
continulet.switch(c1, to=c2) |
| 797 |
raises(error, continulet.switch, c1, to=c2) |
| 798 |
|
| 799 |
def test_exc_info_save_restore(self): |
| 800 |
from _continuation import continulet |
| 801 |
import sys |
| 802 |
main = [] |
| 803 |
|
| 804 |
def f(c): |
| 805 |
print("in f... 222") |
| 806 |
try: |
| 807 |
raise ValueError('fun') |
| 808 |
except: |
| 809 |
print("333") |
| 810 |
exc_info = sys.exc_info() |
| 811 |
print("444") |
| 812 |
c17650 = continulet(h) |
| 813 |
bd50.switch(to=c17650) |
| 814 |
print("back in f...") |
| 815 |
assert exc_info == sys.exc_info() |
| 816 |
|
| 817 |
def h(c): |
| 818 |
print("in h... 555") |
| 819 |
assert sys.exc_info() == (None, None, None) |
| 820 |
print("666") |
| 821 |
|
| 822 |
main = continulet.__new__(continulet) |
| 823 |
print(111) |
| 824 |
bd50 = continulet(f) |
| 825 |
main.switch(to=bd50) |
| 826 |
print(999) |
| 827 |
|
| 828 |
def test_sampling_inside_callback(self): |
| 829 |
if self.appdirect: |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
skip("we can't run this until we have _vmprof.is_sampling_enabled") |
| 834 |
from _continuation import continulet |
| 835 |
|
| 836 |
def my_callback(c1): |
| 837 |
assert self.is_sampling_enabled() |
| 838 |
return 42 |
| 839 |
|
| 840 |
try: |
| 841 |
self.start_sampling() |
| 842 |
assert self.is_sampling_enabled() |
| 843 |
c = continulet(my_callback) |
| 844 |
res = c.switch() |
| 845 |
assert res == 42 |
| 846 |
assert self.is_sampling_enabled() |
| 847 |
finally: |
| 848 |
self.stop_sampling() |