| 1 |
|
| 2 |
CHECK_EG_MATCH |
| 3 |
{ |
| 4 |
PyObject *match_type = POP(); |
| 5 |
if (check_except_star_type_valid(tstate, match_type) < 0) |
| 6 |
{ |
| 7 |
Py_DECREF(match_type); |
| 8 |
goto error; |
| 9 |
} |
| 10 |
|
| 11 |
PyObject *exc_value = TOP(); |
| 12 |
PyObject *match = NULL, *rest = NULL; |
| 13 |
int res = exception_group_match(exc_value, match_type, |
| 14 |
&match, &rest); |
| 15 |
Py_DECREF(match_type); |
| 16 |
if (res < 0) |
| 17 |
{ |
| 18 |
goto error; |
| 19 |
} |
| 20 |
|
| 21 |
if (match == NULL || rest == NULL) |
| 22 |
{ |
| 23 |
assert(match == NULL); |
| 24 |
assert(rest == NULL); |
| 25 |
goto error; |
| 26 |
} |
| 27 |
if (Py_IsNone(match)) |
| 28 |
{ |
| 29 |
PUSH(match); |
| 30 |
Py_XDECREF(rest); |
| 31 |
} |
| 32 |
else |
| 33 |
{ |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
SET_TOP(rest); |
| 42 |
PUSH(match); |
| 43 |
PyErr_SetExcInfo(NULL, Py_NewRef(match), NULL); |
| 44 |
Py_DECREF(exc_value); |
| 45 |
} |
| 46 |
DISPATCH(); |
| 47 |
} |
| 48 |
|
| 49 |
PREP_RERAISE_STAR |
| 50 |
{ |
| 51 |
PyObject *excs = POP(); |
| 52 |
assert(PyList_Check(excs)); |
| 53 |
PyObject *orig = POP(); |
| 54 |
|
| 55 |
PyObject *val = _PyExc_PrepReraiseStar(orig, excs); |
| 56 |
Py_DECREF(excs); |
| 57 |
Py_DECREF(orig); |
| 58 |
|
| 59 |
if (val == NULL) |
| 60 |
{ |
| 61 |
goto error; |
| 62 |
} |
| 63 |
|
| 64 |
PUSH(val); |
| 65 |
DISPATCH(); |
| 66 |
} |
| 67 |
|
| 68 |
static int |
| 69 |
check_except_star_type_valid(PyThreadState *tstate, PyObject *right) |
| 70 |
{ |
| 71 |
if (check_except_type_valid(tstate, right) < 0) |
| 72 |
{ |
| 73 |
return -1; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
int is_subclass = 0; |
| 79 |
if (PyTuple_Check(right)) |
| 80 |
{ |
| 81 |
Py_ssize_t length = PyTuple_GET_SIZE(right); |
| 82 |
for (Py_ssize_t i = 0; i < length; i++) |
| 83 |
{ |
| 84 |
PyObject *exc = PyTuple_GET_ITEM(right, i); |
| 85 |
is_subclass = PyObject_IsSubclass(exc, PyExc_BaseExceptionGroup); |
| 86 |
if (is_subclass < 0) |
| 87 |
{ |
| 88 |
return -1; |
| 89 |
} |
| 90 |
if (is_subclass) |
| 91 |
{ |
| 92 |
break; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
else |
| 97 |
{ |
| 98 |
is_subclass = PyObject_IsSubclass(right, PyExc_BaseExceptionGroup); |
| 99 |
if (is_subclass < 0) |
| 100 |
{ |
| 101 |
return -1; |
| 102 |
} |
| 103 |
} |
| 104 |
if (is_subclass) |
| 105 |
{ |
| 106 |
_PyErr_SetString(tstate, PyExc_TypeError, |
| 107 |
CANNOT_EXCEPT_STAR_EG); |
| 108 |
return -1; |
| 109 |
} |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static int |
| 118 |
exception_group_match(PyObject *exc_value, PyObject *match_type, |
| 119 |
PyObject **match, PyObject **rest) |
| 120 |
{ |
| 121 |
if (Py_IsNone(exc_value)) |
| 122 |
{ |
| 123 |
*match = Py_NewRef(Py_None); |
| 124 |
*rest = Py_NewRef(Py_None); |
| 125 |
return 0; |
| 126 |
} |
| 127 |
assert(PyExceptionInstance_Check(exc_value)); |
| 128 |
|
| 129 |
if (PyErr_GivenExceptionMatches(exc_value, match_type)) |
| 130 |
{ |
| 131 |
|
| 132 |
bool is_eg = _PyBaseExceptionGroup_Check(exc_value); |
| 133 |
if (is_eg) |
| 134 |
{ |
| 135 |
*match = Py_NewRef(exc_value); |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
|
| 140 |
PyObject *excs = PyTuple_Pack(1, exc_value); |
| 141 |
if (excs == NULL) |
| 142 |
{ |
| 143 |
return -1; |
| 144 |
} |
| 145 |
PyObject *wrapped = _PyExc_CreateExceptionGroup("", excs); |
| 146 |
Py_DECREF(excs); |
| 147 |
if (wrapped == NULL) |
| 148 |
{ |
| 149 |
return -1; |
| 150 |
} |
| 151 |
*match = wrapped; |
| 152 |
} |
| 153 |
*rest = Py_NewRef(Py_None); |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
if (_PyBaseExceptionGroup_Check(exc_value)) |
| 161 |
{ |
| 162 |
PyObject *pair = PyObject_CallMethod(exc_value, "split", "(O)", |
| 163 |
match_type); |
| 164 |
if (pair == NULL) |
| 165 |
{ |
| 166 |
return -1; |
| 167 |
} |
| 168 |
assert(PyTuple_CheckExact(pair)); |
| 169 |
assert(PyTuple_GET_SIZE(pair) == 2); |
| 170 |
*match = Py_NewRef(PyTuple_GET_ITEM(pair, 0)); |
| 171 |
*rest = Py_NewRef(PyTuple_GET_ITEM(pair, 1)); |
| 172 |
Py_DECREF(pair); |
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
*match = Py_NewRef(Py_None); |
| 177 |
*rest = Py_NewRef(Py_None); |
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
PyObject * |
| 199 |
_PyExc_PrepReraiseStar(PyObject *orig, PyObject *excs) |
| 200 |
{ |
| 201 |
assert(PyExceptionInstance_Check(orig)); |
| 202 |
assert(PyList_Check(excs)); |
| 203 |
|
| 204 |
Py_ssize_t numexcs = PyList_GET_SIZE(excs); |
| 205 |
|
| 206 |
if (numexcs == 0) |
| 207 |
{ |
| 208 |
return Py_NewRef(Py_None); |
| 209 |
} |
| 210 |
|
| 211 |
if (!_PyBaseExceptionGroup_Check(orig)) |
| 212 |
{ |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
assert(numexcs == 1 || (numexcs == 2 && PyList_GET_ITEM(excs, 1) == Py_None)); |
| 218 |
|
| 219 |
PyObject *e = PyList_GET_ITEM(excs, 0); |
| 220 |
assert(e != NULL); |
| 221 |
return Py_NewRef(e); |
| 222 |
} |
| 223 |
|
| 224 |
PyObject *raised_list = PyList_New(0); |
| 225 |
if (raised_list == NULL) |
| 226 |
{ |
| 227 |
return NULL; |
| 228 |
} |
| 229 |
PyObject *reraised_list = PyList_New(0); |
| 230 |
if (reraised_list == NULL) |
| 231 |
{ |
| 232 |
Py_DECREF(raised_list); |
| 233 |
return NULL; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
PyObject *result = NULL; |
| 239 |
|
| 240 |
|
| 241 |
for (Py_ssize_t i = 0; i < numexcs; i++) |
| 242 |
{ |
| 243 |
PyObject *e = PyList_GET_ITEM(excs, i); |
| 244 |
assert(e != NULL); |
| 245 |
if (Py_IsNone(e)) |
| 246 |
{ |
| 247 |
continue; |
| 248 |
} |
| 249 |
bool is_reraise = is_same_exception_metadata(e, orig); |
| 250 |
PyObject *append_list = is_reraise ? reraised_list : raised_list; |
| 251 |
if (PyList_Append(append_list, e) < 0) |
| 252 |
{ |
| 253 |
goto done; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
PyObject *reraised_eg = exception_group_projection(orig, reraised_list); |
| 258 |
if (reraised_eg == NULL) |
| 259 |
{ |
| 260 |
goto done; |
| 261 |
} |
| 262 |
|
| 263 |
if (!Py_IsNone(reraised_eg)) |
| 264 |
{ |
| 265 |
assert(is_same_exception_metadata(reraised_eg, orig)); |
| 266 |
} |
| 267 |
Py_ssize_t num_raised = PyList_GET_SIZE(raised_list); |
| 268 |
if (num_raised == 0) |
| 269 |
{ |
| 270 |
result = reraised_eg; |
| 271 |
} |
| 272 |
else if (num_raised > 0) |
| 273 |
{ |
| 274 |
int res = 0; |
| 275 |
if (!Py_IsNone(reraised_eg)) |
| 276 |
{ |
| 277 |
res = PyList_Append(raised_list, reraised_eg); |
| 278 |
} |
| 279 |
Py_DECREF(reraised_eg); |
| 280 |
if (res < 0) |
| 281 |
{ |
| 282 |
goto done; |
| 283 |
} |
| 284 |
if (PyList_GET_SIZE(raised_list) > 1) |
| 285 |
{ |
| 286 |
result = _PyExc_CreateExceptionGroup("", raised_list); |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
result = Py_NewRef(PyList_GetItem(raised_list, 0)); |
| 291 |
} |
| 292 |
if (result == NULL) |
| 293 |
{ |
| 294 |
goto done; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
done: |
| 299 |
Py_XDECREF(raised_list); |
| 300 |
Py_XDECREF(reraised_list); |
| 301 |
return result; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
static PyObject * |
| 311 |
exception_group_projection(PyObject *eg, PyObject *keep) |
| 312 |
{ |
| 313 |
assert(_PyBaseExceptionGroup_Check(eg)); |
| 314 |
assert(PyList_CheckExact(keep)); |
| 315 |
|
| 316 |
PyObject *leaf_ids = PySet_New(NULL); |
| 317 |
if (!leaf_ids) |
| 318 |
{ |
| 319 |
return NULL; |
| 320 |
} |
| 321 |
|
| 322 |
Py_ssize_t n = PyList_GET_SIZE(keep); |
| 323 |
for (Py_ssize_t i = 0; i < n; i++) |
| 324 |
{ |
| 325 |
PyObject *e = PyList_GET_ITEM(keep, i); |
| 326 |
assert(e != NULL); |
| 327 |
assert(_PyBaseExceptionGroup_Check(e)); |
| 328 |
if (collect_exception_group_leaf_ids(e, leaf_ids) < 0) |
| 329 |
{ |
| 330 |
Py_DECREF(leaf_ids); |
| 331 |
return NULL; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
_exceptiongroup_split_result split_result; |
| 336 |
bool construct_rest = false; |
| 337 |
int err = exceptiongroup_split_recursive( |
| 338 |
eg, EXCEPTION_GROUP_MATCH_INSTANCE_IDS, leaf_ids, |
| 339 |
construct_rest, &split_result); |
| 340 |
Py_DECREF(leaf_ids); |
| 341 |
if (err < 0) |
| 342 |
{ |
| 343 |
return NULL; |
| 344 |
} |
| 345 |
|
| 346 |
PyObject *result = split_result.match ? split_result.match : Py_NewRef(Py_None); |
| 347 |
assert(split_result.rest == NULL); |
| 348 |
return result; |
| 349 |
} |
| 350 |
|
| 351 |
static bool |
| 352 |
is_same_exception_metadata(PyObject *exc1, PyObject *exc2) |
| 353 |
{ |
| 354 |
assert(PyExceptionInstance_Check(exc1)); |
| 355 |
assert(PyExceptionInstance_Check(exc2)); |
| 356 |
|
| 357 |
PyBaseExceptionObject *e1 = (PyBaseExceptionObject *)exc1; |
| 358 |
PyBaseExceptionObject *e2 = (PyBaseExceptionObject *)exc2; |
| 359 |
|
| 360 |
return (e1->notes == e2->notes && |
| 361 |
e1->traceback == e2->traceback && |
| 362 |
e1->cause == e2->cause && |
| 363 |
e1->context == e2->context); |
| 364 |
} |
| 365 |
|
| 366 |
static int |
| 367 |
collect_exception_group_leaf_ids(PyObject *exc, PyObject *leaf_ids) |
| 368 |
{ |
| 369 |
if (Py_IsNone(exc)) |
| 370 |
{ |
| 371 |
return 0; |
| 372 |
} |
| 373 |
|
| 374 |
assert(PyExceptionInstance_Check(exc)); |
| 375 |
assert(PySet_Check(leaf_ids)); |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
if (!_PyBaseExceptionGroup_Check(exc)) |
| 380 |
{ |
| 381 |
PyObject *exc_id = PyLong_FromVoidPtr(exc); |
| 382 |
if (exc_id == NULL) |
| 383 |
{ |
| 384 |
return -1; |
| 385 |
} |
| 386 |
int res = PySet_Add(leaf_ids, exc_id); |
| 387 |
Py_DECREF(exc_id); |
| 388 |
return res; |
| 389 |
} |
| 390 |
PyBaseExceptionGroupObject *eg = _PyBaseExceptionGroupObject_cast(exc); |
| 391 |
Py_ssize_t num_excs = PyTuple_GET_SIZE(eg->excs); |
| 392 |
|
| 393 |
for (Py_ssize_t i = 0; i < num_excs; i++) |
| 394 |
{ |
| 395 |
PyObject *e = PyTuple_GET_ITEM(eg->excs, i); |
| 396 |
if (_Py_EnterRecursiveCall(" in collect_exception_group_leaf_ids")) |
| 397 |
{ |
| 398 |
return -1; |
| 399 |
} |
| 400 |
int res = collect_exception_group_leaf_ids(e, leaf_ids); |
| 401 |
_Py_LeaveRecursiveCall(); |
| 402 |
if (res < 0) |
| 403 |
{ |
| 404 |
return -1; |
| 405 |
} |
| 406 |
} |
| 407 |
return 0; |
| 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 |
|