Coverage Report

Created: 2025-08-26 06:26

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