ยปCore Development>Code coverage>Python/getargs.c

Python code coverage for Python/getargs.c

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