Coverage Report

Created: 2025-07-04 06:49

/src/cpython/Python/getargs.c
Line
Count
Source (jump to first uncovered line)
1
2
/* New getargs implementation */
3
4
#include <stdbool.h>
5
6
#define PY_CXX_CONST const
7
#include "Python.h"
8
#include "pycore_abstract.h"      // _PyNumber_Index()
9
#include "pycore_dict.h"          // _PyDict_HasOnlyStringKeys()
10
#include "pycore_modsupport.h"    // export _PyArg_NoKeywords()
11
#include "pycore_pylifecycle.h"   // _PyArg_Fini
12
#include "pycore_pystate.h"       // _Py_IsMainInterpreter()
13
#include "pycore_tuple.h"         // _PyTuple_ITEMS()
14
#include "pycore_pyerrors.h"      // _Py_CalculateSuggestions()
15
#include "pycore_unicodeobject.h" // _PyUnicode_InternImmortal
16
17
/* Export Stable ABIs (abi only) */
18
PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...);
19
PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...);
20
PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
21
                                                  const char *, const char * const *, ...);
22
PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list);
23
PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
24
                                              const char *, const char * const *, va_list);
25
26
2.19M
#define FLAG_COMPAT 1
27
28
typedef int (*destr_t)(PyObject *, void *);
29
30
31
/* Keep track of "objects" that have been allocated or initialized and
32
   which will need to be deallocated or cleaned up somehow if overall
33
   parsing fails.
34
*/
35
typedef struct {
36
  void *item;
37
  destr_t destructor;
38
} freelistentry_t;
39
40
typedef struct {
41
  freelistentry_t *entries;
42
  int first_available;
43
  int entries_malloced;
44
} freelist_t;
45
46
765k
#define STATIC_FREELIST_ENTRIES 8
47
48
/* Forward */
49
static int vgetargs1_impl(PyObject *args, PyObject *const *stack, Py_ssize_t nargs,
50
                          const char *format, va_list *p_va, int flags);
51
static int vgetargs1(PyObject *, const char *, va_list *, int);
52
static void seterror(Py_ssize_t, const char *, int *, const char *, const char *);
53
static const char *convertitem(PyObject *, const char **, va_list *, int, int *,
54
                               char *, size_t, freelist_t *);
55
static const char *converttuple(PyObject *, const char **, va_list *, int,
56
                                int *, char *, size_t, freelist_t *);
57
static const char *convertsimple(PyObject *, const char **, va_list *, int,
58
                                 char *, size_t, freelist_t *);
59
static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **);
60
static int getbuffer(PyObject *, Py_buffer *, const char**);
61
62
static int vgetargskeywords(PyObject *, PyObject *,
63
                            const char *, const char * const *, va_list *, int);
64
static int vgetargskeywordsfast(PyObject *, PyObject *,
65
                            struct _PyArg_Parser *, va_list *, int);
66
static int vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
67
                          PyObject *keywords, PyObject *kwnames,
68
                          struct _PyArg_Parser *parser,
69
                          va_list *p_va, int flags);
70
static const char *skipitem(const char **, va_list *, int);
71
72
int
73
PyArg_Parse(PyObject *args, const char *format, ...)
74
0
{
75
0
    int retval;
76
0
    va_list va;
77
78
0
    va_start(va, format);
79
0
    retval = vgetargs1(args, format, &va, FLAG_COMPAT);
80
0
    va_end(va);
81
0
    return retval;
82
0
}
83
84
PyAPI_FUNC(int)
85
_PyArg_Parse_SizeT(PyObject *args, const char *format, ...)
86
0
{
87
0
    int retval;
88
0
    va_list va;
89
90
0
    va_start(va, format);
91
0
    retval = vgetargs1(args, format, &va, FLAG_COMPAT);
92
0
    va_end(va);
93
0
    return retval;
94
0
}
95
96
97
int
98
PyArg_ParseTuple(PyObject *args, const char *format, ...)
99
730k
{
100
730k
    int retval;
101
730k
    va_list va;
102
103
730k
    va_start(va, format);
104
730k
    retval = vgetargs1(args, format, &va, 0);
105
730k
    va_end(va);
106
730k
    return retval;
107
730k
}
108
109
int
110
_PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...)
111
0
{
112
0
    int retval;
113
0
    va_list va;
114
115
0
    va_start(va, format);
116
0
    retval = vgetargs1(args, format, &va, 0);
117
0
    va_end(va);
118
0
    return retval;
119
0
}
120
121
122
int
123
_PyArg_ParseStack(PyObject *const *args, Py_ssize_t nargs, const char *format, ...)
124
0
{
125
0
    int retval;
126
0
    va_list va;
127
128
0
    va_start(va, format);
129
0
    retval = vgetargs1_impl(NULL, args, nargs, format, &va, 0);
130
0
    va_end(va);
131
0
    return retval;
132
0
}
133
134
int
135
PyArg_VaParse(PyObject *args, const char *format, va_list va)
136
0
{
137
0
    va_list lva;
138
0
    int retval;
139
140
0
    va_copy(lva, va);
141
142
0
    retval = vgetargs1(args, format, &lva, 0);
143
0
    va_end(lva);
144
0
    return retval;
145
0
}
146
147
int
148
_PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
149
0
{
150
0
    va_list lva;
151
0
    int retval;
152
153
0
    va_copy(lva, va);
154
155
0
    retval = vgetargs1(args, format, &lva, 0);
156
0
    va_end(lva);
157
0
    return retval;
158
0
}
159
160
161
/* Handle cleanup of allocated memory in case of exception */
162
163
static int
164
cleanup_ptr(PyObject *self, void *ptr)
165
0
{
166
0
    void **pptr = (void **)ptr;
167
0
    PyMem_Free(*pptr);
168
0
    *pptr = NULL;
169
0
    return 0;
170
0
}
171
172
static int
173
cleanup_buffer(PyObject *self, void *ptr)
174
0
{
175
0
    Py_buffer *buf = (Py_buffer *)ptr;
176
0
    if (buf) {
177
0
        PyBuffer_Release(buf);
178
0
    }
179
0
    return 0;
180
0
}
181
182
static int
183
addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
184
0
{
185
0
    int index;
186
187
0
    index = freelist->first_available;
188
0
    freelist->first_available += 1;
189
190
0
    freelist->entries[index].item = ptr;
191
0
    freelist->entries[index].destructor = destructor;
192
193
0
    return 0;
194
0
}
195
196
static int
197
cleanreturn(int retval, freelist_t *freelist)
198
765k
{
199
765k
    int index;
200
201
765k
    if (retval == 0) {
202
      /* A failure occurred, therefore execute all of the cleanup
203
         functions.
204
      */
205
98
      for (index = 0; index < freelist->first_available; ++index) {
206
0
          freelist->entries[index].destructor(NULL,
207
0
                                              freelist->entries[index].item);
208
0
      }
209
98
    }
210
765k
    if (freelist->entries_malloced)
211
6.34k
        PyMem_Free(freelist->entries);
212
765k
    return retval;
213
765k
}
214
215
216
static int
217
vgetargs1_impl(PyObject *compat_args, PyObject *const *stack, Py_ssize_t nargs, const char *format,
218
               va_list *p_va, int flags)
219
730k
{
220
730k
    char msgbuf[256];
221
730k
    int levels[32];
222
730k
    const char *fname = NULL;
223
730k
    const char *message = NULL;
224
730k
    int min = -1;
225
730k
    int max = 0;
226
730k
    int level = 0;
227
730k
    int endfmt = 0;
228
730k
    const char *formatsave = format;
229
730k
    Py_ssize_t i;
230
730k
    const char *msg;
231
730k
    int compat = flags & FLAG_COMPAT;
232
730k
    freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
233
730k
    freelist_t freelist;
234
235
730k
    assert(nargs == 0 || stack != NULL);
236
237
730k
    freelist.entries = static_entries;
238
730k
    freelist.first_available = 0;
239
730k
    freelist.entries_malloced = 0;
240
241
730k
    flags = flags & ~FLAG_COMPAT;
242
243
4.49M
    while (endfmt == 0) {
244
3.76M
        int c = *format++;
245
3.76M
        switch (c) {
246
0
        case '(':
247
0
            if (level == 0)
248
0
                max++;
249
0
            level++;
250
0
            if (level >= 30)
251
0
                Py_FatalError("too many tuple nesting levels "
252
0
                              "in argument format string");
253
0
            break;
254
0
        case ')':
255
0
            if (level == 0)
256
0
                Py_FatalError("excess ')' in getargs format");
257
0
            else
258
0
                level--;
259
0
            break;
260
261k
        case '\0':
261
261k
            endfmt = 1;
262
261k
            break;
263
246k
        case ':':
264
246k
            fname = format;
265
246k
            endfmt = 1;
266
246k
            break;
267
222k
        case ';':
268
222k
            message = format;
269
222k
            endfmt = 1;
270
222k
            break;
271
16.2k
        case '|':
272
16.2k
            if (level == 0)
273
16.2k
                min = max;
274
16.2k
            break;
275
3.01M
        default:
276
3.01M
            if (level == 0) {
277
3.01M
                if (Py_ISALPHA(c))
278
2.52M
                    if (c != 'e') /* skip encoded */
279
2.52M
                        max++;
280
3.01M
            }
281
3.01M
            break;
282
3.76M
        }
283
3.76M
    }
284
285
730k
    if (level != 0)
286
0
        Py_FatalError(/* '(' */ "missing ')' in getargs format");
287
288
730k
    if (min < 0)
289
714k
        min = max;
290
291
730k
    format = formatsave;
292
293
730k
    if (max > STATIC_FREELIST_ENTRIES) {
294
0
        freelist.entries = PyMem_NEW(freelistentry_t, max);
295
0
        if (freelist.entries == NULL) {
296
0
            PyErr_NoMemory();
297
0
            return 0;
298
0
        }
299
0
        freelist.entries_malloced = 1;
300
0
    }
301
302
730k
    if (compat) {
303
0
        if (max == 0) {
304
0
            if (compat_args == NULL)
305
0
                return 1;
306
0
            PyErr_Format(PyExc_TypeError,
307
0
                         "%.200s%s takes no arguments",
308
0
                         fname==NULL ? "function" : fname,
309
0
                         fname==NULL ? "" : "()");
310
0
            return cleanreturn(0, &freelist);
311
0
        }
312
0
        else if (min == 1 && max == 1) {
313
0
            if (compat_args == NULL) {
314
0
                PyErr_Format(PyExc_TypeError,
315
0
                             "%.200s%s takes at least one argument",
316
0
                             fname==NULL ? "function" : fname,
317
0
                             fname==NULL ? "" : "()");
318
0
                return cleanreturn(0, &freelist);
319
0
            }
320
0
            msg = convertitem(compat_args, &format, p_va, flags, levels,
321
0
                              msgbuf, sizeof(msgbuf), &freelist);
322
0
            if (msg == NULL)
323
0
                return cleanreturn(1, &freelist);
324
0
            seterror(levels[0], msg, levels+1, fname, message);
325
0
            return cleanreturn(0, &freelist);
326
0
        }
327
0
        else {
328
0
            PyErr_SetString(PyExc_SystemError,
329
0
                "old style getargs format uses new features");
330
0
            return cleanreturn(0, &freelist);
331
0
        }
332
0
    }
333
334
730k
    if (nargs < min || max < nargs) {
335
0
        if (message == NULL)
336
0
            PyErr_Format(PyExc_TypeError,
337
0
                         "%.150s%s takes %s %d argument%s (%zd given)",
338
0
                         fname==NULL ? "function" : fname,
339
0
                         fname==NULL ? "" : "()",
340
0
                         min==max ? "exactly"
341
0
                         : nargs < min ? "at least" : "at most",
342
0
                         nargs < min ? min : max,
343
0
                         (nargs < min ? min : max) == 1 ? "" : "s",
344
0
                         nargs);
345
0
        else
346
0
            PyErr_SetString(PyExc_TypeError, message);
347
0
        return cleanreturn(0, &freelist);
348
0
    }
349
350
3.23M
    for (i = 0; i < nargs; i++) {
351
2.50M
        if (*format == '|')
352
14.4k
            format++;
353
2.50M
        msg = convertitem(stack[i], &format, p_va,
354
2.50M
                          flags, levels, msgbuf,
355
2.50M
                          sizeof(msgbuf), &freelist);
356
2.50M
        if (msg) {
357
98
            seterror(i+1, msg, levels, fname, message);
358
98
            return cleanreturn(0, &freelist);
359
98
        }
360
2.50M
    }
361
362
730k
    if (*format != '\0' && !Py_ISALPHA(*format) &&
363
730k
        *format != '(' &&
364
730k
        *format != '|' && *format != ':' && *format != ';') {
365
0
        PyErr_Format(PyExc_SystemError,
366
0
                     "bad format string: %.200s", formatsave);
367
0
        return cleanreturn(0, &freelist);
368
0
    }
369
370
730k
    return cleanreturn(1, &freelist);
371
730k
}
372
373
static int
374
vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
375
730k
{
376
730k
    PyObject **stack;
377
730k
    Py_ssize_t nargs;
378
379
730k
    if (!(flags & FLAG_COMPAT)) {
380
730k
        assert(args != NULL);
381
382
730k
        if (!PyTuple_Check(args)) {
383
0
            PyErr_SetString(PyExc_SystemError,
384
0
                "new style getargs format but argument is not a tuple");
385
0
            return 0;
386
0
        }
387
388
730k
        stack = _PyTuple_ITEMS(args);
389
730k
        nargs = PyTuple_GET_SIZE(args);
390
730k
    }
391
0
    else {
392
0
        stack = NULL;
393
0
        nargs = 0;
394
0
    }
395
396
730k
    return vgetargs1_impl(args, stack, nargs, format, p_va, flags);
397
730k
}
398
399
400
static void
401
seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname,
402
         const char *message)
403
98
{
404
98
    char buf[512];
405
98
    int i;
406
98
    char *p = buf;
407
408
98
    if (PyErr_Occurred())
409
98
        return;
410
0
    else if (message == NULL) {
411
0
        if (fname != NULL) {
412
0
            PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
413
0
            p += strlen(p);
414
0
        }
415
0
        if (iarg != 0) {
416
0
            PyOS_snprintf(p, sizeof(buf) - (p - buf),
417
0
                          "argument %zd", iarg);
418
0
            i = 0;
419
0
            p += strlen(p);
420
0
            while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) {
421
0
                PyOS_snprintf(p, sizeof(buf) - (p - buf),
422
0
                              ", item %d", levels[i]-1);
423
0
                p += strlen(p);
424
0
                i++;
425
0
            }
426
0
        }
427
0
        else {
428
0
            PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
429
0
            p += strlen(p);
430
0
        }
431
0
        PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
432
0
        message = buf;
433
0
    }
434
0
    if (msg[0] == '(') {
435
0
        PyErr_SetString(PyExc_SystemError, message);
436
0
    }
437
0
    else {
438
0
        PyErr_SetString(PyExc_TypeError, message);
439
0
    }
440
0
}
441
442
443
/* Convert a tuple argument.
444
   On entry, *p_format points to the character _after_ the opening '('.
445
   On successful exit, *p_format points to the closing ')'.
446
   If successful:
447
      *p_format and *p_va are updated,
448
      *levels and *msgbuf are untouched,
449
      and NULL is returned.
450
   If the argument is invalid:
451
      *p_format is unchanged,
452
      *p_va is undefined,
453
      *levels is a 0-terminated list of item numbers,
454
      *msgbuf contains an error message, whose format is:
455
     "must be <typename1>, not <typename2>", where:
456
        <typename1> is the name of the expected type, and
457
        <typename2> is the name of the actual type,
458
      and msgbuf is returned.
459
*/
460
461
static const char *
462
converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
463
             int *levels, char *msgbuf, size_t bufsize,
464
             freelist_t *freelist)
465
0
{
466
0
    int level = 0;
467
0
    int n = 0;
468
0
    const char *format = *p_format;
469
0
    int i;
470
0
    Py_ssize_t len;
471
0
    bool nullable = false;
472
0
    int istuple = PyTuple_Check(arg);
473
0
    int mustbetuple = istuple;
474
475
0
    assert(*format == '(');
476
0
    format++;
477
0
    for (;;) {
478
0
        int c = *format++;
479
0
        if (c == '(') {
480
0
            if (level == 0)
481
0
                n++;
482
0
            level++;
483
0
        }
484
0
        else if (c == ')') {
485
0
            if (level == 0) {
486
0
                if (*format == '?') {
487
0
                    nullable = true;
488
0
                }
489
0
                break;
490
0
            }
491
0
            level--;
492
0
        }
493
0
        else if (c == ':' || c == ';' || c == '\0')
494
0
            break;
495
0
        else {
496
0
            if (level == 0 && Py_ISALPHA(c)) {
497
0
                n++;
498
0
            }
499
0
            if (c == 'e' && (*format == 's' || *format == 't')) {
500
0
                format++;
501
0
                continue;
502
0
            }
503
0
            if (!mustbetuple) {
504
0
                switch (c) {
505
0
                    case 'y':
506
0
                    case 's':
507
0
                    case 'z':
508
0
                        if (*format != '*') {
509
0
                            mustbetuple = 1;
510
0
                        }
511
0
                        break;
512
0
                    case 'S':
513
0
                    case 'Y':
514
0
                    case 'U':
515
0
                        mustbetuple = 1;
516
0
                        break;
517
0
                    case 'O':
518
0
                        if (*format != '&') {
519
0
                            mustbetuple = 1;
520
0
                        }
521
0
                        break;
522
0
                }
523
0
            }
524
0
        }
525
0
    }
526
527
0
    if (arg == Py_None && nullable) {
528
0
        const char *msg = skipitem(p_format, p_va, flags);
529
0
        if (msg != NULL) {
530
0
            levels[0] = 0;
531
0
        }
532
0
        return msg;
533
0
    }
534
0
    if (istuple) {
535
        /* fallthrough */
536
0
    }
537
0
    else if (!PySequence_Check(arg) ||
538
0
        PyUnicode_Check(arg) || PyBytes_Check(arg) || PyByteArray_Check(arg))
539
0
    {
540
0
        levels[0] = 0;
541
0
        PyOS_snprintf(msgbuf, bufsize,
542
0
                      "must be %d-item tuple%s, not %.50s",
543
0
                      n,
544
0
                      nullable ? " or None" : "",
545
0
                      arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
546
0
        return msgbuf;
547
0
    }
548
0
    else {
549
0
        if (mustbetuple) {
550
0
            if (PyErr_WarnFormat(PyExc_DeprecationWarning, 0,
551
0
                    "argument must be %d-item tuple, not %T", n, arg))
552
0
            {
553
0
                return msgbuf;
554
0
            }
555
0
        }
556
0
        len = PySequence_Size(arg);
557
0
        if (len != n) {
558
0
            levels[0] = 0;
559
0
            PyOS_snprintf(msgbuf, bufsize,
560
0
                          "must be %s of length %d, not %zd",
561
0
                          mustbetuple ? "tuple" : "sequence", n, len);
562
0
            return msgbuf;
563
0
        }
564
0
        arg = PySequence_Tuple(arg);
565
0
        if (arg == NULL) {
566
0
            return msgbuf;
567
0
        }
568
0
    }
569
570
0
    len = PyTuple_GET_SIZE(arg);
571
0
    if (len != n) {
572
0
        levels[0] = 0;
573
0
        PyOS_snprintf(msgbuf, bufsize,
574
0
                      "must be tuple of length %d, not %zd",
575
0
                      n, len);
576
0
        if (!istuple) {
577
0
            Py_DECREF(arg);
578
0
        }
579
0
        return msgbuf;
580
0
    }
581
582
0
    format = *p_format + 1;
583
0
    for (i = 0; i < n; i++) {
584
0
        const char *msg;
585
0
        PyObject *item = PyTuple_GET_ITEM(arg, i);
586
0
        msg = convertitem(item, &format, p_va, flags, levels+1,
587
0
                          msgbuf, bufsize, freelist);
588
0
        if (msg != NULL) {
589
0
            levels[0] = i+1;
590
0
            if (!istuple) {
591
0
                Py_DECREF(arg);
592
0
            }
593
0
            return msg;
594
0
        }
595
0
    }
596
597
0
    format++;
598
0
    if (*format == '?') {
599
0
        format++;
600
0
    }
601
0
    *p_format = format;
602
0
    if (!istuple) {
603
0
        Py_DECREF(arg);
604
0
    }
605
0
    return NULL;
606
0
}
607
608
609
/* Convert a single item. */
610
611
static const char *
612
convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
613
            int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
614
2.57M
{
615
2.57M
    const char *msg;
616
2.57M
    const char *format = *p_format;
617
618
2.57M
    if (*format == '(' /* ')' */) {
619
0
        msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
620
0
                           bufsize, freelist);
621
0
    }
622
2.57M
    else {
623
2.57M
        msg = convertsimple(arg, &format, p_va, flags,
624
2.57M
                            msgbuf, bufsize, freelist);
625
2.57M
        if (msg != NULL)
626
98
            levels[0] = 0;
627
2.57M
    }
628
2.57M
    if (msg == NULL)
629
2.57M
        *p_format = format;
630
2.57M
    return msg;
631
2.57M
}
632
633
634
635
/* Format an error message generated by convertsimple().
636
   displayname must be UTF-8 encoded.
637
*/
638
639
void
640
_PyArg_BadArgument(const char *fname, const char *displayname,
641
                   const char *expected, PyObject *arg)
642
0
{
643
0
    PyErr_Format(PyExc_TypeError,
644
0
                 "%.200s() %.200s must be %.50s, not %.50s",
645
0
                 fname, displayname, expected,
646
0
                 arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
647
0
}
648
649
static const char *
650
converterr(bool nullable, const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
651
0
{
652
0
    assert(expected != NULL);
653
0
    assert(arg != NULL);
654
0
    if (expected[0] == '(') {
655
0
        PyOS_snprintf(msgbuf, bufsize,
656
0
                      "%.100s", expected);
657
0
    }
658
0
    else {
659
0
        PyOS_snprintf(msgbuf, bufsize,
660
0
                      "must be %.50s%s, not %.50s", expected,
661
0
                      nullable ? " or None" : "",
662
0
                      arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
663
0
    }
664
0
    return msgbuf;
665
0
}
666
667
static const char *
668
convertcharerr(bool nullable, const char *expected, const char *what, Py_ssize_t size,
669
               char *msgbuf, size_t bufsize)
670
0
{
671
0
    assert(expected != NULL);
672
0
    PyOS_snprintf(msgbuf, bufsize,
673
0
                  "must be %.50s%s, not %.50s of length %zd",
674
0
                  expected,
675
0
                  nullable ? " or None" : "",
676
0
                  what, size);
677
0
    return msgbuf;
678
0
}
679
680
0
#define CONV_UNICODE "(unicode conversion error)"
681
682
/* Convert a non-tuple argument.  Return NULL if conversion went OK,
683
   or a string with a message describing the failure.  The message is
684
   formatted as "must be <desired type>, not <actual type>".
685
   When failing, an exception may or may not have been raised.
686
   Don't call if a tuple is expected.
687
688
   When you add new format codes, please don't forget poor skipitem() below.
689
*/
690
691
static const char *
692
convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
693
              char *msgbuf, size_t bufsize, freelist_t *freelist)
694
2.57M
{
695
2.57M
#define RETURN_ERR_OCCURRED return msgbuf
696
2.57M
#define HANDLE_NULLABLE                 \
697
2.57M
        if (*format == '?') {           \
698
3.36k
            format++;                   \
699
3.36k
            if (arg == Py_None) {       \
700
0
                break;                  \
701
0
            }                           \
702
3.36k
            nullable = true;            \
703
3.36k
        }
704
705
706
2.57M
    const char *format = *p_format;
707
2.57M
    char c = *format++;
708
2.57M
    const char *sarg;
709
2.57M
    bool nullable = false;
710
711
2.57M
    switch (c) {
712
713
0
    case 'b': { /* unsigned byte -- very short int */
714
0
        unsigned char *p = va_arg(*p_va, unsigned char *);
715
0
        HANDLE_NULLABLE;
716
0
        long ival = PyLong_AsLong(arg);
717
0
        if (ival == -1 && PyErr_Occurred())
718
0
            RETURN_ERR_OCCURRED;
719
0
        else if (ival < 0) {
720
0
            PyErr_SetString(PyExc_OverflowError,
721
0
                            "unsigned byte integer is less than minimum");
722
0
            RETURN_ERR_OCCURRED;
723
0
        }
724
0
        else if (ival > UCHAR_MAX) {
725
0
            PyErr_SetString(PyExc_OverflowError,
726
0
                            "unsigned byte integer is greater than maximum");
727
0
            RETURN_ERR_OCCURRED;
728
0
        }
729
0
            *p = (unsigned char) ival;
730
0
        break;
731
0
    }
732
733
0
    case 'B': {/* byte sized bitfield - both signed and unsigned
734
                  values allowed */
735
0
        unsigned char *p = va_arg(*p_va, unsigned char *);
736
0
        HANDLE_NULLABLE;
737
0
        unsigned long ival = PyLong_AsUnsignedLongMask(arg);
738
0
        if (ival == (unsigned long)-1 && PyErr_Occurred())
739
0
            RETURN_ERR_OCCURRED;
740
0
        else
741
0
            *p = (unsigned char) ival;
742
0
        break;
743
0
    }
744
745
0
    case 'h': {/* signed short int */
746
0
        short *p = va_arg(*p_va, short *);
747
0
        HANDLE_NULLABLE;
748
0
        long ival = PyLong_AsLong(arg);
749
0
        if (ival == -1 && PyErr_Occurred())
750
0
            RETURN_ERR_OCCURRED;
751
0
        else if (ival < SHRT_MIN) {
752
0
            PyErr_SetString(PyExc_OverflowError,
753
0
                            "signed short integer is less than minimum");
754
0
            RETURN_ERR_OCCURRED;
755
0
        }
756
0
        else if (ival > SHRT_MAX) {
757
0
            PyErr_SetString(PyExc_OverflowError,
758
0
                            "signed short integer is greater than maximum");
759
0
            RETURN_ERR_OCCURRED;
760
0
        }
761
0
        else
762
0
            *p = (short) ival;
763
0
        break;
764
0
    }
765
766
0
    case 'H': { /* short int sized bitfield, both signed and
767
                   unsigned allowed */
768
0
        unsigned short *p = va_arg(*p_va, unsigned short *);
769
0
        HANDLE_NULLABLE;
770
0
        unsigned long ival = PyLong_AsUnsignedLongMask(arg);
771
0
        if (ival == (unsigned long)-1 && PyErr_Occurred())
772
0
            RETURN_ERR_OCCURRED;
773
0
        else
774
0
            *p = (unsigned short) ival;
775
0
        break;
776
0
    }
777
778
0
    case 'i': {/* signed int */
779
0
        int *p = va_arg(*p_va, int *);
780
0
        HANDLE_NULLABLE;
781
0
        long ival = PyLong_AsLong(arg);
782
0
        if (ival == -1 && PyErr_Occurred())
783
0
            RETURN_ERR_OCCURRED;
784
0
        else if (ival > INT_MAX) {
785
0
            PyErr_SetString(PyExc_OverflowError,
786
0
                            "signed integer is greater than maximum");
787
0
            RETURN_ERR_OCCURRED;
788
0
        }
789
0
        else if (ival < INT_MIN) {
790
0
            PyErr_SetString(PyExc_OverflowError,
791
0
                            "signed integer is less than minimum");
792
0
            RETURN_ERR_OCCURRED;
793
0
        }
794
0
        else
795
0
            *p = ival;
796
0
        break;
797
0
    }
798
799
0
    case 'I': { /* int sized bitfield, both signed and
800
                   unsigned allowed */
801
0
        unsigned int *p = va_arg(*p_va, unsigned int *);
802
0
        HANDLE_NULLABLE;
803
0
        unsigned long ival = PyLong_AsUnsignedLongMask(arg);
804
0
        if (ival == (unsigned long)-1 && PyErr_Occurred())
805
0
            RETURN_ERR_OCCURRED;
806
0
        else
807
0
            *p = (unsigned int) ival;
808
0
        break;
809
0
    }
810
811
728k
    case 'n': /* Py_ssize_t */
812
728k
    {
813
728k
        PyObject *iobj;
814
728k
        Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
815
728k
        HANDLE_NULLABLE;
816
728k
        Py_ssize_t ival = -1;
817
728k
        iobj = _PyNumber_Index(arg);
818
728k
        if (iobj != NULL) {
819
728k
            ival = PyLong_AsSsize_t(iobj);
820
728k
            Py_DECREF(iobj);
821
728k
        }
822
728k
        if (ival == -1 && PyErr_Occurred())
823
98
            RETURN_ERR_OCCURRED;
824
728k
        *p = ival;
825
728k
        break;
826
728k
    }
827
0
    case 'l': {/* long int */
828
0
        long *p = va_arg(*p_va, long *);
829
0
        HANDLE_NULLABLE;
830
0
        long ival = PyLong_AsLong(arg);
831
0
        if (ival == -1 && PyErr_Occurred())
832
0
            RETURN_ERR_OCCURRED;
833
0
        else
834
0
            *p = ival;
835
0
        break;
836
0
    }
837
838
0
    case 'k': { /* long sized bitfield */
839
0
        unsigned long *p = va_arg(*p_va, unsigned long *);
840
0
        HANDLE_NULLABLE;
841
0
        unsigned long ival;
842
0
        if (!PyIndex_Check(arg)) {
843
0
            return converterr(nullable, "int", arg, msgbuf, bufsize);
844
0
        }
845
0
        ival = PyLong_AsUnsignedLongMask(arg);
846
0
        if (ival == (unsigned long)(long)-1 && PyErr_Occurred()) {
847
0
            RETURN_ERR_OCCURRED;
848
0
        }
849
0
        *p = ival;
850
0
        break;
851
0
    }
852
853
0
    case 'L': {/* long long */
854
0
        long long *p = va_arg( *p_va, long long * );
855
0
        HANDLE_NULLABLE;
856
0
        long long ival = PyLong_AsLongLong(arg);
857
0
        if (ival == (long long)-1 && PyErr_Occurred())
858
0
            RETURN_ERR_OCCURRED;
859
0
        else
860
0
            *p = ival;
861
0
        break;
862
0
    }
863
864
0
    case 'K': { /* long long sized bitfield */
865
0
        unsigned long long *p = va_arg(*p_va, unsigned long long *);
866
0
        HANDLE_NULLABLE;
867
0
        unsigned long long ival;
868
0
        if (!PyIndex_Check(arg)) {
869
0
            return converterr(nullable, "int", arg, msgbuf, bufsize);
870
0
        }
871
0
        ival = PyLong_AsUnsignedLongLongMask(arg);
872
0
        if (ival == (unsigned long long)(long long)-1 && PyErr_Occurred()) {
873
0
            RETURN_ERR_OCCURRED;
874
0
        }
875
0
        *p = ival;
876
0
        break;
877
0
    }
878
879
0
    case 'f': {/* float */
880
0
        float *p = va_arg(*p_va, float *);
881
0
        HANDLE_NULLABLE;
882
0
        double dval = PyFloat_AsDouble(arg);
883
0
        if (dval == -1.0 && PyErr_Occurred())
884
0
            RETURN_ERR_OCCURRED;
885
0
        else
886
0
            *p = (float) dval;
887
0
        break;
888
0
    }
889
890
0
    case 'd': {/* double */
891
0
        double *p = va_arg(*p_va, double *);
892
0
        HANDLE_NULLABLE;
893
0
        double dval = PyFloat_AsDouble(arg);
894
0
        if (dval == -1.0 && PyErr_Occurred())
895
0
            RETURN_ERR_OCCURRED;
896
0
        else
897
0
            *p = dval;
898
0
        break;
899
0
    }
900
901
0
    case 'D': {/* complex double */
902
0
        Py_complex *p = va_arg(*p_va, Py_complex *);
903
0
        HANDLE_NULLABLE;
904
0
        Py_complex cval;
905
0
        cval = PyComplex_AsCComplex(arg);
906
0
        if (PyErr_Occurred())
907
0
            RETURN_ERR_OCCURRED;
908
0
        else
909
0
            *p = cval;
910
0
        break;
911
0
    }
912
913
0
    case 'c': {/* char */
914
0
        char *p = va_arg(*p_va, char *);
915
0
        HANDLE_NULLABLE;
916
0
        if (PyBytes_Check(arg)) {
917
0
            if (PyBytes_GET_SIZE(arg) != 1) {
918
0
                return convertcharerr(nullable, "a byte string of length 1",
919
0
                                      "a bytes object", PyBytes_GET_SIZE(arg),
920
0
                                      msgbuf, bufsize);
921
0
            }
922
0
            *p = PyBytes_AS_STRING(arg)[0];
923
0
        }
924
0
        else if (PyByteArray_Check(arg)) {
925
0
            if (PyByteArray_GET_SIZE(arg) != 1) {
926
0
                return convertcharerr(nullable, "a byte string of length 1",
927
0
                                      "a bytearray object", PyByteArray_GET_SIZE(arg),
928
0
                                      msgbuf, bufsize);
929
0
            }
930
0
            *p = PyByteArray_AS_STRING(arg)[0];
931
0
        }
932
0
        else
933
0
            return converterr(nullable, "a byte string of length 1", arg, msgbuf, bufsize);
934
0
        break;
935
0
    }
936
937
0
    case 'C': {/* unicode char */
938
0
        int *p = va_arg(*p_va, int *);
939
0
        HANDLE_NULLABLE;
940
0
        int kind;
941
0
        const void *data;
942
943
0
        if (!PyUnicode_Check(arg))
944
0
            return converterr(nullable, "a unicode character", arg, msgbuf, bufsize);
945
946
0
        if (PyUnicode_GET_LENGTH(arg) != 1) {
947
0
            return convertcharerr(nullable, "a unicode character",
948
0
                                  "a string", PyUnicode_GET_LENGTH(arg),
949
0
                                  msgbuf, bufsize);
950
0
        }
951
952
0
        kind = PyUnicode_KIND(arg);
953
0
        data = PyUnicode_DATA(arg);
954
0
        *p = PyUnicode_READ(kind, data, 0);
955
0
        break;
956
0
    }
957
958
11.6k
    case 'p': {/* boolean *p*redicate */
959
11.6k
        int *p = va_arg(*p_va, int *);
960
11.6k
        HANDLE_NULLABLE;
961
11.6k
        int val = PyObject_IsTrue(arg);
962
11.6k
        if (val > 0)
963
4.87k
            *p = 1;
964
6.77k
        else if (val == 0)
965
6.77k
            *p = 0;
966
0
        else
967
0
            RETURN_ERR_OCCURRED;
968
11.6k
        break;
969
11.6k
    }
970
971
    /* XXX WAAAAH!  's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
972
       need to be cleaned up! */
973
974
11.6k
    case 'y': {/* any bytes-like object */
975
0
        void **p = (void **)va_arg(*p_va, char **);
976
0
        const char *buf;
977
0
        Py_ssize_t count;
978
0
        if (*format == '*') {
979
0
            format++;
980
0
            HANDLE_NULLABLE;
981
0
            if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
982
0
                return converterr(nullable, buf, arg, msgbuf, bufsize);
983
0
            if (addcleanup(p, freelist, cleanup_buffer)) {
984
0
                return converterr(
985
0
                    nullable, "(cleanup problem)",
986
0
                    arg, msgbuf, bufsize);
987
0
            }
988
0
            break;
989
0
        }
990
0
        else if (*format == '#') {
991
0
            Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*);
992
0
            format++;
993
0
            HANDLE_NULLABLE;
994
0
            count = convertbuffer(arg, (const void **)p, &buf);
995
0
            if (count < 0)
996
0
                return converterr(nullable, buf, arg, msgbuf, bufsize);
997
0
            *psize = count;
998
0
        }
999
0
        else {
1000
0
            HANDLE_NULLABLE;
1001
0
            count = convertbuffer(arg, (const void **)p, &buf);
1002
0
            if (count < 0)
1003
0
                return converterr(nullable, buf, arg, msgbuf, bufsize);
1004
0
            if (strlen(*p) != (size_t)count) {
1005
0
                PyErr_SetString(PyExc_ValueError, "embedded null byte");
1006
0
                RETURN_ERR_OCCURRED;
1007
0
            }
1008
0
        }
1009
0
        break;
1010
0
    }
1011
1012
0
    case 's': /* text string or bytes-like object */
1013
0
    case 'z': /* text string, bytes-like object or None */
1014
0
    {
1015
0
        if (*format == '*') {
1016
            /* "s*" or "z*" */
1017
0
            Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
1018
1019
0
            format++;
1020
0
            HANDLE_NULLABLE;
1021
0
            if (c == 'z' && arg == Py_None)
1022
0
                PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
1023
0
            else if (PyUnicode_Check(arg)) {
1024
0
                Py_ssize_t len;
1025
0
                sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1026
0
                if (sarg == NULL)
1027
0
                    return converterr(nullable, CONV_UNICODE,
1028
0
                                      arg, msgbuf, bufsize);
1029
0
                PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0);
1030
0
            }
1031
0
            else { /* any bytes-like object */
1032
0
                const char *buf;
1033
0
                if (getbuffer(arg, p, &buf) < 0)
1034
0
                    return converterr(nullable, buf, arg, msgbuf, bufsize);
1035
0
            }
1036
0
            if (addcleanup(p, freelist, cleanup_buffer)) {
1037
0
                return converterr(
1038
0
                    nullable, "(cleanup problem)",
1039
0
                    arg, msgbuf, bufsize);
1040
0
            }
1041
0
        } else if (*format == '#') { /* a string or read-only bytes-like object */
1042
            /* "s#" or "z#" */
1043
0
            const void **p = (const void **)va_arg(*p_va, const char **);
1044
0
            Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*);
1045
1046
0
            format++;
1047
0
            HANDLE_NULLABLE;
1048
0
            if (c == 'z' && arg == Py_None) {
1049
0
                *p = NULL;
1050
0
                *psize = 0;
1051
0
            }
1052
0
            else if (PyUnicode_Check(arg)) {
1053
0
                Py_ssize_t len;
1054
0
                sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1055
0
                if (sarg == NULL)
1056
0
                    return converterr(nullable, CONV_UNICODE,
1057
0
                                      arg, msgbuf, bufsize);
1058
0
                *p = sarg;
1059
0
                *psize = len;
1060
0
            }
1061
0
            else { /* read-only bytes-like object */
1062
                /* XXX Really? */
1063
0
                const char *buf;
1064
0
                Py_ssize_t count = convertbuffer(arg, p, &buf);
1065
0
                if (count < 0)
1066
0
                    return converterr(nullable, buf, arg, msgbuf, bufsize);
1067
0
                *psize = count;
1068
0
            }
1069
0
        } else {
1070
            /* "s" or "z" */
1071
0
            const char **p = va_arg(*p_va, const char **);
1072
0
            Py_ssize_t len;
1073
0
            sarg = NULL;
1074
1075
0
            HANDLE_NULLABLE;
1076
0
            if (c == 'z' && arg == Py_None)
1077
0
                *p = NULL;
1078
0
            else if (PyUnicode_Check(arg)) {
1079
0
                sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1080
0
                if (sarg == NULL)
1081
0
                    return converterr(nullable, CONV_UNICODE,
1082
0
                                      arg, msgbuf, bufsize);
1083
0
                if (strlen(sarg) != (size_t)len) {
1084
0
                    PyErr_SetString(PyExc_ValueError, "embedded null character");
1085
0
                    RETURN_ERR_OCCURRED;
1086
0
                }
1087
0
                *p = sarg;
1088
0
            }
1089
0
            else
1090
0
                return converterr(c == 'z' || nullable, "str",
1091
0
                                  arg, msgbuf, bufsize);
1092
0
        }
1093
0
        break;
1094
0
    }
1095
1096
0
    case 'e': {/* encoded string */
1097
0
        char **buffer;
1098
0
        const char *encoding;
1099
0
        PyObject *s;
1100
0
        int recode_strings;
1101
0
        Py_ssize_t size;
1102
0
        const char *ptr;
1103
1104
        /* Get 'e' parameter: the encoding name */
1105
0
        encoding = (const char *)va_arg(*p_va, const char *);
1106
0
        if (encoding == NULL)
1107
0
            encoding = PyUnicode_GetDefaultEncoding();
1108
1109
        /* Get output buffer parameter:
1110
           's' (recode all objects via Unicode) or
1111
           't' (only recode non-string objects)
1112
        */
1113
0
        if (*format == 's')
1114
0
            recode_strings = 1;
1115
0
        else if (*format == 't')
1116
0
            recode_strings = 0;
1117
0
        else
1118
0
            return converterr(
1119
0
                nullable, "(unknown parser marker combination)",
1120
0
                arg, msgbuf, bufsize);
1121
0
        buffer = (char **)va_arg(*p_va, char **);
1122
0
        format++;
1123
0
        if (buffer == NULL)
1124
0
            return converterr(nullable, "(buffer is NULL)",
1125
0
                              arg, msgbuf, bufsize);
1126
0
        Py_ssize_t *psize = NULL;
1127
0
        if (*format == '#') {
1128
            /* Using buffer length parameter '#':
1129
1130
               - if *buffer is NULL, a new buffer of the
1131
               needed size is allocated and the data
1132
               copied into it; *buffer is updated to point
1133
               to the new buffer; the caller is
1134
               responsible for PyMem_Free()ing it after
1135
               usage
1136
1137
               - if *buffer is not NULL, the data is
1138
               copied to *buffer; *buffer_len has to be
1139
               set to the size of the buffer on input;
1140
               buffer overflow is signalled with an error;
1141
               buffer has to provide enough room for the
1142
               encoded string plus the trailing 0-byte
1143
1144
               - in both cases, *buffer_len is updated to
1145
               the size of the buffer /excluding/ the
1146
               trailing 0-byte
1147
1148
            */
1149
0
            psize = va_arg(*p_va, Py_ssize_t*);
1150
1151
0
            format++;
1152
0
            if (psize == NULL) {
1153
0
                return converterr(
1154
0
                    nullable, "(buffer_len is NULL)",
1155
0
                    arg, msgbuf, bufsize);
1156
0
            }
1157
0
        }
1158
0
        HANDLE_NULLABLE;
1159
1160
        /* Encode object */
1161
0
        if (!recode_strings &&
1162
0
            (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1163
0
            s = Py_NewRef(arg);
1164
0
            if (PyBytes_Check(arg)) {
1165
0
                size = PyBytes_GET_SIZE(s);
1166
0
                ptr = PyBytes_AS_STRING(s);
1167
0
            }
1168
0
            else {
1169
0
                size = PyByteArray_GET_SIZE(s);
1170
0
                ptr = PyByteArray_AS_STRING(s);
1171
0
            }
1172
0
        }
1173
0
        else if (PyUnicode_Check(arg)) {
1174
            /* Encode object; use default error handling */
1175
0
            s = PyUnicode_AsEncodedString(arg,
1176
0
                                          encoding,
1177
0
                                          NULL);
1178
0
            if (s == NULL)
1179
0
                return converterr(nullable, "(encoding failed)",
1180
0
                                  arg, msgbuf, bufsize);
1181
0
            assert(PyBytes_Check(s));
1182
0
            size = PyBytes_GET_SIZE(s);
1183
0
            ptr = PyBytes_AS_STRING(s);
1184
0
            if (ptr == NULL)
1185
0
                ptr = "";
1186
0
        }
1187
0
        else {
1188
0
            return converterr(
1189
0
                nullable,
1190
0
                recode_strings ? "str"
1191
0
                : nullable ? "str, bytes, bytearray"
1192
0
                : "str, bytes or bytearray",
1193
0
                arg, msgbuf, bufsize);
1194
0
        }
1195
1196
        /* Write output; output is guaranteed to be 0-terminated */
1197
0
        if (psize != NULL) {
1198
0
            if (*buffer == NULL) {
1199
0
                *buffer = PyMem_NEW(char, size + 1);
1200
0
                if (*buffer == NULL) {
1201
0
                    Py_DECREF(s);
1202
0
                    PyErr_NoMemory();
1203
0
                    RETURN_ERR_OCCURRED;
1204
0
                }
1205
0
                if (addcleanup(buffer, freelist, cleanup_ptr)) {
1206
0
                    Py_DECREF(s);
1207
0
                    return converterr(
1208
0
                        nullable, "(cleanup problem)",
1209
0
                        arg, msgbuf, bufsize);
1210
0
                }
1211
0
            } else {
1212
0
                if (size + 1 > *psize) {
1213
0
                    Py_DECREF(s);
1214
0
                    PyErr_Format(PyExc_ValueError,
1215
0
                                 "encoded string too long "
1216
0
                                 "(%zd, maximum length %zd)",
1217
0
                                 (Py_ssize_t)size, (Py_ssize_t)(*psize - 1));
1218
0
                    RETURN_ERR_OCCURRED;
1219
0
                }
1220
0
            }
1221
0
            memcpy(*buffer, ptr, size+1);
1222
1223
0
            *psize = size;
1224
0
        }
1225
0
        else {
1226
            /* Using a 0-terminated buffer:
1227
1228
               - the encoded string has to be 0-terminated
1229
               for this variant to work; if it is not, an
1230
               error raised
1231
1232
               - a new buffer of the needed size is
1233
               allocated and the data copied into it;
1234
               *buffer is updated to point to the new
1235
               buffer; the caller is responsible for
1236
               PyMem_Free()ing it after usage
1237
1238
            */
1239
0
            if ((Py_ssize_t)strlen(ptr) != size) {
1240
0
                Py_DECREF(s);
1241
0
                return converterr(
1242
0
                    nullable, "encoded string without null bytes",
1243
0
                    arg, msgbuf, bufsize);
1244
0
            }
1245
0
            *buffer = PyMem_NEW(char, size + 1);
1246
0
            if (*buffer == NULL) {
1247
0
                Py_DECREF(s);
1248
0
                PyErr_NoMemory();
1249
0
                RETURN_ERR_OCCURRED;
1250
0
            }
1251
0
            if (addcleanup(buffer, freelist, cleanup_ptr)) {
1252
0
                Py_DECREF(s);
1253
0
                return converterr(nullable, "(cleanup problem)",
1254
0
                                arg, msgbuf, bufsize);
1255
0
            }
1256
0
            memcpy(*buffer, ptr, size+1);
1257
0
        }
1258
0
        Py_DECREF(s);
1259
0
        break;
1260
0
    }
1261
1262
0
    case 'S': { /* PyBytes object */
1263
0
        PyObject **p = va_arg(*p_va, PyObject **);
1264
0
        HANDLE_NULLABLE;
1265
0
        if (PyBytes_Check(arg))
1266
0
            *p = arg;
1267
0
        else
1268
0
            return converterr(nullable, "bytes", arg, msgbuf, bufsize);
1269
0
        break;
1270
0
    }
1271
1272
0
    case 'Y': { /* PyByteArray object */
1273
0
        PyObject **p = va_arg(*p_va, PyObject **);
1274
0
        HANDLE_NULLABLE;
1275
0
        if (PyByteArray_Check(arg))
1276
0
            *p = arg;
1277
0
        else
1278
0
            return converterr(nullable, "bytearray", arg, msgbuf, bufsize);
1279
0
        break;
1280
0
    }
1281
1282
1.13M
    case 'U': { /* PyUnicode object */
1283
1.13M
        PyObject **p = va_arg(*p_va, PyObject **);
1284
1.13M
        HANDLE_NULLABLE;
1285
1.13M
        if (PyUnicode_Check(arg)) {
1286
1.13M
            *p = arg;
1287
1.13M
        }
1288
0
        else
1289
0
            return converterr(nullable, "str", arg, msgbuf, bufsize);
1290
1.13M
        break;
1291
1.13M
    }
1292
1293
1.13M
    case 'O': { /* object */
1294
693k
        PyTypeObject *type;
1295
693k
        PyObject **p;
1296
693k
        if (*format == '!') {
1297
496k
            type = va_arg(*p_va, PyTypeObject*);
1298
496k
            p = va_arg(*p_va, PyObject **);
1299
496k
            format++;
1300
496k
            HANDLE_NULLABLE;
1301
496k
            if (PyType_IsSubtype(Py_TYPE(arg), type))
1302
496k
                *p = arg;
1303
0
            else
1304
0
                return converterr(nullable, type->tp_name, arg, msgbuf, bufsize);
1305
1306
496k
        }
1307
196k
        else if (*format == '&') {
1308
0
            typedef int (*converter)(PyObject *, void *);
1309
0
            converter convert = va_arg(*p_va, converter);
1310
0
            void *addr = va_arg(*p_va, void *);
1311
0
            int res;
1312
0
            format++;
1313
0
            HANDLE_NULLABLE;
1314
0
            if (! (res = (*convert)(arg, addr)))
1315
0
                return converterr(nullable, "(unspecified)",
1316
0
                                  arg, msgbuf, bufsize);
1317
0
            if (res == Py_CLEANUP_SUPPORTED &&
1318
0
                addcleanup(addr, freelist, convert) == -1)
1319
0
                return converterr(nullable, "(cleanup problem)",
1320
0
                                arg, msgbuf, bufsize);
1321
0
        }
1322
196k
        else {
1323
196k
            p = va_arg(*p_va, PyObject **);
1324
196k
            HANDLE_NULLABLE;
1325
196k
            *p = arg;
1326
196k
        }
1327
693k
        break;
1328
693k
    }
1329
1330
1331
693k
    case 'w': { /* "w*": memory buffer, read-write access */
1332
0
        void **p = va_arg(*p_va, void **);
1333
1334
0
        if (*format != '*')
1335
0
            return converterr(
1336
0
                nullable, "(invalid use of 'w' format character)",
1337
0
                arg, msgbuf, bufsize);
1338
0
        format++;
1339
0
        HANDLE_NULLABLE;
1340
1341
        /* Caller is interested in Py_buffer, and the object supports it
1342
           directly. The request implicitly asks for PyBUF_SIMPLE, so the
1343
           result is C-contiguous with format 'B'. */
1344
0
        if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1345
0
            PyErr_Clear();
1346
0
            return converterr(nullable, "read-write bytes-like object",
1347
0
                              arg, msgbuf, bufsize);
1348
0
        }
1349
0
        assert(PyBuffer_IsContiguous((Py_buffer *)p, 'C'));
1350
0
        if (addcleanup(p, freelist, cleanup_buffer)) {
1351
0
            return converterr(
1352
0
                nullable, "(cleanup problem)",
1353
0
                arg, msgbuf, bufsize);
1354
0
        }
1355
0
        break;
1356
0
    }
1357
1358
0
    default:
1359
0
        return converterr(nullable, "(impossible<bad format char>)", arg, msgbuf, bufsize);
1360
1361
2.57M
    }
1362
1363
2.57M
    *p_format = format;
1364
2.57M
    return NULL;
1365
1366
2.57M
#undef RETURN_ERR_OCCURRED
1367
2.57M
}
1368
1369
static Py_ssize_t
1370
convertbuffer(PyObject *arg, const void **p, const char **errmsg)
1371
0
{
1372
0
    PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
1373
0
    Py_ssize_t count;
1374
0
    Py_buffer view;
1375
1376
0
    *errmsg = NULL;
1377
0
    *p = NULL;
1378
0
    if (pb != NULL && pb->bf_releasebuffer != NULL) {
1379
0
        *errmsg = "read-only bytes-like object";
1380
0
        return -1;
1381
0
    }
1382
1383
0
    if (getbuffer(arg, &view, errmsg) < 0)
1384
0
        return -1;
1385
0
    count = view.len;
1386
0
    *p = view.buf;
1387
0
    PyBuffer_Release(&view);
1388
0
    return count;
1389
0
}
1390
1391
static int
1392
getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
1393
0
{
1394
    /* PyBUF_SIMPLE implies C-contiguous */
1395
0
    if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
1396
0
        *errmsg = "bytes-like object";
1397
0
        return -1;
1398
0
    }
1399
0
    assert(PyBuffer_IsContiguous(view, 'C'));
1400
0
    return 0;
1401
0
}
1402
1403
/* Support for keyword arguments donated by
1404
   Geoff Philbrick <philbric@delphi.hks.com> */
1405
1406
/* Return false (0) for error, else true. */
1407
int
1408
PyArg_ParseTupleAndKeywords(PyObject *args,
1409
                            PyObject *keywords,
1410
                            const char *format,
1411
                            const char * const *kwlist, ...)
1412
35.0k
{
1413
35.0k
    int retval;
1414
35.0k
    va_list va;
1415
1416
35.0k
    if ((args == NULL || !PyTuple_Check(args)) ||
1417
35.0k
        (keywords != NULL && !PyDict_Check(keywords)) ||
1418
35.0k
        format == NULL ||
1419
35.0k
        kwlist == NULL)
1420
0
    {
1421
0
        PyErr_BadInternalCall();
1422
0
        return 0;
1423
0
    }
1424
1425
35.0k
    va_start(va, kwlist);
1426
35.0k
    retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1427
35.0k
    va_end(va);
1428
35.0k
    return retval;
1429
35.0k
}
1430
1431
int
1432
_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1433
                                  PyObject *keywords,
1434
                                  const char *format,
1435
                                  const char * const *kwlist, ...)
1436
0
{
1437
0
    int retval;
1438
0
    va_list va;
1439
1440
0
    if ((args == NULL || !PyTuple_Check(args)) ||
1441
0
        (keywords != NULL && !PyDict_Check(keywords)) ||
1442
0
        format == NULL ||
1443
0
        kwlist == NULL)
1444
0
    {
1445
0
        PyErr_BadInternalCall();
1446
0
        return 0;
1447
0
    }
1448
1449
0
    va_start(va, kwlist);
1450
0
    retval = vgetargskeywords(args, keywords, format,
1451
0
                              kwlist, &va, 0);
1452
0
    va_end(va);
1453
0
    return retval;
1454
0
}
1455
1456
1457
int
1458
PyArg_VaParseTupleAndKeywords(PyObject *args,
1459
                              PyObject *keywords,
1460
                              const char *format,
1461
                              const char * const *kwlist, va_list va)
1462
0
{
1463
0
    int retval;
1464
0
    va_list lva;
1465
1466
0
    if ((args == NULL || !PyTuple_Check(args)) ||
1467
0
        (keywords != NULL && !PyDict_Check(keywords)) ||
1468
0
        format == NULL ||
1469
0
        kwlist == NULL)
1470
0
    {
1471
0
        PyErr_BadInternalCall();
1472
0
        return 0;
1473
0
    }
1474
1475
0
    va_copy(lva, va);
1476
1477
0
    retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1478
0
    va_end(lva);
1479
0
    return retval;
1480
0
}
1481
1482
int
1483
_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1484
                                    PyObject *keywords,
1485
                                    const char *format,
1486
                                    const char * const *kwlist, va_list va)
1487
0
{
1488
0
    int retval;
1489
0
    va_list lva;
1490
1491
0
    if ((args == NULL || !PyTuple_Check(args)) ||
1492
0
        (keywords != NULL && !PyDict_Check(keywords)) ||
1493
0
        format == NULL ||
1494
0
        kwlist == NULL)
1495
0
    {
1496
0
        PyErr_BadInternalCall();
1497
0
        return 0;
1498
0
    }
1499
1500
0
    va_copy(lva, va);
1501
1502
0
    retval = vgetargskeywords(args, keywords, format,
1503
0
                              kwlist, &lva, 0);
1504
0
    va_end(lva);
1505
0
    return retval;
1506
0
}
1507
1508
PyAPI_FUNC(int)
1509
_PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1510
                            struct _PyArg_Parser *parser, ...)
1511
0
{
1512
0
    int retval;
1513
0
    va_list va;
1514
1515
0
    va_start(va, parser);
1516
0
    retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1517
0
    va_end(va);
1518
0
    return retval;
1519
0
}
1520
1521
int
1522
_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1523
                            struct _PyArg_Parser *parser, ...)
1524
0
{
1525
0
    int retval;
1526
0
    va_list va;
1527
1528
0
    va_start(va, parser);
1529
0
    retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1530
0
    va_end(va);
1531
0
    return retval;
1532
0
}
1533
1534
int
1535
_PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
1536
                  struct _PyArg_Parser *parser, ...)
1537
0
{
1538
0
    int retval;
1539
0
    va_list va;
1540
1541
0
    va_start(va, parser);
1542
0
    retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
1543
0
    va_end(va);
1544
0
    return retval;
1545
0
}
1546
1547
int
1548
_PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
1549
                        struct _PyArg_Parser *parser, ...)
1550
0
{
1551
0
    int retval;
1552
0
    va_list va;
1553
1554
0
    va_start(va, parser);
1555
0
    retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
1556
0
    va_end(va);
1557
0
    return retval;
1558
0
}
1559
1560
1561
static void
1562
error_unexpected_keyword_arg(PyObject *kwargs, PyObject *kwnames, PyObject *kwtuple, const char *fname)
1563
0
{
1564
    /* make sure there are no extraneous keyword arguments */
1565
0
    Py_ssize_t j = 0;
1566
0
    while (1) {
1567
0
        PyObject *keyword;
1568
0
        if (kwargs != NULL) {
1569
0
            if (!PyDict_Next(kwargs, &j, &keyword, NULL))
1570
0
                break;
1571
0
        }
1572
0
        else {
1573
0
            if (j >= PyTuple_GET_SIZE(kwnames))
1574
0
                break;
1575
0
            keyword = PyTuple_GET_ITEM(kwnames, j);
1576
0
            j++;
1577
0
        }
1578
0
        if (!PyUnicode_Check(keyword)) {
1579
0
            PyErr_SetString(PyExc_TypeError,
1580
0
                            "keywords must be strings");
1581
0
            return;
1582
0
        }
1583
1584
0
        int match = PySequence_Contains(kwtuple, keyword);
1585
0
        if (match <= 0) {
1586
0
            if (!match) {
1587
0
                PyObject *kwlist = PySequence_List(kwtuple);
1588
0
                if (!kwlist) {
1589
0
                    return;
1590
0
                }
1591
0
                PyObject *suggestion_keyword = _Py_CalculateSuggestions(kwlist, keyword);
1592
0
                Py_DECREF(kwlist);
1593
1594
0
                if (suggestion_keyword) {
1595
0
                    PyErr_Format(PyExc_TypeError,
1596
0
                                "%.200s%s got an unexpected keyword argument '%S'."
1597
0
                                " Did you mean '%S'?",
1598
0
                                (fname == NULL) ? "this function" : fname,
1599
0
                                (fname == NULL) ? "" : "()",
1600
0
                                keyword,
1601
0
                                suggestion_keyword);
1602
0
                    Py_DECREF(suggestion_keyword);
1603
0
                }
1604
0
                else {
1605
0
                    PyErr_Format(PyExc_TypeError,
1606
0
                                "%.200s%s got an unexpected keyword argument '%S'",
1607
0
                                (fname == NULL) ? "this function" : fname,
1608
0
                                (fname == NULL) ? "" : "()",
1609
0
                                keyword);
1610
0
                }
1611
1612
0
            }
1613
0
            return;
1614
0
        }
1615
0
    }
1616
    /* Something wrong happened. There are extraneous keyword arguments,
1617
     * but we don't know what. And we don't bother. */
1618
0
    PyErr_Format(PyExc_TypeError,
1619
0
                 "invalid keyword argument for %.200s%s",
1620
0
                 (fname == NULL) ? "this function" : fname,
1621
0
                 (fname == NULL) ? "" : "()");
1622
0
}
1623
1624
int
1625
PyArg_ValidateKeywordArguments(PyObject *kwargs)
1626
202
{
1627
202
    if (!PyDict_Check(kwargs)) {
1628
0
        PyErr_BadInternalCall();
1629
0
        return 0;
1630
0
    }
1631
202
    if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1632
0
        PyErr_SetString(PyExc_TypeError,
1633
0
                        "keywords must be strings");
1634
0
        return 0;
1635
0
    }
1636
202
    return 1;
1637
202
}
1638
1639
static PyObject *
1640
new_kwtuple(const char * const *keywords, int total, int pos);
1641
1642
128k
#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1643
1644
static int
1645
vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
1646
                 const char * const *kwlist, va_list *p_va, int flags)
1647
35.0k
{
1648
35.0k
    char msgbuf[512];
1649
35.0k
    int levels[32];
1650
35.0k
    const char *fname, *msg, *custom_msg;
1651
35.0k
    int min = INT_MAX;
1652
35.0k
    int max = INT_MAX;
1653
35.0k
    int i, pos, len;
1654
35.0k
    int skip = 0;
1655
35.0k
    Py_ssize_t nargs, nkwargs;
1656
35.0k
    freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
1657
35.0k
    freelist_t freelist;
1658
1659
35.0k
    freelist.entries = static_entries;
1660
35.0k
    freelist.first_available = 0;
1661
35.0k
    freelist.entries_malloced = 0;
1662
1663
35.0k
    assert(args != NULL && PyTuple_Check(args));
1664
35.0k
    assert(kwargs == NULL || PyDict_Check(kwargs));
1665
35.0k
    assert(format != NULL);
1666
35.0k
    assert(kwlist != NULL);
1667
35.0k
    assert(p_va != NULL);
1668
1669
    /* grab the function name or custom error msg first (mutually exclusive) */
1670
35.0k
    fname = strchr(format, ':');
1671
35.0k
    if (fname) {
1672
32.0k
        fname++;
1673
32.0k
        custom_msg = NULL;
1674
32.0k
    }
1675
2.98k
    else {
1676
2.98k
        custom_msg = strchr(format,';');
1677
2.98k
        if (custom_msg)
1678
0
            custom_msg++;
1679
2.98k
    }
1680
1681
    /* scan kwlist and count the number of positional-only parameters */
1682
35.0k
    for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
1683
8
    }
1684
    /* scan kwlist and get greatest possible nbr of args */
1685
150k
    for (len = pos; kwlist[len]; len++) {
1686
115k
        if (!*kwlist[len]) {
1687
0
            PyErr_SetString(PyExc_SystemError,
1688
0
                            "Empty keyword parameter name");
1689
0
            return cleanreturn(0, &freelist);
1690
0
        }
1691
115k
    }
1692
1693
35.0k
    if (len > STATIC_FREELIST_ENTRIES) {
1694
6.34k
        freelist.entries = PyMem_NEW(freelistentry_t, len);
1695
6.34k
        if (freelist.entries == NULL) {
1696
0
            PyErr_NoMemory();
1697
0
            return 0;
1698
0
        }
1699
6.34k
        freelist.entries_malloced = 1;
1700
6.34k
    }
1701
1702
35.0k
    nargs = PyTuple_GET_SIZE(args);
1703
35.0k
    nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs);
1704
35.0k
    if (nargs + nkwargs > len) {
1705
        /* Adding "keyword" (when nargs == 0) prevents producing wrong error
1706
           messages in some special cases (see bpo-31229). */
1707
0
        PyErr_Format(PyExc_TypeError,
1708
0
                     "%.200s%s takes at most %d %sargument%s (%zd given)",
1709
0
                     (fname == NULL) ? "function" : fname,
1710
0
                     (fname == NULL) ? "" : "()",
1711
0
                     len,
1712
0
                     (nargs == 0) ? "keyword " : "",
1713
0
                     (len == 1) ? "" : "s",
1714
0
                     nargs + nkwargs);
1715
0
        return cleanreturn(0, &freelist);
1716
0
    }
1717
1718
    /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1719
107k
    for (i = 0; i < len; i++) {
1720
86.3k
        if (*format == '|') {
1721
15.6k
            if (min != INT_MAX) {
1722
0
                PyErr_SetString(PyExc_SystemError,
1723
0
                                "Invalid format string (| specified twice)");
1724
0
                return cleanreturn(0, &freelist);
1725
0
            }
1726
1727
15.6k
            min = i;
1728
15.6k
            format++;
1729
1730
15.6k
            if (max != INT_MAX) {
1731
0
                PyErr_SetString(PyExc_SystemError,
1732
0
                                "Invalid format string ($ before |)");
1733
0
                return cleanreturn(0, &freelist);
1734
0
            }
1735
15.6k
        }
1736
86.3k
        if (*format == '$') {
1737
6.81k
            if (max != INT_MAX) {
1738
0
                PyErr_SetString(PyExc_SystemError,
1739
0
                                "Invalid format string ($ specified twice)");
1740
0
                return cleanreturn(0, &freelist);
1741
0
            }
1742
1743
6.81k
            max = i;
1744
6.81k
            format++;
1745
1746
6.81k
            if (max < pos) {
1747
0
                PyErr_SetString(PyExc_SystemError,
1748
0
                                "Empty parameter name after $");
1749
0
                return cleanreturn(0, &freelist);
1750
0
            }
1751
6.81k
            if (skip) {
1752
                /* Now we know the minimal and the maximal numbers of
1753
                 * positional arguments and can raise an exception with
1754
                 * informative message (see below). */
1755
0
                break;
1756
0
            }
1757
6.81k
            if (max < nargs) {
1758
0
                if (max == 0) {
1759
0
                    PyErr_Format(PyExc_TypeError,
1760
0
                                 "%.200s%s takes no positional arguments",
1761
0
                                 (fname == NULL) ? "function" : fname,
1762
0
                                 (fname == NULL) ? "" : "()");
1763
0
                }
1764
0
                else {
1765
0
                    PyErr_Format(PyExc_TypeError,
1766
0
                                 "%.200s%s takes %s %d positional argument%s"
1767
0
                                 " (%zd given)",
1768
0
                                 (fname == NULL) ? "function" : fname,
1769
0
                                 (fname == NULL) ? "" : "()",
1770
0
                                 (min != INT_MAX) ? "at most" : "exactly",
1771
0
                                 max,
1772
0
                                 max == 1 ? "" : "s",
1773
0
                                 nargs);
1774
0
                }
1775
0
                return cleanreturn(0, &freelist);
1776
0
            }
1777
6.81k
        }
1778
86.3k
        if (IS_END_OF_FORMAT(*format)) {
1779
0
            PyErr_Format(PyExc_SystemError,
1780
0
                         "More keyword list entries (%d) than "
1781
0
                         "format specifiers (%d)", len, i);
1782
0
            return cleanreturn(0, &freelist);
1783
0
        }
1784
86.3k
        if (!skip) {
1785
86.3k
            PyObject *current_arg;
1786
86.3k
            if (i < nargs) {
1787
62.5k
                current_arg = Py_NewRef(PyTuple_GET_ITEM(args, i));
1788
62.5k
            }
1789
23.8k
            else if (nkwargs && i >= pos) {
1790
9.73k
                if (PyDict_GetItemStringRef(kwargs, kwlist[i], &current_arg) < 0) {
1791
0
                    return cleanreturn(0, &freelist);
1792
0
                }
1793
9.73k
                if (current_arg) {
1794
6.61k
                    --nkwargs;
1795
6.61k
                }
1796
9.73k
            }
1797
14.1k
            else {
1798
14.1k
                current_arg = NULL;
1799
14.1k
            }
1800
1801
86.3k
            if (current_arg) {
1802
69.1k
                msg = convertitem(current_arg, &format, p_va, flags,
1803
69.1k
                    levels, msgbuf, sizeof(msgbuf), &freelist);
1804
69.1k
                Py_DECREF(current_arg);
1805
69.1k
                if (msg) {
1806
0
                    seterror(i+1, msg, levels, fname, custom_msg);
1807
0
                    return cleanreturn(0, &freelist);
1808
0
                }
1809
69.1k
                continue;
1810
69.1k
            }
1811
1812
17.2k
            if (i < min) {
1813
0
                if (i < pos) {
1814
0
                    assert (min == INT_MAX);
1815
0
                    assert (max == INT_MAX);
1816
0
                    skip = 1;
1817
                    /* At that moment we still don't know the minimal and
1818
                     * the maximal numbers of positional arguments.  Raising
1819
                     * an exception is deferred until we encounter | and $
1820
                     * or the end of the format. */
1821
0
                }
1822
0
                else {
1823
0
                    PyErr_Format(PyExc_TypeError,  "%.200s%s missing required "
1824
0
                                 "argument '%s' (pos %d)",
1825
0
                                 (fname == NULL) ? "function" : fname,
1826
0
                                 (fname == NULL) ? "" : "()",
1827
0
                                 kwlist[i], i+1);
1828
0
                    return cleanreturn(0, &freelist);
1829
0
                }
1830
0
            }
1831
            /* current code reports success when all required args
1832
             * fulfilled and no keyword args left, with no further
1833
             * validation. XXX Maybe skip this in debug build ?
1834
             */
1835
17.2k
            if (!nkwargs && !skip) {
1836
14.1k
                return cleanreturn(1, &freelist);
1837
14.1k
            }
1838
17.2k
        }
1839
1840
        /* We are into optional args, skip through to any remaining
1841
         * keyword args */
1842
3.11k
        msg = skipitem(&format, p_va, flags);
1843
3.11k
        if (msg) {
1844
0
            PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1845
0
                         format);
1846
0
            return cleanreturn(0, &freelist);
1847
0
        }
1848
3.11k
    }
1849
1850
20.9k
    if (skip) {
1851
0
        PyErr_Format(PyExc_TypeError,
1852
0
                     "%.200s%s takes %s %d positional argument%s"
1853
0
                     " (%zd given)",
1854
0
                     (fname == NULL) ? "function" : fname,
1855
0
                     (fname == NULL) ? "" : "()",
1856
0
                     (Py_MIN(pos, min) < i) ? "at least" : "exactly",
1857
0
                     Py_MIN(pos, min),
1858
0
                     Py_MIN(pos, min) == 1 ? "" : "s",
1859
0
                     nargs);
1860
0
        return cleanreturn(0, &freelist);
1861
0
    }
1862
1863
20.9k
    if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1864
0
        PyErr_Format(PyExc_SystemError,
1865
0
            "more argument specifiers than keyword list entries "
1866
0
            "(remaining format:'%s')", format);
1867
0
        return cleanreturn(0, &freelist);
1868
0
    }
1869
1870
20.9k
    if (nkwargs > 0) {
1871
0
        PyObject *key;
1872
0
        Py_ssize_t j;
1873
        /* make sure there are no arguments given by name and position */
1874
0
        for (i = pos; i < nargs; i++) {
1875
0
            PyObject *current_arg;
1876
0
            if (PyDict_GetItemStringRef(kwargs, kwlist[i], &current_arg) < 0) {
1877
0
                return cleanreturn(0, &freelist);
1878
0
            }
1879
0
            if (current_arg) {
1880
0
                Py_DECREF(current_arg);
1881
                /* arg present in tuple and in dict */
1882
0
                PyErr_Format(PyExc_TypeError,
1883
0
                             "argument for %.200s%s given by name ('%s') "
1884
0
                             "and position (%d)",
1885
0
                             (fname == NULL) ? "function" : fname,
1886
0
                             (fname == NULL) ? "" : "()",
1887
0
                             kwlist[i], i+1);
1888
0
                return cleanreturn(0, &freelist);
1889
0
            }
1890
0
        }
1891
        /* make sure there are no extraneous keyword arguments */
1892
0
        j = 0;
1893
0
        while (PyDict_Next(kwargs, &j, &key, NULL)) {
1894
0
            int match = 0;
1895
0
            if (!PyUnicode_Check(key)) {
1896
0
                PyErr_SetString(PyExc_TypeError,
1897
0
                                "keywords must be strings");
1898
0
                return cleanreturn(0, &freelist);
1899
0
            }
1900
0
            for (i = pos; i < len; i++) {
1901
0
                if (PyUnicode_EqualToUTF8(key, kwlist[i])) {
1902
0
                    match = 1;
1903
0
                    break;
1904
0
                }
1905
0
            }
1906
0
            if (!match) {
1907
0
                PyObject *_pykwtuple = new_kwtuple(kwlist, len, pos);
1908
0
                if (!_pykwtuple) {
1909
0
                    return cleanreturn(0, &freelist);
1910
0
                }
1911
0
                PyObject *pykwlist = PySequence_List(_pykwtuple);
1912
0
                Py_DECREF(_pykwtuple);
1913
0
                if (!pykwlist) {
1914
0
                    return cleanreturn(0, &freelist);
1915
0
                }
1916
0
                PyObject *suggestion_keyword = _Py_CalculateSuggestions(pykwlist, key);
1917
0
                Py_DECREF(pykwlist);
1918
1919
0
                if (suggestion_keyword) {
1920
0
                    PyErr_Format(PyExc_TypeError,
1921
0
                                "%.200s%s got an unexpected keyword argument '%S'."
1922
0
                                " Did you mean '%S'?",
1923
0
                                (fname == NULL) ? "this function" : fname,
1924
0
                                (fname == NULL) ? "" : "()",
1925
0
                                key,
1926
0
                                suggestion_keyword);
1927
0
                    Py_DECREF(suggestion_keyword);
1928
0
                }
1929
0
                else {
1930
0
                    PyErr_Format(PyExc_TypeError,
1931
0
                                "%.200s%s got an unexpected keyword argument '%S'",
1932
0
                                (fname == NULL) ? "this function" : fname,
1933
0
                                (fname == NULL) ? "" : "()",
1934
0
                                key);
1935
0
                }
1936
0
                return cleanreturn(0, &freelist);
1937
0
            }
1938
0
        }
1939
        /* Something wrong happened. There are extraneous keyword arguments,
1940
         * but we don't know what. And we don't bother. */
1941
0
        PyErr_Format(PyExc_TypeError,
1942
0
                     "invalid keyword argument for %.200s%s",
1943
0
                     (fname == NULL) ? "this function" : fname,
1944
0
                     (fname == NULL) ? "" : "()");
1945
0
        return cleanreturn(0, &freelist);
1946
0
    }
1947
1948
20.9k
    return cleanreturn(1, &freelist);
1949
20.9k
}
1950
1951
1952
static int
1953
scan_keywords(const char * const *keywords, int *ptotal, int *pposonly)
1954
84
{
1955
    /* scan keywords and count the number of positional-only parameters */
1956
84
    int i;
1957
85
    for (i = 0; keywords[i] && !*keywords[i]; i++) {
1958
1
    }
1959
84
    *pposonly = i;
1960
1961
    /* scan keywords and get greatest possible nbr of args */
1962
365
    for (; keywords[i]; i++) {
1963
281
        if (!*keywords[i]) {
1964
0
            PyErr_SetString(PyExc_SystemError,
1965
0
                            "Empty keyword parameter name");
1966
0
            return -1;
1967
0
        }
1968
281
    }
1969
84
    *ptotal = i;
1970
84
    return 0;
1971
84
}
1972
1973
static int
1974
parse_format(const char *format, int total, int npos,
1975
             const char **pfname, const char **pcustommsg,
1976
             int *pmin, int *pmax)
1977
0
{
1978
    /* grab the function name or custom error msg first (mutually exclusive) */
1979
0
    const char *custommsg;
1980
0
    const char *fname = strchr(format, ':');
1981
0
    if (fname) {
1982
0
        fname++;
1983
0
        custommsg = NULL;
1984
0
    }
1985
0
    else {
1986
0
        custommsg = strchr(format,';');
1987
0
        if (custommsg) {
1988
0
            custommsg++;
1989
0
        }
1990
0
    }
1991
1992
0
    int min = INT_MAX;
1993
0
    int max = INT_MAX;
1994
0
    for (int i = 0; i < total; i++) {
1995
0
        if (*format == '|') {
1996
0
            if (min != INT_MAX) {
1997
0
                PyErr_SetString(PyExc_SystemError,
1998
0
                                "Invalid format string (| specified twice)");
1999
0
                return -1;
2000
0
            }
2001
0
            if (max != INT_MAX) {
2002
0
                PyErr_SetString(PyExc_SystemError,
2003
0
                                "Invalid format string ($ before |)");
2004
0
                return -1;
2005
0
            }
2006
0
            min = i;
2007
0
            format++;
2008
0
        }
2009
0
        if (*format == '$') {
2010
0
            if (max != INT_MAX) {
2011
0
                PyErr_SetString(PyExc_SystemError,
2012
0
                                "Invalid format string ($ specified twice)");
2013
0
                return -1;
2014
0
            }
2015
0
            if (i < npos) {
2016
0
                PyErr_SetString(PyExc_SystemError,
2017
0
                                "Empty parameter name after $");
2018
0
                return -1;
2019
0
            }
2020
0
            max = i;
2021
0
            format++;
2022
0
        }
2023
0
        if (IS_END_OF_FORMAT(*format)) {
2024
0
            PyErr_Format(PyExc_SystemError,
2025
0
                        "More keyword list entries (%d) than "
2026
0
                        "format specifiers (%d)", total, i);
2027
0
            return -1;
2028
0
        }
2029
2030
0
        const char *msg = skipitem(&format, NULL, 0);
2031
0
        if (msg) {
2032
0
            PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
2033
0
                        format);
2034
0
            return -1;
2035
0
        }
2036
0
    }
2037
0
    min = Py_MIN(min, total);
2038
0
    max = Py_MIN(max, total);
2039
2040
0
    if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
2041
0
        PyErr_Format(PyExc_SystemError,
2042
0
            "more argument specifiers than keyword list entries "
2043
0
            "(remaining format:'%s')", format);
2044
0
        return -1;
2045
0
    }
2046
2047
0
    *pfname = fname;
2048
0
    *pcustommsg = custommsg;
2049
0
    *pmin = min;
2050
0
    *pmax = max;
2051
0
    return 0;
2052
0
}
2053
2054
static PyObject *
2055
new_kwtuple(const char * const *keywords, int total, int pos)
2056
29
{
2057
29
    int nkw = total - pos;
2058
29
    PyObject *kwtuple = PyTuple_New(nkw);
2059
29
    if (kwtuple == NULL) {
2060
0
        return NULL;
2061
0
    }
2062
29
    keywords += pos;
2063
110
    for (int i = 0; i < nkw; i++) {
2064
81
        PyObject *str = PyUnicode_FromString(keywords[i]);
2065
81
        if (str == NULL) {
2066
0
            Py_DECREF(kwtuple);
2067
0
            return NULL;
2068
0
        }
2069
81
        PyInterpreterState *interp = _PyInterpreterState_GET();
2070
81
        _PyUnicode_InternImmortal(interp, &str);
2071
81
        PyTuple_SET_ITEM(kwtuple, i, str);
2072
81
    }
2073
29
    return kwtuple;
2074
29
}
2075
2076
static int
2077
_parser_init(void *arg)
2078
84
{
2079
84
    struct _PyArg_Parser *parser = (struct _PyArg_Parser *)arg;
2080
84
    const char * const *keywords = parser->keywords;
2081
84
    assert(keywords != NULL);
2082
84
    assert(parser->pos == 0 &&
2083
84
           (parser->format == NULL || parser->fname == NULL) &&
2084
84
           parser->custom_msg == NULL &&
2085
84
           parser->min == 0 &&
2086
84
           parser->max == 0);
2087
2088
84
    int len, pos;
2089
84
    if (scan_keywords(keywords, &len, &pos) < 0) {
2090
0
        return -1;
2091
0
    }
2092
2093
84
    const char *fname, *custommsg = NULL;
2094
84
    int min = 0, max = 0;
2095
84
    if (parser->format) {
2096
0
        assert(parser->fname == NULL);
2097
0
        if (parse_format(parser->format, len, pos,
2098
0
                         &fname, &custommsg, &min, &max) < 0) {
2099
0
            return -1;
2100
0
        }
2101
0
    }
2102
84
    else {
2103
84
        assert(parser->fname != NULL);
2104
84
        fname = parser->fname;
2105
84
    }
2106
2107
84
    int owned;
2108
84
    PyObject *kwtuple = parser->kwtuple;
2109
84
    if (kwtuple == NULL) {
2110
        /* We may temporarily switch to the main interpreter to avoid
2111
         * creating a tuple that could outlive its owning interpreter. */
2112
29
        PyThreadState *save_tstate = NULL;
2113
29
        PyThreadState *temp_tstate = NULL;
2114
29
        if (!_Py_IsMainInterpreter(PyInterpreterState_Get())) {
2115
0
            temp_tstate = PyThreadState_New(_PyInterpreterState_Main());
2116
0
            if (temp_tstate == NULL) {
2117
0
                return -1;
2118
0
            }
2119
0
            save_tstate = PyThreadState_Swap(temp_tstate);
2120
0
        }
2121
29
        kwtuple = new_kwtuple(keywords, len, pos);
2122
29
        if (temp_tstate != NULL) {
2123
0
            PyThreadState_Clear(temp_tstate);
2124
0
            (void)PyThreadState_Swap(save_tstate);
2125
0
            PyThreadState_Delete(temp_tstate);
2126
0
        }
2127
29
        if (kwtuple == NULL) {
2128
0
            return -1;
2129
0
        }
2130
29
        owned = 1;
2131
29
    }
2132
55
    else {
2133
55
        owned = 0;
2134
55
    }
2135
2136
84
    parser->pos = pos;
2137
84
    parser->fname = fname;
2138
84
    parser->custom_msg = custommsg;
2139
84
    parser->min = min;
2140
84
    parser->max = max;
2141
84
    parser->kwtuple = kwtuple;
2142
84
    parser->is_kwtuple_owned = owned;
2143
2144
84
    assert(parser->next == NULL);
2145
84
    parser->next = _Py_atomic_load_ptr(&_PyRuntime.getargs.static_parsers);
2146
84
    do {
2147
        // compare-exchange updates parser->next on failure
2148
84
    } while (!_Py_atomic_compare_exchange_ptr(&_PyRuntime.getargs.static_parsers,
2149
84
                                              &parser->next, parser));
2150
84
    return 0;
2151
84
}
2152
2153
static int
2154
parser_init(struct _PyArg_Parser *parser)
2155
609k
{
2156
609k
    return _PyOnceFlag_CallOnce(&parser->once, &_parser_init, parser);
2157
609k
}
2158
2159
static void
2160
parser_clear(struct _PyArg_Parser *parser)
2161
0
{
2162
0
    if (parser->is_kwtuple_owned) {
2163
0
        Py_CLEAR(parser->kwtuple);
2164
0
    }
2165
2166
0
    if (parser->format) {
2167
0
        parser->fname = NULL;
2168
0
    }
2169
0
    else {
2170
0
        assert(parser->fname != NULL);
2171
0
    }
2172
0
    parser->custom_msg = NULL;
2173
0
    parser->pos = 0;
2174
0
    parser->min = 0;
2175
0
    parser->max = 0;
2176
0
    parser->is_kwtuple_owned = 0;
2177
0
    parser->once.v = 0;
2178
0
}
2179
2180
static PyObject*
2181
find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
2182
642k
{
2183
642k
    Py_ssize_t i, nkwargs;
2184
2185
642k
    nkwargs = PyTuple_GET_SIZE(kwnames);
2186
722k
    for (i = 0; i < nkwargs; i++) {
2187
695k
        PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
2188
2189
        /* kwname == key will normally find a match in since keyword keys
2190
           should be interned strings; if not retry below in a new loop. */
2191
695k
        if (kwname == key) {
2192
615k
            return Py_NewRef(kwstack[i]);
2193
615k
        }
2194
695k
    }
2195
2196
81.8k
    for (i = 0; i < nkwargs; i++) {
2197
54.5k
        PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
2198
54.5k
        assert(PyUnicode_Check(kwname));
2199
54.5k
        if (_PyUnicode_Equal(kwname, key)) {
2200
0
            return Py_NewRef(kwstack[i]);
2201
0
        }
2202
54.5k
    }
2203
27.2k
    return NULL;
2204
27.2k
}
2205
2206
static int
2207
vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
2208
                          PyObject *kwargs, PyObject *kwnames,
2209
                          struct _PyArg_Parser *parser,
2210
                          va_list *p_va, int flags)
2211
0
{
2212
0
    PyObject *kwtuple;
2213
0
    char msgbuf[512];
2214
0
    int levels[32];
2215
0
    const char *format;
2216
0
    const char *msg;
2217
0
    PyObject *keyword;
2218
0
    Py_ssize_t i;
2219
0
    int pos, len;
2220
0
    Py_ssize_t nkwargs;
2221
0
    freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
2222
0
    freelist_t freelist;
2223
0
    PyObject *const *kwstack = NULL;
2224
2225
0
    freelist.entries = static_entries;
2226
0
    freelist.first_available = 0;
2227
0
    freelist.entries_malloced = 0;
2228
2229
0
    assert(kwargs == NULL || PyDict_Check(kwargs));
2230
0
    assert(kwargs == NULL || kwnames == NULL);
2231
0
    assert(p_va != NULL);
2232
2233
0
    if (parser == NULL) {
2234
0
        PyErr_BadInternalCall();
2235
0
        return 0;
2236
0
    }
2237
2238
0
    if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2239
0
        PyErr_BadInternalCall();
2240
0
        return 0;
2241
0
    }
2242
2243
0
    if (parser_init(parser) < 0) {
2244
0
        return 0;
2245
0
    }
2246
2247
0
    kwtuple = parser->kwtuple;
2248
0
    pos = parser->pos;
2249
0
    len = pos + (int)PyTuple_GET_SIZE(kwtuple);
2250
2251
0
    if (len > STATIC_FREELIST_ENTRIES) {
2252
0
        freelist.entries = PyMem_NEW(freelistentry_t, len);
2253
0
        if (freelist.entries == NULL) {
2254
0
            PyErr_NoMemory();
2255
0
            return 0;
2256
0
        }
2257
0
        freelist.entries_malloced = 1;
2258
0
    }
2259
2260
0
    if (kwargs != NULL) {
2261
0
        nkwargs = PyDict_GET_SIZE(kwargs);
2262
0
    }
2263
0
    else if (kwnames != NULL) {
2264
0
        nkwargs = PyTuple_GET_SIZE(kwnames);
2265
0
        kwstack = args + nargs;
2266
0
    }
2267
0
    else {
2268
0
        nkwargs = 0;
2269
0
    }
2270
0
    if (nargs + nkwargs > len) {
2271
        /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2272
           messages in some special cases (see bpo-31229). */
2273
0
        PyErr_Format(PyExc_TypeError,
2274
0
                     "%.200s%s takes at most %d %sargument%s (%zd given)",
2275
0
                     (parser->fname == NULL) ? "function" : parser->fname,
2276
0
                     (parser->fname == NULL) ? "" : "()",
2277
0
                     len,
2278
0
                     (nargs == 0) ? "keyword " : "",
2279
0
                     (len == 1) ? "" : "s",
2280
0
                     nargs + nkwargs);
2281
0
        return cleanreturn(0, &freelist);
2282
0
    }
2283
0
    if (parser->max < nargs) {
2284
0
        if (parser->max == 0) {
2285
0
            PyErr_Format(PyExc_TypeError,
2286
0
                         "%.200s%s takes no positional arguments",
2287
0
                         (parser->fname == NULL) ? "function" : parser->fname,
2288
0
                         (parser->fname == NULL) ? "" : "()");
2289
0
        }
2290
0
        else {
2291
0
            PyErr_Format(PyExc_TypeError,
2292
0
                         "%.200s%s takes %s %d positional argument%s (%zd given)",
2293
0
                         (parser->fname == NULL) ? "function" : parser->fname,
2294
0
                         (parser->fname == NULL) ? "" : "()",
2295
0
                         (parser->min < parser->max) ? "at most" : "exactly",
2296
0
                         parser->max,
2297
0
                         parser->max == 1 ? "" : "s",
2298
0
                         nargs);
2299
0
        }
2300
0
        return cleanreturn(0, &freelist);
2301
0
    }
2302
2303
0
    format = parser->format;
2304
0
    assert(format != NULL || len == 0);
2305
    /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
2306
0
    for (i = 0; i < len; i++) {
2307
0
        if (*format == '|') {
2308
0
            format++;
2309
0
        }
2310
0
        if (*format == '$') {
2311
0
            format++;
2312
0
        }
2313
0
        assert(!IS_END_OF_FORMAT(*format));
2314
2315
0
        PyObject *current_arg;
2316
0
        if (i < nargs) {
2317
0
            current_arg = Py_NewRef(args[i]);
2318
0
        }
2319
0
        else if (nkwargs && i >= pos) {
2320
0
            keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2321
0
            if (kwargs != NULL) {
2322
0
                if (PyDict_GetItemRef(kwargs, keyword, &current_arg) < 0) {
2323
0
                    return cleanreturn(0, &freelist);
2324
0
                }
2325
0
            }
2326
0
            else {
2327
0
                current_arg = find_keyword(kwnames, kwstack, keyword);
2328
0
            }
2329
0
            if (current_arg) {
2330
0
                --nkwargs;
2331
0
            }
2332
0
        }
2333
0
        else {
2334
0
            current_arg = NULL;
2335
0
        }
2336
2337
0
        if (current_arg) {
2338
0
            msg = convertitem(current_arg, &format, p_va, flags,
2339
0
                levels, msgbuf, sizeof(msgbuf), &freelist);
2340
0
            Py_DECREF(current_arg);
2341
0
            if (msg) {
2342
0
                seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
2343
0
                return cleanreturn(0, &freelist);
2344
0
            }
2345
0
            continue;
2346
0
        }
2347
2348
0
        if (i < parser->min) {
2349
            /* Less arguments than required */
2350
0
            if (i < pos) {
2351
0
                Py_ssize_t min = Py_MIN(pos, parser->min);
2352
0
                PyErr_Format(PyExc_TypeError,
2353
0
                             "%.200s%s takes %s %d positional argument%s"
2354
0
                             " (%zd given)",
2355
0
                             (parser->fname == NULL) ? "function" : parser->fname,
2356
0
                             (parser->fname == NULL) ? "" : "()",
2357
0
                             min < parser->max ? "at least" : "exactly",
2358
0
                             min,
2359
0
                             min == 1 ? "" : "s",
2360
0
                             nargs);
2361
0
            }
2362
0
            else {
2363
0
                keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2364
0
                PyErr_Format(PyExc_TypeError,  "%.200s%s missing required "
2365
0
                             "argument '%U' (pos %d)",
2366
0
                             (parser->fname == NULL) ? "function" : parser->fname,
2367
0
                             (parser->fname == NULL) ? "" : "()",
2368
0
                             keyword, i+1);
2369
0
            }
2370
0
            return cleanreturn(0, &freelist);
2371
0
        }
2372
        /* current code reports success when all required args
2373
         * fulfilled and no keyword args left, with no further
2374
         * validation. XXX Maybe skip this in debug build ?
2375
         */
2376
0
        if (!nkwargs) {
2377
0
            return cleanreturn(1, &freelist);
2378
0
        }
2379
2380
        /* We are into optional args, skip through to any remaining
2381
         * keyword args */
2382
0
        msg = skipitem(&format, p_va, flags);
2383
0
        assert(msg == NULL);
2384
0
    }
2385
2386
0
    assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
2387
2388
0
    if (nkwargs > 0) {
2389
        /* make sure there are no arguments given by name and position */
2390
0
        for (i = pos; i < nargs; i++) {
2391
0
            PyObject *current_arg;
2392
0
            keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2393
0
            if (kwargs != NULL) {
2394
0
                if (PyDict_GetItemRef(kwargs, keyword, &current_arg) < 0) {
2395
0
                    return cleanreturn(0, &freelist);
2396
0
                }
2397
0
            }
2398
0
            else {
2399
0
                current_arg = find_keyword(kwnames, kwstack, keyword);
2400
0
            }
2401
0
            if (current_arg) {
2402
0
                Py_DECREF(current_arg);
2403
                /* arg present in tuple and in dict */
2404
0
                PyErr_Format(PyExc_TypeError,
2405
0
                             "argument for %.200s%s given by name ('%U') "
2406
0
                             "and position (%d)",
2407
0
                             (parser->fname == NULL) ? "function" : parser->fname,
2408
0
                             (parser->fname == NULL) ? "" : "()",
2409
0
                             keyword, i+1);
2410
0
                return cleanreturn(0, &freelist);
2411
0
            }
2412
0
        }
2413
2414
0
        error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname);
2415
0
        return cleanreturn(0, &freelist);
2416
0
    }
2417
2418
0
    return cleanreturn(1, &freelist);
2419
0
}
2420
2421
static int
2422
vgetargskeywordsfast(PyObject *args, PyObject *keywords,
2423
                     struct _PyArg_Parser *parser, va_list *p_va, int flags)
2424
0
{
2425
0
    PyObject **stack;
2426
0
    Py_ssize_t nargs;
2427
2428
0
    if (args == NULL
2429
0
        || !PyTuple_Check(args)
2430
0
        || (keywords != NULL && !PyDict_Check(keywords)))
2431
0
    {
2432
0
        PyErr_BadInternalCall();
2433
0
        return 0;
2434
0
    }
2435
2436
0
    stack = _PyTuple_ITEMS(args);
2437
0
    nargs = PyTuple_GET_SIZE(args);
2438
0
    return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
2439
0
                                     parser, p_va, flags);
2440
0
}
2441
2442
2443
#undef _PyArg_UnpackKeywords
2444
2445
PyObject * const *
2446
_PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
2447
                      PyObject *kwargs, PyObject *kwnames,
2448
                      struct _PyArg_Parser *parser,
2449
                      int minpos, int maxpos, int minkw, int varpos,
2450
                      PyObject **buf)
2451
609k
{
2452
609k
    PyObject *kwtuple;
2453
609k
    PyObject *keyword;
2454
609k
    int i, posonly, minposonly, maxargs;
2455
609k
    int reqlimit = minkw ? maxpos + minkw : minpos;
2456
609k
    Py_ssize_t nkwargs;
2457
609k
    PyObject * const *kwstack = NULL;
2458
2459
609k
    assert(kwargs == NULL || PyDict_Check(kwargs));
2460
609k
    assert(kwargs == NULL || kwnames == NULL);
2461
2462
609k
    if (parser == NULL) {
2463
0
        PyErr_BadInternalCall();
2464
0
        return NULL;
2465
0
    }
2466
2467
609k
    if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2468
0
        PyErr_BadInternalCall();
2469
0
        return NULL;
2470
0
    }
2471
2472
609k
    if (args == NULL && nargs == 0) {
2473
1.88k
        args = buf;
2474
1.88k
    }
2475
2476
609k
    if (parser_init(parser) < 0) {
2477
0
        return NULL;
2478
0
    }
2479
2480
609k
    kwtuple = parser->kwtuple;
2481
609k
    posonly = parser->pos;
2482
609k
    minposonly = Py_MIN(posonly, minpos);
2483
609k
    maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple);
2484
2485
609k
    if (kwargs != NULL) {
2486
17.4k
        nkwargs = PyDict_GET_SIZE(kwargs);
2487
17.4k
    }
2488
592k
    else if (kwnames != NULL) {
2489
590k
        nkwargs = PyTuple_GET_SIZE(kwnames);
2490
590k
        kwstack = args + nargs;
2491
590k
    }
2492
1.88k
    else {
2493
1.88k
        nkwargs = 0;
2494
1.88k
    }
2495
609k
    if (nkwargs == 0 && minkw == 0 && minpos <= nargs && (varpos || nargs <= maxpos)) {
2496
        /* Fast path. */
2497
1.88k
        return args;
2498
1.88k
    }
2499
607k
    if (!varpos && nargs + nkwargs > maxargs) {
2500
        /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2501
           messages in some special cases (see bpo-31229). */
2502
0
        PyErr_Format(PyExc_TypeError,
2503
0
                     "%.200s%s takes at most %d %sargument%s (%zd given)",
2504
0
                     (parser->fname == NULL) ? "function" : parser->fname,
2505
0
                     (parser->fname == NULL) ? "" : "()",
2506
0
                     maxargs,
2507
0
                     (nargs == 0) ? "keyword " : "",
2508
0
                     (maxargs == 1) ? "" : "s",
2509
0
                     nargs + nkwargs);
2510
0
        return NULL;
2511
0
    }
2512
607k
    if (!varpos && nargs > maxpos) {
2513
0
        if (maxpos == 0) {
2514
0
            PyErr_Format(PyExc_TypeError,
2515
0
                         "%.200s%s takes no positional arguments",
2516
0
                         (parser->fname == NULL) ? "function" : parser->fname,
2517
0
                         (parser->fname == NULL) ? "" : "()");
2518
0
        }
2519
0
        else {
2520
0
            PyErr_Format(PyExc_TypeError,
2521
0
                         "%.200s%s takes %s %d positional argument%s (%zd given)",
2522
0
                         (parser->fname == NULL) ? "function" : parser->fname,
2523
0
                         (parser->fname == NULL) ? "" : "()",
2524
0
                         (minpos < maxpos) ? "at most" : "exactly",
2525
0
                         maxpos,
2526
0
                         (maxpos == 1) ? "" : "s",
2527
0
                         nargs);
2528
0
        }
2529
0
        return NULL;
2530
0
    }
2531
607k
    if (nargs < minposonly) {
2532
0
        PyErr_Format(PyExc_TypeError,
2533
0
                     "%.200s%s takes %s %d positional argument%s"
2534
0
                     " (%zd given)",
2535
0
                     (parser->fname == NULL) ? "function" : parser->fname,
2536
0
                     (parser->fname == NULL) ? "" : "()",
2537
0
                     (varpos || minposonly < maxpos) ? "at least" : "exactly",
2538
0
                     minposonly,
2539
0
                     minposonly == 1 ? "" : "s",
2540
0
                     nargs);
2541
0
        return NULL;
2542
0
    }
2543
2544
607k
    if (varpos) {
2545
0
        nargs = Py_MIN(maxpos, nargs);
2546
0
    }
2547
    /* copy tuple args */
2548
754k
    for (i = 0; i < nargs; i++) {
2549
146k
        buf[i] = args[i];
2550
146k
    }
2551
2552
    /* copy keyword args using kwtuple to drive process */
2553
1.28M
    for (i = Py_MAX((int)nargs, posonly); i < maxargs; i++) {
2554
1.19M
        PyObject *current_arg;
2555
1.19M
        if (nkwargs) {
2556
677k
            keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2557
677k
            if (kwargs != NULL) {
2558
34.8k
                if (PyDict_GetItemRef(kwargs, keyword, &current_arg) < 0) {
2559
0
                    return NULL;
2560
0
                }
2561
34.8k
            }
2562
642k
            else {
2563
642k
                current_arg = find_keyword(kwnames, kwstack, keyword);
2564
642k
            }
2565
677k
        }
2566
513k
        else if (i >= reqlimit) {
2567
513k
            break;
2568
513k
        }
2569
0
        else {
2570
0
            current_arg = NULL;
2571
0
        }
2572
2573
677k
        buf[i] = current_arg;
2574
2575
677k
        if (current_arg) {
2576
633k
            Py_DECREF(current_arg);
2577
633k
            --nkwargs;
2578
633k
        }
2579
44.7k
        else if (i < minpos || (maxpos <= i && i < reqlimit)) {
2580
            /* Less arguments than required */
2581
0
            keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2582
0
            PyErr_Format(PyExc_TypeError,  "%.200s%s missing required "
2583
0
                         "argument '%U' (pos %d)",
2584
0
                         (parser->fname == NULL) ? "function" : parser->fname,
2585
0
                         (parser->fname == NULL) ? "" : "()",
2586
0
                         keyword, i+1);
2587
0
            return NULL;
2588
0
        }
2589
677k
    }
2590
2591
607k
    if (nkwargs > 0) {
2592
        /* make sure there are no arguments given by name and position */
2593
0
        for (i = posonly; i < nargs; i++) {
2594
0
            PyObject *current_arg;
2595
0
            keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2596
0
            if (kwargs != NULL) {
2597
0
                if (PyDict_GetItemRef(kwargs, keyword, &current_arg) < 0) {
2598
0
                    return NULL;
2599
0
                }
2600
0
            }
2601
0
            else {
2602
0
                current_arg = find_keyword(kwnames, kwstack, keyword);
2603
0
            }
2604
0
            if (current_arg) {
2605
0
                Py_DECREF(current_arg);
2606
                /* arg present in tuple and in dict */
2607
0
                PyErr_Format(PyExc_TypeError,
2608
0
                             "argument for %.200s%s given by name ('%U') "
2609
0
                             "and position (%d)",
2610
0
                             (parser->fname == NULL) ? "function" : parser->fname,
2611
0
                             (parser->fname == NULL) ? "" : "()",
2612
0
                             keyword, i+1);
2613
0
                return NULL;
2614
0
            }
2615
0
        }
2616
2617
0
        error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname);
2618
0
        return NULL;
2619
0
    }
2620
2621
607k
    return buf;
2622
607k
}
2623
2624
static const char *
2625
skipitem(const char **p_format, va_list *p_va, int flags)
2626
3.11k
{
2627
3.11k
    const char *format = *p_format;
2628
3.11k
    char c = *format++;
2629
2630
3.11k
    switch (c) {
2631
2632
    /*
2633
     * codes that take a single data pointer as an argument
2634
     * (the type of the pointer is irrelevant)
2635
     */
2636
2637
0
    case 'b': /* byte -- very short int */
2638
0
    case 'B': /* byte as bitfield */
2639
0
    case 'h': /* short int */
2640
0
    case 'H': /* short int as bitfield */
2641
0
    case 'i': /* int */
2642
0
    case 'I': /* int sized bitfield */
2643
0
    case 'l': /* long int */
2644
0
    case 'k': /* long int sized bitfield */
2645
0
    case 'L': /* long long */
2646
0
    case 'K': /* long long sized bitfield */
2647
0
    case 'n': /* Py_ssize_t */
2648
0
    case 'f': /* float */
2649
0
    case 'd': /* double */
2650
0
    case 'D': /* complex double */
2651
0
    case 'c': /* char */
2652
0
    case 'C': /* unicode char */
2653
0
    case 'p': /* boolean predicate */
2654
0
    case 'S': /* string object */
2655
0
    case 'Y': /* string object */
2656
0
    case 'U': /* unicode string object */
2657
0
        {
2658
0
            if (p_va != NULL) {
2659
0
                (void) va_arg(*p_va, void *);
2660
0
            }
2661
0
            break;
2662
0
        }
2663
2664
    /* string codes */
2665
2666
0
    case 'e': /* string with encoding */
2667
0
        {
2668
0
            if (p_va != NULL) {
2669
0
                (void) va_arg(*p_va, const char *);
2670
0
            }
2671
0
            if (!(*format == 's' || *format == 't'))
2672
                /* after 'e', only 's' and 't' is allowed */
2673
0
                goto err;
2674
0
            format++;
2675
0
        }
2676
0
        _Py_FALLTHROUGH;
2677
2678
0
    case 's': /* string */
2679
0
    case 'z': /* string or None */
2680
0
    case 'y': /* bytes */
2681
0
    case 'w': /* buffer, read-write */
2682
0
        {
2683
0
            if (p_va != NULL) {
2684
0
                (void) va_arg(*p_va, char **);
2685
0
            }
2686
0
            if (c == 'w' && *format != '*')
2687
0
            {
2688
                /* after 'w', only '*' is allowed */
2689
0
                goto err;
2690
0
            }
2691
0
            if (*format == '#') {
2692
0
                if (p_va != NULL) {
2693
0
                    (void) va_arg(*p_va, Py_ssize_t *);
2694
0
                }
2695
0
                format++;
2696
0
            } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w')
2697
0
                       && *format == '*')
2698
0
            {
2699
0
                format++;
2700
0
            }
2701
0
            break;
2702
0
        }
2703
2704
3.11k
    case 'O': /* object */
2705
3.11k
        {
2706
3.11k
            if (*format == '!') {
2707
0
                format++;
2708
0
                if (p_va != NULL) {
2709
0
                    (void) va_arg(*p_va, PyTypeObject*);
2710
0
                    (void) va_arg(*p_va, PyObject **);
2711
0
                }
2712
0
            }
2713
3.11k
            else if (*format == '&') {
2714
0
                typedef int (*converter)(PyObject *, void *);
2715
0
                if (p_va != NULL) {
2716
0
                    (void) va_arg(*p_va, converter);
2717
0
                    (void) va_arg(*p_va, void *);
2718
0
                }
2719
0
                format++;
2720
0
            }
2721
3.11k
            else {
2722
3.11k
                if (p_va != NULL) {
2723
3.11k
                    (void) va_arg(*p_va, PyObject **);
2724
3.11k
                }
2725
3.11k
            }
2726
3.11k
            break;
2727
0
        }
2728
2729
0
    case '(':           /* bypass tuple, not handled at all previously */
2730
0
        {
2731
0
            const char *msg;
2732
0
            for (;;) {
2733
0
                if (*format==')')
2734
0
                    break;
2735
0
                if (IS_END_OF_FORMAT(*format))
2736
0
                    return "Unmatched left paren in format "
2737
0
                           "string";
2738
0
                msg = skipitem(&format, p_va, flags);
2739
0
                if (msg)
2740
0
                    return msg;
2741
0
            }
2742
0
            format++;
2743
0
            break;
2744
0
        }
2745
2746
0
    case ')':
2747
0
        return "Unmatched right paren in format string";
2748
2749
0
    default:
2750
0
err:
2751
0
        return "impossible<bad format char>";
2752
2753
3.11k
    }
2754
3.11k
    if (*format == '?') {
2755
0
        format++;
2756
0
    }
2757
2758
3.11k
    *p_format = format;
2759
3.11k
    return NULL;
2760
3.11k
}
2761
2762
2763
#undef _PyArg_CheckPositional
2764
2765
int
2766
_PyArg_CheckPositional(const char *name, Py_ssize_t nargs,
2767
                       Py_ssize_t min, Py_ssize_t max)
2768
568k
{
2769
568k
    assert(min >= 0);
2770
568k
    assert(min <= max);
2771
2772
568k
    if (nargs < min) {
2773
0
        if (name != NULL)
2774
0
            PyErr_Format(
2775
0
                PyExc_TypeError,
2776
0
                "%.200s expected %s%zd argument%s, got %zd",
2777
0
                name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
2778
0
        else
2779
0
            PyErr_Format(
2780
0
                PyExc_TypeError,
2781
0
                "unpacked tuple should have %s%zd element%s,"
2782
0
                " but has %zd",
2783
0
                (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
2784
0
        return 0;
2785
0
    }
2786
2787
568k
    if (nargs == 0) {
2788
289
        return 1;
2789
289
    }
2790
2791
568k
    if (nargs > max) {
2792
0
        if (name != NULL)
2793
0
            PyErr_Format(
2794
0
                PyExc_TypeError,
2795
0
                "%.200s expected %s%zd argument%s, got %zd",
2796
0
                name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
2797
0
        else
2798
0
            PyErr_Format(
2799
0
                PyExc_TypeError,
2800
0
                "unpacked tuple should have %s%zd element%s,"
2801
0
                " but has %zd",
2802
0
                (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
2803
0
        return 0;
2804
0
    }
2805
2806
568k
    return 1;
2807
568k
}
2808
2809
static int
2810
unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name,
2811
             Py_ssize_t min, Py_ssize_t max, va_list vargs)
2812
568k
{
2813
568k
    Py_ssize_t i;
2814
568k
    PyObject **o;
2815
2816
568k
    if (!_PyArg_CheckPositional(name, nargs, min, max)) {
2817
0
        return 0;
2818
0
    }
2819
2820
1.17M
    for (i = 0; i < nargs; i++) {
2821
608k
        o = va_arg(vargs, PyObject **);
2822
608k
        *o = args[i];
2823
608k
    }
2824
568k
    return 1;
2825
568k
}
2826
2827
int
2828
PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
2829
289k
{
2830
289k
    PyObject **stack;
2831
289k
    Py_ssize_t nargs;
2832
289k
    int retval;
2833
289k
    va_list vargs;
2834
2835
289k
    if (!PyTuple_Check(args)) {
2836
0
        PyErr_SetString(PyExc_SystemError,
2837
0
            "PyArg_UnpackTuple() argument list is not a tuple");
2838
0
        return 0;
2839
0
    }
2840
289k
    stack = _PyTuple_ITEMS(args);
2841
289k
    nargs = PyTuple_GET_SIZE(args);
2842
2843
289k
    va_start(vargs, max);
2844
289k
    retval = unpack_stack(stack, nargs, name, min, max, vargs);
2845
289k
    va_end(vargs);
2846
289k
    return retval;
2847
289k
}
2848
2849
int
2850
_PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name,
2851
                   Py_ssize_t min, Py_ssize_t max, ...)
2852
278k
{
2853
278k
    int retval;
2854
278k
    va_list vargs;
2855
2856
278k
    va_start(vargs, max);
2857
278k
    retval = unpack_stack(args, nargs, name, min, max, vargs);
2858
278k
    va_end(vargs);
2859
278k
    return retval;
2860
278k
}
2861
2862
2863
#undef _PyArg_NoKeywords
2864
#undef _PyArg_NoKwnames
2865
#undef _PyArg_NoPositional
2866
2867
/* For type constructors that don't take keyword args
2868
 *
2869
 * Sets a TypeError and returns 0 if the args/kwargs is
2870
 * not empty, returns 1 otherwise
2871
 */
2872
int
2873
_PyArg_NoKeywords(const char *funcname, PyObject *kwargs)
2874
11.2M
{
2875
11.2M
    if (kwargs == NULL) {
2876
0
        return 1;
2877
0
    }
2878
11.2M
    if (!PyDict_CheckExact(kwargs)) {
2879
0
        PyErr_BadInternalCall();
2880
0
        return 0;
2881
0
    }
2882
11.2M
    if (PyDict_GET_SIZE(kwargs) == 0) {
2883
11.2M
        return 1;
2884
11.2M
    }
2885
2886
0
    PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
2887
0
                    funcname);
2888
0
    return 0;
2889
11.2M
}
2890
2891
int
2892
_PyArg_NoPositional(const char *funcname, PyObject *args)
2893
2.74k
{
2894
2.74k
    if (args == NULL)
2895
0
        return 1;
2896
2.74k
    if (!PyTuple_CheckExact(args)) {
2897
0
        PyErr_BadInternalCall();
2898
0
        return 0;
2899
0
    }
2900
2.74k
    if (PyTuple_GET_SIZE(args) == 0)
2901
2.74k
        return 1;
2902
2903
0
    PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments",
2904
0
                    funcname);
2905
0
    return 0;
2906
2.74k
}
2907
2908
int
2909
_PyArg_NoKwnames(const char *funcname, PyObject *kwnames)
2910
0
{
2911
0
    if (kwnames == NULL) {
2912
0
        return 1;
2913
0
    }
2914
2915
0
    assert(PyTuple_CheckExact(kwnames));
2916
2917
0
    if (PyTuple_GET_SIZE(kwnames) == 0) {
2918
0
        return 1;
2919
0
    }
2920
2921
0
    PyErr_Format(PyExc_TypeError, "%s() takes no keyword arguments", funcname);
2922
0
    return 0;
2923
0
}
2924
2925
void
2926
_PyArg_Fini(void)
2927
0
{
2928
0
    struct _PyArg_Parser *tmp, *s = _PyRuntime.getargs.static_parsers;
2929
0
    while (s) {
2930
0
        tmp = s->next;
2931
0
        s->next = NULL;
2932
0
        parser_clear(s);
2933
0
        s = tmp;
2934
0
    }
2935
0
    _PyRuntime.getargs.static_parsers = NULL;
2936
0
}