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