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

Python code coverage for Python/modsupport.c

#countcontent
1n/a
2n/a/* Module support implementation */
3n/a
4n/a#include "Python.h"
5n/a
6n/a#define FLAG_SIZE_T 1
7n/atypedef double va_double;
8n/a
9n/astatic PyObject *va_build_value(const char *, va_list, int);
10n/astatic PyObject **va_build_stack(PyObject **small_stack, Py_ssize_t small_stack_len, const char *, va_list, int, Py_ssize_t*);
11n/a
12n/a/* Package context -- the full module name for package imports */
13n/aconst char *_Py_PackageContext = NULL;
14n/a
15n/a/* Helper for mkvalue() to scan the length of a format */
16n/a
17n/astatic Py_ssize_t
18n/acountformat(const char *format, char endchar)
19n/a{
20n/a Py_ssize_t count = 0;
21n/a int level = 0;
22n/a while (level > 0 || *format != endchar) {
23n/a switch (*format) {
24n/a case '\0':
25n/a /* Premature end */
26n/a PyErr_SetString(PyExc_SystemError,
27n/a "unmatched paren in format");
28n/a return -1;
29n/a case '(':
30n/a case '[':
31n/a case '{':
32n/a if (level == 0) {
33n/a count++;
34n/a }
35n/a level++;
36n/a break;
37n/a case ')':
38n/a case ']':
39n/a case '}':
40n/a level--;
41n/a break;
42n/a case '#':
43n/a case '&':
44n/a case ',':
45n/a case ':':
46n/a case ' ':
47n/a case '\t':
48n/a break;
49n/a default:
50n/a if (level == 0) {
51n/a count++;
52n/a }
53n/a }
54n/a format++;
55n/a }
56n/a return count;
57n/a}
58n/a
59n/a
60n/a/* Generic function to create a value -- the inverse of getargs() */
61n/a/* After an original idea and first implementation by Steven Miale */
62n/a
63n/astatic PyObject *do_mktuple(const char**, va_list *, char, Py_ssize_t, int);
64n/astatic int do_mkstack(PyObject **, const char**, va_list *, char, Py_ssize_t, int);
65n/astatic PyObject *do_mklist(const char**, va_list *, char, Py_ssize_t, int);
66n/astatic PyObject *do_mkdict(const char**, va_list *, char, Py_ssize_t, int);
67n/astatic PyObject *do_mkvalue(const char**, va_list *, int);
68n/a
69n/a
70n/astatic void
71n/ado_ignore(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
72n/a{
73n/a PyObject *v;
74n/a Py_ssize_t i;
75n/a assert(PyErr_Occurred());
76n/a v = PyTuple_New(n);
77n/a for (i = 0; i < n; i++) {
78n/a PyObject *exception, *value, *tb, *w;
79n/a
80n/a PyErr_Fetch(&exception, &value, &tb);
81n/a w = do_mkvalue(p_format, p_va, flags);
82n/a PyErr_Restore(exception, value, tb);
83n/a if (w != NULL) {
84n/a if (v != NULL) {
85n/a PyTuple_SET_ITEM(v, i, w);
86n/a }
87n/a else {
88n/a Py_DECREF(w);
89n/a }
90n/a }
91n/a }
92n/a Py_XDECREF(v);
93n/a if (**p_format != endchar) {
94n/a PyErr_SetString(PyExc_SystemError,
95n/a "Unmatched paren in format");
96n/a return;
97n/a }
98n/a if (endchar) {
99n/a ++*p_format;
100n/a }
101n/a}
102n/a
103n/astatic PyObject *
104n/ado_mkdict(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
105n/a{
106n/a PyObject *d;
107n/a Py_ssize_t i;
108n/a if (n < 0)
109n/a return NULL;
110n/a if (n % 2) {
111n/a PyErr_SetString(PyExc_SystemError,
112n/a "Bad dict format");
113n/a do_ignore(p_format, p_va, endchar, n, flags);
114n/a return NULL;
115n/a }
116n/a /* Note that we can't bail immediately on error as this will leak
117n/a refcounts on any 'N' arguments. */
118n/a if ((d = PyDict_New()) == NULL) {
119n/a do_ignore(p_format, p_va, endchar, n, flags);
120n/a return NULL;
121n/a }
122n/a for (i = 0; i < n; i+= 2) {
123n/a PyObject *k, *v;
124n/a
125n/a k = do_mkvalue(p_format, p_va, flags);
126n/a if (k == NULL) {
127n/a do_ignore(p_format, p_va, endchar, n - i - 1, flags);
128n/a Py_DECREF(d);
129n/a return NULL;
130n/a }
131n/a v = do_mkvalue(p_format, p_va, flags);
132n/a if (v == NULL || PyDict_SetItem(d, k, v) < 0) {
133n/a do_ignore(p_format, p_va, endchar, n - i - 2, flags);
134n/a Py_DECREF(k);
135n/a Py_XDECREF(v);
136n/a Py_DECREF(d);
137n/a return NULL;
138n/a }
139n/a Py_DECREF(k);
140n/a Py_DECREF(v);
141n/a }
142n/a if (**p_format != endchar) {
143n/a Py_DECREF(d);
144n/a PyErr_SetString(PyExc_SystemError,
145n/a "Unmatched paren in format");
146n/a return NULL;
147n/a }
148n/a if (endchar)
149n/a ++*p_format;
150n/a return d;
151n/a}
152n/a
153n/astatic PyObject *
154n/ado_mklist(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
155n/a{
156n/a PyObject *v;
157n/a Py_ssize_t i;
158n/a if (n < 0)
159n/a return NULL;
160n/a /* Note that we can't bail immediately on error as this will leak
161n/a refcounts on any 'N' arguments. */
162n/a v = PyList_New(n);
163n/a if (v == NULL) {
164n/a do_ignore(p_format, p_va, endchar, n, flags);
165n/a return NULL;
166n/a }
167n/a for (i = 0; i < n; i++) {
168n/a PyObject *w = do_mkvalue(p_format, p_va, flags);
169n/a if (w == NULL) {
170n/a do_ignore(p_format, p_va, endchar, n - i - 1, flags);
171n/a Py_DECREF(v);
172n/a return NULL;
173n/a }
174n/a PyList_SET_ITEM(v, i, w);
175n/a }
176n/a if (**p_format != endchar) {
177n/a Py_DECREF(v);
178n/a PyErr_SetString(PyExc_SystemError,
179n/a "Unmatched paren in format");
180n/a return NULL;
181n/a }
182n/a if (endchar)
183n/a ++*p_format;
184n/a return v;
185n/a}
186n/a
187n/astatic int
188n/ado_mkstack(PyObject **stack, const char **p_format, va_list *p_va,
189n/a char endchar, Py_ssize_t n, int flags)
190n/a{
191n/a Py_ssize_t i;
192n/a
193n/a if (n < 0) {
194n/a return -1;
195n/a }
196n/a /* Note that we can't bail immediately on error as this will leak
197n/a refcounts on any 'N' arguments. */
198n/a for (i = 0; i < n; i++) {
199n/a PyObject *w = do_mkvalue(p_format, p_va, flags);
200n/a if (w == NULL) {
201n/a do_ignore(p_format, p_va, endchar, n - i - 1, flags);
202n/a goto error;
203n/a }
204n/a stack[i] = w;
205n/a }
206n/a if (**p_format != endchar) {
207n/a PyErr_SetString(PyExc_SystemError,
208n/a "Unmatched paren in format");
209n/a goto error;
210n/a }
211n/a if (endchar) {
212n/a ++*p_format;
213n/a }
214n/a return 0;
215n/a
216n/aerror:
217n/a n = i;
218n/a for (i=0; i < n; i++) {
219n/a Py_DECREF(stack[i]);
220n/a }
221n/a return -1;
222n/a}
223n/a
224n/astatic PyObject *
225n/ado_mktuple(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
226n/a{
227n/a PyObject *v;
228n/a Py_ssize_t i;
229n/a if (n < 0)
230n/a return NULL;
231n/a /* Note that we can't bail immediately on error as this will leak
232n/a refcounts on any 'N' arguments. */
233n/a if ((v = PyTuple_New(n)) == NULL) {
234n/a do_ignore(p_format, p_va, endchar, n, flags);
235n/a return NULL;
236n/a }
237n/a for (i = 0; i < n; i++) {
238n/a PyObject *w = do_mkvalue(p_format, p_va, flags);
239n/a if (w == NULL) {
240n/a do_ignore(p_format, p_va, endchar, n - i - 1, flags);
241n/a Py_DECREF(v);
242n/a return NULL;
243n/a }
244n/a PyTuple_SET_ITEM(v, i, w);
245n/a }
246n/a if (**p_format != endchar) {
247n/a Py_DECREF(v);
248n/a PyErr_SetString(PyExc_SystemError,
249n/a "Unmatched paren in format");
250n/a return NULL;
251n/a }
252n/a if (endchar)
253n/a ++*p_format;
254n/a return v;
255n/a}
256n/a
257n/astatic PyObject *
258n/ado_mkvalue(const char **p_format, va_list *p_va, int flags)
259n/a{
260n/a for (;;) {
261n/a switch (*(*p_format)++) {
262n/a case '(':
263n/a return do_mktuple(p_format, p_va, ')',
264n/a countformat(*p_format, ')'), flags);
265n/a
266n/a case '[':
267n/a return do_mklist(p_format, p_va, ']',
268n/a countformat(*p_format, ']'), flags);
269n/a
270n/a case '{':
271n/a return do_mkdict(p_format, p_va, '}',
272n/a countformat(*p_format, '}'), flags);
273n/a
274n/a case 'b':
275n/a case 'B':
276n/a case 'h':
277n/a case 'i':
278n/a return PyLong_FromLong((long)va_arg(*p_va, int));
279n/a
280n/a case 'H':
281n/a return PyLong_FromLong((long)va_arg(*p_va, unsigned int));
282n/a
283n/a case 'I':
284n/a {
285n/a unsigned int n;
286n/a n = va_arg(*p_va, unsigned int);
287n/a return PyLong_FromUnsignedLong(n);
288n/a }
289n/a
290n/a case 'n':
291n/a#if SIZEOF_SIZE_T!=SIZEOF_LONG
292n/a return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t));
293n/a#endif
294n/a /* Fall through from 'n' to 'l' if Py_ssize_t is long */
295n/a case 'l':
296n/a return PyLong_FromLong(va_arg(*p_va, long));
297n/a
298n/a case 'k':
299n/a {
300n/a unsigned long n;
301n/a n = va_arg(*p_va, unsigned long);
302n/a return PyLong_FromUnsignedLong(n);
303n/a }
304n/a
305n/a case 'L':
306n/a return PyLong_FromLongLong((long long)va_arg(*p_va, long long));
307n/a
308n/a case 'K':
309n/a return PyLong_FromUnsignedLongLong((long long)va_arg(*p_va, unsigned long long));
310n/a
311n/a case 'u':
312n/a {
313n/a PyObject *v;
314n/a Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
315n/a Py_ssize_t n;
316n/a if (**p_format == '#') {
317n/a ++*p_format;
318n/a if (flags & FLAG_SIZE_T)
319n/a n = va_arg(*p_va, Py_ssize_t);
320n/a else
321n/a n = va_arg(*p_va, int);
322n/a }
323n/a else
324n/a n = -1;
325n/a if (u == NULL) {
326n/a v = Py_None;
327n/a Py_INCREF(v);
328n/a }
329n/a else {
330n/a if (n < 0)
331n/a n = wcslen(u);
332n/a v = PyUnicode_FromWideChar(u, n);
333n/a }
334n/a return v;
335n/a }
336n/a case 'f':
337n/a case 'd':
338n/a return PyFloat_FromDouble(
339n/a (double)va_arg(*p_va, va_double));
340n/a
341n/a case 'D':
342n/a return PyComplex_FromCComplex(
343n/a *((Py_complex *)va_arg(*p_va, Py_complex *)));
344n/a
345n/a case 'c':
346n/a {
347n/a char p[1];
348n/a p[0] = (char)va_arg(*p_va, int);
349n/a return PyBytes_FromStringAndSize(p, 1);
350n/a }
351n/a case 'C':
352n/a {
353n/a int i = va_arg(*p_va, int);
354n/a return PyUnicode_FromOrdinal(i);
355n/a }
356n/a
357n/a case 's':
358n/a case 'z':
359n/a case 'U': /* XXX deprecated alias */
360n/a {
361n/a PyObject *v;
362n/a const char *str = va_arg(*p_va, const char *);
363n/a Py_ssize_t n;
364n/a if (**p_format == '#') {
365n/a ++*p_format;
366n/a if (flags & FLAG_SIZE_T)
367n/a n = va_arg(*p_va, Py_ssize_t);
368n/a else
369n/a n = va_arg(*p_va, int);
370n/a }
371n/a else
372n/a n = -1;
373n/a if (str == NULL) {
374n/a v = Py_None;
375n/a Py_INCREF(v);
376n/a }
377n/a else {
378n/a if (n < 0) {
379n/a size_t m = strlen(str);
380n/a if (m > PY_SSIZE_T_MAX) {
381n/a PyErr_SetString(PyExc_OverflowError,
382n/a "string too long for Python string");
383n/a return NULL;
384n/a }
385n/a n = (Py_ssize_t)m;
386n/a }
387n/a v = PyUnicode_FromStringAndSize(str, n);
388n/a }
389n/a return v;
390n/a }
391n/a
392n/a case 'y':
393n/a {
394n/a PyObject *v;
395n/a const char *str = va_arg(*p_va, const char *);
396n/a Py_ssize_t n;
397n/a if (**p_format == '#') {
398n/a ++*p_format;
399n/a if (flags & FLAG_SIZE_T)
400n/a n = va_arg(*p_va, Py_ssize_t);
401n/a else
402n/a n = va_arg(*p_va, int);
403n/a }
404n/a else
405n/a n = -1;
406n/a if (str == NULL) {
407n/a v = Py_None;
408n/a Py_INCREF(v);
409n/a }
410n/a else {
411n/a if (n < 0) {
412n/a size_t m = strlen(str);
413n/a if (m > PY_SSIZE_T_MAX) {
414n/a PyErr_SetString(PyExc_OverflowError,
415n/a "string too long for Python bytes");
416n/a return NULL;
417n/a }
418n/a n = (Py_ssize_t)m;
419n/a }
420n/a v = PyBytes_FromStringAndSize(str, n);
421n/a }
422n/a return v;
423n/a }
424n/a
425n/a case 'N':
426n/a case 'S':
427n/a case 'O':
428n/a if (**p_format == '&') {
429n/a typedef PyObject *(*converter)(void *);
430n/a converter func = va_arg(*p_va, converter);
431n/a void *arg = va_arg(*p_va, void *);
432n/a ++*p_format;
433n/a return (*func)(arg);
434n/a }
435n/a else {
436n/a PyObject *v;
437n/a v = va_arg(*p_va, PyObject *);
438n/a if (v != NULL) {
439n/a if (*(*p_format - 1) != 'N')
440n/a Py_INCREF(v);
441n/a }
442n/a else if (!PyErr_Occurred())
443n/a /* If a NULL was passed
444n/a * because a call that should
445n/a * have constructed a value
446n/a * failed, that's OK, and we
447n/a * pass the error on; but if
448n/a * no error occurred it's not
449n/a * clear that the caller knew
450n/a * what she was doing. */
451n/a PyErr_SetString(PyExc_SystemError,
452n/a "NULL object passed to Py_BuildValue");
453n/a return v;
454n/a }
455n/a
456n/a case ':':
457n/a case ',':
458n/a case ' ':
459n/a case '\t':
460n/a break;
461n/a
462n/a default:
463n/a PyErr_SetString(PyExc_SystemError,
464n/a "bad format char passed to Py_BuildValue");
465n/a return NULL;
466n/a
467n/a }
468n/a }
469n/a}
470n/a
471n/a
472n/aPyObject *
473n/aPy_BuildValue(const char *format, ...)
474n/a{
475n/a va_list va;
476n/a PyObject* retval;
477n/a va_start(va, format);
478n/a retval = va_build_value(format, va, 0);
479n/a va_end(va);
480n/a return retval;
481n/a}
482n/a
483n/aPyObject *
484n/a_Py_BuildValue_SizeT(const char *format, ...)
485n/a{
486n/a va_list va;
487n/a PyObject* retval;
488n/a va_start(va, format);
489n/a retval = va_build_value(format, va, FLAG_SIZE_T);
490n/a va_end(va);
491n/a return retval;
492n/a}
493n/a
494n/aPyObject *
495n/aPy_VaBuildValue(const char *format, va_list va)
496n/a{
497n/a return va_build_value(format, va, 0);
498n/a}
499n/a
500n/aPyObject *
501n/a_Py_VaBuildValue_SizeT(const char *format, va_list va)
502n/a{
503n/a return va_build_value(format, va, FLAG_SIZE_T);
504n/a}
505n/a
506n/astatic PyObject *
507n/ava_build_value(const char *format, va_list va, int flags)
508n/a{
509n/a const char *f = format;
510n/a Py_ssize_t n = countformat(f, '\0');
511n/a va_list lva;
512n/a PyObject *retval;
513n/a
514n/a if (n < 0)
515n/a return NULL;
516n/a if (n == 0) {
517n/a Py_RETURN_NONE;
518n/a }
519n/a va_copy(lva, va);
520n/a if (n == 1) {
521n/a retval = do_mkvalue(&f, &lva, flags);
522n/a } else {
523n/a retval = do_mktuple(&f, &lva, '\0', n, flags);
524n/a }
525n/a va_end(lva);
526n/a return retval;
527n/a}
528n/a
529n/aPyObject **
530n/a_Py_VaBuildStack(PyObject **small_stack, Py_ssize_t small_stack_len,
531n/a const char *format, va_list va, Py_ssize_t *p_nargs)
532n/a{
533n/a return va_build_stack(small_stack, small_stack_len, format, va, 0, p_nargs);
534n/a}
535n/a
536n/aPyObject **
537n/a_Py_VaBuildStack_SizeT(PyObject **small_stack, Py_ssize_t small_stack_len,
538n/a const char *format, va_list va, Py_ssize_t *p_nargs)
539n/a{
540n/a return va_build_stack(small_stack, small_stack_len, format, va, FLAG_SIZE_T, p_nargs);
541n/a}
542n/a
543n/astatic PyObject **
544n/ava_build_stack(PyObject **small_stack, Py_ssize_t small_stack_len,
545n/a const char *format, va_list va, int flags, Py_ssize_t *p_nargs)
546n/a{
547n/a const char *f;
548n/a Py_ssize_t n;
549n/a va_list lva;
550n/a PyObject **stack;
551n/a int res;
552n/a
553n/a n = countformat(format, '\0');
554n/a if (n < 0) {
555n/a *p_nargs = 0;
556n/a return NULL;
557n/a }
558n/a
559n/a if (n == 0) {
560n/a *p_nargs = 0;
561n/a return small_stack;
562n/a }
563n/a
564n/a if (n <= small_stack_len) {
565n/a stack = small_stack;
566n/a }
567n/a else {
568n/a stack = PyMem_Malloc(n * sizeof(stack[0]));
569n/a if (stack == NULL) {
570n/a PyErr_NoMemory();
571n/a return NULL;
572n/a }
573n/a }
574n/a
575n/a va_copy(lva, va);
576n/a f = format;
577n/a res = do_mkstack(stack, &f, &lva, '\0', n, flags);
578n/a va_end(lva);
579n/a
580n/a if (res < 0) {
581n/a return NULL;
582n/a }
583n/a
584n/a *p_nargs = n;
585n/a return stack;
586n/a}
587n/a
588n/a
589n/aPyObject *
590n/aPyEval_CallFunction(PyObject *callable, const char *format, ...)
591n/a{
592n/a va_list vargs;
593n/a PyObject *args;
594n/a PyObject *res;
595n/a
596n/a va_start(vargs, format);
597n/a
598n/a args = Py_VaBuildValue(format, vargs);
599n/a va_end(vargs);
600n/a
601n/a if (args == NULL)
602n/a return NULL;
603n/a
604n/a res = PyEval_CallObject(callable, args);
605n/a Py_DECREF(args);
606n/a
607n/a return res;
608n/a}
609n/a
610n/a
611n/aPyObject *
612n/aPyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
613n/a{
614n/a va_list vargs;
615n/a PyObject *meth;
616n/a PyObject *args;
617n/a PyObject *res;
618n/a
619n/a meth = PyObject_GetAttrString(obj, name);
620n/a if (meth == NULL)
621n/a return NULL;
622n/a
623n/a va_start(vargs, format);
624n/a
625n/a args = Py_VaBuildValue(format, vargs);
626n/a va_end(vargs);
627n/a
628n/a if (args == NULL) {
629n/a Py_DECREF(meth);
630n/a return NULL;
631n/a }
632n/a
633n/a res = PyEval_CallObject(meth, args);
634n/a Py_DECREF(meth);
635n/a Py_DECREF(args);
636n/a
637n/a return res;
638n/a}
639n/a
640n/aint
641n/aPyModule_AddObject(PyObject *m, const char *name, PyObject *o)
642n/a{
643n/a PyObject *dict;
644n/a if (!PyModule_Check(m)) {
645n/a PyErr_SetString(PyExc_TypeError,
646n/a "PyModule_AddObject() needs module as first arg");
647n/a return -1;
648n/a }
649n/a if (!o) {
650n/a if (!PyErr_Occurred())
651n/a PyErr_SetString(PyExc_TypeError,
652n/a "PyModule_AddObject() needs non-NULL value");
653n/a return -1;
654n/a }
655n/a
656n/a dict = PyModule_GetDict(m);
657n/a if (dict == NULL) {
658n/a /* Internal error -- modules must have a dict! */
659n/a PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
660n/a PyModule_GetName(m));
661n/a return -1;
662n/a }
663n/a if (PyDict_SetItemString(dict, name, o))
664n/a return -1;
665n/a Py_DECREF(o);
666n/a return 0;
667n/a}
668n/a
669n/aint
670n/aPyModule_AddIntConstant(PyObject *m, const char *name, long value)
671n/a{
672n/a PyObject *o = PyLong_FromLong(value);
673n/a if (!o)
674n/a return -1;
675n/a if (PyModule_AddObject(m, name, o) == 0)
676n/a return 0;
677n/a Py_DECREF(o);
678n/a return -1;
679n/a}
680n/a
681n/aint
682n/aPyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
683n/a{
684n/a PyObject *o = PyUnicode_FromString(value);
685n/a if (!o)
686n/a return -1;
687n/a if (PyModule_AddObject(m, name, o) == 0)
688n/a return 0;
689n/a Py_DECREF(o);
690n/a return -1;
691n/a}