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