| 1 |
import pytest |
| 2 |
|
| 3 |
def test_arg_as_string(): |
| 4 |
with pytest.raises(TypeError): |
| 5 |
raise "test" |
| 6 |
|
| 7 |
def test_control_flow(): |
| 8 |
try: |
| 9 |
raise Exception |
| 10 |
raise AssertionError("exception failed to raise") |
| 11 |
except: |
| 12 |
pass |
| 13 |
else: |
| 14 |
raise AssertionError("exception executing else clause!") |
| 15 |
|
| 16 |
def test_store_exception(): |
| 17 |
try: |
| 18 |
raise ValueError |
| 19 |
except Exception as e: |
| 20 |
assert e |
| 21 |
|
| 22 |
|
| 23 |
def test_args(): |
| 24 |
try: |
| 25 |
raise SystemError(1, 2) |
| 26 |
except Exception as e: |
| 27 |
assert e.args[0] == 1 |
| 28 |
assert e.args[1] == 2 |
| 29 |
|
| 30 |
def test_builtin_exc(): |
| 31 |
try: |
| 32 |
[][0] |
| 33 |
except IndexError as e: |
| 34 |
assert isinstance(e, IndexError) |
| 35 |
|
| 36 |
def test_raise_cls(): |
| 37 |
with pytest.raises(IndexError): |
| 38 |
raise IndexError |
| 39 |
|
| 40 |
def test_raise_cls_catch(): |
| 41 |
def f(r): |
| 42 |
try: |
| 43 |
raise r |
| 44 |
except LookupError: |
| 45 |
return 1 |
| 46 |
with pytest.raises(Exception): |
| 47 |
f(Exception) |
| 48 |
assert f(IndexError) == 1 |
| 49 |
|
| 50 |
def test_raise_wrong(): |
| 51 |
try: |
| 52 |
raise 1 |
| 53 |
except TypeError: |
| 54 |
pass |
| 55 |
else: |
| 56 |
raise AssertionError("shouldn't be able to raise 1") |
| 57 |
|
| 58 |
def test_revert_exc_info_1(): |
| 59 |
import sys |
| 60 |
assert sys.exc_info() == (None, None, None) |
| 61 |
try: |
| 62 |
raise ValueError |
| 63 |
except: |
| 64 |
pass |
| 65 |
assert sys.exc_info() == (None, None, None) |
| 66 |
|
| 67 |
def test_revert_exc_info_2(): |
| 68 |
import sys |
| 69 |
assert sys.exc_info() == (None, None, None) |
| 70 |
try: |
| 71 |
raise ValueError |
| 72 |
except: |
| 73 |
try: |
| 74 |
raise IndexError |
| 75 |
except: |
| 76 |
assert sys.exc_info()[0] is IndexError |
| 77 |
assert sys.exc_info()[0] is ValueError |
| 78 |
assert sys.exc_info() == (None, None, None) |
| 79 |
|
| 80 |
def test_revert_exc_info_2_finally(): |
| 81 |
import sys, dis |
| 82 |
|
| 83 |
def the_test(): |
| 84 |
assert sys.exc_info() == (None, None, None) |
| 85 |
try: |
| 86 |
try: |
| 87 |
raise ValueError |
| 88 |
finally: |
| 89 |
try: |
| 90 |
try: |
| 91 |
raise IndexError |
| 92 |
finally: |
| 93 |
assert sys.exc_info()[0] is IndexError |
| 94 |
except IndexError: |
| 95 |
pass |
| 96 |
assert sys.exc_info()[0] is ValueError |
| 97 |
except ValueError: |
| 98 |
pass |
| 99 |
assert sys.exc_info() == (None, None, None) |
| 100 |
|
| 101 |
dis.dis(the_test) |
| 102 |
the_test() |
| 103 |
|
| 104 |
def test_reraise_1(): |
| 105 |
with pytest.raises(IndexError): |
| 106 |
import sys |
| 107 |
try: |
| 108 |
raise ValueError |
| 109 |
except: |
| 110 |
try: |
| 111 |
raise IndexError |
| 112 |
finally: |
| 113 |
assert sys.exc_info()[0] is IndexError |
| 114 |
raise |
| 115 |
|
| 116 |
|
| 117 |
def test_reraise_2(): |
| 118 |
with pytest.raises(IndexError): |
| 119 |
def foo(): |
| 120 |
import sys |
| 121 |
assert sys.exc_info()[0] is IndexError |
| 122 |
raise |
| 123 |
try: |
| 124 |
raise ValueError |
| 125 |
except: |
| 126 |
try: |
| 127 |
raise IndexError |
| 128 |
finally: |
| 129 |
foo() |
| 130 |
|
| 131 |
|
| 132 |
def test_reraise_3(): |
| 133 |
with pytest.raises(IndexError): |
| 134 |
def spam(): |
| 135 |
import sys |
| 136 |
try: |
| 137 |
raise KeyError |
| 138 |
except KeyError: |
| 139 |
pass |
| 140 |
assert sys.exc_info()[0] is IndexError |
| 141 |
try: |
| 142 |
raise ValueError |
| 143 |
except: |
| 144 |
try: |
| 145 |
raise IndexError |
| 146 |
finally: |
| 147 |
spam() |
| 148 |
|
| 149 |
def test_reraise_4(): |
| 150 |
import sys |
| 151 |
try: |
| 152 |
raise ValueError |
| 153 |
except: |
| 154 |
try: |
| 155 |
raise KeyError |
| 156 |
except: |
| 157 |
ok = sys.exc_info()[0] is KeyError |
| 158 |
assert ok |
| 159 |
|
| 160 |
def test_reraise_5(): |
| 161 |
with pytest.raises(IndexError): |
| 162 |
import sys |
| 163 |
try: |
| 164 |
raise ValueError |
| 165 |
except: |
| 166 |
some_traceback = sys.exc_info()[2] |
| 167 |
try: |
| 168 |
raise KeyError |
| 169 |
except: |
| 170 |
try: |
| 171 |
raise IndexError().with_traceback(some_traceback) |
| 172 |
finally: |
| 173 |
assert sys.exc_info()[0] is IndexError |
| 174 |
assert sys.exc_info()[2].tb_next is some_traceback |
| 175 |
|
| 176 |
def test_nested_reraise(): |
| 177 |
with pytest.raises(TypeError): |
| 178 |
def nested_reraise(): |
| 179 |
raise |
| 180 |
try: |
| 181 |
raise TypeError("foo") |
| 182 |
except: |
| 183 |
nested_reraise() |
| 184 |
|
| 185 |
|
| 186 |
def test_with_reraise_1(): |
| 187 |
class Context: |
| 188 |
def __enter__(self): |
| 189 |
return self |
| 190 |
def __exit__(self, exc_type, exc_value, exc_tb): |
| 191 |
return True |
| 192 |
|
| 193 |
def fn(): |
| 194 |
try: |
| 195 |
raise ValueError("foo") |
| 196 |
except: |
| 197 |
with Context(): |
| 198 |
pass |
| 199 |
raise |
| 200 |
with pytest.raises(ValueError): |
| 201 |
fn() |
| 202 |
|
| 203 |
def test_with_reraise_2(): |
| 204 |
import dis |
| 205 |
class Context: |
| 206 |
def __enter__(self): |
| 207 |
return self |
| 208 |
def __exit__(self, exc_type, exc_value, exc_tb): |
| 209 |
return True |
| 210 |
|
| 211 |
def the_test(): |
| 212 |
def fn(): |
| 213 |
try: |
| 214 |
raise ValueError("foo") |
| 215 |
except: |
| 216 |
with Context(): |
| 217 |
raise KeyError("caught") |
| 218 |
raise |
| 219 |
with pytest.raises(ValueError): |
| 220 |
fn() |
| 221 |
|
| 222 |
dis.dis(the_test) |
| 223 |
the_test() |
| 224 |
|
| 225 |
def test_userclass(): |
| 226 |
|
| 227 |
|
| 228 |
class A(object): |
| 229 |
def __init__(self, x=None): |
| 230 |
self.x = x |
| 231 |
|
| 232 |
with pytest.raises(TypeError): |
| 233 |
raise A |
| 234 |
with pytest.raises(TypeError): |
| 235 |
raise A(42) |
| 236 |
|
| 237 |
def test_userclass_catch(): |
| 238 |
|
| 239 |
class A(object): |
| 240 |
pass |
| 241 |
|
| 242 |
for exc in A, (ZeroDivisionError, A): |
| 243 |
try: |
| 244 |
try: |
| 245 |
1 / 0 |
| 246 |
except exc: |
| 247 |
pass |
| 248 |
except TypeError: |
| 249 |
pass |
| 250 |
else: |
| 251 |
fail('Expected TypeError') |
| 252 |
|
| 253 |
def test_it(): |
| 254 |
class C: |
| 255 |
pass |
| 256 |
|
| 257 |
try: |
| 258 |
{}[C] |
| 259 |
except KeyError: |
| 260 |
pass |
| 261 |
|
| 262 |
def test_catch_tuple(): |
| 263 |
class A(Exception): |
| 264 |
pass |
| 265 |
|
| 266 |
try: |
| 267 |
raise ValueError |
| 268 |
except (ValueError, A): |
| 269 |
pass |
| 270 |
else: |
| 271 |
fail("Did not raise") |
| 272 |
|
| 273 |
try: |
| 274 |
raise A() |
| 275 |
except (ValueError, A): |
| 276 |
pass |
| 277 |
else: |
| 278 |
fail("Did not raise") |
| 279 |
|
| 280 |
def test_obscure_bases(): |
| 281 |
|
| 282 |
e = ValueError() |
| 283 |
e.__bases__ = (5,) |
| 284 |
try: |
| 285 |
raise e |
| 286 |
except ValueError: |
| 287 |
pass |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
flag = False |
| 292 |
class metaclass(type): |
| 293 |
def __getattribute__(self, name): |
| 294 |
if flag and name == '__bases__': |
| 295 |
fail("someone read bases attr") |
| 296 |
else: |
| 297 |
return type.__getattribute__(self, name) |
| 298 |
class A(BaseException, metaclass=metaclass): |
| 299 |
pass |
| 300 |
try: |
| 301 |
a = A() |
| 302 |
flag = True |
| 303 |
raise a |
| 304 |
except A: |
| 305 |
pass |
| 306 |
|
| 307 |
def test_new_returns_bad_instance(): |
| 308 |
class MyException(Exception): |
| 309 |
def __new__(cls, *args): |
| 310 |
return object() |
| 311 |
|
| 312 |
with pytest.raises(TypeError): |
| 313 |
raise MyException |
| 314 |
|
| 315 |
def test_with_exit_True(): |
| 316 |
class X: |
| 317 |
def __enter__(self): |
| 318 |
pass |
| 319 |
def __exit__(self, *args): |
| 320 |
return True |
| 321 |
def g(): |
| 322 |
with X(): |
| 323 |
return 42 |
| 324 |
assert False, "unreachable" |
| 325 |
assert g() == 42 |
| 326 |
|
| 327 |
def _test_unraisable_error_in_repr(): |
| 328 |
|
| 329 |
class ReprError(): |
| 330 |
def __repr__(self): |
| 331 |
raise RuntimeError("Oh no!") |
| 332 |
try: |
| 333 |
[].index(ReprError()) |
| 334 |
except ValueError as e: |
| 335 |
assert str(e) == '<repr raised> is not in list' |
| 336 |
|
| 337 |
def test_pop_exception_value(): |
| 338 |
|
| 339 |
for i in range(10): |
| 340 |
try: |
| 341 |
raise ValueError |
| 342 |
except ValueError as e: |
| 343 |
continue |
| 344 |
|
| 345 |
def test_clear_last_exception_on_break(): |
| 346 |
import sys |
| 347 |
for i in [0]: |
| 348 |
try: |
| 349 |
raise ValueError |
| 350 |
except ValueError: |
| 351 |
break |
| 352 |
assert sys.exc_info() == (None, None, None) |
| 353 |
|
| 354 |
|
| 355 |
def test_instance_context(): |
| 356 |
context = IndexError() |
| 357 |
try: |
| 358 |
try: |
| 359 |
raise context |
| 360 |
except: |
| 361 |
raise OSError() |
| 362 |
except OSError as e: |
| 363 |
assert e.__context__ is context |
| 364 |
else: |
| 365 |
fail('No exception raised') |
| 366 |
|
| 367 |
def test_class_context(): |
| 368 |
context = IndexError |
| 369 |
try: |
| 370 |
try: |
| 371 |
raise context |
| 372 |
except: |
| 373 |
raise OSError() |
| 374 |
except OSError as e: |
| 375 |
assert e.__context__ != context |
| 376 |
assert isinstance(e.__context__, context) |
| 377 |
else: |
| 378 |
fail('No exception raised') |
| 379 |
|
| 380 |
def test_internal_exception(): |
| 381 |
try: |
| 382 |
try: |
| 383 |
1/0 |
| 384 |
except: |
| 385 |
xyzzy |
| 386 |
except NameError as e: |
| 387 |
assert isinstance(e.__context__, ZeroDivisionError) |
| 388 |
else: |
| 389 |
fail("No exception raised") |
| 390 |
|
| 391 |
def test_context_cycle_broken(): |
| 392 |
try: |
| 393 |
try: |
| 394 |
1/0 |
| 395 |
except ZeroDivisionError as e: |
| 396 |
raise e |
| 397 |
except ZeroDivisionError as e: |
| 398 |
assert e.__context__ is None |
| 399 |
else: |
| 400 |
fail("No exception raised") |
| 401 |
|
| 402 |
def test_context_preexisting_cycle(): |
| 403 |
def chain(e, i=0): |
| 404 |
res = Exception(i) |
| 405 |
res.__context__ = e |
| 406 |
return res |
| 407 |
def cycle(): |
| 408 |
try: |
| 409 |
raise ValueError(1) |
| 410 |
except ValueError as ex: |
| 411 |
start = curr = Exception() |
| 412 |
for i in range(chainlength): |
| 413 |
curr = chain(curr, i) |
| 414 |
start.__context__ = curr |
| 415 |
for i in range(prelength): |
| 416 |
curr = chain(curr, i + chainlength) |
| 417 |
ex.__context__ = curr |
| 418 |
raise TypeError(2) |
| 419 |
for chainlength in range(2, 7): |
| 420 |
for prelength in range(2, 7): |
| 421 |
|
| 422 |
raises(TypeError, cycle) |
| 423 |
|
| 424 |
def test_context_long_cycle_broken(): |
| 425 |
def chain(e, i=0): |
| 426 |
res = Exception(i) |
| 427 |
res.__context__ = e |
| 428 |
return res |
| 429 |
def cycle(): |
| 430 |
try: |
| 431 |
raise ValueError(1) |
| 432 |
except ValueError as ex: |
| 433 |
start = curr = TypeError() |
| 434 |
for i in range(chainlength): |
| 435 |
curr = chain(curr, i) |
| 436 |
ex.__context__ = curr |
| 437 |
raise start |
| 438 |
for chainlength in range(2, 7): |
| 439 |
|
| 440 |
exc = raises(TypeError, cycle).value |
| 441 |
for i in range(chainlength + 1): |
| 442 |
exc = exc.__context__ |
| 443 |
assert exc.__context__ is None |
| 444 |
|
| 445 |
def test_context_reraise_cycle_broken(): |
| 446 |
try: |
| 447 |
try: |
| 448 |
xyzzy |
| 449 |
except NameError as a: |
| 450 |
try: |
| 451 |
1/0 |
| 452 |
except ZeroDivisionError: |
| 453 |
raise a |
| 454 |
except NameError as e: |
| 455 |
assert e.__context__.__context__ is None |
| 456 |
else: |
| 457 |
fail("No exception raised") |
| 458 |
|
| 459 |
def test_context_once_removed(): |
| 460 |
context = IndexError() |
| 461 |
def func1(): |
| 462 |
func2() |
| 463 |
def func2(): |
| 464 |
try: |
| 465 |
1/0 |
| 466 |
except ZeroDivisionError as e: |
| 467 |
assert e.__context__ is context |
| 468 |
else: |
| 469 |
fail('No exception raised') |
| 470 |
try: |
| 471 |
raise context |
| 472 |
except: |
| 473 |
func1() |
| 474 |
|
| 475 |
def test_context_frame_spanning_cycle_broken(): |
| 476 |
context = IndexError() |
| 477 |
def func(): |
| 478 |
try: |
| 479 |
1/0 |
| 480 |
except Exception as e1: |
| 481 |
try: |
| 482 |
raise context |
| 483 |
except Exception as e2: |
| 484 |
assert e2.__context__ is e1 |
| 485 |
assert e1.__context__ is None |
| 486 |
else: |
| 487 |
fail('No exception raised') |
| 488 |
try: |
| 489 |
raise context |
| 490 |
except: |
| 491 |
func() |
| 492 |
|
| 493 |
def testCauseSyntax(): |
| 494 |
""" |
| 495 |
try: |
| 496 |
try: |
| 497 |
try: |
| 498 |
raise TypeError |
| 499 |
except Exception: |
| 500 |
raise ValueError from None |
| 501 |
except ValueError as exc: |
| 502 |
assert exc.__cause__ is None |
| 503 |
assert exc.__suppress_context__ is True |
| 504 |
assert isinstance(exc.__context__, TypeError) |
| 505 |
exc.__suppress_context__ = False |
| 506 |
raise exc |
| 507 |
except ValueError as exc: |
| 508 |
e = exc |
| 509 |
assert e.__cause__ is None |
| 510 |
assert e.__suppress_context__ is False |
| 511 |
assert isinstance(e.__context__, TypeError) |
| 512 |
""" |
| 513 |
|
| 514 |
def test_context_in_builtin(): |
| 515 |
context = IndexError() |
| 516 |
try: |
| 517 |
try: |
| 518 |
raise context |
| 519 |
except: |
| 520 |
compile('pass', 'foo', 'doh') |
| 521 |
except ValueError as e: |
| 522 |
assert e.__context__ is context |
| 523 |
else: |
| 524 |
fail('No exception raised') |
| 525 |
|
| 526 |
def test_context_with_suppressed(): |
| 527 |
class RaiseExc: |
| 528 |
def __init__(self, exc): |
| 529 |
self.exc = exc |
| 530 |
def __enter__(self): |
| 531 |
return self |
| 532 |
def __exit__(self, *exc_details): |
| 533 |
raise self.exc |
| 534 |
|
| 535 |
class SuppressExc: |
| 536 |
def __enter__(self): |
| 537 |
return self |
| 538 |
def __exit__(self, *exc_details): |
| 539 |
return True |
| 540 |
|
| 541 |
try: |
| 542 |
with RaiseExc(IndexError): |
| 543 |
with SuppressExc(): |
| 544 |
with RaiseExc(ValueError): |
| 545 |
1/0 |
| 546 |
except IndexError as exc: |
| 547 |
assert exc.__context__ is None |
| 548 |
else: |
| 549 |
assert False, "should have raised" |
| 550 |
|
| 551 |
def test_with_exception_context(): |
| 552 |
class Ctx: |
| 553 |
def __enter__(self): |
| 554 |
pass |
| 555 |
def __exit__(self, *e): |
| 556 |
1/0 |
| 557 |
try: |
| 558 |
with Ctx(): |
| 559 |
raise ValueError |
| 560 |
except ZeroDivisionError as e: |
| 561 |
assert e.__context__ is not None |
| 562 |
assert isinstance(e.__context__, ValueError) |
| 563 |
else: |
| 564 |
assert False, "should have raised" |
| 565 |
|
| 566 |
def test_context_setter_ignored(): |
| 567 |
class MyExc(Exception): |
| 568 |
def __setattr__(self, name, value): |
| 569 |
assert name != "__context__" |
| 570 |
|
| 571 |
with pytest.raises(MyExc) as excinfo: |
| 572 |
try: |
| 573 |
raise KeyError |
| 574 |
except KeyError: |
| 575 |
raise MyExc |
| 576 |
assert isinstance(excinfo.value.__context__, KeyError) |
| 577 |
|
| 578 |
def test_context_getter_ignored(): |
| 579 |
class MyExc(Exception): |
| 580 |
__context__ = property(None, None, None) |
| 581 |
|
| 582 |
with pytest.raises(KeyError): |
| 583 |
try: |
| 584 |
raise MyExc |
| 585 |
except MyExc: |
| 586 |
raise KeyError |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
def test_raise_with___traceback__(): |
| 591 |
import sys |
| 592 |
try: |
| 593 |
raise ValueError |
| 594 |
except: |
| 595 |
exc_type,exc_val,exc_tb = sys.exc_info() |
| 596 |
try: |
| 597 |
exc_val.__traceback__ = exc_tb |
| 598 |
raise exc_val |
| 599 |
except: |
| 600 |
exc_type2,exc_val2,exc_tb2 = sys.exc_info() |
| 601 |
assert exc_type is exc_type2 |
| 602 |
assert exc_val is exc_val2 |
| 603 |
assert exc_tb is exc_tb2.tb_next |
| 604 |
|
| 605 |
def test_sets_traceback(): |
| 606 |
import types |
| 607 |
try: |
| 608 |
raise IndexError() |
| 609 |
except IndexError as e: |
| 610 |
assert isinstance(e.__traceback__, types.TracebackType) |
| 611 |
else: |
| 612 |
fail("No exception raised") |
| 613 |
|
| 614 |
def test_accepts_traceback(): |
| 615 |
import sys |
| 616 |
def get_tb(): |
| 617 |
try: |
| 618 |
raise OSError() |
| 619 |
except: |
| 620 |
return sys.exc_info()[2] |
| 621 |
tb = get_tb() |
| 622 |
try: |
| 623 |
raise IndexError().with_traceback(tb) |
| 624 |
except IndexError as e: |
| 625 |
assert e.__traceback__ != tb |
| 626 |
assert e.__traceback__.tb_next is tb |
| 627 |
else: |
| 628 |
fail("No exception raised") |
| 629 |
|
| 630 |
def test_invalid_reraise(): |
| 631 |
try: |
| 632 |
raise |
| 633 |
except RuntimeError as e: |
| 634 |
assert "No active exception" in str(e) |
| 635 |
else: |
| 636 |
fail("Expected RuntimeError") |
| 637 |
|
| 638 |
def test_invalid_cause(): |
| 639 |
try: |
| 640 |
raise IndexError from 5 |
| 641 |
except TypeError as e: |
| 642 |
assert "exception cause" in str(e) |
| 643 |
else: |
| 644 |
fail("Expected TypeError") |
| 645 |
|
| 646 |
def test_normalize_exception_subclasscheck(): |
| 647 |
|
| 648 |
|
| 649 |
class Meta(type): |
| 650 |
def __subclasscheck__(cls, sub): |
| 651 |
1/0 |
| 652 |
class Broken(Exception, metaclass=Meta): |
| 653 |
pass |
| 654 |
with pytest.raises(ZeroDivisionError): |
| 655 |
raise Broken(42) |
| 656 |
|
| 657 |
def test_invalid_cause_setter(): |
| 658 |
class Setter(BaseException): |
| 659 |
def set_cause(self, cause): |
| 660 |
self.cause = cause |
| 661 |
__cause__ = property(fset=set_cause) |
| 662 |
try: |
| 663 |
raise Setter from 5 |
| 664 |
except TypeError as e: |
| 665 |
assert "exception cause" in str(e) |
| 666 |
else: |
| 667 |
fail("Expected TypeError") |
| 668 |
|
| 669 |
def test_reraise_with_cleared_traceback(): |
| 670 |
try: |
| 671 |
try: |
| 672 |
raise ValueError("err") |
| 673 |
except ValueError as err: |
| 674 |
err.__traceback__ = None |
| 675 |
raise |
| 676 |
except ValueError as err: |
| 677 |
assert err.__traceback__ is None |
| 678 |
|
| 679 |
def test_reraise_with_replaced_traceback(): |
| 680 |
import sys |
| 681 |
try: |
| 682 |
raise KeyError |
| 683 |
except KeyError: |
| 684 |
othr = sys.exc_info()[2] |
| 685 |
try: |
| 686 |
try: |
| 687 |
raise ValueError("err") |
| 688 |
except ValueError as err: |
| 689 |
err.__traceback__ = othr |
| 690 |
raise |
| 691 |
except ValueError as err: |
| 692 |
assert err.__traceback__ is othr |