1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9 #include "pycore_modsupport.h" // _PyArg_CheckPositional()
10
11 PyDoc_STRVAR(_testcapi_err_set_raised__doc__,
12 "err_set_raised($module, exception, /)\n"
13 "--\n"
14 "\n");
15
16 #define _TESTCAPI_ERR_SET_RAISED_METHODDEF \
17 {"err_set_raised", (PyCFunction)_testcapi_err_set_raised, METH_O, _testcapi_err_set_raised__doc__},
18
19 PyDoc_STRVAR(_testcapi_exception_print__doc__,
20 "exception_print($module, exception, legacy=False, /)\n"
21 "--\n"
22 "\n"
23 "To test the format of exceptions as printed out.");
24
25 #define _TESTCAPI_EXCEPTION_PRINT_METHODDEF \
26 {"exception_print", _PyCFunction_CAST(_testcapi_exception_print), METH_FASTCALL, _testcapi_exception_print__doc__},
27
28 static PyObject *
29 _testcapi_exception_print_impl(PyObject *module, PyObject *exc, int legacy);
30
31 static PyObject *
_testcapi_exception_print(PyObject * module,PyObject * const * args,Py_ssize_t nargs)32 _testcapi_exception_print(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
33 {
34 PyObject *return_value = NULL;
35 PyObject *exc;
36 int legacy = 0;
37
38 if (!_PyArg_CheckPositional("exception_print", nargs, 1, 2)) {
39 goto exit;
40 }
41 exc = args[0];
42 if (nargs < 2) {
43 goto skip_optional;
44 }
45 legacy = PyObject_IsTrue(args[1]);
46 if (legacy < 0) {
47 goto exit;
48 }
49 skip_optional:
50 return_value = _testcapi_exception_print_impl(module, exc, legacy);
51
52 exit:
53 return return_value;
54 }
55
56 PyDoc_STRVAR(_testcapi_make_exception_with_doc__doc__,
57 "make_exception_with_doc($module, /, name, doc=<unrepresentable>,\n"
58 " base=<unrepresentable>, dict=<unrepresentable>)\n"
59 "--\n"
60 "\n"
61 "Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). Run via Lib/test/test_exceptions.py");
62
63 #define _TESTCAPI_MAKE_EXCEPTION_WITH_DOC_METHODDEF \
64 {"make_exception_with_doc", _PyCFunction_CAST(_testcapi_make_exception_with_doc), METH_FASTCALL|METH_KEYWORDS, _testcapi_make_exception_with_doc__doc__},
65
66 static PyObject *
67 _testcapi_make_exception_with_doc_impl(PyObject *module, const char *name,
68 const char *doc, PyObject *base,
69 PyObject *dict);
70
71 static PyObject *
_testcapi_make_exception_with_doc(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)72 _testcapi_make_exception_with_doc(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
73 {
74 PyObject *return_value = NULL;
75 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
76
77 #define NUM_KEYWORDS 4
78 static struct {
79 PyGC_Head _this_is_not_used;
80 PyObject_VAR_HEAD
81 PyObject *ob_item[NUM_KEYWORDS];
82 } _kwtuple = {
83 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
84 .ob_item = { &_Py_ID(name), &_Py_ID(doc), &_Py_ID(base), &_Py_ID(dict), },
85 };
86 #undef NUM_KEYWORDS
87 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
88
89 #else // !Py_BUILD_CORE
90 # define KWTUPLE NULL
91 #endif // !Py_BUILD_CORE
92
93 static const char * const _keywords[] = {"name", "doc", "base", "dict", NULL};
94 static _PyArg_Parser _parser = {
95 .keywords = _keywords,
96 .fname = "make_exception_with_doc",
97 .kwtuple = KWTUPLE,
98 };
99 #undef KWTUPLE
100 PyObject *argsbuf[4];
101 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
102 const char *name;
103 const char *doc = NULL;
104 PyObject *base = NULL;
105 PyObject *dict = NULL;
106
107 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 4, 0, argsbuf);
108 if (!args) {
109 goto exit;
110 }
111 if (!PyUnicode_Check(args[0])) {
112 _PyArg_BadArgument("make_exception_with_doc", "argument 'name'", "str", args[0]);
113 goto exit;
114 }
115 Py_ssize_t name_length;
116 name = PyUnicode_AsUTF8AndSize(args[0], &name_length);
117 if (name == NULL) {
118 goto exit;
119 }
120 if (strlen(name) != (size_t)name_length) {
121 PyErr_SetString(PyExc_ValueError, "embedded null character");
122 goto exit;
123 }
124 if (!noptargs) {
125 goto skip_optional_pos;
126 }
127 if (args[1]) {
128 if (!PyUnicode_Check(args[1])) {
129 _PyArg_BadArgument("make_exception_with_doc", "argument 'doc'", "str", args[1]);
130 goto exit;
131 }
132 Py_ssize_t doc_length;
133 doc = PyUnicode_AsUTF8AndSize(args[1], &doc_length);
134 if (doc == NULL) {
135 goto exit;
136 }
137 if (strlen(doc) != (size_t)doc_length) {
138 PyErr_SetString(PyExc_ValueError, "embedded null character");
139 goto exit;
140 }
141 if (!--noptargs) {
142 goto skip_optional_pos;
143 }
144 }
145 if (args[2]) {
146 base = args[2];
147 if (!--noptargs) {
148 goto skip_optional_pos;
149 }
150 }
151 dict = args[3];
152 skip_optional_pos:
153 return_value = _testcapi_make_exception_with_doc_impl(module, name, doc, base, dict);
154
155 exit:
156 return return_value;
157 }
158
159 PyDoc_STRVAR(_testcapi_exc_set_object__doc__,
160 "exc_set_object($module, exception, obj, /)\n"
161 "--\n"
162 "\n");
163
164 #define _TESTCAPI_EXC_SET_OBJECT_METHODDEF \
165 {"exc_set_object", _PyCFunction_CAST(_testcapi_exc_set_object), METH_FASTCALL, _testcapi_exc_set_object__doc__},
166
167 static PyObject *
168 _testcapi_exc_set_object_impl(PyObject *module, PyObject *exc, PyObject *obj);
169
170 static PyObject *
_testcapi_exc_set_object(PyObject * module,PyObject * const * args,Py_ssize_t nargs)171 _testcapi_exc_set_object(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
172 {
173 PyObject *return_value = NULL;
174 PyObject *exc;
175 PyObject *obj;
176
177 if (!_PyArg_CheckPositional("exc_set_object", nargs, 2, 2)) {
178 goto exit;
179 }
180 exc = args[0];
181 obj = args[1];
182 return_value = _testcapi_exc_set_object_impl(module, exc, obj);
183
184 exit:
185 return return_value;
186 }
187
188 PyDoc_STRVAR(_testcapi_exc_set_object_fetch__doc__,
189 "exc_set_object_fetch($module, exception, obj, /)\n"
190 "--\n"
191 "\n");
192
193 #define _TESTCAPI_EXC_SET_OBJECT_FETCH_METHODDEF \
194 {"exc_set_object_fetch", _PyCFunction_CAST(_testcapi_exc_set_object_fetch), METH_FASTCALL, _testcapi_exc_set_object_fetch__doc__},
195
196 static PyObject *
197 _testcapi_exc_set_object_fetch_impl(PyObject *module, PyObject *exc,
198 PyObject *obj);
199
200 static PyObject *
_testcapi_exc_set_object_fetch(PyObject * module,PyObject * const * args,Py_ssize_t nargs)201 _testcapi_exc_set_object_fetch(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
202 {
203 PyObject *return_value = NULL;
204 PyObject *exc;
205 PyObject *obj;
206
207 if (!_PyArg_CheckPositional("exc_set_object_fetch", nargs, 2, 2)) {
208 goto exit;
209 }
210 exc = args[0];
211 obj = args[1];
212 return_value = _testcapi_exc_set_object_fetch_impl(module, exc, obj);
213
214 exit:
215 return return_value;
216 }
217
218 PyDoc_STRVAR(_testcapi_err_setstring__doc__,
219 "err_setstring($module, exc, value, /)\n"
220 "--\n"
221 "\n");
222
223 #define _TESTCAPI_ERR_SETSTRING_METHODDEF \
224 {"err_setstring", _PyCFunction_CAST(_testcapi_err_setstring), METH_FASTCALL, _testcapi_err_setstring__doc__},
225
226 static PyObject *
227 _testcapi_err_setstring_impl(PyObject *module, PyObject *exc,
228 const char *value, Py_ssize_t value_length);
229
230 static PyObject *
_testcapi_err_setstring(PyObject * module,PyObject * const * args,Py_ssize_t nargs)231 _testcapi_err_setstring(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
232 {
233 PyObject *return_value = NULL;
234 PyObject *exc;
235 const char *value;
236 Py_ssize_t value_length;
237
238 if (!_PyArg_ParseStack(args, nargs, "Oz#:err_setstring",
239 &exc, &value, &value_length)) {
240 goto exit;
241 }
242 return_value = _testcapi_err_setstring_impl(module, exc, value, value_length);
243
244 exit:
245 return return_value;
246 }
247
248 PyDoc_STRVAR(_testcapi_err_setfromerrnowithfilename__doc__,
249 "err_setfromerrnowithfilename($module, error, exc, value, /)\n"
250 "--\n"
251 "\n");
252
253 #define _TESTCAPI_ERR_SETFROMERRNOWITHFILENAME_METHODDEF \
254 {"err_setfromerrnowithfilename", _PyCFunction_CAST(_testcapi_err_setfromerrnowithfilename), METH_FASTCALL, _testcapi_err_setfromerrnowithfilename__doc__},
255
256 static PyObject *
257 _testcapi_err_setfromerrnowithfilename_impl(PyObject *module, int error,
258 PyObject *exc, const char *value,
259 Py_ssize_t value_length);
260
261 static PyObject *
_testcapi_err_setfromerrnowithfilename(PyObject * module,PyObject * const * args,Py_ssize_t nargs)262 _testcapi_err_setfromerrnowithfilename(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
263 {
264 PyObject *return_value = NULL;
265 int error;
266 PyObject *exc;
267 const char *value;
268 Py_ssize_t value_length;
269
270 if (!_PyArg_ParseStack(args, nargs, "iOz#:err_setfromerrnowithfilename",
271 &error, &exc, &value, &value_length)) {
272 goto exit;
273 }
274 return_value = _testcapi_err_setfromerrnowithfilename_impl(module, error, exc, value, value_length);
275
276 exit:
277 return return_value;
278 }
279
280 PyDoc_STRVAR(_testcapi_raise_exception__doc__,
281 "raise_exception($module, exception, num_args, /)\n"
282 "--\n"
283 "\n");
284
285 #define _TESTCAPI_RAISE_EXCEPTION_METHODDEF \
286 {"raise_exception", _PyCFunction_CAST(_testcapi_raise_exception), METH_FASTCALL, _testcapi_raise_exception__doc__},
287
288 static PyObject *
289 _testcapi_raise_exception_impl(PyObject *module, PyObject *exc, int num_args);
290
291 static PyObject *
_testcapi_raise_exception(PyObject * module,PyObject * const * args,Py_ssize_t nargs)292 _testcapi_raise_exception(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
293 {
294 PyObject *return_value = NULL;
295 PyObject *exc;
296 int num_args;
297
298 if (!_PyArg_CheckPositional("raise_exception", nargs, 2, 2)) {
299 goto exit;
300 }
301 exc = args[0];
302 num_args = PyLong_AsInt(args[1]);
303 if (num_args == -1 && PyErr_Occurred()) {
304 goto exit;
305 }
306 return_value = _testcapi_raise_exception_impl(module, exc, num_args);
307
308 exit:
309 return return_value;
310 }
311
312 PyDoc_STRVAR(_testcapi_raise_memoryerror__doc__,
313 "raise_memoryerror($module, /)\n"
314 "--\n"
315 "\n");
316
317 #define _TESTCAPI_RAISE_MEMORYERROR_METHODDEF \
318 {"raise_memoryerror", (PyCFunction)_testcapi_raise_memoryerror, METH_NOARGS, _testcapi_raise_memoryerror__doc__},
319
320 static PyObject *
321 _testcapi_raise_memoryerror_impl(PyObject *module);
322
323 static PyObject *
_testcapi_raise_memoryerror(PyObject * module,PyObject * Py_UNUSED (ignored))324 _testcapi_raise_memoryerror(PyObject *module, PyObject *Py_UNUSED(ignored))
325 {
326 return _testcapi_raise_memoryerror_impl(module);
327 }
328
329 PyDoc_STRVAR(_testcapi_fatal_error__doc__,
330 "fatal_error($module, message, release_gil=False, /)\n"
331 "--\n"
332 "\n");
333
334 #define _TESTCAPI_FATAL_ERROR_METHODDEF \
335 {"fatal_error", _PyCFunction_CAST(_testcapi_fatal_error), METH_FASTCALL, _testcapi_fatal_error__doc__},
336
337 static PyObject *
338 _testcapi_fatal_error_impl(PyObject *module, const char *message,
339 int release_gil);
340
341 static PyObject *
_testcapi_fatal_error(PyObject * module,PyObject * const * args,Py_ssize_t nargs)342 _testcapi_fatal_error(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
343 {
344 PyObject *return_value = NULL;
345 const char *message;
346 int release_gil = 0;
347
348 if (!_PyArg_ParseStack(args, nargs, "y|p:fatal_error",
349 &message, &release_gil)) {
350 goto exit;
351 }
352 return_value = _testcapi_fatal_error_impl(module, message, release_gil);
353
354 exit:
355 return return_value;
356 }
357
358 PyDoc_STRVAR(_testcapi_set_exc_info__doc__,
359 "set_exc_info($module, new_type, new_value, new_tb, /)\n"
360 "--\n"
361 "\n");
362
363 #define _TESTCAPI_SET_EXC_INFO_METHODDEF \
364 {"set_exc_info", _PyCFunction_CAST(_testcapi_set_exc_info), METH_FASTCALL, _testcapi_set_exc_info__doc__},
365
366 static PyObject *
367 _testcapi_set_exc_info_impl(PyObject *module, PyObject *new_type,
368 PyObject *new_value, PyObject *new_tb);
369
370 static PyObject *
_testcapi_set_exc_info(PyObject * module,PyObject * const * args,Py_ssize_t nargs)371 _testcapi_set_exc_info(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
372 {
373 PyObject *return_value = NULL;
374 PyObject *new_type;
375 PyObject *new_value;
376 PyObject *new_tb;
377
378 if (!_PyArg_CheckPositional("set_exc_info", nargs, 3, 3)) {
379 goto exit;
380 }
381 new_type = args[0];
382 new_value = args[1];
383 new_tb = args[2];
384 return_value = _testcapi_set_exc_info_impl(module, new_type, new_value, new_tb);
385
386 exit:
387 return return_value;
388 }
389
390 PyDoc_STRVAR(_testcapi_set_exception__doc__,
391 "set_exception($module, new_exc, /)\n"
392 "--\n"
393 "\n");
394
395 #define _TESTCAPI_SET_EXCEPTION_METHODDEF \
396 {"set_exception", (PyCFunction)_testcapi_set_exception, METH_O, _testcapi_set_exception__doc__},
397
398 PyDoc_STRVAR(_testcapi_traceback_print__doc__,
399 "traceback_print($module, traceback, file, /)\n"
400 "--\n"
401 "\n"
402 "To test the format of tracebacks as printed out.");
403
404 #define _TESTCAPI_TRACEBACK_PRINT_METHODDEF \
405 {"traceback_print", _PyCFunction_CAST(_testcapi_traceback_print), METH_FASTCALL, _testcapi_traceback_print__doc__},
406
407 static PyObject *
408 _testcapi_traceback_print_impl(PyObject *module, PyObject *traceback,
409 PyObject *file);
410
411 static PyObject *
_testcapi_traceback_print(PyObject * module,PyObject * const * args,Py_ssize_t nargs)412 _testcapi_traceback_print(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
413 {
414 PyObject *return_value = NULL;
415 PyObject *traceback;
416 PyObject *file;
417
418 if (!_PyArg_CheckPositional("traceback_print", nargs, 2, 2)) {
419 goto exit;
420 }
421 traceback = args[0];
422 file = args[1];
423 return_value = _testcapi_traceback_print_impl(module, traceback, file);
424
425 exit:
426 return return_value;
427 }
428
429 PyDoc_STRVAR(_testcapi_unstable_exc_prep_reraise_star__doc__,
430 "unstable_exc_prep_reraise_star($module, orig, excs, /)\n"
431 "--\n"
432 "\n"
433 "To test PyUnstable_Exc_PrepReraiseStar.");
434
435 #define _TESTCAPI_UNSTABLE_EXC_PREP_RERAISE_STAR_METHODDEF \
436 {"unstable_exc_prep_reraise_star", _PyCFunction_CAST(_testcapi_unstable_exc_prep_reraise_star), METH_FASTCALL, _testcapi_unstable_exc_prep_reraise_star__doc__},
437
438 static PyObject *
439 _testcapi_unstable_exc_prep_reraise_star_impl(PyObject *module,
440 PyObject *orig, PyObject *excs);
441
442 static PyObject *
_testcapi_unstable_exc_prep_reraise_star(PyObject * module,PyObject * const * args,Py_ssize_t nargs)443 _testcapi_unstable_exc_prep_reraise_star(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
444 {
445 PyObject *return_value = NULL;
446 PyObject *orig;
447 PyObject *excs;
448
449 if (!_PyArg_CheckPositional("unstable_exc_prep_reraise_star", nargs, 2, 2)) {
450 goto exit;
451 }
452 orig = args[0];
453 excs = args[1];
454 return_value = _testcapi_unstable_exc_prep_reraise_star_impl(module, orig, excs);
455
456 exit:
457 return return_value;
458 }
459 /*[clinic end generated code: output=0b11ef105030a48e input=a9049054013a1b77]*/
460