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