| 1 |
import pytest |
| 2 |
import sys |
| 3 |
|
| 4 |
def test_delete_attrs(): |
| 5 |
|
| 6 |
|
| 7 |
for attr in ["__cause__", "__context__", "args", "__traceback__", "__suppress_context__"]: |
| 8 |
with pytest.raises(TypeError) as info: |
| 9 |
delattr(Exception(), attr) |
| 10 |
if sys.implementation.name != 'pypy' and attr in ("__suppress_context__",): |
| 11 |
assert str(info.value).endswith("can't delete numeric/char attribute") |
| 12 |
else: |
| 13 |
assert str(info.value).endswith("may not be deleted") |
| 14 |
|
| 15 |
def test_notes(): |
| 16 |
base = BaseException() |
| 17 |
base.add_note('test note') |
| 18 |
assert base.__notes__ == ['test note'] |
| 19 |
base.add_note('second note') |
| 20 |
assert base.__notes__ == ['test note', 'second note'] |
| 21 |
with pytest.raises(TypeError): |
| 22 |
base.add_note(42) |
| 23 |
assert base.__notes__ == ['test note', 'second note'] |
| 24 |
|
| 25 |
e = Exception() |
| 26 |
e.__notes__ = 12 |
| 27 |
with pytest.raises(TypeError): |
| 28 |
e.add_note('abc') |
| 29 |
|
| 30 |
def test_importerror_kwarg_error(): |
| 31 |
msg = "'invalid' is an invalid keyword argument for ImportError" |
| 32 |
|
| 33 |
exc = raises(TypeError, ImportError, 'test', invalid='keyword') |
| 34 |
assert str(exc.value) == msg |
| 35 |
|
| 36 |
exc = raises(TypeError, |
| 37 |
ImportError, |
| 38 |
'test', name='name', invalid='keyword') |
| 39 |
assert str(exc.value) == msg |
| 40 |
|
| 41 |
exc = raises(TypeError, |
| 42 |
ImportError, |
| 43 |
'test', path='path', invalid='keyword') |
| 44 |
assert str(exc.value) == msg |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
def test_exception_group(): |
| 51 |
|
| 52 |
|
| 53 |
assert issubclass(ExceptionGroup, BaseExceptionGroup) |
| 54 |
|
| 55 |
def test_exceptiongroup_instantiate(): |
| 56 |
t1, v1 = TypeError(), ValueError() |
| 57 |
exceptions = [t1, v1] |
| 58 |
message = "42" |
| 59 |
excgroup = ExceptionGroup(message, exceptions) |
| 60 |
assert excgroup.message == message |
| 61 |
assert excgroup.exceptions == tuple(exceptions) |
| 62 |
|
| 63 |
def test_exceptiongroup_instantiate_check_message(): |
| 64 |
t1, v1 = TypeError(), ValueError() |
| 65 |
exceptions = [t1, v1] |
| 66 |
with pytest.raises(TypeError) as e: |
| 67 |
ExceptionGroup(42, exceptions) |
| 68 |
with pytest.raises(TypeError) as e: |
| 69 |
ExceptionGroup(KeyError(), exceptions) |
| 70 |
with pytest.raises(TypeError) as e: |
| 71 |
ExceptionGroup(exceptions, exceptions) |
| 72 |
|
| 73 |
def test_exceptiongroup_instantiate_check_exceptions(): |
| 74 |
message = "bla bla bla" |
| 75 |
with pytest.raises(TypeError) as e: |
| 76 |
ExceptionGroup(message, ValueError(42)) |
| 77 |
with pytest.raises(TypeError) as e: |
| 78 |
ExceptionGroup(message, {ValueError(42)}) |
| 79 |
with pytest.raises(ValueError) as e: |
| 80 |
ExceptionGroup(message, []) |
| 81 |
with pytest.raises(ValueError) as e: |
| 82 |
ExceptionGroup(message, (ValueError(42), 42)) |
| 83 |
|
| 84 |
def test_fields_are_readonly(): |
| 85 |
eg = ExceptionGroup("eg", [TypeError(1), OSError(2)]) |
| 86 |
assert type(eg.exceptions) == tuple |
| 87 |
eg.message |
| 88 |
with pytest.raises(AttributeError): |
| 89 |
eg.message = "new msg" |
| 90 |
eg.exceptions |
| 91 |
with pytest.raises(AttributeError): |
| 92 |
eg.exceptions = [OSError("xyz")] |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
def h_create_simple_eg(): |
| 97 |
excs = [] |
| 98 |
try: |
| 99 |
try: |
| 100 |
raise MemoryError("context and cause for ValueError(1)") |
| 101 |
except MemoryError as e: |
| 102 |
raise ValueError(1) from e |
| 103 |
except ValueError as e: |
| 104 |
excs.append(e) |
| 105 |
|
| 106 |
try: |
| 107 |
try: |
| 108 |
raise OSError("context for TypeError") |
| 109 |
except OSError: |
| 110 |
raise TypeError(int) |
| 111 |
except TypeError as e: |
| 112 |
excs.append(e) |
| 113 |
|
| 114 |
try: |
| 115 |
try: |
| 116 |
raise ImportError("context for ValueError(2)") |
| 117 |
except ImportError: |
| 118 |
raise ValueError(2) |
| 119 |
except ValueError as e: |
| 120 |
excs.append(e) |
| 121 |
|
| 122 |
try: |
| 123 |
raise ExceptionGroup("simple eg", excs) |
| 124 |
except ExceptionGroup as e: |
| 125 |
return e |
| 126 |
|
| 127 |
def test_basics_exceptiongroup_fields(): |
| 128 |
eg = h_create_simple_eg() |
| 129 |
|
| 130 |
|
| 131 |
assert eg.message == "simple eg" |
| 132 |
assert eg.args[0] == "simple eg" |
| 133 |
|
| 134 |
|
| 135 |
assert isinstance(eg.exceptions[0], ValueError) |
| 136 |
assert isinstance(eg.exceptions[0].__cause__, MemoryError) |
| 137 |
assert isinstance(eg.exceptions[0].__context__, MemoryError) |
| 138 |
assert isinstance(eg.exceptions[1], TypeError) |
| 139 |
assert eg.exceptions[1].__cause__ == None |
| 140 |
assert isinstance(eg.exceptions[1].__context__, OSError) |
| 141 |
assert isinstance(eg.exceptions[2], ValueError) |
| 142 |
assert eg.exceptions[2].__cause__ == None |
| 143 |
assert isinstance(eg.exceptions[2].__context__, ImportError) |
| 144 |
|
| 145 |
|
| 146 |
line0 = h_create_simple_eg.__code__.co_firstlineno |
| 147 |
tb_linenos = [line0 + 27, [line0 + 6, line0 + 14, line0 + 22]] |
| 148 |
assert eg.__traceback__.tb_lineno == tb_linenos[0] |
| 149 |
assert eg.__traceback__.tb_next == None |
| 150 |
for i in range(3): |
| 151 |
tb = eg.exceptions[i].__traceback__ |
| 152 |
assert tb.tb_next == None |
| 153 |
assert tb.tb_lineno == tb_linenos[1][i] |
| 154 |
|
| 155 |
def test_notes_is_list_of_strings_if_it_exists(): |
| 156 |
eg = h_create_simple_eg() |
| 157 |
note = "This is a happy note for the exception group" |
| 158 |
assert not hasattr(eg, "__notes__") |
| 159 |
eg.add_note(note) |
| 160 |
assert eg.__notes__ == [note] |
| 161 |
|
| 162 |
def test_exceptiongroup_wraps_BaseException__raises_TypeError(): |
| 163 |
with pytest.raises(TypeError): |
| 164 |
ExceptionGroup("eg", [ValueError(1), KeyboardInterrupt(2)]) |
| 165 |
|
| 166 |
def test_exceptiongroup_subclass_wraps_non_base_exceptions(): |
| 167 |
class MyEG(ExceptionGroup): |
| 168 |
pass |
| 169 |
assert type(MyEG("eg", [ValueError(12), TypeError(42)])) == MyEG |
| 170 |
|
| 171 |
def test_exceptiongroup_inheritance_hierarchy(): |
| 172 |
assert issubclass(BaseExceptionGroup, BaseException) |
| 173 |
assert issubclass(ExceptionGroup, BaseExceptionGroup) |
| 174 |
assert issubclass(ExceptionGroup, Exception) |
| 175 |
|
| 176 |
def test_baseexceptiongroup_instantiate(): |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
excgroup = BaseExceptionGroup('1', [ValueError()]) |
| 181 |
assert type(excgroup) is ExceptionGroup |
| 182 |
|
| 183 |
excgroup = BaseExceptionGroup('1', [ValueError(), KeyboardInterrupt()]) |
| 184 |
assert type(excgroup) is BaseExceptionGroup |
| 185 |
|
| 186 |
def test_str_repr(): |
| 187 |
assert str(ExceptionGroup("abc", [ValueError()])) == "abc (1 sub-exception)" |
| 188 |
assert str(ExceptionGroup("abc", [ValueError(), TypeError()])) == "abc (2 sub-exceptions)" |
| 189 |
assert repr(ExceptionGroup("abc", [ValueError(), TypeError()])) == "ExceptionGroup('abc', [ValueError(), TypeError()])" |
| 190 |
assert repr(ExceptionGroup("abc", [ValueError()])) == "ExceptionGroup('abc', [ValueError()])" |
| 191 |
|
| 192 |
def h_create_nested_eg(): |
| 193 |
excs = [] |
| 194 |
try: |
| 195 |
try: |
| 196 |
raise TypeError(bytes) |
| 197 |
except TypeError as e: |
| 198 |
raise ExceptionGroup("nested", [e]) |
| 199 |
except ExceptionGroup as e: |
| 200 |
excs.append(e) |
| 201 |
|
| 202 |
try: |
| 203 |
try: |
| 204 |
raise MemoryError("out of memory") |
| 205 |
except MemoryError as e: |
| 206 |
raise ValueError(1) from e |
| 207 |
except ValueError as e: |
| 208 |
excs.append(e) |
| 209 |
|
| 210 |
try: |
| 211 |
raise ExceptionGroup("root", excs) |
| 212 |
except ExceptionGroup as eg: |
| 213 |
return eg |
| 214 |
|
| 215 |
def h_leaf_generator(exc, tbs=None): |
| 216 |
if tbs is None: |
| 217 |
tbs = [] |
| 218 |
tbs.append(exc.__traceback__) |
| 219 |
if isinstance(exc, BaseExceptionGroup): |
| 220 |
for e in exc.exceptions: |
| 221 |
yield from h_leaf_generator(e, tbs) |
| 222 |
else: |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
yield exc, tbs |
| 227 |
tbs.pop() |
| 228 |
|
| 229 |
def test_nested_exception_group_tracebacks(): |
| 230 |
eg = h_create_nested_eg() |
| 231 |
|
| 232 |
line0 = h_create_nested_eg.__code__.co_firstlineno |
| 233 |
for tb, expected in [ |
| 234 |
(eg.__traceback__, line0 + 19), |
| 235 |
(eg.exceptions[0].__traceback__, line0 + 6), |
| 236 |
(eg.exceptions[1].__traceback__, line0 + 14), |
| 237 |
(eg.exceptions[0].exceptions[0].__traceback__, line0 + 4), |
| 238 |
]: |
| 239 |
assert tb.tb_lineno == expected |
| 240 |
assert tb.tb_next == None |
| 241 |
|
| 242 |
def test_nested_exception_group_subgroup_tracebacks_preserved(): |
| 243 |
eg_r = h_create_nested_eg() |
| 244 |
eg = eg_r.subgroup((TypeError, ValueError)) |
| 245 |
|
| 246 |
line0 = h_create_nested_eg.__code__.co_firstlineno |
| 247 |
for tb, expected in [ |
| 248 |
(eg.__traceback__, line0 + 19), |
| 249 |
(eg.exceptions[0].__traceback__, line0 + 6), |
| 250 |
(eg.exceptions[0].exceptions[0].__traceback__, line0 + 4), |
| 251 |
]: |
| 252 |
assert tb.tb_lineno == expected |
| 253 |
assert tb.tb_next == None |
| 254 |
|
| 255 |
def test_iteration_full_tracebacks(): |
| 256 |
eg = h_create_nested_eg() |
| 257 |
|
| 258 |
|
| 259 |
assert len(list(h_leaf_generator(eg))) == 2 |
| 260 |
|
| 261 |
line0 = h_create_nested_eg.__code__.co_firstlineno |
| 262 |
expected_tbs = [[line0 + 19, line0 + 6, line0 + 4], [line0 + 19, line0 + 14]] |
| 263 |
|
| 264 |
for i, (_, tbs) in enumerate(h_leaf_generator(eg)): |
| 265 |
assert [tb.tb_lineno for tb in tbs] == expected_tbs[i] |
| 266 |
|
| 267 |
|
| 268 |
def test_attribute_error_from_getattr_has_name_and_object(): |
| 269 |
class A: |
| 270 |
def __getattr__(self, name): |
| 271 |
raise AttributeError('nope') |
| 272 |
a = A() |
| 273 |
with raises(AttributeError) as info: |
| 274 |
a.abc |
| 275 |
assert info.value.name == 'abc' |
| 276 |
assert info.value.obj is a |
| 277 |
|
| 278 |
class A: |
| 279 |
def __getattr__(self, name): |
| 280 |
return getattr(list, name + 'nd') |
| 281 |
a = A() |
| 282 |
with raises(AttributeError) as info: |
| 283 |
a.app |
| 284 |
assert info.value.name == 'appnd' |
| 285 |
assert info.value.obj is list |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
def test_subgroup_invalid_args(): |
| 291 |
eg = ExceptionGroup("abc", [ValueError(), TypeError()]) |
| 292 |
with pytest.raises(TypeError): |
| 293 |
eg.subgroup(42) |
| 294 |
with pytest.raises(TypeError): |
| 295 |
eg.subgroup(ValueError()) |
| 296 |
with pytest.raises(TypeError): |
| 297 |
eg.subgroup(int) |
| 298 |
with pytest.raises(TypeError): |
| 299 |
eg.subgroup(eg) |
| 300 |
with pytest.raises(TypeError): |
| 301 |
eg.subgroup({TypeError, ValueError}) |
| 302 |
with pytest.raises(TypeError): |
| 303 |
eg.subgroup([TypeError, ValueError]) |
| 304 |
with pytest.raises(TypeError): |
| 305 |
eg.subgroup([TypeError, 42, ValueError]) |
| 306 |
|
| 307 |
def test_subgroup_bytype_single_simple(): |
| 308 |
val1 = ValueError(1) |
| 309 |
typ1 = TypeError() |
| 310 |
val2 = ValueError(2) |
| 311 |
val3 = ValueError(3) |
| 312 |
typ2 = TypeError() |
| 313 |
eg = ExceptionGroup("def", [typ2]) |
| 314 |
assert repr(eg.subgroup(TypeError)) == "ExceptionGroup('def', [TypeError()])" |
| 315 |
assert repr(eg.subgroup(ValueError)) == "None" |
| 316 |
eg = ExceptionGroup("abc", [val1, typ1]) |
| 317 |
assert repr(eg.subgroup(TypeError)) == "ExceptionGroup('abc', [TypeError()])" |
| 318 |
assert repr(eg.subgroup(ValueError)) == "ExceptionGroup('abc', [ValueError(1)])" |
| 319 |
eg = ExceptionGroup("abc", [val1, typ1, val2, val3]) |
| 320 |
assert repr(eg.subgroup(TypeError)) == "ExceptionGroup('abc', [TypeError()])" |
| 321 |
assert repr(eg.subgroup(ValueError)) == "ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)])" |
| 322 |
assert repr(eg.subgroup(BaseExceptionGroup)) == "ExceptionGroup('abc', [ValueError(1), TypeError(), ValueError(2), ValueError(3)])" |
| 323 |
assert repr(eg.subgroup(ExceptionGroup)) == "ExceptionGroup('abc', [ValueError(1), TypeError(), ValueError(2), ValueError(3)])" |
| 324 |
|
| 325 |
def test_subgroup_bytype_single_nested(): |
| 326 |
val1 = ValueError(1) |
| 327 |
typ1 = TypeError() |
| 328 |
val2 = ValueError(2) |
| 329 |
val3 = ValueError(3) |
| 330 |
typ2 = TypeError() |
| 331 |
key1 = KeyError() |
| 332 |
div1 = ZeroDivisionError() |
| 333 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 334 |
assert repr(eg.subgroup(ValueError)) == "ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3)])])" |
| 335 |
assert repr(eg.subgroup(TypeError)) == "ExceptionGroup('abc', [ExceptionGroup('def', [TypeError()]), TypeError()])" |
| 336 |
assert repr(eg.subgroup(KeyError)) == "ExceptionGroup('abc', [KeyError()])" |
| 337 |
assert repr(eg.subgroup(ZeroDivisionError)) == "ExceptionGroup('abc', [ExceptionGroup('def', [ZeroDivisionError()])])" |
| 338 |
assert repr(eg.subgroup(BaseExceptionGroup)) == "ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()])" |
| 339 |
assert repr(eg.subgroup(ExceptionGroup)) == "ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()])" |
| 340 |
|
| 341 |
def test_subgroup_bytype_multi_simple(): |
| 342 |
val1 = ValueError(1) |
| 343 |
typ1 = TypeError() |
| 344 |
val2 = ValueError(2) |
| 345 |
val3 = ValueError(3) |
| 346 |
typ2 = TypeError() |
| 347 |
eg = ExceptionGroup("def", [typ2]) |
| 348 |
assert repr(eg.subgroup((TypeError, ValueError))) == "ExceptionGroup('def', [TypeError()])" |
| 349 |
assert repr(eg.subgroup((ValueError, TypeError))) == "ExceptionGroup('def', [TypeError()])" |
| 350 |
eg = ExceptionGroup("abc", [val1, typ1]) |
| 351 |
assert repr(eg.subgroup((ValueError, TypeError))) == "ExceptionGroup('abc', [ValueError(1), TypeError()])" |
| 352 |
assert repr(eg.subgroup((TypeError, ValueError))) == "ExceptionGroup('abc', [ValueError(1), TypeError()])" |
| 353 |
eg = ExceptionGroup("abc", [val1, typ1, val2, val3]) |
| 354 |
assert repr(eg.subgroup(TypeError)) == "ExceptionGroup('abc', [TypeError()])" |
| 355 |
assert repr(eg.subgroup(ValueError)) == "ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)])" |
| 356 |
assert eg.subgroup(tuple([])) == None |
| 357 |
|
| 358 |
def test_subgroup_bytype_multi_nested(): |
| 359 |
val1 = ValueError(1) |
| 360 |
typ1 = TypeError() |
| 361 |
val2 = ValueError(2) |
| 362 |
val3 = ValueError(3) |
| 363 |
typ2 = TypeError() |
| 364 |
key1 = KeyError() |
| 365 |
div1 = ZeroDivisionError() |
| 366 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 367 |
assert repr(eg.subgroup((ValueError, TypeError))) == "ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError()]), TypeError()])" |
| 368 |
assert repr(eg.subgroup((KeyError, KeyError))) == "ExceptionGroup('abc', [KeyError()])" |
| 369 |
assert eg.subgroup(tuple([])) == None |
| 370 |
|
| 371 |
|
| 372 |
def test_subgroup_bypredicate_passthrough(): |
| 373 |
val1 = ValueError(1) |
| 374 |
typ1 = TypeError() |
| 375 |
val2 = ValueError(2) |
| 376 |
val3 = ValueError(3) |
| 377 |
typ2 = TypeError() |
| 378 |
key1 = KeyError() |
| 379 |
div1 = ZeroDivisionError() |
| 380 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 381 |
assert eg is eg.subgroup(lambda e: True) |
| 382 |
|
| 383 |
def test_subgroup_bypredicate_no_match(): |
| 384 |
val1 = ValueError(1) |
| 385 |
typ1 = TypeError() |
| 386 |
val2 = ValueError(2) |
| 387 |
val3 = ValueError(3) |
| 388 |
typ2 = TypeError() |
| 389 |
key1 = KeyError() |
| 390 |
div1 = ZeroDivisionError() |
| 391 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 392 |
assert eg.subgroup(lambda e: False) == None |
| 393 |
|
| 394 |
def test_subgroup_bypredicate_nested(): |
| 395 |
val1 = ValueError(1) |
| 396 |
typ1 = TypeError() |
| 397 |
val2 = ValueError(2) |
| 398 |
val3 = ValueError(3) |
| 399 |
typ2 = TypeError() |
| 400 |
key1 = KeyError() |
| 401 |
div1 = ZeroDivisionError() |
| 402 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 403 |
assert repr(eg.subgroup(lambda e: isinstance(e, ValueError) and e.args[0] < 3)) \ |
| 404 |
== "ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2)])])" |
| 405 |
|
| 406 |
def test_subgroup_bytype_is_id_if_all_subexceptions_match_and_split_is_not(): |
| 407 |
|
| 408 |
typ2 = TypeError() |
| 409 |
eg = ExceptionGroup("def", [typ2]) |
| 410 |
eg_subgroup = eg.subgroup(TypeError) |
| 411 |
eg_split = eg.split(TypeError)[0] |
| 412 |
assert repr(eg_subgroup) == repr(eg_split) |
| 413 |
if sys.implementation.name == 'pypy': |
| 414 |
assert eg_subgroup is eg |
| 415 |
assert not eg_split is eg |
| 416 |
|
| 417 |
def test_split_bytype_single_simple(): |
| 418 |
val1 = ValueError(1) |
| 419 |
typ1 = TypeError() |
| 420 |
val2 = ValueError(2) |
| 421 |
val3 = ValueError(3) |
| 422 |
typ2 = TypeError() |
| 423 |
eg = ExceptionGroup("def", [typ2]) |
| 424 |
assert repr(eg.split(TypeError)) == "(ExceptionGroup('def', [TypeError()]), None)" |
| 425 |
assert repr(eg.split(ValueError)) == "(None, ExceptionGroup('def', [TypeError()]))" |
| 426 |
eg = ExceptionGroup("abc", [val1, typ1]) |
| 427 |
assert repr(eg.split(TypeError)) == "(ExceptionGroup('abc', [TypeError()]), ExceptionGroup('abc', [ValueError(1)]))" |
| 428 |
assert repr(eg.split(ValueError)) == "(ExceptionGroup('abc', [ValueError(1)]), ExceptionGroup('abc', [TypeError()]))" |
| 429 |
eg = ExceptionGroup("abc", [val1, typ1, val2, val3]) |
| 430 |
assert repr(eg.split(TypeError)) == "(ExceptionGroup('abc', [TypeError()]), ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)]))" |
| 431 |
assert repr(eg.split(ValueError)) == "(ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)]), ExceptionGroup('abc', [TypeError()]))" |
| 432 |
assert repr(eg.split(BaseExceptionGroup)) == "(ExceptionGroup('abc', [ValueError(1), TypeError(), ValueError(2), ValueError(3)]), None)" |
| 433 |
assert repr(eg.split(ExceptionGroup)) == "(ExceptionGroup('abc', [ValueError(1), TypeError(), ValueError(2), ValueError(3)]), None)" |
| 434 |
|
| 435 |
def test_split_bytype_single_nested(): |
| 436 |
val1 = ValueError(1) |
| 437 |
typ1 = TypeError() |
| 438 |
val2 = ValueError(2) |
| 439 |
val3 = ValueError(3) |
| 440 |
typ2 = TypeError() |
| 441 |
key1 = KeyError() |
| 442 |
div1 = ZeroDivisionError() |
| 443 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 444 |
assert repr(eg.split(ValueError)) == "(ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3)])]), ExceptionGroup('abc', [KeyError(), ExceptionGroup('def', [TypeError(), ZeroDivisionError()]), TypeError()]))" |
| 445 |
assert repr(eg.split(TypeError)) == "(ExceptionGroup('abc', [ExceptionGroup('def', [TypeError()]), TypeError()]), ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), ZeroDivisionError()])]))" |
| 446 |
assert repr(eg.split(KeyError)) == "(ExceptionGroup('abc', [KeyError()]), ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]))" |
| 447 |
assert repr(eg.split(ZeroDivisionError)) == "(ExceptionGroup('abc', [ExceptionGroup('def', [ZeroDivisionError()])]), ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError()]), TypeError()]))" |
| 448 |
assert repr(eg.split(BaseExceptionGroup)) == "(ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]), None)" |
| 449 |
assert repr(eg.split(ExceptionGroup)) == "(ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]), None)" |
| 450 |
|
| 451 |
def test_split_bytype_multi_simple(): |
| 452 |
val1 = ValueError(1) |
| 453 |
typ1 = TypeError() |
| 454 |
val2 = ValueError(2) |
| 455 |
val3 = ValueError(3) |
| 456 |
typ2 = TypeError() |
| 457 |
eg = ExceptionGroup("def", [typ2]) |
| 458 |
assert repr(eg.split((TypeError, ValueError))) == "(ExceptionGroup('def', [TypeError()]), None)" |
| 459 |
assert repr(eg.split((ValueError, TypeError))) == "(ExceptionGroup('def', [TypeError()]), None)" |
| 460 |
eg = ExceptionGroup("abc", [val1, typ1]) |
| 461 |
assert repr(eg.split((ValueError, TypeError))) == "(ExceptionGroup('abc', [ValueError(1), TypeError()]), None)" |
| 462 |
assert repr(eg.split((TypeError, ValueError))) == "(ExceptionGroup('abc', [ValueError(1), TypeError()]), None)" |
| 463 |
eg = ExceptionGroup("abc", [val1, typ1, val2, val3]) |
| 464 |
assert repr(eg.split(TypeError)) == "(ExceptionGroup('abc', [TypeError()]), ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)]))" |
| 465 |
assert repr(eg.split(ValueError)) == "(ExceptionGroup('abc', [ValueError(1), ValueError(2), ValueError(3)]), ExceptionGroup('abc', [TypeError()]))" |
| 466 |
assert repr(eg.split(tuple([]))) == "(None, ExceptionGroup('abc', [ValueError(1), TypeError(), ValueError(2), ValueError(3)]))" |
| 467 |
|
| 468 |
def test_split_bytype_multi_nested(): |
| 469 |
val1 = ValueError(1) |
| 470 |
typ1 = TypeError() |
| 471 |
val2 = ValueError(2) |
| 472 |
val3 = ValueError(3) |
| 473 |
typ2 = TypeError() |
| 474 |
key1 = KeyError() |
| 475 |
div1 = ZeroDivisionError() |
| 476 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 477 |
assert repr(eg.split((ValueError, TypeError))) == "(ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError()]), TypeError()]), ExceptionGroup('abc', [KeyError(), ExceptionGroup('def', [ZeroDivisionError()])]))" |
| 478 |
assert repr(eg.split((KeyError, KeyError))) == "(ExceptionGroup('abc', [KeyError()]), ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]))" |
| 479 |
assert repr(eg.split(tuple([]))) == "(None, ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]))" |
| 480 |
|
| 481 |
def test_split_bypredicate_passthrough(): |
| 482 |
val1 = ValueError(1) |
| 483 |
typ1 = TypeError() |
| 484 |
val2 = ValueError(2) |
| 485 |
val3 = ValueError(3) |
| 486 |
typ2 = TypeError() |
| 487 |
key1 = KeyError() |
| 488 |
div1 = ZeroDivisionError() |
| 489 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 490 |
assert repr(eg.split(lambda e: True)) == "(ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]), None)" |
| 491 |
|
| 492 |
def test_split_bypredicate_no_match(): |
| 493 |
val1 = ValueError(1) |
| 494 |
typ1 = TypeError() |
| 495 |
val2 = ValueError(2) |
| 496 |
val3 = ValueError(3) |
| 497 |
typ2 = TypeError() |
| 498 |
key1 = KeyError() |
| 499 |
div1 = ZeroDivisionError() |
| 500 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 501 |
assert repr(eg.split(lambda e: False)) == "(None, ExceptionGroup('abc', [KeyError(), ValueError(1), ExceptionGroup('def', [ValueError(2), ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]))" |
| 502 |
|
| 503 |
def test_split_bypredicate_nested(): |
| 504 |
val1 = ValueError(1) |
| 505 |
typ1 = TypeError() |
| 506 |
val2 = ValueError(2) |
| 507 |
val3 = ValueError(3) |
| 508 |
typ2 = TypeError() |
| 509 |
key1 = KeyError() |
| 510 |
div1 = ZeroDivisionError() |
| 511 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 512 |
assert repr(eg.split(lambda e: isinstance(e, ValueError) and e.args[0] < 3)) \ |
| 513 |
== "(ExceptionGroup('abc', [ValueError(1), ExceptionGroup('def', [ValueError(2)])]), ExceptionGroup('abc', [KeyError(), ExceptionGroup('def', [ValueError(3), TypeError(), ZeroDivisionError()]), TypeError()]))" |
| 514 |
|
| 515 |
def h_make_deep_eg(): |
| 516 |
e = TypeError(1) |
| 517 |
for _ in range(2000): |
| 518 |
e = ExceptionGroup("eg", [e]) |
| 519 |
return e |
| 520 |
|
| 521 |
def test_deep_split(): |
| 522 |
e = h_make_deep_eg() |
| 523 |
with pytest.raises(RecursionError): |
| 524 |
e.split(TypeError) |
| 525 |
|
| 526 |
def test_deep_subgroup(): |
| 527 |
e = h_make_deep_eg() |
| 528 |
with pytest.raises(RecursionError): |
| 529 |
e.subgroup(TypeError) |
| 530 |
|
| 531 |
def test_subgroup_copies_cause_etc(): |
| 532 |
e = ExceptionGroup("23", [TypeError(), ValueError()]) |
| 533 |
e.__notes__ = ['hello'] |
| 534 |
se = e.subgroup(ValueError) |
| 535 |
assert e.__cause__ == se.__cause__ |
| 536 |
assert e.__traceback__ == se.__traceback__ |
| 537 |
assert e.__context__ == se.__context__ |
| 538 |
assert e.__notes__ == se.__notes__ |
| 539 |
|
| 540 |
def test_derive_does_not_copies_cause_etc(): |
| 541 |
e = ExceptionGroup("23", [TypeError(), ValueError()]) |
| 542 |
e.__notes__ = ['hello'] |
| 543 |
se = e.derive([IndexError()]) |
| 544 |
assert se.__cause__ is None |
| 545 |
assert se.__traceback__ is None |
| 546 |
assert se.__context__ is None |
| 547 |
assert not hasattr(se, '__notes__') |
| 548 |
|
| 549 |
def test_derive_always_creates_exception_group(): |
| 550 |
class MyEG(ExceptionGroup): |
| 551 |
pass |
| 552 |
eg = MyEG("abc", [ValueError(), TypeError()]) |
| 553 |
eg2 = eg.derive([ValueError()]) |
| 554 |
assert type(eg2) is ExceptionGroup |
| 555 |
|
| 556 |
def test_init_called(): |
| 557 |
|
| 558 |
initcalled = [] |
| 559 |
|
| 560 |
class MyException(Exception): |
| 561 |
def __init__(self, message): |
| 562 |
initcalled.append(message) |
| 563 |
|
| 564 |
class MyExceptionGroup(ExceptionGroup, MyException): |
| 565 |
pass |
| 566 |
|
| 567 |
MyExceptionGroup("...", [Exception()]) |
| 568 |
assert len(initcalled) == 0 |
| 569 |
|
| 570 |
def test_enrich_attribute_error(): |
| 571 |
class A: |
| 572 |
def __getattr__(self, name): |
| 573 |
raise AttributeError("adds_name") |
| 574 |
a = A() |
| 575 |
with raises(AttributeError) as info: |
| 576 |
a.abc |
| 577 |
assert info.value.name =="abc" |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
if sys.implementation.name == 'pypy': |
| 582 |
from __exceptions__ import _prep_reraise_star, _collect_eg_leafs, _exception_group_projection |
| 583 |
from __pypy__ import identity_dict |
| 584 |
|
| 585 |
def test_prep_reraise_star_simple(): |
| 586 |
assert _prep_reraise_star(TypeError(), [None]) is None |
| 587 |
assert _prep_reraise_star(ExceptionGroup('abc', [ValueError(), TypeError()]), [None]) is None |
| 588 |
|
| 589 |
value = ValueError() |
| 590 |
res = _prep_reraise_star(ExceptionGroup('abc', [value, TypeError()]), [ExceptionGroup('abc', [value])]) |
| 591 |
assert repr(res) == "ExceptionGroup('abc', [ValueError()])" |
| 592 |
assert res.exceptions[0] is value |
| 593 |
|
| 594 |
def test_prep_reraise_exception_happens_in_except_star(): |
| 595 |
value = ValueError() |
| 596 |
full_eg = ExceptionGroup('abc', [value, TypeError()]) |
| 597 |
value_eg = ExceptionGroup('abc', [value]) |
| 598 |
try: |
| 599 |
raise Exception |
| 600 |
except Exception as e: |
| 601 |
tb1 = e.__traceback__ |
| 602 |
try: |
| 603 |
raise Exception |
| 604 |
except Exception as e: |
| 605 |
tb2 = e.__traceback__ |
| 606 |
full_eg.__traceback__ = tb1 |
| 607 |
value_eg.__traceback__ = tb1 |
| 608 |
zerodiv = ZeroDivisionError('division by zero') |
| 609 |
zerodiv.__traceback__ = tb2 |
| 610 |
assert repr(_prep_reraise_star(full_eg, [zerodiv, value_eg])) == "ExceptionGroup('', [ZeroDivisionError('division by zero'), ExceptionGroup('abc', [ValueError()])])" |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
def test_eg_leafs_basic(): |
| 615 |
t1, v1 = TypeError(), ValueError() |
| 616 |
exceptions = [t1, v1] |
| 617 |
message = "42" |
| 618 |
excgroup = ExceptionGroup(message, exceptions) |
| 619 |
resultset = identity_dict() |
| 620 |
_collect_eg_leafs(excgroup, resultset) |
| 621 |
print(resultset.keys()) |
| 622 |
assert t1 in resultset |
| 623 |
assert v1 in resultset |
| 624 |
|
| 625 |
def test_eg_leafs_null(): |
| 626 |
resultset = identity_dict() |
| 627 |
_collect_eg_leafs(None, resultset) |
| 628 |
assert not resultset |
| 629 |
|
| 630 |
def test_eg_leafs_nogroup(): |
| 631 |
exc = TypeError() |
| 632 |
resultset = identity_dict() |
| 633 |
_collect_eg_leafs(exc, resultset) |
| 634 |
assert exc in resultset |
| 635 |
|
| 636 |
def test_eg_leafs_recursive(): |
| 637 |
|
| 638 |
val1 = ValueError(1) |
| 639 |
typ1 = TypeError() |
| 640 |
val2 = ValueError(2) |
| 641 |
val3 = ValueError(3) |
| 642 |
typ2 = TypeError() |
| 643 |
key1 = KeyError() |
| 644 |
div1 = ZeroDivisionError() |
| 645 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 646 |
resultset = identity_dict() |
| 647 |
collected = _collect_eg_leafs(eg, resultset) |
| 648 |
assert len(resultset) == 7 |
| 649 |
for e in [val1, typ1, val2, val3, typ2, key1, div1]: |
| 650 |
assert e in resultset |
| 651 |
|
| 652 |
def test_exception_group_projection_basic(): |
| 653 |
val1 = ValueError(1) |
| 654 |
typ1 = TypeError() |
| 655 |
val2 = ValueError(2) |
| 656 |
val3 = ValueError(3) |
| 657 |
typ2 = TypeError() |
| 658 |
key1 = KeyError() |
| 659 |
div1 = ZeroDivisionError() |
| 660 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 661 |
keep1 = ExceptionGroup("meep", [key1, typ1]) |
| 662 |
keep2 = ExceptionGroup("moop", [val2, ExceptionGroup("doop", [val3])]) |
| 663 |
result = _exception_group_projection(eg, [keep1, keep2]) |
| 664 |
assert repr(result) == \ |
| 665 |
"ExceptionGroup('abc', [KeyError(), ExceptionGroup('def', [ValueError(2), ValueError(3)]), TypeError()])" |
| 666 |
|
| 667 |
def test_exception_group_projection_duplicated_in_keep(): |
| 668 |
val1 = ValueError(1) |
| 669 |
typ1 = TypeError() |
| 670 |
val2 = ValueError(2) |
| 671 |
val3 = ValueError(3) |
| 672 |
typ2 = TypeError() |
| 673 |
key1 = KeyError() |
| 674 |
div1 = ZeroDivisionError() |
| 675 |
eg = ExceptionGroup("abc", [key1, val1, ExceptionGroup("def", [val2, val3, typ2, div1]), typ1]) |
| 676 |
keep1 = ExceptionGroup("meep", [key1, typ1, val2]) |
| 677 |
keep2 = ExceptionGroup("moop", [val2, ExceptionGroup("doop", [key1, val3])]) |
| 678 |
result = _exception_group_projection(eg, [keep1, keep2]) |
| 679 |
assert repr(result) == \ |
| 680 |
"ExceptionGroup('abc', [KeyError(), ExceptionGroup('def', [ValueError(2), ValueError(3)]), TypeError()])" |
| 681 |
|
| 682 |
|