| 1 |
/* getargs implementation copied from Python 3.8 and stripped down to only include |
| 2 |
* the functions we need. |
| 3 |
* We also add support for required kwonly args and accepting *args / **kwargs. |
| 4 |
* A good idea would be to also vendor in the Fast versions and get our stuff |
| 5 |
* working with *that*. |
| 6 |
* Another probably good idea is to strip out all the formatting stuff we don't need |
| 7 |
* and then add in custom stuff that we do need. |
| 8 |
* |
| 9 |
* DOCUMENTATION OF THE EXTENSIONS: |
| 10 |
* - Arguments given after a @ format specify are required keyword-only arguments. |
| 11 |
* The | and $ specifiers must both appear before @. |
| 12 |
* - If the first character of a format string is %, then the function can support |
| 13 |
* *args and **kwargs. On seeing a %, the parser will consume two arguments, |
| 14 |
* which should be pointers to variables to store the *args and **kwargs, respectively. |
| 15 |
* Either pointer can be NULL, in which case the function doesn't take that |
| 16 |
* variety of vararg. |
| 17 |
* Unlike most format specifiers, the caller takes ownership of these objects |
| 18 |
* and is responsible for decrefing them. |
| 19 |
* - All arguments must use the 'O' format. |
| 20 |
* - There's minimal error checking of format strings. They are generated |
| 21 |
* programmatically and can be assumed valid. |
| 22 |
*/ |
| 23 |
|
| 24 |
// These macro definitions are copied from pyport.h in Python 3.9 and later |
| 25 |
// https://bugs.python.org/issue19569 |
| 26 |
#if defined(__clang__) |
| 27 |
#define _Py_COMP_DIAG_PUSH _Pragma("clang diagnostic push") |
| 28 |
#define _Py_COMP_DIAG_IGNORE_DEPR_DECLS \ |
| 29 |
_Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") |
| 30 |
#define _Py_COMP_DIAG_POP _Pragma("clang diagnostic pop") |
| 31 |
#elif defined(__GNUC__) \ |
| 32 |
&& ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) |
| 33 |
#define _Py_COMP_DIAG_PUSH _Pragma("GCC diagnostic push") |
| 34 |
#define _Py_COMP_DIAG_IGNORE_DEPR_DECLS \ |
| 35 |
_Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") |
| 36 |
#define _Py_COMP_DIAG_POP _Pragma("GCC diagnostic pop") |
| 37 |
#elif defined(_MSC_VER) |
| 38 |
#define _Py_COMP_DIAG_PUSH __pragma(warning(push)) |
| 39 |
#define _Py_COMP_DIAG_IGNORE_DEPR_DECLS __pragma(warning(disable: 4996)) |
| 40 |
#define _Py_COMP_DIAG_POP __pragma(warning(pop)) |
| 41 |
#else |
| 42 |
#define _Py_COMP_DIAG_PUSH |
| 43 |
#define _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
| 44 |
#define _Py_COMP_DIAG_POP |
| 45 |
#endif |
| 46 |
|
| 47 |
#include "Python.h" |
| 48 |
#include "pythonsupport.h" |
| 49 |
|
| 50 |
#include <ctype.h> |
| 51 |
#include <float.h> |
| 52 |
|
| 53 |
#ifndef PyDict_GET_SIZE |
| 54 |
#define PyDict_GET_SIZE(d) PyDict_Size(d) |
| 55 |
#endif |
| 56 |
|
| 57 |
|
| 58 |
#ifdef __cplusplus |
| 59 |
extern "C" { |
| 60 |
#endif |
| 61 |
int CPyArg_ParseTupleAndKeywords(PyObject *, PyObject *, |
| 62 |
const char *, const char *, const char * const *, ...); |
| 63 |
|
| 64 |
/* Forward */ |
| 65 |
static int vgetargskeywords(PyObject *, PyObject *, |
| 66 |
const char *, const char *, const char * const *, va_list *); |
| 67 |
static void skipitem(const char **, va_list *); |
| 68 |
|
| 69 |
/* Support for keyword arguments donated by |
| 70 |
Geoff Philbrick <[email protected]> */ |
| 71 |
|
| 72 |
/* Return false (0) for error, else true. */ |
| 73 |
int |
| 74 |
CPyArg_ParseTupleAndKeywords(PyObject *args, |
| 75 |
PyObject *keywords, |
| 76 |
const char *format, |
| 77 |
const char *fname, |
| 78 |
const char * const *kwlist, ...) |
| 79 |
{ |
| 80 |
int retval; |
| 81 |
va_list va; |
| 82 |
|
| 83 |
va_start(va, kwlist); |
| 84 |
retval = vgetargskeywords(args, keywords, format, fname, kwlist, &va); |
| 85 |
va_end(va); |
| 86 |
return retval; |
| 87 |
} |
| 88 |
|
| 89 |
#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':') |
| 90 |
|
| 91 |
static int |
| 92 |
vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, |
| 93 |
const char *fname, const char * const *kwlist, va_list *p_va) |
| 94 |
{ |
| 95 |
int min = INT_MAX; |
| 96 |
int max = INT_MAX; |
| 97 |
int required_kwonly_start = INT_MAX; |
| 98 |
int has_required_kws = 0; |
| 99 |
int i, pos, len; |
| 100 |
int skip = 0; |
| 101 |
Py_ssize_t nargs, nkwargs; |
| 102 |
PyObject *current_arg; |
| 103 |
int bound_pos_args; |
| 104 |
|
| 105 |
PyObject **p_args = NULL, **p_kwargs = NULL; |
| 106 |
|
| 107 |
assert(args != NULL && PyTuple_Check(args)); |
| 108 |
assert(kwargs == NULL || PyDict_Check(kwargs)); |
| 109 |
assert(format != NULL); |
| 110 |
assert(kwlist != NULL); |
| 111 |
assert(p_va != NULL); |
| 112 |
|
| 113 |
/* scan kwlist and count the number of positional-only parameters */ |
| 114 |
for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) { |
| 115 |
} |
| 116 |
/* scan kwlist and get greatest possible nbr of args */ |
| 117 |
for (len = pos; kwlist[len]; len++) { |
| 118 |
#ifdef DEBUG |
| 119 |
if (!*kwlist[len]) { |
| 120 |
PyErr_SetString(PyExc_SystemError, |
| 121 |
"Empty keyword parameter name"); |
| 122 |
return 0; |
| 123 |
} |
| 124 |
#endif |
| 125 |
} |
| 126 |
|
| 127 |
if (*format == '%') { |
| 128 |
p_args = va_arg(*p_va, PyObject **); |
| 129 |
p_kwargs = va_arg(*p_va, PyObject **); |
| 130 |
format++; |
| 131 |
} |
| 132 |
|
| 133 |
nargs = PyTuple_GET_SIZE(args); |
| 134 |
nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs); |
| 135 |
if (unlikely(nargs + nkwargs > len && !p_args && !p_kwargs)) { |
| 136 |
/* Adding "keyword" (when nargs == 0) prevents producing wrong error |
| 137 |
messages in some special cases (see bpo-31229). */ |
| 138 |
PyErr_Format(PyExc_TypeError, |
| 139 |
"%.200s%s takes at most %d %sargument%s (%zd given)", |
| 140 |
(fname == NULL) ? "function" : fname, |
| 141 |
(fname == NULL) ? "" : "()", |
| 142 |
len, |
| 143 |
(nargs == 0) ? "keyword " : "", |
| 144 |
(len == 1) ? "" : "s", |
| 145 |
nargs + nkwargs); |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
/* convert tuple args and keyword args in same loop, using kwlist to drive process */ |
| 150 |
for (i = 0; i < len; i++) { |
| 151 |
if (*format == '|') { |
| 152 |
#ifdef DEBUG |
| 153 |
if (min != INT_MAX) { |
| 154 |
PyErr_SetString(PyExc_SystemError, |
| 155 |
"Invalid format string (| specified twice)"); |
| 156 |
return 0; |
| 157 |
} |
| 158 |
#endif |
| 159 |
|
| 160 |
min = i; |
| 161 |
format++; |
| 162 |
|
| 163 |
#ifdef DEBUG |
| 164 |
if (max != INT_MAX) { |
| 165 |
PyErr_SetString(PyExc_SystemError, |
| 166 |
"Invalid format string ($ before |)"); |
| 167 |
return 0; |
| 168 |
} |
| 169 |
#endif |
| 170 |
|
| 171 |
/* If there are optional args, figure out whether we have |
| 172 |
* required keyword arguments so that we don't bail without |
| 173 |
* enforcing them. */ |
| 174 |
has_required_kws = strchr(format, '@') != NULL; |
| 175 |
} |
| 176 |
if (*format == '$') { |
| 177 |
#ifdef DEBUG |
| 178 |
if (max != INT_MAX) { |
| 179 |
PyErr_SetString(PyExc_SystemError, |
| 180 |
"Invalid format string ($ specified twice)"); |
| 181 |
return 0; |
| 182 |
} |
| 183 |
#endif |
| 184 |
|
| 185 |
max = i; |
| 186 |
format++; |
| 187 |
|
| 188 |
#ifdef DEBUG |
| 189 |
if (max < pos) { |
| 190 |
PyErr_SetString(PyExc_SystemError, |
| 191 |
"Empty parameter name after $"); |
| 192 |
return 0; |
| 193 |
} |
| 194 |
#endif |
| 195 |
if (skip) { |
| 196 |
/* Now we know the minimal and the maximal numbers of |
| 197 |
* positional arguments and can raise an exception with |
| 198 |
* informative message (see below). */ |
| 199 |
break; |
| 200 |
} |
| 201 |
if (unlikely(max < nargs && !p_args)) { |
| 202 |
if (max == 0) { |
| 203 |
PyErr_Format(PyExc_TypeError, |
| 204 |
"%.200s%s takes no positional arguments", |
| 205 |
(fname == NULL) ? "function" : fname, |
| 206 |
(fname == NULL) ? "" : "()"); |
| 207 |
} |
| 208 |
else { |
| 209 |
PyErr_Format(PyExc_TypeError, |
| 210 |
"%.200s%s takes %s %d positional argument%s" |
| 211 |
" (%zd given)", |
| 212 |
(fname == NULL) ? "function" : fname, |
| 213 |
(fname == NULL) ? "" : "()", |
| 214 |
(min < max) ? "at most" : "exactly", |
| 215 |
max, |
| 216 |
max == 1 ? "" : "s", |
| 217 |
nargs); |
| 218 |
} |
| 219 |
return 0; |
| 220 |
} |
| 221 |
} |
| 222 |
if (*format == '@') { |
| 223 |
#ifdef DEBUG |
| 224 |
if (min == INT_MAX && max == INT_MAX) { |
| 225 |
PyErr_SetString(PyExc_SystemError, |
| 226 |
"Invalid format string " |
| 227 |
"(@ without preceding | and $)"); |
| 228 |
return 0; |
| 229 |
} |
| 230 |
if (required_kwonly_start != INT_MAX) { |
| 231 |
PyErr_SetString(PyExc_SystemError, |
| 232 |
"Invalid format string (@ specified twice)"); |
| 233 |
return 0; |
| 234 |
} |
| 235 |
#endif |
| 236 |
|
| 237 |
required_kwonly_start = i; |
| 238 |
format++; |
| 239 |
} |
| 240 |
#ifdef DEBUG |
| 241 |
if (IS_END_OF_FORMAT(*format)) { |
| 242 |
PyErr_Format(PyExc_SystemError, |
| 243 |
"More keyword list entries (%d) than " |
| 244 |
"format specifiers (%d)", len, i); |
| 245 |
return 0; |
| 246 |
} |
| 247 |
#endif |
| 248 |
if (!skip) { |
| 249 |
if (i < nargs && i < max) { |
| 250 |
current_arg = Py_NewRef(PyTuple_GET_ITEM(args, i)); |
| 251 |
} |
| 252 |
else if (nkwargs && i >= pos) { |
| 253 |
if (unlikely(PyDict_GetItemStringRef(kwargs, kwlist[i], ¤t_arg) < 0)) { |
| 254 |
return 0; |
| 255 |
} |
| 256 |
if (current_arg) { |
| 257 |
--nkwargs; |
| 258 |
} |
| 259 |
} |
| 260 |
else { |
| 261 |
current_arg = NULL; |
| 262 |
} |
| 263 |
|
| 264 |
if (current_arg) { |
| 265 |
PyObject **p = va_arg(*p_va, PyObject **); |
| 266 |
*p = current_arg; |
| 267 |
Py_DECREF(current_arg); |
| 268 |
format++; |
| 269 |
continue; |
| 270 |
} |
| 271 |
|
| 272 |
if (i < min || i >= required_kwonly_start) { |
| 273 |
if (likely(i < pos)) { |
| 274 |
assert (min == INT_MAX); |
| 275 |
assert (max == INT_MAX); |
| 276 |
skip = 1; |
| 277 |
/* At that moment we still don't know the minimal and |
| 278 |
* the maximal numbers of positional arguments. Raising |
| 279 |
* an exception is deferred until we encounter | and $ |
| 280 |
* or the end of the format. */ |
| 281 |
} |
| 282 |
else { |
| 283 |
if (i >= max) { |
| 284 |
PyErr_Format(PyExc_TypeError, |
| 285 |
"%.200s%s missing required " |
| 286 |
"keyword-only argument '%s'", |
| 287 |
(fname == NULL) ? "function" : fname, |
| 288 |
(fname == NULL) ? "" : "()", |
| 289 |
kwlist[i]); |
| 290 |
} |
| 291 |
else { |
| 292 |
PyErr_Format(PyExc_TypeError, |
| 293 |
"%.200s%s missing required " |
| 294 |
"argument '%s' (pos %d)", |
| 295 |
(fname == NULL) ? "function" : fname, |
| 296 |
(fname == NULL) ? "" : "()", |
| 297 |
kwlist[i], i+1); |
| 298 |
} |
| 299 |
return 0; |
| 300 |
} |
| 301 |
} |
| 302 |
/* current code reports success when all required args |
| 303 |
* fulfilled and no keyword args left, with no further |
| 304 |
* validation. XXX Maybe skip this in debug build ? |
| 305 |
*/ |
| 306 |
if (!nkwargs && !skip && !has_required_kws && |
| 307 |
!p_args && !p_kwargs) |
| 308 |
{ |
| 309 |
return 1; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/* We are into optional args, skip through to any remaining |
| 314 |
* keyword args */ |
| 315 |
skipitem(&format, p_va); |
| 316 |
} |
| 317 |
|
| 318 |
if (unlikely(skip)) { |
| 319 |
PyErr_Format(PyExc_TypeError, |
| 320 |
"%.200s%s takes %s %d positional argument%s" |
| 321 |
" (%zd given)", |
| 322 |
(fname == NULL) ? "function" : fname, |
| 323 |
(fname == NULL) ? "" : "()", |
| 324 |
(Py_MIN(pos, min) < i) ? "at least" : "exactly", |
| 325 |
Py_MIN(pos, min), |
| 326 |
Py_MIN(pos, min) == 1 ? "" : "s", |
| 327 |
nargs); |
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
#ifdef DEBUG |
| 332 |
if (!IS_END_OF_FORMAT(*format) && |
| 333 |
(*format != '|') && (*format != '$') && (*format != '@')) |
| 334 |
{ |
| 335 |
PyErr_Format(PyExc_SystemError, |
| 336 |
"more argument specifiers than keyword list entries " |
| 337 |
"(remaining format:'%s')", format); |
| 338 |
return 0; |
| 339 |
} |
| 340 |
#endif |
| 341 |
|
| 342 |
bound_pos_args = Py_MIN(nargs, Py_MIN(max, len)); |
| 343 |
if (p_args) { |
| 344 |
*p_args = PyTuple_GetSlice(args, bound_pos_args, nargs); |
| 345 |
if (!*p_args) { |
| 346 |
return 0; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if (p_kwargs) { |
| 351 |
/* This unfortunately needs to be special cased because if len is 0 then we |
| 352 |
* never go through the main loop. */ |
| 353 |
if (unlikely(nargs > 0 && len == 0 && !p_args)) { |
| 354 |
PyErr_Format(PyExc_TypeError, |
| 355 |
"%.200s%s takes no positional arguments", |
| 356 |
(fname == NULL) ? "function" : fname, |
| 357 |
(fname == NULL) ? "" : "()"); |
| 358 |
|
| 359 |
return 0; |
| 360 |
} |
| 361 |
|
| 362 |
*p_kwargs = PyDict_New(); |
| 363 |
if (!*p_kwargs) { |
| 364 |
goto latefail; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
if (nkwargs > 0) { |
| 369 |
PyObject *key, *value; |
| 370 |
Py_ssize_t j; |
| 371 |
/* make sure there are no arguments given by name and position */ |
| 372 |
for (i = pos; i < bound_pos_args && i < len; i++) { |
| 373 |
PyObject *current_arg; |
| 374 |
if (unlikely(PyDict_GetItemStringRef(kwargs, kwlist[i], ¤t_arg) < 0)) { |
| 375 |
goto latefail; |
| 376 |
} |
| 377 |
if (unlikely(current_arg != NULL)) { |
| 378 |
Py_DECREF(current_arg); |
| 379 |
/* arg present in tuple and in dict */ |
| 380 |
PyErr_Format(PyExc_TypeError, |
| 381 |
"argument for %.200s%s given by name ('%s') " |
| 382 |
"and position (%d)", |
| 383 |
(fname == NULL) ? "function" : fname, |
| 384 |
(fname == NULL) ? "" : "()", |
| 385 |
kwlist[i], i+1); |
| 386 |
goto latefail; |
| 387 |
} |
| 388 |
} |
| 389 |
/* make sure there are no extraneous keyword arguments */ |
| 390 |
j = 0; |
| 391 |
while (PyDict_Next(kwargs, &j, &key, &value)) { |
| 392 |
int match = 0; |
| 393 |
if (unlikely(!PyUnicode_Check(key))) { |
| 394 |
PyErr_SetString(PyExc_TypeError, |
| 395 |
"keywords must be strings"); |
| 396 |
goto latefail; |
| 397 |
} |
| 398 |
for (i = pos; i < len; i++) { |
| 399 |
if (PyUnicode_EqualToUTF8(key, kwlist[i])) { |
| 400 |
match = 1; |
| 401 |
break; |
| 402 |
} |
| 403 |
} |
| 404 |
if (!match) { |
| 405 |
if (unlikely(!p_kwargs)) { |
| 406 |
PyErr_Format(PyExc_TypeError, |
| 407 |
"'%U' is an invalid keyword " |
| 408 |
"argument for %.200s%s", |
| 409 |
key, |
| 410 |
(fname == NULL) ? "this function" : fname, |
| 411 |
(fname == NULL) ? "" : "()"); |
| 412 |
goto latefail; |
| 413 |
} else { |
| 414 |
if (PyDict_SetItem(*p_kwargs, key, value) < 0) { |
| 415 |
goto latefail; |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
return 1; |
| 423 |
/* Handle failures that have happened after we have tried to |
| 424 |
* create *args and **kwargs, if they exist. */ |
| 425 |
latefail: |
| 426 |
if (p_args) { |
| 427 |
Py_XDECREF(*p_args); |
| 428 |
} |
| 429 |
if (p_kwargs) { |
| 430 |
Py_XDECREF(*p_kwargs); |
| 431 |
} |
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
static void |
| 437 |
skipitem(const char **p_format, va_list *p_va) |
| 438 |
{ |
| 439 |
const char *format = *p_format; |
| 440 |
format++; |
| 441 |
|
| 442 |
if (p_va != NULL) { |
| 443 |
(void) va_arg(*p_va, PyObject **); |
| 444 |
} |
| 445 |
|
| 446 |
*p_format = format; |
| 447 |
} |
| 448 |
|
| 449 |
#ifdef __cplusplus |
| 450 |
}; |
| 451 |
#endif |