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 /* fall through */
39 case 2:
40 value = PyTuple_GetItem(args, 1);
41 Py_INCREF(value);
42 /* fall through */
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
364 /*
365 * Define the PyRecurdingInfinitelyError_Type
366 */
367
368 static PyTypeObject PyRecursingInfinitelyError_Type;
369
370 static int
371 recurse_infinitely_error_init(PyObject *self, PyObject *args, PyObject *kwds)
372 {
373 PyObject *type = (PyObject *)&PyRecursingInfinitelyError_Type;
374
375 /* Instantiating this exception starts infinite recursion. */
376 Py_INCREF(type);
377 PyErr_SetObject(type, NULL);
378 return -1;
379 }
380
381 static PyTypeObject PyRecursingInfinitelyError_Type = {
382 .tp_name = "RecursingInfinitelyError",
383 .tp_basicsize = sizeof(PyBaseExceptionObject),
384 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
385 .tp_doc = PyDoc_STR("Instantiating this exception starts infinite recursion."),
386 .tp_init = (initproc)recurse_infinitely_error_init,
387 };
388
389 static PyMethodDef test_methods[] = {
390 {"err_restore", err_restore, METH_VARARGS},
391 {"err_writeunraisable", err_writeunraisable, METH_VARARGS},
392 {"err_formatunraisable", err_formatunraisable, METH_VARARGS},
393 _TESTCAPI_ERR_SET_RAISED_METHODDEF
394 _TESTCAPI_EXCEPTION_PRINT_METHODDEF
395 _TESTCAPI_FATAL_ERROR_METHODDEF
396 _TESTCAPI_MAKE_EXCEPTION_WITH_DOC_METHODDEF
397 _TESTCAPI_EXC_SET_OBJECT_METHODDEF
398 _TESTCAPI_EXC_SET_OBJECT_FETCH_METHODDEF
399 _TESTCAPI_ERR_SETSTRING_METHODDEF
400 _TESTCAPI_ERR_SETFROMERRNOWITHFILENAME_METHODDEF
401 _TESTCAPI_RAISE_EXCEPTION_METHODDEF
402 _TESTCAPI_RAISE_MEMORYERROR_METHODDEF
403 _TESTCAPI_SET_EXC_INFO_METHODDEF
404 _TESTCAPI_SET_EXCEPTION_METHODDEF
405 _TESTCAPI_TRACEBACK_PRINT_METHODDEF
406 _TESTCAPI_UNSTABLE_EXC_PREP_RERAISE_STAR_METHODDEF
407 {NULL},
408 };
409
410 int
411 _PyTestCapi_Init_Exceptions(PyObject *mod)
412 {
413 PyRecursingInfinitelyError_Type.tp_base = (PyTypeObject *)PyExc_Exception;
414 if (PyType_Ready(&PyRecursingInfinitelyError_Type) < 0) {
415 return -1;
416 }
417 if (PyModule_AddObjectRef(mod, "RecursingInfinitelyError",
418 (PyObject *)&PyRecursingInfinitelyError_Type) < 0)
419 {
420 return -1;
421 }
422
423 if (PyModule_AddFunctions(mod, test_methods) < 0) {
424 return -1;
425 }
426
427 return 0;
428 }