| 1 |
from pytest import raises, skip |
| 2 |
|
| 3 |
import sys |
| 4 |
|
| 5 |
def test_generator(): |
| 6 |
def f(): |
| 7 |
yield 1 |
| 8 |
assert next(f()) == 1 |
| 9 |
|
| 10 |
def test_generator2(): |
| 11 |
def f(): |
| 12 |
yield 1 |
| 13 |
g = f() |
| 14 |
assert next(g) == 1 |
| 15 |
with raises(StopIteration): |
| 16 |
next(g) |
| 17 |
|
| 18 |
def test_attributes(): |
| 19 |
def f(): |
| 20 |
yield 1 |
| 21 |
assert g.gi_running |
| 22 |
g = f() |
| 23 |
assert g.gi_code is f.__code__ |
| 24 |
assert g.__name__ == 'f' |
| 25 |
assert g.gi_frame is not None |
| 26 |
assert not g.gi_running |
| 27 |
next(g) |
| 28 |
assert not g.gi_running |
| 29 |
with raises(StopIteration): |
| 30 |
next(g) |
| 31 |
assert not g.gi_running |
| 32 |
assert g.gi_frame is None |
| 33 |
assert g.gi_code is f.__code__ |
| 34 |
assert g.__name__ == 'f' |
| 35 |
|
| 36 |
def test_gi_suspended(): |
| 37 |
def gisuspended(): |
| 38 |
yield 1 |
| 39 |
assert not g.gi_suspended |
| 40 |
g = gisuspended() |
| 41 |
assert not g.gi_suspended |
| 42 |
next(g) |
| 43 |
assert g.gi_suspended |
| 44 |
with raises(StopIteration): |
| 45 |
next(g) |
| 46 |
assert not g.gi_suspended |
| 47 |
assert g.gi_frame is None |
| 48 |
|
| 49 |
def test_generator3(): |
| 50 |
def f(): |
| 51 |
yield 1 |
| 52 |
g = f() |
| 53 |
assert list(g) == [1] |
| 54 |
|
| 55 |
def test_generator4(): |
| 56 |
def f(): |
| 57 |
yield 1 |
| 58 |
g = f() |
| 59 |
assert [x for x in g] == [1] |
| 60 |
|
| 61 |
def test_generator5(): |
| 62 |
def f(): |
| 63 |
v = (yield) |
| 64 |
yield v |
| 65 |
g = f() |
| 66 |
next(g) |
| 67 |
assert g.send(42) == 42 |
| 68 |
|
| 69 |
def test_throw1(): |
| 70 |
def f(): |
| 71 |
yield 2 |
| 72 |
g = f() |
| 73 |
|
| 74 |
with raises(NameError): |
| 75 |
g.throw(NameError, "Error") |
| 76 |
|
| 77 |
def test_throw2(): |
| 78 |
def f(): |
| 79 |
yield 2 |
| 80 |
g = f() |
| 81 |
|
| 82 |
with raises(NameError): |
| 83 |
g.throw(NameError("Error")) |
| 84 |
|
| 85 |
def test_throw3(): |
| 86 |
def f(): |
| 87 |
try: |
| 88 |
yield 1 |
| 89 |
yield 2 |
| 90 |
except NameError: |
| 91 |
yield 3 |
| 92 |
g = f() |
| 93 |
assert next(g) == 1 |
| 94 |
assert g.throw(NameError("Error")) == 3 |
| 95 |
with raises(StopIteration): |
| 96 |
next(g) |
| 97 |
|
| 98 |
def test_throw4(): |
| 99 |
def f(): |
| 100 |
try: |
| 101 |
yield 1 |
| 102 |
v = (yield 2) |
| 103 |
except NameError: |
| 104 |
yield 3 |
| 105 |
g = f() |
| 106 |
assert next(g) == 1 |
| 107 |
assert next(g) == 2 |
| 108 |
assert g.throw(NameError("Error")) == 3 |
| 109 |
with raises(StopIteration): |
| 110 |
next(g) |
| 111 |
|
| 112 |
def test_throw5(): |
| 113 |
def f(): |
| 114 |
try: |
| 115 |
yield 1 |
| 116 |
except Exception: |
| 117 |
x = 3 |
| 118 |
try: |
| 119 |
yield x |
| 120 |
except Exception: |
| 121 |
pass |
| 122 |
g = f() |
| 123 |
next(g) |
| 124 |
|
| 125 |
with raises(TypeError): |
| 126 |
g.throw("Error") |
| 127 |
assert g.throw(Exception) == 3 |
| 128 |
with raises(StopIteration): |
| 129 |
g.throw(Exception) |
| 130 |
|
| 131 |
def test_throw6(): |
| 132 |
def f(): |
| 133 |
yield 2 |
| 134 |
g = f() |
| 135 |
with raises(NameError): |
| 136 |
g.throw(NameError, "Error", None) |
| 137 |
|
| 138 |
|
| 139 |
def test_throw_fail(): |
| 140 |
def f(): |
| 141 |
yield 1 |
| 142 |
g = f() |
| 143 |
with raises(TypeError): |
| 144 |
g.throw(NameError("Error"), "error") |
| 145 |
|
| 146 |
def test_throw_fail2(): |
| 147 |
def f(): |
| 148 |
yield 1 |
| 149 |
g = f() |
| 150 |
with raises(TypeError): |
| 151 |
g.throw(list()) |
| 152 |
|
| 153 |
def test_throw_fail3(): |
| 154 |
def f(): |
| 155 |
yield 1 |
| 156 |
g = f() |
| 157 |
with raises(TypeError): |
| 158 |
g.throw(NameError("Error"), None, "not tb object") |
| 159 |
|
| 160 |
def test_throw_finishes_generator(): |
| 161 |
def f(): |
| 162 |
yield 1 |
| 163 |
g = f() |
| 164 |
assert g.gi_frame is not None |
| 165 |
with raises(ValueError): |
| 166 |
g.throw(ValueError) |
| 167 |
assert g.gi_frame is None |
| 168 |
|
| 169 |
def test_throw_bug(): |
| 170 |
def f(): |
| 171 |
try: |
| 172 |
x.throw(IndexError) |
| 173 |
except ValueError: |
| 174 |
yield 1 |
| 175 |
x = f() |
| 176 |
res = list(x) |
| 177 |
assert res == [1] |
| 178 |
|
| 179 |
def test_throw_on_finished_generator(): |
| 180 |
def f(): |
| 181 |
yield 1 |
| 182 |
g = f() |
| 183 |
res = next(g) |
| 184 |
assert res == 1 |
| 185 |
with raises(StopIteration): |
| 186 |
next(g) |
| 187 |
with raises(NameError): |
| 188 |
g.throw(NameError) |
| 189 |
|
| 190 |
def test_throw_tb(): |
| 191 |
def f(): |
| 192 |
try: |
| 193 |
yield |
| 194 |
except ZeroDivisionError: |
| 195 |
raise |
| 196 |
g = f() |
| 197 |
try: |
| 198 |
1 / 0 |
| 199 |
except ZeroDivisionError as v: |
| 200 |
try: |
| 201 |
g.throw(v) |
| 202 |
except Exception as w: |
| 203 |
tb = w.__traceback__ |
| 204 |
levels = 0 |
| 205 |
while tb: |
| 206 |
levels += 1 |
| 207 |
tb = tb.tb_next |
| 208 |
assert levels == 3 |
| 209 |
|
| 210 |
def test_throw_context(): |
| 211 |
|
| 212 |
def gen(): |
| 213 |
try: |
| 214 |
yield |
| 215 |
except Exception: |
| 216 |
raise ValueError |
| 217 |
|
| 218 |
try: |
| 219 |
raise KeyError |
| 220 |
except KeyError: |
| 221 |
g = gen() |
| 222 |
next(g) |
| 223 |
exc1 = Exception(1) |
| 224 |
exc2 = Exception(2) |
| 225 |
exc2.__context__ = exc1 |
| 226 |
try: |
| 227 |
g.throw(exc2) |
| 228 |
except ValueError: |
| 229 |
assert exc2.__context__ is exc1 |
| 230 |
|
| 231 |
def test_throw_bad_new_delivered_to_generator(): |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
class E(Exception): |
| 236 |
def __new__(cls, *args, **kwargs): |
| 237 |
return cls |
| 238 |
|
| 239 |
|
| 240 |
def boring_generator(): |
| 241 |
yield |
| 242 |
|
| 243 |
gen = boring_generator() |
| 244 |
with raises(TypeError, match='should have returned an instance of BaseException'): |
| 245 |
gen.throw(E) |
| 246 |
|
| 247 |
with raises(StopIteration): |
| 248 |
next(gen) |
| 249 |
|
| 250 |
|
| 251 |
caught = [] |
| 252 |
|
| 253 |
def catching_generator(): |
| 254 |
try: |
| 255 |
yield |
| 256 |
except TypeError as exc: |
| 257 |
caught.append(str(exc)) |
| 258 |
|
| 259 |
gen = catching_generator() |
| 260 |
next(gen) |
| 261 |
|
| 262 |
|
| 263 |
with raises(StopIteration): |
| 264 |
gen.throw(E) |
| 265 |
assert len(caught) == 1 |
| 266 |
assert 'should have returned an instance of BaseException' in caught[0] |
| 267 |
|
| 268 |
|
| 269 |
def test_throw_invalid_args_do_not_kill_generator(): |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
results = [] |
| 275 |
|
| 276 |
def f(): |
| 277 |
while True: |
| 278 |
try: |
| 279 |
results.append((yield)) |
| 280 |
except ValueError as v: |
| 281 |
results.append('caught:%s' % str(v)) |
| 282 |
|
| 283 |
g = f() |
| 284 |
next(g) |
| 285 |
|
| 286 |
|
| 287 |
with raises(TypeError, match='instance exception may not have a separate value'): |
| 288 |
g.throw(ValueError(1), "bad") |
| 289 |
|
| 290 |
|
| 291 |
assert g.gi_frame is not None |
| 292 |
|
| 293 |
|
| 294 |
g.throw(ValueError("ok")) |
| 295 |
assert results == ['caught:ok'] |
| 296 |
|
| 297 |
|
| 298 |
with raises(TypeError, match='throw\\(\\) third argument must be a traceback object'): |
| 299 |
g.throw(ValueError, "foo", 23) |
| 300 |
|
| 301 |
assert g.gi_frame is not None |
| 302 |
|
| 303 |
|
| 304 |
try: |
| 305 |
raise ValueError |
| 306 |
except Exception: |
| 307 |
g.throw(*sys.exc_info()) |
| 308 |
assert results == ['caught:ok', 'caught:'] |
| 309 |
|
| 310 |
|
| 311 |
g.send(42) |
| 312 |
assert results == ['caught:ok', 'caught:', 42] |
| 313 |
|
| 314 |
|
| 315 |
def test_close(): |
| 316 |
def f(): |
| 317 |
yield 1 |
| 318 |
g = f() |
| 319 |
assert g.close() is None |
| 320 |
|
| 321 |
def test_close3(): |
| 322 |
def f(): |
| 323 |
try: |
| 324 |
yield 1 |
| 325 |
except GeneratorExit: |
| 326 |
raise NameError |
| 327 |
g = f() |
| 328 |
next(g) |
| 329 |
with raises(NameError): |
| 330 |
g.close() |
| 331 |
|
| 332 |
def test_close_fail(): |
| 333 |
def f(): |
| 334 |
try: |
| 335 |
yield 1 |
| 336 |
except GeneratorExit: |
| 337 |
yield 2 |
| 338 |
g = f() |
| 339 |
next(g) |
| 340 |
with raises(RuntimeError): |
| 341 |
g.close() |
| 342 |
|
| 343 |
def test_close_on_collect(): |
| 344 |
import gc |
| 345 |
def f(): |
| 346 |
try: |
| 347 |
yield |
| 348 |
finally: |
| 349 |
f.x = 42 |
| 350 |
g = f() |
| 351 |
next(g) |
| 352 |
del g |
| 353 |
gc.collect() |
| 354 |
assert f.x == 42 |
| 355 |
|
| 356 |
def test_generator_raises_typeerror(): |
| 357 |
def f(): |
| 358 |
yield 1 |
| 359 |
g = f() |
| 360 |
with raises(TypeError): |
| 361 |
g.send() |
| 362 |
with raises(TypeError): |
| 363 |
g.send(1) |
| 364 |
|
| 365 |
def test_generator_restart(): |
| 366 |
def g(): |
| 367 |
i = next(me) |
| 368 |
yield i |
| 369 |
me = g() |
| 370 |
with raises(ValueError): |
| 371 |
next(me) |
| 372 |
|
| 373 |
def test_generator_expression(): |
| 374 |
d = {} |
| 375 |
exec("res = sum(i*i for i in range(5))", d, d) |
| 376 |
assert d['res'] == 30 |
| 377 |
|
| 378 |
def test_generator_expression_2(): |
| 379 |
def f(): |
| 380 |
total = sum(i for i in [x for x in z]) |
| 381 |
return total |
| 382 |
z = [1, 2, 7] |
| 383 |
assert f() == 10 |
| 384 |
|
| 385 |
def test_repr(): |
| 386 |
def myFunc(): |
| 387 |
yield 1 |
| 388 |
g = myFunc() |
| 389 |
r = repr(g) |
| 390 |
assert r.startswith("<generator object test_repr.<locals>.myFunc at 0x") |
| 391 |
assert list(g) == [1] |
| 392 |
assert repr(g) == r |
| 393 |
|
| 394 |
def test_unpackiterable_gen(): |
| 395 |
g = (i * i for i in range(-5, 3)) |
| 396 |
assert set(g) == set([0, 1, 4, 9, 16, 25]) |
| 397 |
assert set(g) == set() |
| 398 |
assert set(i for i in range(0)) == set() |
| 399 |
|
| 400 |
def test_exception_is_cleared_by_yield(): |
| 401 |
def f(): |
| 402 |
try: |
| 403 |
foobar |
| 404 |
except NameError: |
| 405 |
yield 5 |
| 406 |
raise |
| 407 |
gen = f() |
| 408 |
next(gen) |
| 409 |
try: |
| 410 |
next(gen) |
| 411 |
except NameError: |
| 412 |
pass |
| 413 |
|
| 414 |
def test_yield_return(): |
| 415 |
def f(): |
| 416 |
yield 1 |
| 417 |
return 2 |
| 418 |
g = f() |
| 419 |
assert next(g) == 1 |
| 420 |
try: |
| 421 |
next(g) |
| 422 |
except StopIteration as e: |
| 423 |
assert e.value == 2 |
| 424 |
else: |
| 425 |
assert False, 'Expected StopIteration' |
| 426 |
|
| 427 |
def test_yield_from_basic(): |
| 428 |
def f1(): |
| 429 |
yield from [] |
| 430 |
yield from [1, 2, 3] |
| 431 |
yield from f2() |
| 432 |
def f2(): |
| 433 |
yield 4 |
| 434 |
yield 5 |
| 435 |
gen = f1() |
| 436 |
assert next(gen) == 1 |
| 437 |
assert next(gen) == 2 |
| 438 |
assert next(gen) == 3 |
| 439 |
assert next(gen) == 4 |
| 440 |
assert next(gen) == 5 |
| 441 |
assert list(gen) == [] |
| 442 |
|
| 443 |
def test_yield_from_return(): |
| 444 |
def f1(): |
| 445 |
result = yield from f2() |
| 446 |
return result |
| 447 |
def f2(): |
| 448 |
yield 1 |
| 449 |
return 2 |
| 450 |
g = f1() |
| 451 |
assert next(g) == 1 |
| 452 |
try: |
| 453 |
next(g) |
| 454 |
except StopIteration as e: |
| 455 |
assert e.value == 2 |
| 456 |
else: |
| 457 |
assert False, 'Expected StopIteration' |
| 458 |
|
| 459 |
def test_yield_from_return_tuple(): |
| 460 |
def f1(): |
| 461 |
result = yield from f2() |
| 462 |
return result |
| 463 |
def f2(): |
| 464 |
yield 1 |
| 465 |
return (1, 2) |
| 466 |
g = f1() |
| 467 |
assert next(g) == 1 |
| 468 |
try: |
| 469 |
next(g) |
| 470 |
except StopIteration as e: |
| 471 |
assert e.value == (1, 2) |
| 472 |
else: |
| 473 |
assert False, 'Expected StopIteration' |
| 474 |
|
| 475 |
def test_set_name_qualname(): |
| 476 |
class A: |
| 477 |
def f(self): |
| 478 |
yield 5 |
| 479 |
g = A().f() |
| 480 |
assert g.__name__ == "f" |
| 481 |
assert g.__qualname__ == "test_set_name_qualname.<locals>.A.f" |
| 482 |
g.__name__ = "h.i" |
| 483 |
g.__qualname__ = "j.k" |
| 484 |
assert g.__name__ == "h.i" |
| 485 |
assert g.__qualname__ == "j.k" |
| 486 |
with raises(TypeError): |
| 487 |
g.__name__ = 42 |
| 488 |
with raises(TypeError): |
| 489 |
g.__qualname__ = 42 |
| 490 |
with raises((TypeError, AttributeError)): |
| 491 |
del g.__name__ |
| 492 |
with raises((TypeError, AttributeError)): |
| 493 |
del g.__qualname__ |
| 494 |
|
| 495 |
def test_gi_yieldfrom(): |
| 496 |
def g(x): |
| 497 |
assert gen.gi_yieldfrom is None |
| 498 |
yield x |
| 499 |
assert gen.gi_yieldfrom is None |
| 500 |
def f(x): |
| 501 |
assert gen.gi_yieldfrom is None |
| 502 |
yield from g(x) |
| 503 |
assert gen.gi_yieldfrom is None |
| 504 |
yield 42 |
| 505 |
assert gen.gi_yieldfrom is None |
| 506 |
gen = f(5) |
| 507 |
assert gen.gi_yieldfrom is None |
| 508 |
assert next(gen) == 5 |
| 509 |
assert gen.gi_yieldfrom.__name__ == 'g' |
| 510 |
assert next(gen) == 42 |
| 511 |
assert gen.gi_yieldfrom is None |
| 512 |
|
| 513 |
def test_gi_running_in_throw_generatorexit(): |
| 514 |
|
| 515 |
|
| 516 |
class A: |
| 517 |
def __iter__(self): |
| 518 |
return self |
| 519 |
def __next__(self): |
| 520 |
return 42 |
| 521 |
def close(self): |
| 522 |
closed.append(gen.gi_running) |
| 523 |
def g(): |
| 524 |
yield from A() |
| 525 |
gen = g() |
| 526 |
assert next(gen) == 42 |
| 527 |
closed = [] |
| 528 |
with raises(GeneratorExit): |
| 529 |
gen.throw(GeneratorExit) |
| 530 |
assert closed == [True] |
| 531 |
|
| 532 |
def test_exc_info_in_generator(): |
| 533 |
def g(): |
| 534 |
try: |
| 535 |
raise ValueError |
| 536 |
except ValueError: |
| 537 |
yield sys.exc_info()[0] |
| 538 |
yield sys.exc_info()[0] |
| 539 |
try: |
| 540 |
raise IndexError |
| 541 |
except IndexError: |
| 542 |
gen = g() |
| 543 |
assert sys.exc_info()[0] is IndexError |
| 544 |
assert next(gen) is ValueError |
| 545 |
assert sys.exc_info()[0] is IndexError |
| 546 |
assert next(gen) is ValueError |
| 547 |
assert sys.exc_info()[0] is IndexError |
| 548 |
with raises(StopIteration): |
| 549 |
next(gen) |
| 550 |
assert sys.exc_info()[0] is IndexError |
| 551 |
|
| 552 |
def test_exc_info_in_generator_2(): |
| 553 |
import sys |
| 554 |
def g(): |
| 555 |
yield sys.exc_info()[0] |
| 556 |
try: |
| 557 |
raise LookupError |
| 558 |
except LookupError: |
| 559 |
yield sys.exc_info()[0] |
| 560 |
yield sys.exc_info()[0] |
| 561 |
try: |
| 562 |
raise IndexError |
| 563 |
except IndexError: |
| 564 |
gen = g() |
| 565 |
try: |
| 566 |
raise ValueError |
| 567 |
except ValueError: |
| 568 |
assert next(gen) is ValueError |
| 569 |
assert next(gen) is LookupError |
| 570 |
assert next(gen) is ValueError |
| 571 |
|
| 572 |
def test_exc_info_in_generator_3(): |
| 573 |
import sys |
| 574 |
def g(): |
| 575 |
yield sys.exc_info()[0] |
| 576 |
yield sys.exc_info()[0] |
| 577 |
yield sys.exc_info()[0] |
| 578 |
gen = g() |
| 579 |
try: |
| 580 |
raise IndexError |
| 581 |
except IndexError: |
| 582 |
assert next(gen) is IndexError |
| 583 |
assert next(gen) is None |
| 584 |
try: |
| 585 |
raise ValueError |
| 586 |
except ValueError: |
| 587 |
assert next(gen) is ValueError |
| 588 |
|
| 589 |
def test_exc_info_in_generator_4(): |
| 590 |
import sys |
| 591 |
def g(): |
| 592 |
assert sys.exc_info()[0] == IndexError |
| 593 |
try: |
| 594 |
raise ValueError |
| 595 |
except ValueError: |
| 596 |
assert sys.exc_info()[0] == ValueError |
| 597 |
yield 1 |
| 598 |
assert sys.exc_info()[0] == ValueError |
| 599 |
assert sys.exc_info() == (None, None, None) |
| 600 |
yield 2 |
| 601 |
gen = g() |
| 602 |
try: |
| 603 |
raise IndexError |
| 604 |
except IndexError: |
| 605 |
assert sys.exc_info()[0] == IndexError |
| 606 |
assert next(gen) == 1 |
| 607 |
assert sys.exc_info()[0] == IndexError |
| 608 |
assert sys.exc_info() == (None, None, None) |
| 609 |
assert next(gen) == 2 |
| 610 |
assert sys.exc_info() == (None, None, None) |
| 611 |
|
| 612 |
def test_except_gen_except(): |
| 613 |
def gen(): |
| 614 |
try: |
| 615 |
assert sys.exc_info()[0] is None |
| 616 |
yield |
| 617 |
|
| 618 |
|
| 619 |
raise TypeError() |
| 620 |
except TypeError as exc: |
| 621 |
assert sys.exc_info()[0] is TypeError |
| 622 |
assert type(exc.__context__) is ValueError |
| 623 |
|
| 624 |
assert sys.exc_info()[0] is ValueError |
| 625 |
yield |
| 626 |
assert sys.exc_info()[0] is None |
| 627 |
yield "done" |
| 628 |
|
| 629 |
g = gen() |
| 630 |
next(g) |
| 631 |
try: |
| 632 |
raise ValueError |
| 633 |
except Exception: |
| 634 |
next(g) |
| 635 |
|
| 636 |
assert next(g) == "done" |
| 637 |
assert sys.exc_info() == (None, None, None) |
| 638 |
|
| 639 |
def test_multiple_invalid_sends(): |
| 640 |
def mygen(): |
| 641 |
yield 42 |
| 642 |
g = mygen() |
| 643 |
with raises(TypeError): |
| 644 |
g.send(2) |
| 645 |
with raises(TypeError): |
| 646 |
g.send(2) |
| 647 |
|
| 648 |
def test_delegating_close(): |
| 649 |
""" |
| 650 |
Test delegating 'close' |
| 651 |
""" |
| 652 |
trace = [] |
| 653 |
def g1(): |
| 654 |
try: |
| 655 |
trace.append("Starting g1") |
| 656 |
yield "g1 ham" |
| 657 |
yield from g2() |
| 658 |
yield "g1 eggs" |
| 659 |
finally: |
| 660 |
trace.append("Finishing g1") |
| 661 |
def g2(): |
| 662 |
try: |
| 663 |
trace.append("Starting g2") |
| 664 |
yield "g2 spam" |
| 665 |
yield "g2 more spam" |
| 666 |
finally: |
| 667 |
trace.append("Finishing g2") |
| 668 |
g = g1() |
| 669 |
for i in range(2): |
| 670 |
x = next(g) |
| 671 |
trace.append("Yielded %s" % (x,)) |
| 672 |
g.close() |
| 673 |
assert trace == [ |
| 674 |
"Starting g1", |
| 675 |
"Yielded g1 ham", |
| 676 |
"Starting g2", |
| 677 |
"Yielded g2 spam", |
| 678 |
"Finishing g2", |
| 679 |
"Finishing g1" |
| 680 |
] |
| 681 |
|
| 682 |
def test_handing_exception_while_delegating_close(): |
| 683 |
""" |
| 684 |
Test handling exception while delegating 'close' |
| 685 |
""" |
| 686 |
trace = [] |
| 687 |
def g1(): |
| 688 |
try: |
| 689 |
trace.append("Starting g1") |
| 690 |
yield "g1 ham" |
| 691 |
yield from g2() |
| 692 |
yield "g1 eggs" |
| 693 |
finally: |
| 694 |
trace.append("Finishing g1") |
| 695 |
def g2(): |
| 696 |
try: |
| 697 |
trace.append("Starting g2") |
| 698 |
yield "g2 spam" |
| 699 |
yield "g2 more spam" |
| 700 |
finally: |
| 701 |
trace.append("Finishing g2") |
| 702 |
raise ValueError("nybbles have exploded with delight") |
| 703 |
g = g1() |
| 704 |
for i in range(2): |
| 705 |
x = next(g) |
| 706 |
trace.append("Yielded %s" % (x,)) |
| 707 |
with raises(ValueError) as excinfo: |
| 708 |
g.close() |
| 709 |
assert excinfo.value.args[0] == "nybbles have exploded with delight" |
| 710 |
assert isinstance(excinfo.value.__context__, GeneratorExit) |
| 711 |
assert trace == [ |
| 712 |
"Starting g1", |
| 713 |
"Yielded g1 ham", |
| 714 |
"Starting g2", |
| 715 |
"Yielded g2 spam", |
| 716 |
"Finishing g2", |
| 717 |
"Finishing g1", |
| 718 |
] |
| 719 |
|
| 720 |
def test_delegating_throw(): |
| 721 |
""" |
| 722 |
Test delegating 'throw' |
| 723 |
""" |
| 724 |
trace = [] |
| 725 |
def g1(): |
| 726 |
try: |
| 727 |
trace.append("Starting g1") |
| 728 |
yield "g1 ham" |
| 729 |
yield from g2() |
| 730 |
yield "g1 eggs" |
| 731 |
finally: |
| 732 |
trace.append("Finishing g1") |
| 733 |
def g2(): |
| 734 |
try: |
| 735 |
trace.append("Starting g2") |
| 736 |
yield "g2 spam" |
| 737 |
yield "g2 more spam" |
| 738 |
finally: |
| 739 |
trace.append("Finishing g2") |
| 740 |
g = g1() |
| 741 |
for i in range(2): |
| 742 |
x = next(g) |
| 743 |
trace.append("Yielded %s" % (x,)) |
| 744 |
e = ValueError("tomato ejected") |
| 745 |
with raises(ValueError) as excinfo: |
| 746 |
g.throw(e) |
| 747 |
assert excinfo.value.args[0] == "tomato ejected" |
| 748 |
assert trace == [ |
| 749 |
"Starting g1", |
| 750 |
"Yielded g1 ham", |
| 751 |
"Starting g2", |
| 752 |
"Yielded g2 spam", |
| 753 |
"Finishing g2", |
| 754 |
"Finishing g1", |
| 755 |
] |
| 756 |
|
| 757 |
def test_delegating_throw_to_non_generator(): |
| 758 |
""" |
| 759 |
Test delegating 'throw' to non-generator |
| 760 |
""" |
| 761 |
trace = [] |
| 762 |
def g(): |
| 763 |
try: |
| 764 |
trace.append("Starting g") |
| 765 |
yield from range(10) |
| 766 |
finally: |
| 767 |
trace.append("Finishing g") |
| 768 |
gi = g() |
| 769 |
for i in range(5): |
| 770 |
x = next(gi) |
| 771 |
trace.append("Yielded %s" % (x,)) |
| 772 |
with raises(ValueError) as excinfo: |
| 773 |
gi.throw(ValueError("tomato ejected")) |
| 774 |
assert excinfo.value.args[0] == "tomato ejected" |
| 775 |
assert trace == [ |
| 776 |
"Starting g", |
| 777 |
"Yielded 0", |
| 778 |
"Yielded 1", |
| 779 |
"Yielded 2", |
| 780 |
"Yielded 3", |
| 781 |
"Yielded 4", |
| 782 |
"Finishing g", |
| 783 |
] |
| 784 |
|
| 785 |
def test_broken_getattr_handling(): |
| 786 |
""" |
| 787 |
Test subiterator with a broken getattr implementation |
| 788 |
""" |
| 789 |
import _io, sys |
| 790 |
class Broken: |
| 791 |
def __iter__(self): |
| 792 |
return self |
| 793 |
def __next__(self): |
| 794 |
return 1 |
| 795 |
def __getattr__(self, attr): |
| 796 |
1 / 0 |
| 797 |
|
| 798 |
def g(): |
| 799 |
yield from Broken() |
| 800 |
|
| 801 |
gi = g() |
| 802 |
assert next(gi) == 1 |
| 803 |
with raises(ZeroDivisionError): |
| 804 |
gi.send(1) |
| 805 |
|
| 806 |
gi = g() |
| 807 |
assert next(gi) == 1 |
| 808 |
with raises(ZeroDivisionError): |
| 809 |
gi.throw(RuntimeError) |
| 810 |
|
| 811 |
gi = g() |
| 812 |
assert next(gi) == 1 |
| 813 |
sys.stderr = _io.StringIO() |
| 814 |
unraisables = [] |
| 815 |
def ownhook(hookargs): |
| 816 |
unraisables.append(hookargs) |
| 817 |
oldhook = sys.unraisablehook |
| 818 |
sys.unraisablehook = ownhook |
| 819 |
try: |
| 820 |
gi.close() |
| 821 |
finally: |
| 822 |
sys.unraisablehook = oldhook |
| 823 |
assert len(unraisables) == 1 |
| 824 |
assert isinstance(unraisables[0].exc_value, ZeroDivisionError) |
| 825 |
|
| 826 |
def test_returning_value_from_delegated_throw(): |
| 827 |
""" |
| 828 |
Test returning value from delegated 'throw' |
| 829 |
""" |
| 830 |
trace = [] |
| 831 |
class LunchError(Exception): |
| 832 |
pass |
| 833 |
def g1(): |
| 834 |
try: |
| 835 |
trace.append("Starting g1") |
| 836 |
yield "g1 ham" |
| 837 |
yield from g2() |
| 838 |
yield "g1 eggs" |
| 839 |
finally: |
| 840 |
trace.append("Finishing g1") |
| 841 |
def g2(): |
| 842 |
try: |
| 843 |
trace.append("Starting g2") |
| 844 |
yield "g2 spam" |
| 845 |
yield "g2 more spam" |
| 846 |
except LunchError: |
| 847 |
trace.append("Caught LunchError in g2") |
| 848 |
yield "g2 lunch saved" |
| 849 |
yield "g2 yet more spam" |
| 850 |
g = g1() |
| 851 |
for i in range(2): |
| 852 |
x = next(g) |
| 853 |
trace.append("Yielded %s" % (x,)) |
| 854 |
e = LunchError("tomato ejected") |
| 855 |
g.throw(e) |
| 856 |
for x in g: |
| 857 |
trace.append("Yielded %s" % (x,)) |
| 858 |
assert trace == [ |
| 859 |
"Starting g1", |
| 860 |
"Yielded g1 ham", |
| 861 |
"Starting g2", |
| 862 |
"Yielded g2 spam", |
| 863 |
"Caught LunchError in g2", |
| 864 |
"Yielded g2 yet more spam", |
| 865 |
"Yielded g1 eggs", |
| 866 |
"Finishing g1", |
| 867 |
] |
| 868 |
|
| 869 |
def test_catching_exception_from_subgen_and_returning(): |
| 870 |
""" |
| 871 |
Test catching an exception thrown into a |
| 872 |
subgenerator and returning a value |
| 873 |
""" |
| 874 |
trace = [] |
| 875 |
def inner(): |
| 876 |
try: |
| 877 |
yield 1 |
| 878 |
except ValueError: |
| 879 |
trace.append("inner caught ValueError") |
| 880 |
return 2 |
| 881 |
|
| 882 |
def outer(): |
| 883 |
v = yield from inner() |
| 884 |
trace.append("inner returned %r to outer" % v) |
| 885 |
yield v |
| 886 |
g = outer() |
| 887 |
trace.append(next(g)) |
| 888 |
trace.append(g.throw(ValueError)) |
| 889 |
assert trace == [ |
| 890 |
1, |
| 891 |
"inner caught ValueError", |
| 892 |
"inner returned 2 to outer", |
| 893 |
2, |
| 894 |
] |
| 895 |
|
| 896 |
def test_exception_context(): |
| 897 |
import operator |
| 898 |
def f(): |
| 899 |
try: |
| 900 |
raise ValueError |
| 901 |
except ValueError: |
| 902 |
yield from map(operator.truediv, [2, 3], [4, 0]) |
| 903 |
gen = f() |
| 904 |
assert next(gen) == 0.5 |
| 905 |
with raises(ZeroDivisionError) as excinfo: |
| 906 |
next(gen) |
| 907 |
assert isinstance(excinfo.value.__context__, ValueError) |
| 908 |
|
| 909 |
|
| 910 |
def test_stopiteration_turned_into_runtime_error(): |
| 911 |
def badgenerator(x): |
| 912 |
if x == 5: |
| 913 |
raise StopIteration |
| 914 |
yield x |
| 915 |
with raises(RuntimeError): |
| 916 |
next(badgenerator(5)) |
| 917 |
|
| 918 |
def test_stopiteration_can_be_caught(): |
| 919 |
def g(): |
| 920 |
raise StopIteration |
| 921 |
def finegenerator(x): |
| 922 |
yield x |
| 923 |
if x == 5: |
| 924 |
try: |
| 925 |
g() |
| 926 |
except StopIteration: |
| 927 |
pass |
| 928 |
yield x |
| 929 |
gen = finegenerator(5) |
| 930 |
next(gen) |
| 931 |
next(gen) |
| 932 |
|
| 933 |
def test_generator_stop_cause(): |
| 934 |
def gen1(): |
| 935 |
yield 42 |
| 936 |
my_gen = gen1() |
| 937 |
assert next(my_gen) == 42 |
| 938 |
stop_exc = StopIteration('spam') |
| 939 |
with raises(RuntimeError) as e: |
| 940 |
my_gen.throw(StopIteration, stop_exc, None) |
| 941 |
assert e.value.__cause__ is stop_exc |
| 942 |
assert e.value.__context__ is stop_exc |
| 943 |
|
| 944 |
def test_return_tuple(): |
| 945 |
def gen1(): |
| 946 |
return (yield 1) |
| 947 |
gen = gen1() |
| 948 |
assert next(gen) == 1 |
| 949 |
with raises(StopIteration) as excinfo: |
| 950 |
gen.send((2,)) |
| 951 |
assert excinfo.value.value == (2,) |
| 952 |
|
| 953 |
def test_return_stopiteration(): |
| 954 |
def gen1(): |
| 955 |
return (yield 1) |
| 956 |
gen = gen1() |
| 957 |
assert next(gen) == 1 |
| 958 |
with raises(StopIteration) as excinfo: |
| 959 |
gen.send(StopIteration(2)) |
| 960 |
assert isinstance(excinfo.value, StopIteration) |
| 961 |
assert excinfo.value.value.value == 2 |
| 962 |
|
| 963 |
def test_list_building_wrong_exception(): |
| 964 |
def gen1(): |
| 965 |
raise ValueError |
| 966 |
yield 2 |
| 967 |
with raises(ValueError): |
| 968 |
[*gen1()] |
| 969 |
|
| 970 |
def gen1(): |
| 971 |
raise ValueError |
| 972 |
yield from [1, 2, 3] |
| 973 |
with raises(ValueError): |
| 974 |
[*gen1()] |
| 975 |
|
| 976 |
def test_throw_context_set_from_active_exception(): |
| 977 |
|
| 978 |
|
| 979 |
def f(): |
| 980 |
try: |
| 981 |
raise KeyError('a') |
| 982 |
except Exception: |
| 983 |
yield |
| 984 |
|
| 985 |
gen = f() |
| 986 |
gen.send(None) |
| 987 |
with raises(ValueError) as cm: |
| 988 |
gen.throw(ValueError) |
| 989 |
context = cm.value.__context__ |
| 990 |
assert type(context) is KeyError |
| 991 |
assert context.args == ('a',) |
| 992 |
|
| 993 |
def test_throw_context_set_inside_generator(): |
| 994 |
|
| 995 |
results = [] |
| 996 |
def f(): |
| 997 |
try: |
| 998 |
raise KeyError('a') |
| 999 |
except Exception: |
| 1000 |
try: |
| 1001 |
yield |
| 1002 |
except Exception as exc: |
| 1003 |
results.append((type(exc.__context__), exc.__context__.args)) |
| 1004 |
yield 'done' |
| 1005 |
|
| 1006 |
gen = f() |
| 1007 |
gen.send(None) |
| 1008 |
val = gen.throw(ValueError) |
| 1009 |
assert val == 'done' |
| 1010 |
assert results == [(KeyError, ('a',))] |
| 1011 |
|
| 1012 |
def test_throw_non_exception_error_message(): |
| 1013 |
|
| 1014 |
|
| 1015 |
def f(): |
| 1016 |
yield |
| 1017 |
g = f() |
| 1018 |
with raises(TypeError) as cm: |
| 1019 |
g.throw("abc") |
| 1020 |
assert "classes or instances deriving from BaseException" in str(cm.value) |
| 1021 |
assert "str" in str(cm.value) |
| 1022 |
|
| 1023 |
g2 = f() |
| 1024 |
with raises(TypeError) as cm2: |
| 1025 |
g2.throw(list) |
| 1026 |
assert "classes or instances deriving from BaseException" in str(cm2.value) |
| 1027 |
|
| 1028 |
def test_throw_bad_new_returns_non_instance(): |
| 1029 |
|
| 1030 |
|
| 1031 |
class E(Exception): |
| 1032 |
def __new__(cls, *args, **kwargs): |
| 1033 |
return cls |
| 1034 |
|
| 1035 |
def f(): |
| 1036 |
yield |
| 1037 |
|
| 1038 |
g = f() |
| 1039 |
with raises(TypeError) as cm: |
| 1040 |
g.throw(E) |
| 1041 |
assert "should have returned an instance of BaseException" in str(cm.value) |
| 1042 |
|
| 1043 |
def test_throw_bad_new_closes_generator(): |
| 1044 |
|
| 1045 |
|
| 1046 |
class E(Exception): |
| 1047 |
def __new__(cls, *args, **kwargs): |
| 1048 |
return cls |
| 1049 |
|
| 1050 |
def f(): |
| 1051 |
yield |
| 1052 |
|
| 1053 |
g = f() |
| 1054 |
with raises(TypeError): |
| 1055 |
g.throw(E) |
| 1056 |
with raises(StopIteration): |
| 1057 |
next(g) |
| 1058 |
|
| 1059 |
def test_generatorexit_unraisable_has_traceback(): |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
import gc |
| 1064 |
|
| 1065 |
def f(): |
| 1066 |
try: |
| 1067 |
yield |
| 1068 |
except GeneratorExit: |
| 1069 |
yield "ignored!" |
| 1070 |
|
| 1071 |
unraisables = [] |
| 1072 |
import sys |
| 1073 |
old_hook = sys.unraisablehook |
| 1074 |
def hook(info): |
| 1075 |
unraisables.append(info) |
| 1076 |
sys.unraisablehook = hook |
| 1077 |
try: |
| 1078 |
g = f() |
| 1079 |
next(g) |
| 1080 |
del g |
| 1081 |
gc.collect() |
| 1082 |
finally: |
| 1083 |
sys.unraisablehook = old_hook |
| 1084 |
|
| 1085 |
assert len(unraisables) == 1 |
| 1086 |
info = unraisables[0] |
| 1087 |
assert info.exc_type is RuntimeError |
| 1088 |
assert "generator ignored GeneratorExit" in str(info.exc_value) |
| 1089 |
assert info.exc_traceback is not None |