Debian Codegraph @

Modules/_testcapi/exceptions.c

raw
1 // clinic/exceptions.c.h uses internal pycore_modsupport.h API
2 #define PYTESTCAPI_NEED_INTERNAL_API
3
4 #include "parts.h"
5 #include "util.h"
6
7 #include "clinic/exceptions.c.h"
8
9
10 /*[clinic input]
11 module _testcapi
12 [clinic start generated code]*/
13 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6361033e795369fc]*/
14
15 /*[clinic input]
16 _testcapi.err_set_raised
17 exception as exc: object
18 /
19 [clinic start generated code]*/
20
21 static PyObject *
22 _testcapi_err_set_raised(PyObject *module, PyObject *exc)
23 /*[clinic end generated code: output=0a0c7743961fcae5 input=c5f7331864a94df9]*/
24 {
25 Py_INCREF(exc);
26 PyErr_SetRaisedException(exc);
27 assert(PyErr_Occurred());
28 return NULL;
29 }
30
31 static PyObject *
32 err_restore(PyObject *self, PyObject *args) {
33 PyObject *type = NULL, *value = NULL, *traceback = NULL;
34 switch(PyTuple_Size(args)) {
35 case 3:
36 traceback = PyTuple_GetItem(args, 2);
37 Py_INCREF(traceback);
38 _Py_FALLTHROUGH;
39 case 2:
40 value = PyTuple_GetItem(args, 1);
41 Py_INCREF(value);
42 _Py_FALLTHROUGH;
43 case 1:
44 type = PyTuple_GetItem(args, 0);
45 Py_INCREF(type);
46 break;
47 default:
48 PyErr_SetString(PyExc_TypeError,
49 "wrong number of arguments");
50 return NULL;
51 }
52 PyErr_Restore(type, value, traceback);
53 assert(PyErr_Occurred());
54 return NULL;
55 }
56
57 /*[clinic input]
58 _testcapi.exception_print
59 exception as exc: object
60 legacy: bool = False
61 /
62
63 To test the format of exceptions as printed out.
64 [clinic start generated code]*/
65
66 static PyObject *
67 _testcapi_exception_print_impl(PyObject *module, PyObject *exc, int legacy)
68 /*[clinic end generated code: output=3f04fe0c18412ae0 input=c76f42cb94136dbf]*/
69 {
70 if (legacy) {
71 PyObject *tb = NULL;
72 if (PyExceptionInstance_Check(exc)) {
73 tb = PyException_GetTraceback(exc);
74 }
75 PyErr_Display((PyObject *) Py_TYPE(exc), exc, tb);
76 Py_XDECREF(tb);
77 }
78 else {
79 PyErr_DisplayException(exc);
80 }
81 Py_RETURN_NONE;
82 }
83
84 /*[clinic input]
85 _testcapi.make_exception_with_doc
86 name: str
87 doc: str = NULL
88 base: object = NULL
89 dict: object = NULL
90
91 Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). Run via Lib/test/test_exceptions.py
92 [clinic start generated code]*/
93
94 static PyObject *
95 _testcapi_make_exception_with_doc_impl(PyObject *module, const char *name,
96 const char *doc, PyObject *base,
97 PyObject *dict)
98 /*[clinic end generated code: output=439f0d963c1ce2c4 input=23a73013f8a8795a]*/
99 {
100 return PyErr_NewExceptionWithDoc(name, doc, base, dict);
101 }
102
103 /*[clinic input]
104 _testcapi.exc_set_object
105 exception as exc: object
106 obj: object
107 /
108 [clinic start generated code]*/
109
110 static PyObject *
111 _testcapi_exc_set_object_impl(PyObject *module, PyObject *exc, PyObject *obj)
112 /*[clinic end generated code: output=34c8c7c83e5c8463 input=fc530aafb1b0a360]*/
113 {
114 PyErr_SetObject(exc, obj);
115 return NULL;
116 }
117
118 /*[clinic input]
119 _testcapi.exc_set_object_fetch = _testcapi.exc_set_object
120 [clinic start generated code]*/
121
122 static PyObject *
123 _testcapi_exc_set_object_fetch_impl(PyObject *module, PyObject *exc,
124 PyObject *obj)
125 /*[clinic end generated code: output=7a5ff5f6d3cf687f input=77ec686f1f95fa38]*/
126 {
127 PyObject *type = UNINITIALIZED_PTR;
128 PyObject *value = UNINITIALIZED_PTR;
129 PyObject *tb = UNINITIALIZED_PTR;
130
131 PyErr_SetObject(exc, obj);
132 PyErr_Fetch(&type, &value, &tb);
133 assert(type != UNINITIALIZED_PTR);
134 assert(value != UNINITIALIZED_PTR);
135 assert(tb != UNINITIALIZED_PTR);
136 Py_XDECREF(type);
137 Py_XDECREF(tb);
138 return value;
139 }
140
141 /*[clinic input]
142 _testcapi.err_setstring
143 exc: object
144 value: str(zeroes=True, accept={robuffer, str, NoneType})
145 /
146 [clinic start generated code]*/
147
148 static PyObject *
149 _testcapi_err_setstring_impl(PyObject *module, PyObject *exc,
150 const char *value, Py_ssize_t value_length)
151 /*[clinic end generated code: output=fba8705e5703dd3f input=e8a95fad66d9004b]*/
152 {
153 NULLABLE(exc);
154 PyErr_SetString(exc, value);
155 return NULL;
156 }
157
158 /*[clinic input]
159 _testcapi.err_setfromerrnowithfilename
160 error: int
161 exc: object
162 value: str(zeroes=True, accept={robuffer, str, NoneType})
163 /
164 [clinic start generated code]*/
165
166 static PyObject *
167 _testcapi_err_setfromerrnowithfilename_impl(PyObject *module, int error,
168 PyObject *exc, const char *value,
169 Py_ssize_t value_length)
170 /*[clinic end generated code: output=d02df5749a01850e input=ff7c384234bf097f]*/
171 {
172 NULLABLE(exc);
173 errno = error;
174 PyErr_SetFromErrnoWithFilename(exc, value);
175 return NULL;
176 }
177
178 /*[clinic input]
179 _testcapi.raise_exception
180 exception as exc: object
181 num_args: int
182 /
183 [clinic start generated code]*/
184
185 static PyObject *
186 _testcapi_raise_exception_impl(PyObject *module, PyObject *exc, int num_args)
187 /*[clinic end generated code: output=eb0a9c5d69e0542d input=83d6262c3829d088]*/
188 {
189 PyObject *exc_args = PyTuple_New(num_args);
190 if (exc_args == NULL) {
191 return NULL;
192 }
193 for (int i = 0; i < num_args; ++i) {
194 PyObject *v = PyLong_FromLong(i);
195 if (v == NULL) {
196 Py_DECREF(exc_args);
197 return NULL;
198 }
199 PyTuple_SET_ITEM(exc_args, i, v);
200 }
201 PyErr_SetObject(exc, exc_args);
202 Py_DECREF(exc_args);
203 return NULL;
204 }
205
206 /*[clinic input]
207 _testcapi.raise_memoryerror
208 [clinic start generated code]*/
209
210 static PyObject *
211 _testcapi_raise_memoryerror_impl(PyObject *module)
212 /*[clinic end generated code: output=dd057803fb0131e6 input=6ca521bd07fb73cb]*/
213 {
214 return PyErr_NoMemory();
215 }
216
217 /*[clinic input]
218 _testcapi.fatal_error
219 message: str(accept={robuffer})
220 release_gil: bool = False
221 /
222 [clinic start generated code]*/
223
224 static PyObject *
225 _testcapi_fatal_error_impl(PyObject *module, const char *message,
226 int release_gil)
227 /*[clinic end generated code: output=9c3237116e6a03e8 input=1be357a2ccb04c8c]*/
228 {
229 if (release_gil) {
230 Py_BEGIN_ALLOW_THREADS
231 Py_FatalError(message);
232 Py_END_ALLOW_THREADS
233 }
234 else {
235 Py_FatalError(message);
236 }
237 // Py_FatalError() does not return, but exits the process.
238 Py_RETURN_NONE;
239 }
240
241 /*[clinic input]
242 _testcapi.set_exc_info
243 new_type: object
244 new_value: object
245 new_tb: object
246 /
247 [clinic start generated code]*/
248
249 static PyObject *
250 _testcapi_set_exc_info_impl(PyObject *module, PyObject *new_type,
251 PyObject *new_value, PyObject *new_tb)
252 /*[clinic end generated code: output=b55fa35dec31300e input=ea9f19e0f55fe5b3]*/
253 {
254 PyObject *type = UNINITIALIZED_PTR, *value = UNINITIALIZED_PTR, *tb = UNINITIALIZED_PTR;
255 PyErr_GetExcInfo(&type, &value, &tb);
256
257 Py_INCREF(new_type);
258 Py_INCREF(new_value);
259 Py_INCREF(new_tb);
260 PyErr_SetExcInfo(new_type, new_value, new_tb);
261
262 PyObject *orig_exc = PyTuple_Pack(3,
263 type ? type : Py_None,
264 value ? value : Py_None,
265 tb ? tb : Py_None);
266 Py_XDECREF(type);
267 Py_XDECREF(value);
268 Py_XDECREF(tb);
269 return orig_exc;
270 }
271
272 /*[clinic input]
273 _testcapi.set_exception
274 new_exc: object
275 /
276 [clinic start generated code]*/
277
278 static PyObject *
279 _testcapi_set_exception(PyObject *module, PyObject *new_exc)
280 /*[clinic end generated code: output=8b969b35d029e96d input=c89d4ca966c69738]*/
281 {
282 PyObject *exc = PyErr_GetHandledException();
283 assert(PyExceptionInstance_Check(exc) || exc == NULL);
284 PyErr_SetHandledException(new_exc);
285 return exc;
286 }
287
288 /*[clinic input]
289 _testcapi.traceback_print
290 traceback: object
291 file: object
292 /
293 To test the format of tracebacks as printed out.
294 [clinic start generated code]*/
295
296 static PyObject *
297 _testcapi_traceback_print_impl(PyObject *module, PyObject *traceback,
298 PyObject *file)
299 /*[clinic end generated code: output=17074ecf9d95cf30 input=9423f2857b008ca8]*/
300 {
301 if (PyTraceBack_Print(traceback, file) < 0) {
302 return NULL;
303 }
304 Py_RETURN_NONE;
305 }
306
307 static PyObject *
308 err_writeunraisable(PyObject *Py_UNUSED(module), PyObject *args)
309 {
310 PyObject *exc, *obj;
311 if (!PyArg_ParseTuple(args, "OO", &exc, &obj)) {
312 return NULL;
313 }
314 NULLABLE(exc);
315 NULLABLE(obj);
316 if (exc) {
317 PyErr_SetRaisedException(Py_NewRef(exc));
318 }
319 PyErr_WriteUnraisable(obj);
320 Py_RETURN_NONE;
321 }
322
323 static PyObject *
324 err_formatunraisable(PyObject *Py_UNUSED(module), PyObject *args)
325 {
326 PyObject *exc;
327 const char *fmt;
328 Py_ssize_t fmtlen;
329 PyObject *objs[10] = {NULL};
330
331 if (!PyArg_ParseTuple(args, "Oz#|OOOOOOOOOO", &exc, &fmt, &fmtlen,
332 &objs[0], &objs[1], &objs[2], &objs[3], &objs[4],
333 &objs[5], &objs[6], &objs[7], &objs[8], &objs[9]))
334 {
335 return NULL;
336 }
337 NULLABLE(exc);
338 if (exc) {
339 PyErr_SetRaisedException(Py_NewRef(exc));
340 }
341 PyErr_FormatUnraisable(fmt,
342 objs[0], objs[1], objs[2], objs[3], objs[4],
343 objs[5], objs[6], objs[7], objs[8], objs[9]);
344 Py_RETURN_NONE;
345 }
346
347 /*[clinic input]
348 _testcapi.unstable_exc_prep_reraise_star
349 orig: object
350 excs: object
351 /
352 To test PyUnstable_Exc_PrepReraiseStar.
353 [clinic start generated code]*/
354
355 static PyObject *
356 _testcapi_unstable_exc_prep_reraise_star_impl(PyObject *module,
357 PyObject *orig, PyObject *excs)
358 /*[clinic end generated code: output=850cf008e0563c77 input=27fbcda2203eb301]*/
359 {
360 return PyUnstable_Exc_PrepReraiseStar(orig, excs);
361 }
362
363 /* Test PyUnicodeEncodeError_GetStart */
364 static PyObject *
365 unicode_encode_get_start(PyObject *Py_UNUSED(module), PyObject *arg)
366 {
367 Py_ssize_t start;
368 if (PyUnicodeEncodeError_GetStart(arg, &start) < 0) {
369 return NULL;
370 }
371 RETURN_SIZE(start);
372 }
373
374 /* Test PyUnicodeDecodeError_GetStart */
375 static PyObject *
376 unicode_decode_get_start(PyObject *Py_UNUSED(module), PyObject *arg)
377 {
378 Py_ssize_t start;
379 if (PyUnicodeDecodeError_GetStart(arg, &start) < 0) {
380 return NULL;
381 }
382 RETURN_SIZE(start);
383 }
384
385 /* Test PyUnicodeTranslateError_GetStart */
386 static PyObject *
387 unicode_translate_get_start(PyObject *Py_UNUSED(module), PyObject *arg)
388 {
389 Py_ssize_t start;
390 if (PyUnicodeTranslateError_GetStart(arg, &start) < 0) {
391 return NULL;
392 }
393 RETURN_SIZE(start);
394 }
395
396 /* Test PyUnicodeEncodeError_SetStart */
397 static PyObject *
398 unicode_encode_set_start(PyObject *Py_UNUSED(module), PyObject *args)
399 {
400 PyObject *exc;
401 Py_ssize_t start;
402 if (PyArg_ParseTuple(args, "On", &exc, &start) < 0) {
403 return NULL;
404 }
405 if (PyUnicodeEncodeError_SetStart(exc, start) < 0) {
406 return NULL;
407 }
408 Py_RETURN_NONE;
409 }
410
411 /* Test PyUnicodeDecodeError_SetStart */
412 static PyObject *
413 unicode_decode_set_start(PyObject *Py_UNUSED(module), PyObject *args)
414 {
415 PyObject *exc;
416 Py_ssize_t start;
417 if (PyArg_ParseTuple(args, "On", &exc, &start) < 0) {
418 return NULL;
419 }
420 if (PyUnicodeDecodeError_SetStart(exc, start) < 0) {
421 return NULL;
422 }
423 Py_RETURN_NONE;
424 }
425
426 /* Test PyUnicodeTranslateError_SetStart */
427 static PyObject *
428 unicode_translate_set_start(PyObject *Py_UNUSED(module), PyObject *args)
429 {
430 PyObject *exc;
431 Py_ssize_t start;
432 if (PyArg_ParseTuple(args, "On", &exc, &start) < 0) {
433 return NULL;
434 }
435 if (PyUnicodeTranslateError_SetStart(exc, start) < 0) {
436 return NULL;
437 }
438 Py_RETURN_NONE;
439 }
440
441 /* Test PyUnicodeEncodeError_GetEnd */
442 static PyObject *
443 unicode_encode_get_end(PyObject *Py_UNUSED(module), PyObject *arg)
444 {
445 Py_ssize_t end;
446 if (PyUnicodeEncodeError_GetEnd(arg, &end) < 0) {
447 return NULL;
448 }
449 RETURN_SIZE(end);
450 }
451
452 /* Test PyUnicodeDecodeError_GetEnd */
453 static PyObject *
454 unicode_decode_get_end(PyObject *Py_UNUSED(module), PyObject *arg)
455 {
456 Py_ssize_t end;
457 if (PyUnicodeDecodeError_GetEnd(arg, &end) < 0) {
458 return NULL;
459 }
460 RETURN_SIZE(end);
461 }
462
463 /* Test PyUnicodeTranslateError_GetEnd */
464 static PyObject *
465 unicode_translate_get_end(PyObject *Py_UNUSED(module), PyObject *arg)
466 {
467 Py_ssize_t end;
468 if (PyUnicodeTranslateError_GetEnd(arg, &end) < 0) {
469 return NULL;
470 }
471 RETURN_SIZE(end);
472 }
473
474 /* Test PyUnicodeEncodeError_SetEnd */
475 static PyObject *
476 unicode_encode_set_end(PyObject *Py_UNUSED(module), PyObject *args)
477 {
478 PyObject *exc;
479 Py_ssize_t end;
480 if (PyArg_ParseTuple(args, "On", &exc, &end) < 0) {
481 return NULL;
482 }
483 if (PyUnicodeEncodeError_SetEnd(exc, end) < 0) {
484 return NULL;
485 }
486 Py_RETURN_NONE;
487 }
488
489 /* Test PyUnicodeDecodeError_SetEnd */
490 static PyObject *
491 unicode_decode_set_end(PyObject *Py_UNUSED(module), PyObject *args)
492 {
493 PyObject *exc;
494 Py_ssize_t end;
495 if (PyArg_ParseTuple(args, "On", &exc, &end) < 0) {
496 return NULL;
497 }
498 if (PyUnicodeDecodeError_SetEnd(exc, end) < 0) {
499 return NULL;
500 }
501 Py_RETURN_NONE;
502 }
503
504 /* Test PyUnicodeTranslateError_SetEnd */
505 static PyObject *
506 unicode_translate_set_end(PyObject *Py_UNUSED(module), PyObject *args)
507 {
508 PyObject *exc;
509 Py_ssize_t end;
510 if (PyArg_ParseTuple(args, "On", &exc, &end) < 0) {
511 return NULL;
512 }
513 if (PyUnicodeTranslateError_SetEnd(exc, end) < 0) {
514 return NULL;
515 }
516 Py_RETURN_NONE;
517 }
518
519 /*
520 * Define the PyRecurdingInfinitelyError_Type
521 */
522
523 static PyTypeObject PyRecursingInfinitelyError_Type;
524
525 static int
526 recurse_infinitely_error_init(PyObject *self, PyObject *args, PyObject *kwds)
527 {
528 PyObject *type = (PyObject *)&PyRecursingInfinitelyError_Type;
529
530 /* Instantiating this exception starts infinite recursion. */
531 Py_INCREF(type);
532 PyErr_SetObject(type, NULL);
533 return -1;
534 }
535
536 static PyTypeObject PyRecursingInfinitelyError_Type = {
537 .tp_name = "RecursingInfinitelyError",
538 .tp_basicsize = sizeof(PyBaseExceptionObject),
539 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
540 .tp_doc = PyDoc_STR("Instantiating this exception starts infinite recursion."),
541 .tp_init = recurse_infinitely_error_init,
542 };
543
544 static PyMethodDef test_methods[] = {
545 {"err_restore", err_restore, METH_VARARGS},
546 {"err_writeunraisable", err_writeunraisable, METH_VARARGS},
547 {"err_formatunraisable", err_formatunraisable, METH_VARARGS},
548 _TESTCAPI_ERR_SET_RAISED_METHODDEF
549 _TESTCAPI_EXCEPTION_PRINT_METHODDEF
550 _TESTCAPI_FATAL_ERROR_METHODDEF
551 _TESTCAPI_MAKE_EXCEPTION_WITH_DOC_METHODDEF
552 _TESTCAPI_EXC_SET_OBJECT_METHODDEF
553 _TESTCAPI_EXC_SET_OBJECT_FETCH_METHODDEF
554 _TESTCAPI_ERR_SETSTRING_METHODDEF
555 _TESTCAPI_ERR_SETFROMERRNOWITHFILENAME_METHODDEF
556 _TESTCAPI_RAISE_EXCEPTION_METHODDEF
557 _TESTCAPI_RAISE_MEMORYERROR_METHODDEF
558 _TESTCAPI_SET_EXC_INFO_METHODDEF
559 _TESTCAPI_SET_EXCEPTION_METHODDEF
560 _TESTCAPI_TRACEBACK_PRINT_METHODDEF
561 _TESTCAPI_UNSTABLE_EXC_PREP_RERAISE_STAR_METHODDEF
562 {"unicode_encode_get_start", unicode_encode_get_start, METH_O},
563 {"unicode_decode_get_start", unicode_decode_get_start, METH_O},
564 {"unicode_translate_get_start", unicode_translate_get_start, METH_O},
565 {"unicode_encode_set_start", unicode_encode_set_start, METH_VARARGS},
566 {"unicode_decode_set_start", unicode_decode_set_start, METH_VARARGS},
567 {"unicode_translate_set_start", unicode_translate_set_start, METH_VARARGS},
568 {"unicode_encode_get_end", unicode_encode_get_end, METH_O},
569 {"unicode_decode_get_end", unicode_decode_get_end, METH_O},
570 {"unicode_translate_get_end", unicode_translate_get_end, METH_O},
571 {"unicode_encode_set_end", unicode_encode_set_end, METH_VARARGS},
572 {"unicode_decode_set_end", unicode_decode_set_end, METH_VARARGS},
573 {"unicode_translate_set_end", unicode_translate_set_end, METH_VARARGS},
574 {NULL},
575 };
576
577 int
578 _PyTestCapi_Init_Exceptions(PyObject *mod)
579 {
580 PyRecursingInfinitelyError_Type.tp_base = (PyTypeObject *)PyExc_Exception;
581 if (PyType_Ready(&PyRecursingInfinitelyError_Type) < 0) {
582 return -1;
583 }
584 if (PyModule_AddObjectRef(mod, "RecursingInfinitelyError",
585 (PyObject *)&PyRecursingInfinitelyError_Type) < 0)
586 {
587 return -1;
588 }
589
590 if (PyModule_AddFunctions(mod, test_methods) < 0) {
591 return -1;
592 }
593
594 return 0;
595 }