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