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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
|
import pytest
import sys
def test_delete_attrs():
# for whatever reason CPython raises TypeError instead of AttributeError
# here
for attr in ["__cause__", "__context__", "args", "__traceback__", "__suppress_context__"]:
with pytest.raises(TypeError) as info:
delattr(Exception(), attr)
if sys.implementation.name != 'pypy' and attr in ("__suppress_context__",):
assert str(info.value).endswith("can't delete numeric/char attribute")
else:
assert str(info.value).endswith("may not be deleted")
def test_notes():
base = BaseException()
base.add_note('test note')
assert base.__notes__ == ['test note']
base.add_note('second note')
assert base.__notes__ == ['test note', 'second note']
with pytest.raises(TypeError):
base.add_note(42)
assert base.__notes__ == ['test note', 'second note']
e = Exception()
e.__notes__ = 12
with pytest.raises(TypeError):
e.add_note('abc')
def test_importerror_kwarg_error():
if sys.implementation.name == 'pypy':
msg = "ImportError.__init__() got an unexpected keyword argument 'invalid'"
else:
msg = "'invalid' is an invalid keyword argument for ImportError()"
exc = raises(TypeError,
ImportError,
'test', invalid='keyword', another=True)
assert str(exc.value) == "ImportError.__init__() got 2 unexpected keyword arguments"
exc = raises(TypeError, ImportError, 'test', invalid='keyword')
assert str(exc.value) == msg
exc = raises(TypeError,
ImportError,
'test', name='name', invalid='keyword')
assert str(exc.value) == msg
exc = raises(TypeError,
ImportError,
'test', path='path', invalid='keyword')
assert str(exc.value) == msg
# ExceptionGroup tests
def test_exception_group():
# the actual tests are in extra_tests, this is a simple smoke test that the
# integration into builtins works
assert issubclass(ExceptionGroup, BaseExceptionGroup)
def test_exceptiongroup_instantiate():
t1, v1 = TypeError(), ValueError()
exceptions = [t1, v1]
message = "42"
excgroup = ExceptionGroup(message, exceptions)
assert excgroup.message == message
assert excgroup.exceptions == tuple(exceptions)
def test_exceptiongroup_instantiate_check_message():
t1, v1 = TypeError(), ValueError()
exceptions = [t1, v1]
with pytest.raises(TypeError) as e:
ExceptionGroup(42, exceptions)
with pytest.raises(TypeError) as e:
ExceptionGroup(KeyError(), exceptions)
with pytest.raises(TypeError) as e:
ExceptionGroup(exceptions, exceptions)
def test_exceptiongroup_instantiate_check_exceptions():
message = "bla bla bla"
with pytest.raises(TypeError) as e:
ExceptionGroup(message, ValueError(42))
with pytest.raises(TypeError) as e:
ExceptionGroup(message, {ValueError(42)})
with pytest.raises(ValueError) as e:
ExceptionGroup(message, [])
with pytest.raises(ValueError) as e:
ExceptionGroup(message, (ValueError(42), 42))
def test_fields_are_readonly():
eg = ExceptionGroup("eg", [TypeError(1), OSError(2)])
assert type(eg.exceptions) == tuple
eg.message
with pytest.raises(AttributeError):
eg.message = "new msg"
eg.exceptions
with pytest.raises(AttributeError):
eg.exceptions = [OSError("xyz")]
# NOTE: The empty lines in here are important for
# test_basics_exceptiongroup_fields
def h_create_simple_eg():
excs = []
try:
try:
raise MemoryError("context and cause for ValueError(1)")
except MemoryError as e:
raise ValueError(1) from e
except ValueError as e:
excs.append(e)
try:
try:
raise OSError("context for TypeError")
except OSError:
raise TypeError(int)
except TypeError as e:
excs.append(e)
try:
try:
raise ImportError("context for ValueError(2)")
except ImportError:
raise ValueError(2)
except ValueError as e:
excs.append(e)
try:
raise ExceptionGroup("simple eg", excs)
except ExceptionGroup as e:
return e
def test_basics_exceptiongroup_fields():
eg = h_create_simple_eg()
# check msg
assert eg.message == "simple eg"
assert eg.args[0] == "simple eg"
# check cause and context
assert isinstance(eg.exceptions[0], ValueError)
assert isinstance(eg.exceptions[0].__cause__, MemoryError)
assert isinstance(eg.exceptions[0].__context__, MemoryError)
assert isinstance(eg.exceptions[1], TypeError)
assert eg.exceptions[1].__cause__ == None
assert isinstance(eg.exceptions[1].__context__, OSError)
assert isinstance(eg.exceptions[2], ValueError)
assert eg.exceptions[2].__cause__ == None
assert isinstance(eg.exceptions[2].__context__, ImportError)
# check tracebacks
line0 = h_create_simple_eg.__code__.co_firstlineno
tb_linenos = [line0 + 27, [line0 + 6, line0 + 14, line0 + 22]]
assert eg.__traceback__.tb_lineno == tb_linenos[0]
assert eg.__traceback__.tb_next == None
for i in range(3):
tb = eg.exceptions[i].__traceback__
assert tb.tb_next == None
assert tb.tb_lineno == tb_linenos[1][i]
def test_notes_is_list_of_strings_if_it_exists():
eg = h_create_simple_eg()
note = "This is a happy note for the exception group"
assert not hasattr(eg, "__notes__")
eg.add_note(note)
assert eg.__notes__ == [note]
def test_exceptiongroup_wraps_BaseException__raises_TypeError():
with pytest.raises(TypeError):
ExceptionGroup("eg", [ValueError(1), KeyboardInterrupt(2)])
def test_exceptiongroup_subclass_wraps_non_base_exceptions():
class MyEG(ExceptionGroup):
pass
assert type(MyEG("eg", [ValueError(12), TypeError(42)])) == MyEG
def test_exceptiongroup_inheritance_hierarchy():
assert issubclass(BaseExceptionGroup, BaseException)
assert issubclass(ExceptionGroup, BaseExceptionGroup)
assert issubclass(ExceptionGroup, Exception)
def test_baseexceptiongroup_instantiate():
# a BaseExceptionGroup instantiation will magically
# return an ExceptionGroup, if all of the exceptions
# are instances of Exception.
excgroup = BaseExceptionGroup('1', [ValueError()])
assert type(excgroup) is ExceptionGroup
# KeyboardInterrupt inherits from BaseException, not Exception
excgroup = BaseExceptionGroup('1', [ValueError(), KeyboardInterrupt()])
assert type(excgroup) is BaseExceptionGroup
def test_str_repr():
assert str(ExceptionGroup("abc", [ValueError()])) == "abc (1 sub-exception)"
assert str(ExceptionGroup("abc", [ValueError(), TypeError()])) == "abc (2 sub-exceptions)"
assert repr(ExceptionGroup("abc", [ValueError(), TypeError()])) == "ExceptionGroup('abc', [ValueError(), TypeError()])"
assert repr(ExceptionGroup("abc", [ValueError()])) == "ExceptionGroup('abc', [ValueError()])"
def h_create_nested_eg():
excs = []
try:
try:
raise TypeError(bytes)
except TypeError as e:
raise ExceptionGroup("nested", [e])
except ExceptionGroup as e:
excs.append(e)
try:
try:
raise MemoryError("out of memory")
except MemoryError as e:
raise ValueError(1) from e
except ValueError as e:
excs.append(e)
try:
raise ExceptionGroup("root", excs)
except ExceptionGroup as eg:
return eg
def h_leaf_generator(exc, tbs=None):
if tbs is None:
tbs = []
tbs.append(exc.__traceback__)
if isinstance(exc, BaseExceptionGroup):
for e in exc.exceptions:
yield from h_leaf_generator(e, tbs)
else:
# exc is a leaf exception and its traceback
# is the concatenation of the traceback
# segments in tbs
yield exc, tbs
tbs.pop()
def test_nested_exception_group_tracebacks():
eg = h_create_nested_eg()
line0 = h_create_nested_eg.__code__.co_firstlineno
for tb, expected in [
(eg.__traceback__, line0 + 19),
(eg.exceptions[0].__traceback__, line0 + 6),
(eg.exceptions[1].__traceback__, line0 + 14),
(eg.exceptions[0].exceptions[0].__traceback__, line0 + 4),
]:
assert tb.tb_lineno == expected
assert tb.tb_next == None
def test_nested_exception_group_subgroup_tracebacks_preserved():
eg_r = h_create_nested_eg()
eg = eg_r.subgroup((TypeError, ValueError))
line0 = h_create_nested_eg.__code__.co_firstlineno
for tb, expected in [
(eg.__traceback__, line0 + 19),
(eg.exceptions[0].__traceback__, line0 + 6),
(eg.exceptions[0].exceptions[0].__traceback__, line0 + 4),
]:
assert tb.tb_lineno == expected
assert tb.tb_next == None
def test_iteration_full_tracebacks():
eg = h_create_nested_eg()
# check that iteration over leaves
# produces the expected tracebacks
assert len(list(h_leaf_generator(eg))) == 2
line0 = h_create_nested_eg.__code__.co_firstlineno
expected_tbs = [[line0 + 19, line0 + 6, line0 + 4], [line0 + 19, line0 + 14]]
for i, (_, tbs) in enumerate(h_leaf_generator(eg)):
assert [tb.tb_lineno for tb in tbs] == expected_tbs[i]
def test_attribute_error_from_getattr_has_name_and_object():
class A:
def __getattr__(self, name):
raise AttributeError('nope')
a = A()
with raises(AttributeError) as info:
a.abc
assert info.value.name == 'abc'
assert info.value.obj is a
class A:
def __getattr__(self, name):
return getattr(list, name + 'nd')
a = A()
with raises(AttributeError) as info:
a.app
assert info.value.name == 'appnd'
assert info.value.obj is list
# TODO: Duplicates in eg?
def test_subgroup_invalid_args():
eg = ExceptionGroup("abc", [ValueError(), TypeError()])
with pytest.raises(TypeError):
eg.subgroup(42)
with pytest.raises(TypeError):
eg.subgroup(ValueError())
with pytest.raises(TypeError):
eg.subgroup(int)
with pytest.raises(TypeError):
eg.subgroup(eg)
with pytest.raises(TypeError):
eg.subgroup({TypeError, ValueError})
with pytest.raises(TypeError):
eg.subgroup([TypeError, ValueError])
with pytest.raises(TypeError):
eg.subgroup([TypeError, 42, ValueError])
def test_subgroup_bytype_single_simple():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
eg = ExceptionGroup("def", [typ2])
assert repr(eg.subgroup(TypeError)) == "ExceptionGroup('def', [TypeError()])"
assert repr(eg.subgroup(ValueError)) == "None"
eg = ExceptionGroup("abc", [val1, typ1])
assert repr(eg.subgroup(TypeError)) == "ExceptionGroup('abc', [TypeError()])"
assert repr(eg.subgroup(ValueError)) == "ExceptionGroup('abc', [ValueError(1)])"
eg = ExceptionGroup("abc", [val1, typ1, val2, val3])
assert repr(eg.subgroup(TypeError)) == "ExceptionGroup('abc', [TypeError()])"
assert repr(eg.subgroup(ValueError)) == "ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)])"
assert repr(eg.subgroup(BaseExceptionGroup)) == "ExceptionGroup('abc', [ValueError(1), TypeError(), ValueError(2), ValueError(3)])"
assert repr(eg.subgroup(ExceptionGroup)) == "ExceptionGroup('abc', [ValueError(1), TypeError(), ValueError(2), ValueError(3)])"
def test_subgroup_bytype_single_nested():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
assert repr(eg.subgroup(ValueError)) == "ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3)])])"
assert repr(eg.subgroup(TypeError)) == "ExceptionGroup('abc', [ExceptionGroup('def', [TypeError()]), TypeError()])"
assert repr(eg.subgroup(KeyError)) == "ExceptionGroup('abc', [KeyError()])"
assert repr(eg.subgroup(ZeroDivisionError)) == "ExceptionGroup('abc', [ExceptionGroup('def', [ZeroDivisionError()])])"
assert repr(eg.subgroup(BaseExceptionGroup)) == "ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()])"
assert repr(eg.subgroup(ExceptionGroup)) == "ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()])"
def test_subgroup_bytype_multi_simple():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
eg = ExceptionGroup("def", [typ2])
assert repr(eg.subgroup((TypeError, ValueError))) == "ExceptionGroup('def', [TypeError()])"
assert repr(eg.subgroup((ValueError, TypeError))) == "ExceptionGroup('def', [TypeError()])"
eg = ExceptionGroup("abc", [val1, typ1])
assert repr(eg.subgroup((ValueError, TypeError))) == "ExceptionGroup('abc', [ValueError(1), TypeError()])"
assert repr(eg.subgroup((TypeError, ValueError))) == "ExceptionGroup('abc', [ValueError(1), TypeError()])"
eg = ExceptionGroup("abc", [val1, typ1, val2, val3])
assert repr(eg.subgroup(TypeError)) == "ExceptionGroup('abc', [TypeError()])"
assert repr(eg.subgroup(ValueError)) == "ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)])"
assert eg.subgroup(tuple([])) == None
def test_subgroup_bytype_multi_nested():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
assert repr(eg.subgroup((ValueError, TypeError))) == "ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError()]), TypeError()])"
assert repr(eg.subgroup((KeyError, KeyError))) == "ExceptionGroup('abc', [KeyError()])"
assert eg.subgroup(tuple([])) == None
def test_subgroup_bypredicate_passthrough():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
assert eg is eg.subgroup(lambda e: True)
def test_subgroup_bypredicate_no_match():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
assert eg.subgroup(lambda e: False) == None
def test_subgroup_bypredicate_nested():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
assert repr(eg.subgroup(lambda e: isinstance(e, ValueError) and e.args[0] < 3)) \
== "ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2)])])"
def test_subgroup_bytype_is_id_if_all_subexceptions_match_and_split_is_not():
# NOTE: This is why split and subgroup are different
typ2 = TypeError()
eg = ExceptionGroup("def", [typ2])
eg_subgroup = eg.subgroup(TypeError)
eg_split = eg.split(TypeError)[0]
assert repr(eg_subgroup) == repr(eg_split)
if sys.implementation.name == 'pypy':
assert eg_subgroup is eg
assert not eg_split is eg
def test_split_bytype_single_simple():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
eg = ExceptionGroup("def", [typ2])
assert repr(eg.split(TypeError)) == "(ExceptionGroup('def', [TypeError()]), None)"
assert repr(eg.split(ValueError)) == "(None, ExceptionGroup('def', [TypeError()]))"
eg = ExceptionGroup("abc", [val1, typ1])
assert repr(eg.split(TypeError)) == "(ExceptionGroup('abc', [TypeError()]), ExceptionGroup('abc', [ValueError(1)]))"
assert repr(eg.split(ValueError)) == "(ExceptionGroup('abc', [ValueError(1)]), ExceptionGroup('abc', [TypeError()]))"
eg = ExceptionGroup("abc", [val1, typ1, val2, val3])
assert repr(eg.split(TypeError)) == "(ExceptionGroup('abc', [TypeError()]), ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)]))"
assert repr(eg.split(ValueError)) == "(ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)]), ExceptionGroup('abc', [TypeError()]))"
assert repr(eg.split(BaseExceptionGroup)) == "(ExceptionGroup('abc', [ValueError(1), TypeError(), ValueError(2), ValueError(3)]), None)"
assert repr(eg.split(ExceptionGroup)) == "(ExceptionGroup('abc', [ValueError(1), TypeError(), ValueError(2), ValueError(3)]), None)"
def test_split_bytype_single_nested():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
assert repr(eg.split(ValueError)) == "(ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3)])]), ExceptionGroup('abc', [KeyError(), ExceptionGroup('def', [TypeError(), ZeroDivisionError()]), TypeError()]))"
assert repr(eg.split(TypeError)) == "(ExceptionGroup('abc', [ExceptionGroup('def', [TypeError()]), TypeError()]), ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), ZeroDivisionError()])]))"
assert repr(eg.split(KeyError)) == "(ExceptionGroup('abc', [KeyError()]), ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]))"
assert repr(eg.split(ZeroDivisionError)) == "(ExceptionGroup('abc', [ExceptionGroup('def', [ZeroDivisionError()])]), ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError()]), TypeError()]))"
assert repr(eg.split(BaseExceptionGroup)) == "(ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]), None)"
assert repr(eg.split(ExceptionGroup)) == "(ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]), None)"
def test_split_bytype_multi_simple():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
eg = ExceptionGroup("def", [typ2])
assert repr(eg.split((TypeError, ValueError))) == "(ExceptionGroup('def', [TypeError()]), None)"
assert repr(eg.split((ValueError, TypeError))) == "(ExceptionGroup('def', [TypeError()]), None)"
eg = ExceptionGroup("abc", [val1, typ1])
assert repr(eg.split((ValueError, TypeError))) == "(ExceptionGroup('abc', [ValueError(1), TypeError()]), None)"
assert repr(eg.split((TypeError, ValueError))) == "(ExceptionGroup('abc', [ValueError(1), TypeError()]), None)"
eg = ExceptionGroup("abc", [val1, typ1, val2, val3])
assert repr(eg.split(TypeError)) == "(ExceptionGroup('abc', [TypeError()]), ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)]))"
assert repr(eg.split(ValueError)) == "(ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)]), ExceptionGroup('abc', [TypeError()]))"
assert repr(eg.split(tuple([]))) == "(None, ExceptionGroup('abc', [ValueError(1), TypeError(), ValueError(2), ValueError(3)]))"
def test_split_bytype_multi_nested():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
assert repr(eg.split((ValueError, TypeError))) == "(ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError()]), TypeError()]), ExceptionGroup('abc', [KeyError(), ExceptionGroup('def', [ZeroDivisionError()])]))"
assert repr(eg.split((KeyError, KeyError))) == "(ExceptionGroup('abc', [KeyError()]), ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]))"
assert repr(eg.split(tuple([]))) == "(None, ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]))"
def test_split_bypredicate_passthrough():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
assert repr(eg.split(lambda e: True)) == "(ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]), None)"
def test_split_bypredicate_no_match():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
assert repr(eg.split(lambda e: False)) == "(None, ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]))"
def test_split_bypredicate_nested():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
assert repr(eg.split(lambda e: isinstance(e, ValueError) and e.args[0] < 3)) \
== "(ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2)])]), ExceptionGroup('abc', [KeyError(), ExceptionGroup('def', [ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]))"
def h_make_deep_eg():
e = TypeError(1)
for _ in range(2000):
e = ExceptionGroup("eg", [e])
return e
def test_deep_split():
e = h_make_deep_eg()
with pytest.raises(RecursionError):
e.split(TypeError)
def test_deep_subgroup():
e = h_make_deep_eg()
with pytest.raises(RecursionError):
e.subgroup(TypeError)
def test_subgroup_copies_cause_etc():
e = ExceptionGroup("23", [TypeError(), ValueError()])
e.__notes__ = ['hello']
se = e.subgroup(ValueError)
assert e.__cause__ == se.__cause__
assert e.__traceback__ == se.__traceback__
assert e.__context__ == se.__context__
assert e.__notes__ == se.__notes__
def test_derive_does_not_copies_cause_etc():
e = ExceptionGroup("23", [TypeError(), ValueError()])
e.__notes__ = ['hello']
se = e.derive([IndexError()])
assert se.__cause__ is None
assert se.__traceback__ is None
assert se.__context__ is None
assert not hasattr(se, '__notes__')
def test_derive_always_creates_exception_group():
class MyEG(ExceptionGroup):
pass
eg = MyEG("abc", [ValueError(), TypeError()])
eg2 = eg.derive([ValueError()])
assert type(eg2) is ExceptionGroup
def test_init_called():
# issue 5218
initcalled = []
class MyException(Exception):
def __init__(self, message):
initcalled.append(message)
class MyExceptionGroup(ExceptionGroup, MyException):
pass
MyExceptionGroup("...", [Exception()])
assert len(initcalled) == 0
# _prep_reraise_star tests
if sys.implementation.name == 'pypy':
from __exceptions__ import _prep_reraise_star, _collect_eg_leafs, _exception_group_projection
from __pypy__ import identity_dict
def test_prep_reraise_star_simple():
assert _prep_reraise_star(TypeError(), [None]) is None
assert _prep_reraise_star(ExceptionGroup('abc', [ValueError(), TypeError()]), [None]) is None
value = ValueError()
res = _prep_reraise_star(ExceptionGroup('abc', [value, TypeError()]), [ExceptionGroup('abc', [value])])
assert repr(res) == "ExceptionGroup('abc', [ValueError()])"
assert res.exceptions[0] is value
def test_prep_reraise_exception_happens_in_except_star():
value = ValueError()
full_eg = ExceptionGroup('abc', [value, TypeError()])
value_eg = ExceptionGroup('abc', [value])
try:
raise Exception
except Exception as e:
tb1 = e.__traceback__
try:
raise Exception
except Exception as e:
tb2 = e.__traceback__
full_eg.__traceback__ = tb1
value_eg.__traceback__ = tb1
zerodiv = ZeroDivisionError('division by zero')
zerodiv.__traceback__ = tb2
assert repr(_prep_reraise_star(full_eg, [zerodiv, value_eg])) == "ExceptionGroup('', [ZeroDivisionError('division by zero'), ExceptionGroup('abc', [ValueError()])])"
# helper function tests
def test_eg_leafs_basic():
t1, v1 = TypeError(), ValueError()
exceptions = [t1, v1]
message = "42"
excgroup = ExceptionGroup(message, exceptions)
resultset = identity_dict()
_collect_eg_leafs(excgroup, resultset)
print(resultset.keys())
assert t1 in resultset
assert v1 in resultset
def test_eg_leafs_null():
resultset = identity_dict()
_collect_eg_leafs(None, resultset)
assert not resultset
def test_eg_leafs_nogroup():
exc = TypeError()
resultset = identity_dict()
_collect_eg_leafs(exc, resultset)
assert exc in resultset
def test_eg_leafs_recursive():
# TODO: fix
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
resultset = identity_dict()
collected = _collect_eg_leafs(eg, resultset)
assert len(resultset) == 7
for e in [val1, typ1, val2, val3, typ2, key1, div1]:
assert e in resultset
def test_exception_group_projection_basic():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
keep1 = ExceptionGroup("meep", [key1, typ1])
keep2 = ExceptionGroup("moop", [val2, ExceptionGroup("doop", [val3])])
result = _exception_group_projection(eg, [keep1, keep2])
assert repr(result) == \
"ExceptionGroup('abc', [KeyError(), ExceptionGroup('def', [ValueError(2), ValueError(3)]), TypeError()])"
def test_exception_group_projection_duplicated_in_keep():
val1 = ValueError(1)
typ1 = TypeError()
val2 = ValueError(2)
val3 = ValueError(3)
typ2 = TypeError()
key1 = KeyError()
div1 = ZeroDivisionError()
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1])
keep1 = ExceptionGroup("meep", [key1, typ1, val2])
keep2 = ExceptionGroup("moop", [val2, ExceptionGroup("doop", [key1, val3])])
result = _exception_group_projection(eg, [keep1, keep2])
assert repr(result) == \
"ExceptionGroup('abc', [KeyError(), ExceptionGroup('def', [ValueError(2), ValueError(3)]), TypeError()])"
|