Debian Codegraph @

pypy/interpreter/cpycode.c

raw symbols SCIP
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 /* Total or partial match - update the stack from
35 * [val]
36 * to
37 * [rest, match]
38 * (rest can be Py_None)
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 /* reject except *ExceptionGroup */
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 /* Logic for matching an exception in an except* clause (too
114 complicated for inlining).
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 /* Full match of exc itself */
132 bool is_eg = _PyBaseExceptionGroup_Check(exc_value);
133 if (is_eg)
134 {
135 *match = Py_NewRef(exc_value);
136 }
137 else
138 {
139 /* naked exception - wrap it */
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 /* exc_value does not match match_type.
158 * Check for partial match if it's an exception group.
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 /* no match */
176 *match = Py_NewRef(Py_None);
177 *rest = Py_NewRef(Py_None);
178 return 0;
179 }
180
181 /*
182 This function is used by the interpreter to calculate
183 the exception group to be raised at the end of a
184 try-except* construct.
185
186 orig: the original except that was caught.
187 excs: a list of exceptions that were raised/reraised
188 in the except* clauses.
189
190 Calculates an exception group to raise. It contains
191 all exceptions in excs, where those that were reraised
192 have same nesting structure as in orig, and those that
193 were raised (if any) are added as siblings in a new EG.
194
195 Returns NULL and sets an exception on failure.
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 /* a naked exception was caught and wrapped. Only one except* clause
214 * could have executed,so there is at most one exception to raise.
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 /* Now we are holding refs to raised_list and reraised_list */
237
238 PyObject *result = NULL;
239
240 /* Split excs into raised and reraised by comparing metadata with orig */
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; // TODO Was ist hiermit?
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 /* This function is used by the interpreter to construct reraised
305 * exception groups. It takes an exception group eg and a list
306 * of exception groups keep and returns the sub-exception group
307 * of eg which contains all leaf exceptions that are contained
308 * in any exception group in keep.
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 /* Add IDs of all leaf exceptions in exc to the leaf_ids set */
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 /* recursive calls */
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 2 0 SETUP_EXCEPT 3 (to 8)
413
414 3 2 LOAD_GLOBAL 0 (TypeError)
415 4 CALL_FUNCTION 0
416 6 RAISE_VARARGS 1
417
418 4 >> 8 POP_TOP
419 10 DUP_TOP
420 12 BUILD_LIST 0
421 14 ROT_TWO
422 16 LOAD_GLOBAL 0 (TypeError)
423 18 <37>
424 20 DUP_TOP
425 22 LOAD_CONST 0 (None)
426 24 IS_OP 0
427 26 POP_JUMP_IF_TRUE 23 (to 46)
428 28 POP_TOP
429 30 SETUP_EXCEPT 4 (to 40)
430
431 5 32 LOAD_CONST 1 (1)
432 34 STORE_FAST 0 (a)
433 36 POP_BLOCK
434 38 JUMP_FORWARD 4 (to 48)
435 >> 40 LIST_APPEND 3
436 42 POP_TOP
437 44 JUMP_FORWARD 1 (to 48)
438 >> 46 POP_TOP
439
440 6 >> 48 LOAD_GLOBAL 1 (ValueError)
441 50 <37>
442 52 DUP_TOP
443 54 LOAD_CONST 0 (None)
444 56 IS_OP 0
445 58 POP_JUMP_IF_TRUE 39 (to 78)
446 60 POP_TOP
447 62 SETUP_EXCEPT 4 (to 72)
448
449 7 64 LOAD_CONST 2 (2)
450 66 STORE_FAST 0 (a)
451 68 POP_BLOCK
452 70 JUMP_FORWARD 4 (to 80)
453 >> 72 LIST_APPEND 3
454 74 POP_TOP
455 76 JUMP_FORWARD 1 (to 80)
456 >> 78 POP_TOP
457 >> 80 LIST_APPEND 1
458 82 <88>
459 84 DUP_TOP
460 86 LOAD_CONST 0 (None)
461 88 IS_OP 0
462 90 POP_JUMP_IF_FALSE 48 (to 96)
463 92 POP_TOP
464 94 JUMP_FORWARD 3 (to 102)
465
466 6 >> 96 POP_TOP
467 98 POP_TOP
468 100 RERAISE 0
469
470 8 >> 102 LOAD_CONST 1 (1)
471 104 STORE_FAST 1 (@py_assert2)
472 106 LOAD_FAST 0 (a)
473 108 LOAD_FAST 1 (@py_assert2)
474 110 COMPARE_OP 2 (==)
475 112 STORE_FAST 2 (@py_assert1)
476 114 LOAD_FAST 2 (@py_assert1)
477 116 POP_JUMP_IF_TRUE 107 (to 214)
478 118 LOAD_GLOBAL 2 (@pytest)
479 120 LOAD_METHOD 3 (ar_call_reprcompare)
480 122 LOAD_CONST 3 ((u'==',))
481 124 LOAD_FAST 2 (@py_assert1)
482 126 BUILD_TUPLE 1
483 128 LOAD_CONST 4 ((u'%(py0)s == %(py3)s',))
484 130 LOAD_FAST 0 (a)
485 132 LOAD_FAST 1 (@py_assert2)
486 134 BUILD_TUPLE 2
487 136 CALL_METHOD 4
488 138 LOAD_CONST 5 (u'a')
489 140 LOAD_GLOBAL 4 (@py_builtins)
490 142 LOAD_METHOD 5 (locals)
491 144 CALL_METHOD 0
492 146 CONTAINS_OP 0
493 148 POP_JUMP_IF_TRUE 80 (to 160)
494 150 LOAD_GLOBAL 2 (@pytest)
495 152 LOAD_METHOD 6 (ar_should_repr_global_name)
496 154 LOAD_FAST 0 (a)
497 156 CALL_METHOD 1
498 158 POP_JUMP_IF_FALSE 85 (to 170)
499 >> 160 LOAD_GLOBAL 2 (@pytest)
500 162 LOAD_METHOD 7 (ar_saferepr)
501 164 LOAD_FAST 0 (a)
502 166 CALL_METHOD 1
503 168 JUMP_FORWARD 1 (to 172)
504 >> 170 LOAD_CONST 5 (u'a')
505 >> 172 LOAD_GLOBAL 2 (@pytest)
506 174 LOAD_METHOD 7 (ar_saferepr)
507 176 LOAD_FAST 1 (@py_assert2)
508 178 CALL_METHOD 1
509 180 LOAD_CONST 6 ((u'py0', u'py3'))
510 182 BUILD_CONST_KEY_MAP 2
511 184 BINARY_MODULO
512 186 STORE_FAST 3 (@py_format4)
513 188 LOAD_CONST 7 (u'assert %(py5)s')
514 190 LOAD_FAST 3 (@py_format4)
515 192 LOAD_CONST 8 ((u'py5',))
516 194 BUILD_CONST_KEY_MAP 1
517 196 BINARY_MODULO
518 198 STORE_FAST 4 (@py_format6)
519 200 LOAD_GLOBAL 8 (AssertionError)
520 202 LOAD_GLOBAL 2 (@pytest)
521 204 LOAD_METHOD 9 (ar_format_explanation)
522 206 LOAD_FAST 4 (@py_format6)
523 208 CALL_METHOD 1
524 210 CALL_FUNCTION 1
525 212 RAISE_VARARGS 1
526 >> 214 LOAD_CONST 0 (None)
527 216 DUP_TOP
528 218 STORE_FAST 2 (@py_assert1)
529 220 STORE_FAST 1 (@py_assert2)
530 222 LOAD_CONST 0 (None)
531 224 RETURN_VALUE
532
533 */