| 1 |
/* |
| 2 |
* Tests for Python/getargs.c and Python/modsupport.c; |
| 3 |
* APIs that parse and build arguments. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "parts.h" |
| 7 |
|
| 8 |
static PyObject * |
| 9 |
parse_tuple_and_keywords(PyObject *self, PyObject *args) |
| 10 |
{ |
| 11 |
PyObject *sub_args; |
| 12 |
PyObject *sub_kwargs; |
| 13 |
const char *sub_format; |
| 14 |
PyObject *sub_keywords; |
| 15 |
|
| 16 |
#define MAX_PARAMS 8 |
| 17 |
double buffers[MAX_PARAMS][4]; /* double ensures alignment where necessary */ |
| 18 |
char *keywords[MAX_PARAMS + 1]; /* space for NULL at end */ |
| 19 |
|
| 20 |
PyObject *return_value = NULL; |
| 21 |
|
| 22 |
if (!PyArg_ParseTuple(args, "OOsO:parse_tuple_and_keywords", |
| 23 |
&sub_args, &sub_kwargs, &sub_format, &sub_keywords)) |
| 24 |
{ |
| 25 |
return NULL; |
| 26 |
} |
| 27 |
|
| 28 |
if (!(PyList_CheckExact(sub_keywords) || |
| 29 |
PyTuple_CheckExact(sub_keywords))) |
| 30 |
{ |
| 31 |
PyErr_SetString(PyExc_ValueError, |
| 32 |
"parse_tuple_and_keywords: " |
| 33 |
"sub_keywords must be either list or tuple"); |
| 34 |
return NULL; |
| 35 |
} |
| 36 |
|
| 37 |
memset(buffers, 0, sizeof(buffers)); |
| 38 |
memset(keywords, 0, sizeof(keywords)); |
| 39 |
|
| 40 |
Py_ssize_t size = PySequence_Fast_GET_SIZE(sub_keywords); |
| 41 |
if (size > MAX_PARAMS) { |
| 42 |
PyErr_SetString(PyExc_ValueError, |
| 43 |
"parse_tuple_and_keywords: too many keywords in sub_keywords"); |
| 44 |
goto exit; |
| 45 |
} |
| 46 |
|
| 47 |
for (Py_ssize_t i = 0; i < size; i++) { |
| 48 |
PyObject *o = PySequence_Fast_GET_ITEM(sub_keywords, i); |
| 49 |
if (PyUnicode_Check(o)) { |
| 50 |
keywords[i] = (char *)PyUnicode_AsUTF8(o); |
| 51 |
if (keywords[i] == NULL) { |
| 52 |
goto exit; |
| 53 |
} |
| 54 |
} |
| 55 |
else if (PyBytes_Check(o)) { |
| 56 |
keywords[i] = PyBytes_AS_STRING(o); |
| 57 |
} |
| 58 |
else { |
| 59 |
PyErr_SetString(PyExc_ValueError, |
| 60 |
"parse_tuple_and_keywords: " |
| 61 |
"keywords must be str or bytes"); |
| 62 |
goto exit; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
assert(MAX_PARAMS == 8); |
| 67 |
int result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs, |
| 68 |
sub_format, keywords, |
| 69 |
buffers + 0, buffers + 1, buffers + 2, buffers + 3, |
| 70 |
buffers + 4, buffers + 5, buffers + 6, buffers + 7); |
| 71 |
|
| 72 |
if (result) { |
| 73 |
int objects_only = 1; |
| 74 |
int count = 0; |
| 75 |
for (const char *f = sub_format; *f; f++) { |
| 76 |
if (Py_ISALNUM(*f)) { |
| 77 |
if (strchr("OSUY", *f) == NULL) { |
| 78 |
objects_only = 0; |
| 79 |
break; |
| 80 |
} |
| 81 |
count++; |
| 82 |
} |
| 83 |
} |
| 84 |
if (objects_only) { |
| 85 |
return_value = PyTuple_New(count); |
| 86 |
if (return_value == NULL) { |
| 87 |
goto exit; |
| 88 |
} |
| 89 |
for (Py_ssize_t i = 0; i < count; i++) { |
| 90 |
PyObject *arg = *(PyObject **)(buffers + i); |
| 91 |
if (arg == NULL) { |
| 92 |
arg = Py_None; |
| 93 |
} |
| 94 |
PyTuple_SET_ITEM(return_value, i, Py_NewRef(arg)); |
| 95 |
} |
| 96 |
} |
| 97 |
else { |
| 98 |
return_value = Py_NewRef(Py_None); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
exit: |
| 103 |
return return_value; |
| 104 |
} |
| 105 |
|
| 106 |
static PyObject * |
| 107 |
get_args(PyObject *self, PyObject *args) |
| 108 |
{ |
| 109 |
if (args == NULL) { |
| 110 |
args = Py_None; |
| 111 |
} |
| 112 |
return Py_NewRef(args); |
| 113 |
} |
| 114 |
|
| 115 |
static PyObject * |
| 116 |
get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs) |
| 117 |
{ |
| 118 |
if (kwargs == NULL) { |
| 119 |
kwargs = Py_None; |
| 120 |
} |
| 121 |
return Py_NewRef(kwargs); |
| 122 |
} |
| 123 |
|
| 124 |
static PyObject * |
| 125 |
getargs_w_star(PyObject *self, PyObject *args) |
| 126 |
{ |
| 127 |
Py_buffer buffer; |
| 128 |
|
| 129 |
if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) { |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
if (2 <= buffer.len) { |
| 134 |
char *str = buffer.buf; |
| 135 |
str[0] = '['; |
| 136 |
str[buffer.len-1] = ']'; |
| 137 |
} |
| 138 |
|
| 139 |
PyObject *result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 140 |
PyBuffer_Release(&buffer); |
| 141 |
return result; |
| 142 |
} |
| 143 |
|
| 144 |
static PyObject * |
| 145 |
getargs_w_star_opt(PyObject *self, PyObject *args) |
| 146 |
{ |
| 147 |
Py_buffer buffer; |
| 148 |
Py_buffer buf2; |
| 149 |
int number = 1; |
| 150 |
|
| 151 |
if (!PyArg_ParseTuple(args, "w*|w*i:getargs_w_star", |
| 152 |
&buffer, &buf2, &number)) { |
| 153 |
return NULL; |
| 154 |
} |
| 155 |
|
| 156 |
if (2 <= buffer.len) { |
| 157 |
char *str = buffer.buf; |
| 158 |
str[0] = '['; |
| 159 |
str[buffer.len-1] = ']'; |
| 160 |
} |
| 161 |
|
| 162 |
PyObject *result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 163 |
PyBuffer_Release(&buffer); |
| 164 |
return result; |
| 165 |
} |
| 166 |
|
| 167 |
/* Test the old w and w# codes that no longer work */ |
| 168 |
static PyObject * |
| 169 |
test_w_code_invalid(PyObject *self, PyObject *arg) |
| 170 |
{ |
| 171 |
static const char * const keywords[] = {"a", "b", "c", "d", NULL}; |
| 172 |
char *formats_3[] = {"O|w#$O", |
| 173 |
"O|w$O", |
| 174 |
"O|w#O", |
| 175 |
"O|wO", |
| 176 |
NULL}; |
| 177 |
char *formats_4[] = {"O|w#O$O", |
| 178 |
"O|wO$O", |
| 179 |
"O|Ow#O", |
| 180 |
"O|OwO", |
| 181 |
"O|Ow#$O", |
| 182 |
"O|Ow$O", |
| 183 |
NULL}; |
| 184 |
size_t n; |
| 185 |
PyObject *args; |
| 186 |
PyObject *kwargs; |
| 187 |
PyObject *tmp; |
| 188 |
|
| 189 |
if (!(args = PyTuple_Pack(1, Py_None))) { |
| 190 |
return NULL; |
| 191 |
} |
| 192 |
|
| 193 |
kwargs = PyDict_New(); |
| 194 |
if (!kwargs) { |
| 195 |
Py_DECREF(args); |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
|
| 199 |
if (PyDict_SetItemString(kwargs, "c", Py_None)) { |
| 200 |
Py_DECREF(args); |
| 201 |
Py_XDECREF(kwargs); |
| 202 |
return NULL; |
| 203 |
} |
| 204 |
|
| 205 |
for (n = 0; formats_3[n]; ++n) { |
| 206 |
if (PyArg_ParseTupleAndKeywords(args, kwargs, formats_3[n], |
| 207 |
(char**) keywords, |
| 208 |
&tmp, &tmp, &tmp)) { |
| 209 |
Py_DECREF(args); |
| 210 |
Py_DECREF(kwargs); |
| 211 |
PyErr_Format(PyExc_AssertionError, |
| 212 |
"test_w_code_invalid_suffix: %s", |
| 213 |
formats_3[n]); |
| 214 |
return NULL; |
| 215 |
} |
| 216 |
else { |
| 217 |
if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
| 218 |
Py_DECREF(args); |
| 219 |
Py_DECREF(kwargs); |
| 220 |
return NULL; |
| 221 |
} |
| 222 |
PyErr_Clear(); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if (PyDict_DelItemString(kwargs, "c") || |
| 227 |
PyDict_SetItemString(kwargs, "d", Py_None)) { |
| 228 |
|
| 229 |
Py_DECREF(kwargs); |
| 230 |
Py_DECREF(args); |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
|
| 234 |
for (n = 0; formats_4[n]; ++n) { |
| 235 |
if (PyArg_ParseTupleAndKeywords(args, kwargs, formats_4[n], |
| 236 |
(char**) keywords, |
| 237 |
&tmp, &tmp, &tmp, &tmp)) { |
| 238 |
Py_DECREF(args); |
| 239 |
Py_DECREF(kwargs); |
| 240 |
PyErr_Format(PyExc_AssertionError, |
| 241 |
"test_w_code_invalid_suffix: %s", |
| 242 |
formats_4[n]); |
| 243 |
return NULL; |
| 244 |
} |
| 245 |
else { |
| 246 |
if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
| 247 |
Py_DECREF(args); |
| 248 |
Py_DECREF(kwargs); |
| 249 |
return NULL; |
| 250 |
} |
| 251 |
PyErr_Clear(); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
Py_DECREF(args); |
| 256 |
Py_DECREF(kwargs); |
| 257 |
Py_RETURN_NONE; |
| 258 |
} |
| 259 |
|
| 260 |
static PyObject * |
| 261 |
getargs_empty(PyObject *self, PyObject *args, PyObject *kwargs) |
| 262 |
{ |
| 263 |
/* Test that formats can begin with '|'. See issue #4720. */ |
| 264 |
assert(PyTuple_CheckExact(args)); |
| 265 |
assert(kwargs == NULL || PyDict_CheckExact(kwargs)); |
| 266 |
|
| 267 |
int result; |
| 268 |
if (kwargs != NULL && PyDict_GET_SIZE(kwargs) > 0) { |
| 269 |
static char *kwlist[] = {NULL}; |
| 270 |
result = PyArg_ParseTupleAndKeywords(args, kwargs, "|:getargs_empty", |
| 271 |
kwlist); |
| 272 |
} |
| 273 |
else { |
| 274 |
result = PyArg_ParseTuple(args, "|:getargs_empty"); |
| 275 |
} |
| 276 |
if (!result) { |
| 277 |
return NULL; |
| 278 |
} |
| 279 |
return PyLong_FromLong(result); |
| 280 |
} |
| 281 |
|
| 282 |
/* Test tuple argument processing */ |
| 283 |
static PyObject * |
| 284 |
getargs_tuple(PyObject *self, PyObject *args) |
| 285 |
{ |
| 286 |
int a, b, c; |
| 287 |
if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) { |
| 288 |
return NULL; |
| 289 |
} |
| 290 |
return Py_BuildValue("iii", a, b, c); |
| 291 |
} |
| 292 |
|
| 293 |
/* test PyArg_ParseTupleAndKeywords */ |
| 294 |
static PyObject * |
| 295 |
getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 296 |
{ |
| 297 |
static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 298 |
static const char fmt[] = "(ii)i|(i(ii))(iii)i"; |
| 299 |
int int_args[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
| 300 |
|
| 301 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 302 |
&int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 303 |
&int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 304 |
{ |
| 305 |
return NULL; |
| 306 |
} |
| 307 |
return Py_BuildValue("iiiiiiiiii", |
| 308 |
int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 309 |
int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]); |
| 310 |
} |
| 311 |
|
| 312 |
/* test PyArg_ParseTupleAndKeywords keyword-only arguments */ |
| 313 |
static PyObject * |
| 314 |
getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs) |
| 315 |
{ |
| 316 |
static char *keywords[] = {"required", "optional", "keyword_only", NULL}; |
| 317 |
int required = -1; |
| 318 |
int optional = -1; |
| 319 |
int keyword_only = -1; |
| 320 |
|
| 321 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords, |
| 322 |
&required, &optional, &keyword_only)) |
| 323 |
{ |
| 324 |
return NULL; |
| 325 |
} |
| 326 |
return Py_BuildValue("iii", required, optional, keyword_only); |
| 327 |
} |
| 328 |
|
| 329 |
/* test PyArg_ParseTupleAndKeywords positional-only arguments */ |
| 330 |
static PyObject * |
| 331 |
getargs_positional_only_and_keywords(PyObject *self, PyObject *args, |
| 332 |
PyObject *kwargs) |
| 333 |
{ |
| 334 |
static char *keywords[] = {"", "", "keyword", NULL}; |
| 335 |
int required = -1; |
| 336 |
int optional = -1; |
| 337 |
int keyword = -1; |
| 338 |
|
| 339 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords, |
| 340 |
&required, &optional, &keyword)) |
| 341 |
{ |
| 342 |
return NULL; |
| 343 |
} |
| 344 |
return Py_BuildValue("iii", required, optional, keyword); |
| 345 |
} |
| 346 |
|
| 347 |
/* Functions to call PyArg_ParseTuple with integer format codes, |
| 348 |
and return the result. |
| 349 |
*/ |
| 350 |
static PyObject * |
| 351 |
getargs_b(PyObject *self, PyObject *args) |
| 352 |
{ |
| 353 |
unsigned char value; |
| 354 |
if (!PyArg_ParseTuple(args, "b", &value)) { |
| 355 |
return NULL; |
| 356 |
} |
| 357 |
return PyLong_FromUnsignedLong((unsigned long)value); |
| 358 |
} |
| 359 |
|
| 360 |
static PyObject * |
| 361 |
getargs_B(PyObject *self, PyObject *args) |
| 362 |
{ |
| 363 |
unsigned char value; |
| 364 |
if (!PyArg_ParseTuple(args, "B", &value)) { |
| 365 |
return NULL; |
| 366 |
} |
| 367 |
return PyLong_FromUnsignedLong((unsigned long)value); |
| 368 |
} |
| 369 |
|
| 370 |
static PyObject * |
| 371 |
getargs_h(PyObject *self, PyObject *args) |
| 372 |
{ |
| 373 |
short value; |
| 374 |
if (!PyArg_ParseTuple(args, "h", &value)) { |
| 375 |
return NULL; |
| 376 |
} |
| 377 |
return PyLong_FromLong((long)value); |
| 378 |
} |
| 379 |
|
| 380 |
static PyObject * |
| 381 |
getargs_H(PyObject *self, PyObject *args) |
| 382 |
{ |
| 383 |
unsigned short value; |
| 384 |
if (!PyArg_ParseTuple(args, "H", &value)) { |
| 385 |
return NULL; |
| 386 |
} |
| 387 |
return PyLong_FromUnsignedLong((unsigned long)value); |
| 388 |
} |
| 389 |
|
| 390 |
static PyObject * |
| 391 |
getargs_I(PyObject *self, PyObject *args) |
| 392 |
{ |
| 393 |
unsigned int value; |
| 394 |
if (!PyArg_ParseTuple(args, "I", &value)) { |
| 395 |
return NULL; |
| 396 |
} |
| 397 |
return PyLong_FromUnsignedLong((unsigned long)value); |
| 398 |
} |
| 399 |
|
| 400 |
static PyObject * |
| 401 |
getargs_k(PyObject *self, PyObject *args) |
| 402 |
{ |
| 403 |
unsigned long value; |
| 404 |
if (!PyArg_ParseTuple(args, "k", &value)) { |
| 405 |
return NULL; |
| 406 |
} |
| 407 |
return PyLong_FromUnsignedLong(value); |
| 408 |
} |
| 409 |
|
| 410 |
static PyObject * |
| 411 |
getargs_i(PyObject *self, PyObject *args) |
| 412 |
{ |
| 413 |
int value; |
| 414 |
if (!PyArg_ParseTuple(args, "i", &value)) { |
| 415 |
return NULL; |
| 416 |
} |
| 417 |
return PyLong_FromLong((long)value); |
| 418 |
} |
| 419 |
|
| 420 |
static PyObject * |
| 421 |
getargs_l(PyObject *self, PyObject *args) |
| 422 |
{ |
| 423 |
long value; |
| 424 |
if (!PyArg_ParseTuple(args, "l", &value)) { |
| 425 |
return NULL; |
| 426 |
} |
| 427 |
return PyLong_FromLong(value); |
| 428 |
} |
| 429 |
|
| 430 |
static PyObject * |
| 431 |
getargs_n(PyObject *self, PyObject *args) |
| 432 |
{ |
| 433 |
Py_ssize_t value; |
| 434 |
if (!PyArg_ParseTuple(args, "n", &value)) { |
| 435 |
return NULL; |
| 436 |
} |
| 437 |
return PyLong_FromSsize_t(value); |
| 438 |
} |
| 439 |
|
| 440 |
static PyObject * |
| 441 |
getargs_p(PyObject *self, PyObject *args) |
| 442 |
{ |
| 443 |
int value; |
| 444 |
if (!PyArg_ParseTuple(args, "p", &value)) { |
| 445 |
return NULL; |
| 446 |
} |
| 447 |
return PyLong_FromLong(value); |
| 448 |
} |
| 449 |
|
| 450 |
static PyObject * |
| 451 |
getargs_L(PyObject *self, PyObject *args) |
| 452 |
{ |
| 453 |
long long value; |
| 454 |
if (!PyArg_ParseTuple(args, "L", &value)) { |
| 455 |
return NULL; |
| 456 |
} |
| 457 |
return PyLong_FromLongLong(value); |
| 458 |
} |
| 459 |
|
| 460 |
static PyObject * |
| 461 |
getargs_K(PyObject *self, PyObject *args) |
| 462 |
{ |
| 463 |
unsigned long long value; |
| 464 |
if (!PyArg_ParseTuple(args, "K", &value)) { |
| 465 |
return NULL; |
| 466 |
} |
| 467 |
return PyLong_FromUnsignedLongLong(value); |
| 468 |
} |
| 469 |
|
| 470 |
static PyObject * |
| 471 |
getargs_f(PyObject *self, PyObject *args) |
| 472 |
{ |
| 473 |
float f; |
| 474 |
if (!PyArg_ParseTuple(args, "f", &f)) { |
| 475 |
return NULL; |
| 476 |
} |
| 477 |
return PyFloat_FromDouble(f); |
| 478 |
} |
| 479 |
|
| 480 |
static PyObject * |
| 481 |
getargs_d(PyObject *self, PyObject *args) |
| 482 |
{ |
| 483 |
double d; |
| 484 |
if (!PyArg_ParseTuple(args, "d", &d)) { |
| 485 |
return NULL; |
| 486 |
} |
| 487 |
return PyFloat_FromDouble(d); |
| 488 |
} |
| 489 |
|
| 490 |
static PyObject * |
| 491 |
getargs_D(PyObject *self, PyObject *args) |
| 492 |
{ |
| 493 |
Py_complex cval; |
| 494 |
if (!PyArg_ParseTuple(args, "D", &cval)) { |
| 495 |
return NULL; |
| 496 |
} |
| 497 |
return PyComplex_FromCComplex(cval); |
| 498 |
} |
| 499 |
|
| 500 |
static PyObject * |
| 501 |
getargs_S(PyObject *self, PyObject *args) |
| 502 |
{ |
| 503 |
PyObject *obj; |
| 504 |
if (!PyArg_ParseTuple(args, "S", &obj)) { |
| 505 |
return NULL; |
| 506 |
} |
| 507 |
return Py_NewRef(obj); |
| 508 |
} |
| 509 |
|
| 510 |
static PyObject * |
| 511 |
getargs_Y(PyObject *self, PyObject *args) |
| 512 |
{ |
| 513 |
PyObject *obj; |
| 514 |
if (!PyArg_ParseTuple(args, "Y", &obj)) { |
| 515 |
return NULL; |
| 516 |
} |
| 517 |
return Py_NewRef(obj); |
| 518 |
} |
| 519 |
|
| 520 |
static PyObject * |
| 521 |
getargs_U(PyObject *self, PyObject *args) |
| 522 |
{ |
| 523 |
PyObject *obj; |
| 524 |
if (!PyArg_ParseTuple(args, "U", &obj)) { |
| 525 |
return NULL; |
| 526 |
} |
| 527 |
return Py_NewRef(obj); |
| 528 |
} |
| 529 |
|
| 530 |
static PyObject * |
| 531 |
getargs_c(PyObject *self, PyObject *args) |
| 532 |
{ |
| 533 |
char c; |
| 534 |
if (!PyArg_ParseTuple(args, "c", &c)) { |
| 535 |
return NULL; |
| 536 |
} |
| 537 |
return PyLong_FromLong((unsigned char)c); |
| 538 |
} |
| 539 |
|
| 540 |
static PyObject * |
| 541 |
getargs_C(PyObject *self, PyObject *args) |
| 542 |
{ |
| 543 |
int c; |
| 544 |
if (!PyArg_ParseTuple(args, "C", &c)) { |
| 545 |
return NULL; |
| 546 |
} |
| 547 |
return PyLong_FromLong(c); |
| 548 |
} |
| 549 |
|
| 550 |
static PyObject * |
| 551 |
getargs_s(PyObject *self, PyObject *args) |
| 552 |
{ |
| 553 |
char *str; |
| 554 |
if (!PyArg_ParseTuple(args, "s", &str)) { |
| 555 |
return NULL; |
| 556 |
} |
| 557 |
return PyBytes_FromString(str); |
| 558 |
} |
| 559 |
|
| 560 |
static PyObject * |
| 561 |
getargs_s_star(PyObject *self, PyObject *args) |
| 562 |
{ |
| 563 |
Py_buffer buffer; |
| 564 |
PyObject *bytes; |
| 565 |
if (!PyArg_ParseTuple(args, "s*", &buffer)) { |
| 566 |
return NULL; |
| 567 |
} |
| 568 |
bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 569 |
PyBuffer_Release(&buffer); |
| 570 |
return bytes; |
| 571 |
} |
| 572 |
|
| 573 |
static PyObject * |
| 574 |
getargs_s_hash(PyObject *self, PyObject *args) |
| 575 |
{ |
| 576 |
char *str; |
| 577 |
Py_ssize_t size; |
| 578 |
if (!PyArg_ParseTuple(args, "s#", &str, &size)) { |
| 579 |
return NULL; |
| 580 |
} |
| 581 |
return PyBytes_FromStringAndSize(str, size); |
| 582 |
} |
| 583 |
|
| 584 |
static PyObject * |
| 585 |
getargs_z(PyObject *self, PyObject *args) |
| 586 |
{ |
| 587 |
char *str; |
| 588 |
if (!PyArg_ParseTuple(args, "z", &str)) { |
| 589 |
return NULL; |
| 590 |
} |
| 591 |
if (str != NULL) { |
| 592 |
return PyBytes_FromString(str); |
| 593 |
} |
| 594 |
Py_RETURN_NONE; |
| 595 |
} |
| 596 |
|
| 597 |
static PyObject * |
| 598 |
getargs_z_star(PyObject *self, PyObject *args) |
| 599 |
{ |
| 600 |
Py_buffer buffer; |
| 601 |
PyObject *bytes; |
| 602 |
if (!PyArg_ParseTuple(args, "z*", &buffer)) { |
| 603 |
return NULL; |
| 604 |
} |
| 605 |
if (buffer.buf != NULL) { |
| 606 |
bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 607 |
} |
| 608 |
else { |
| 609 |
bytes = Py_NewRef(Py_None); |
| 610 |
} |
| 611 |
PyBuffer_Release(&buffer); |
| 612 |
return bytes; |
| 613 |
} |
| 614 |
|
| 615 |
static PyObject * |
| 616 |
getargs_z_hash(PyObject *self, PyObject *args) |
| 617 |
{ |
| 618 |
char *str; |
| 619 |
Py_ssize_t size; |
| 620 |
if (!PyArg_ParseTuple(args, "z#", &str, &size)) { |
| 621 |
return NULL; |
| 622 |
} |
| 623 |
if (str != NULL) { |
| 624 |
return PyBytes_FromStringAndSize(str, size); |
| 625 |
} |
| 626 |
Py_RETURN_NONE; |
| 627 |
} |
| 628 |
|
| 629 |
static PyObject * |
| 630 |
getargs_y(PyObject *self, PyObject *args) |
| 631 |
{ |
| 632 |
char *str; |
| 633 |
if (!PyArg_ParseTuple(args, "y", &str)) { |
| 634 |
return NULL; |
| 635 |
} |
| 636 |
return PyBytes_FromString(str); |
| 637 |
} |
| 638 |
|
| 639 |
static PyObject * |
| 640 |
getargs_y_star(PyObject *self, PyObject *args) |
| 641 |
{ |
| 642 |
Py_buffer buffer; |
| 643 |
if (!PyArg_ParseTuple(args, "y*", &buffer)) { |
| 644 |
return NULL; |
| 645 |
} |
| 646 |
PyObject *bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 647 |
PyBuffer_Release(&buffer); |
| 648 |
return bytes; |
| 649 |
} |
| 650 |
|
| 651 |
static PyObject * |
| 652 |
getargs_y_hash(PyObject *self, PyObject *args) |
| 653 |
{ |
| 654 |
char *str; |
| 655 |
Py_ssize_t size; |
| 656 |
if (!PyArg_ParseTuple(args, "y#", &str, &size)) { |
| 657 |
return NULL; |
| 658 |
} |
| 659 |
return PyBytes_FromStringAndSize(str, size); |
| 660 |
} |
| 661 |
|
| 662 |
static PyObject * |
| 663 |
getargs_es(PyObject *self, PyObject *args) |
| 664 |
{ |
| 665 |
PyObject *arg; |
| 666 |
const char *encoding = NULL; |
| 667 |
char *str; |
| 668 |
|
| 669 |
if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) { |
| 670 |
return NULL; |
| 671 |
} |
| 672 |
if (!PyArg_Parse(arg, "es", encoding, &str)) { |
| 673 |
return NULL; |
| 674 |
} |
| 675 |
PyObject *result = PyBytes_FromString(str); |
| 676 |
PyMem_Free(str); |
| 677 |
return result; |
| 678 |
} |
| 679 |
|
| 680 |
static PyObject * |
| 681 |
getargs_et(PyObject *self, PyObject *args) |
| 682 |
{ |
| 683 |
PyObject *arg; |
| 684 |
const char *encoding = NULL; |
| 685 |
char *str; |
| 686 |
|
| 687 |
if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) { |
| 688 |
return NULL; |
| 689 |
} |
| 690 |
if (!PyArg_Parse(arg, "et", encoding, &str)) { |
| 691 |
return NULL; |
| 692 |
} |
| 693 |
PyObject *result = PyBytes_FromString(str); |
| 694 |
PyMem_Free(str); |
| 695 |
return result; |
| 696 |
} |
| 697 |
|
| 698 |
static PyObject * |
| 699 |
getargs_es_hash(PyObject *self, PyObject *args) |
| 700 |
{ |
| 701 |
PyObject *arg; |
| 702 |
const char *encoding = NULL; |
| 703 |
PyByteArrayObject *buffer = NULL; |
| 704 |
char *str = NULL; |
| 705 |
Py_ssize_t size; |
| 706 |
|
| 707 |
if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) { |
| 708 |
return NULL; |
| 709 |
} |
| 710 |
if (buffer != NULL) { |
| 711 |
str = PyByteArray_AS_STRING(buffer); |
| 712 |
size = PyByteArray_GET_SIZE(buffer); |
| 713 |
} |
| 714 |
if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) { |
| 715 |
return NULL; |
| 716 |
} |
| 717 |
PyObject *result = PyBytes_FromStringAndSize(str, size); |
| 718 |
if (buffer == NULL) { |
| 719 |
PyMem_Free(str); |
| 720 |
} |
| 721 |
return result; |
| 722 |
} |
| 723 |
|
| 724 |
static PyObject * |
| 725 |
getargs_et_hash(PyObject *self, PyObject *args) |
| 726 |
{ |
| 727 |
PyObject *arg; |
| 728 |
const char *encoding = NULL; |
| 729 |
PyByteArrayObject *buffer = NULL; |
| 730 |
char *str = NULL; |
| 731 |
Py_ssize_t size; |
| 732 |
|
| 733 |
if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) { |
| 734 |
return NULL; |
| 735 |
} |
| 736 |
if (buffer != NULL) { |
| 737 |
str = PyByteArray_AS_STRING(buffer); |
| 738 |
size = PyByteArray_GET_SIZE(buffer); |
| 739 |
} |
| 740 |
if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) { |
| 741 |
return NULL; |
| 742 |
} |
| 743 |
PyObject *result = PyBytes_FromStringAndSize(str, size); |
| 744 |
if (buffer == NULL) { |
| 745 |
PyMem_Free(str); |
| 746 |
} |
| 747 |
return result; |
| 748 |
} |
| 749 |
|
| 750 |
static PyObject * |
| 751 |
gh_99240_clear_args(PyObject *self, PyObject *args) |
| 752 |
{ |
| 753 |
char *a = NULL; |
| 754 |
char *b = NULL; |
| 755 |
|
| 756 |
if (!PyArg_ParseTuple(args, "eses", "idna", &a, "idna", &b)) { |
| 757 |
if (a || b) { |
| 758 |
PyErr_Clear(); |
| 759 |
PyErr_SetString(PyExc_AssertionError, "Arguments are not cleared."); |
| 760 |
} |
| 761 |
return NULL; |
| 762 |
} |
| 763 |
PyMem_Free(a); |
| 764 |
PyMem_Free(b); |
| 765 |
Py_RETURN_NONE; |
| 766 |
} |
| 767 |
|
| 768 |
static PyMethodDef test_methods[] = { |
| 769 |
{"get_args", get_args, METH_VARARGS}, |
| 770 |
{"get_kwargs", _PyCFunction_CAST(get_kwargs), METH_VARARGS|METH_KEYWORDS}, |
| 771 |
{"getargs_B", getargs_B, METH_VARARGS}, |
| 772 |
{"getargs_C", getargs_C, METH_VARARGS}, |
| 773 |
{"getargs_D", getargs_D, METH_VARARGS}, |
| 774 |
{"getargs_H", getargs_H, METH_VARARGS}, |
| 775 |
{"getargs_I", getargs_I, METH_VARARGS}, |
| 776 |
{"getargs_K", getargs_K, METH_VARARGS}, |
| 777 |
{"getargs_L", getargs_L, METH_VARARGS}, |
| 778 |
{"getargs_S", getargs_S, METH_VARARGS}, |
| 779 |
{"getargs_U", getargs_U, METH_VARARGS}, |
| 780 |
{"getargs_Y", getargs_Y, METH_VARARGS}, |
| 781 |
{"getargs_b", getargs_b, METH_VARARGS}, |
| 782 |
{"getargs_c", getargs_c, METH_VARARGS}, |
| 783 |
{"getargs_d", getargs_d, METH_VARARGS}, |
| 784 |
{"getargs_es", getargs_es, METH_VARARGS}, |
| 785 |
{"getargs_es_hash", getargs_es_hash, METH_VARARGS}, |
| 786 |
{"getargs_et", getargs_et, METH_VARARGS}, |
| 787 |
{"getargs_et_hash", getargs_et_hash, METH_VARARGS}, |
| 788 |
{"getargs_f", getargs_f, METH_VARARGS}, |
| 789 |
{"getargs_h", getargs_h, METH_VARARGS}, |
| 790 |
{"getargs_i", getargs_i, METH_VARARGS}, |
| 791 |
{"getargs_k", getargs_k, METH_VARARGS}, |
| 792 |
{"getargs_keyword_only", _PyCFunction_CAST(getargs_keyword_only), METH_VARARGS|METH_KEYWORDS}, |
| 793 |
{"getargs_keywords", _PyCFunction_CAST(getargs_keywords), METH_VARARGS|METH_KEYWORDS}, |
| 794 |
{"getargs_l", getargs_l, METH_VARARGS}, |
| 795 |
{"getargs_n", getargs_n, METH_VARARGS}, |
| 796 |
{"getargs_p", getargs_p, METH_VARARGS}, |
| 797 |
{"getargs_positional_only_and_keywords", _PyCFunction_CAST(getargs_positional_only_and_keywords), METH_VARARGS|METH_KEYWORDS}, |
| 798 |
{"getargs_s", getargs_s, METH_VARARGS}, |
| 799 |
{"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 800 |
{"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 801 |
{"getargs_tuple", getargs_tuple, METH_VARARGS}, |
| 802 |
{"getargs_w_star", getargs_w_star, METH_VARARGS}, |
| 803 |
{"getargs_w_star_opt", getargs_w_star_opt, METH_VARARGS}, |
| 804 |
{"getargs_empty", _PyCFunction_CAST(getargs_empty), METH_VARARGS|METH_KEYWORDS}, |
| 805 |
{"getargs_y", getargs_y, METH_VARARGS}, |
| 806 |
{"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
| 807 |
{"getargs_y_star", getargs_y_star, METH_VARARGS}, |
| 808 |
{"getargs_z", getargs_z, METH_VARARGS}, |
| 809 |
{"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 810 |
{"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 811 |
{"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, |
| 812 |
{"gh_99240_clear_args", gh_99240_clear_args, METH_VARARGS}, |
| 813 |
{"test_w_code_invalid", test_w_code_invalid, METH_NOARGS}, |
| 814 |
{NULL}, |
| 815 |
}; |
| 816 |
|
| 817 |
int |
| 818 |
_PyTestCapi_Init_GetArgs(PyObject *mod) |
| 819 |
{ |
| 820 |
if (PyModule_AddFunctions(mod, test_methods) < 0) { |
| 821 |
return -1; |
| 822 |
} |
| 823 |
|
| 824 |
return 0; |
| 825 |
} |