• 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     for (;;) {
287         switch (*(*p_format)++) {
288         case '(':
289             return do_mktuple(p_format, p_va, ')',
290                               countformat(*p_format, ')'), flags);
291 
292         case '[':
293             return do_mklist(p_format, p_va, ']',
294                              countformat(*p_format, ']'), flags);
295 
296         case '{':
297             return do_mkdict(p_format, p_va, '}',
298                              countformat(*p_format, '}'), flags);
299 
300         case 'b':
301         case 'B':
302         case 'h':
303         case 'i':
304             return PyLong_FromLong((long)va_arg(*p_va, int));
305 
306         case 'H':
307             return PyLong_FromLong((long)va_arg(*p_va, unsigned int));
308 
309         case 'I':
310         {
311             unsigned int n;
312             n = va_arg(*p_va, unsigned int);
313             return PyLong_FromUnsignedLong(n);
314         }
315 
316         case 'n':
317 #if SIZEOF_SIZE_T!=SIZEOF_LONG
318             return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t));
319 #endif
320             /* Fall through from 'n' to 'l' if Py_ssize_t is long */
321         case 'l':
322             return PyLong_FromLong(va_arg(*p_va, long));
323 
324         case 'k':
325         {
326             unsigned long n;
327             n = va_arg(*p_va, unsigned long);
328             return PyLong_FromUnsignedLong(n);
329         }
330 
331         case 'L':
332             return PyLong_FromLongLong((long long)va_arg(*p_va, long long));
333 
334         case 'K':
335             return PyLong_FromUnsignedLongLong((long long)va_arg(*p_va, unsigned long long));
336 
337         case 'u':
338         {
339             PyObject *v;
340             Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
341             Py_ssize_t n;
342             if (**p_format == '#') {
343                 ++*p_format;
344                 if (flags & FLAG_SIZE_T)
345                     n = va_arg(*p_va, Py_ssize_t);
346                 else {
347                     n = va_arg(*p_va, int);
348                     if (PyErr_WarnEx(PyExc_DeprecationWarning,
349                                 "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
350                         return NULL;
351                     }
352                 }
353             }
354             else
355                 n = -1;
356             if (u == NULL) {
357                 v = Py_None;
358                 Py_INCREF(v);
359             }
360             else {
361                 if (n < 0)
362                     n = wcslen(u);
363                 v = PyUnicode_FromWideChar(u, n);
364             }
365             return v;
366         }
367         case 'f':
368         case 'd':
369             return PyFloat_FromDouble(
370                 (double)va_arg(*p_va, va_double));
371 
372         case 'D':
373             return PyComplex_FromCComplex(
374                 *((Py_complex *)va_arg(*p_va, Py_complex *)));
375 
376         case 'c':
377         {
378             char p[1];
379             p[0] = (char)va_arg(*p_va, int);
380             return PyBytes_FromStringAndSize(p, 1);
381         }
382         case 'C':
383         {
384             int i = va_arg(*p_va, int);
385             return PyUnicode_FromOrdinal(i);
386         }
387 
388         case 's':
389         case 'z':
390         case 'U':   /* XXX deprecated alias */
391         {
392             PyObject *v;
393             const char *str = va_arg(*p_va, const char *);
394             Py_ssize_t n;
395             if (**p_format == '#') {
396                 ++*p_format;
397                 if (flags & FLAG_SIZE_T)
398                     n = va_arg(*p_va, Py_ssize_t);
399                 else {
400                     n = va_arg(*p_va, int);
401                     if (PyErr_WarnEx(PyExc_DeprecationWarning,
402                                 "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
403                         return NULL;
404                     }
405                 }
406             }
407             else
408                 n = -1;
409             if (str == NULL) {
410                 v = Py_None;
411                 Py_INCREF(v);
412             }
413             else {
414                 if (n < 0) {
415                     size_t m = strlen(str);
416                     if (m > PY_SSIZE_T_MAX) {
417                         PyErr_SetString(PyExc_OverflowError,
418                             "string too long for Python string");
419                         return NULL;
420                     }
421                     n = (Py_ssize_t)m;
422                 }
423                 v = PyUnicode_FromStringAndSize(str, n);
424             }
425             return v;
426         }
427 
428         case 'y':
429         {
430             PyObject *v;
431             const char *str = va_arg(*p_va, const char *);
432             Py_ssize_t n;
433             if (**p_format == '#') {
434                 ++*p_format;
435                 if (flags & FLAG_SIZE_T)
436                     n = va_arg(*p_va, Py_ssize_t);
437                 else {
438                     n = va_arg(*p_va, int);
439                     if (PyErr_WarnEx(PyExc_DeprecationWarning,
440                                 "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
441                         return NULL;
442                     }
443                 }
444             }
445             else
446                 n = -1;
447             if (str == NULL) {
448                 v = Py_None;
449                 Py_INCREF(v);
450             }
451             else {
452                 if (n < 0) {
453                     size_t m = strlen(str);
454                     if (m > PY_SSIZE_T_MAX) {
455                         PyErr_SetString(PyExc_OverflowError,
456                             "string too long for Python bytes");
457                         return NULL;
458                     }
459                     n = (Py_ssize_t)m;
460                 }
461                 v = PyBytes_FromStringAndSize(str, n);
462             }
463             return v;
464         }
465 
466         case 'N':
467         case 'S':
468         case 'O':
469         if (**p_format == '&') {
470             typedef PyObject *(*converter)(void *);
471             converter func = va_arg(*p_va, converter);
472             void *arg = va_arg(*p_va, void *);
473             ++*p_format;
474             return (*func)(arg);
475         }
476         else {
477             PyObject *v;
478             v = va_arg(*p_va, PyObject *);
479             if (v != NULL) {
480                 if (*(*p_format - 1) != 'N')
481                     Py_INCREF(v);
482             }
483             else if (!PyErr_Occurred())
484                 /* If a NULL was passed
485                  * because a call that should
486                  * have constructed a value
487                  * failed, that's OK, and we
488                  * pass the error on; but if
489                  * no error occurred it's not
490                  * clear that the caller knew
491                  * what she was doing. */
492                 PyErr_SetString(PyExc_SystemError,
493                     "NULL object passed to Py_BuildValue");
494             return v;
495         }
496 
497         case ':':
498         case ',':
499         case ' ':
500         case '\t':
501             break;
502 
503         default:
504             PyErr_SetString(PyExc_SystemError,
505                 "bad format char passed to Py_BuildValue");
506             return NULL;
507 
508         }
509     }
510 }
511 
512 
513 PyObject *
Py_BuildValue(const char * format,...)514 Py_BuildValue(const char *format, ...)
515 {
516     va_list va;
517     PyObject* retval;
518     va_start(va, format);
519     retval = va_build_value(format, va, 0);
520     va_end(va);
521     return retval;
522 }
523 
524 PyObject *
_Py_BuildValue_SizeT(const char * format,...)525 _Py_BuildValue_SizeT(const char *format, ...)
526 {
527     va_list va;
528     PyObject* retval;
529     va_start(va, format);
530     retval = va_build_value(format, va, FLAG_SIZE_T);
531     va_end(va);
532     return retval;
533 }
534 
535 PyObject *
Py_VaBuildValue(const char * format,va_list va)536 Py_VaBuildValue(const char *format, va_list va)
537 {
538     return va_build_value(format, va, 0);
539 }
540 
541 PyObject *
_Py_VaBuildValue_SizeT(const char * format,va_list va)542 _Py_VaBuildValue_SizeT(const char *format, va_list va)
543 {
544     return va_build_value(format, va, FLAG_SIZE_T);
545 }
546 
547 static PyObject *
va_build_value(const char * format,va_list va,int flags)548 va_build_value(const char *format, va_list va, int flags)
549 {
550     const char *f = format;
551     Py_ssize_t n = countformat(f, '\0');
552     va_list lva;
553     PyObject *retval;
554 
555     if (n < 0)
556         return NULL;
557     if (n == 0) {
558         Py_RETURN_NONE;
559     }
560     va_copy(lva, va);
561     if (n == 1) {
562         retval = do_mkvalue(&f, &lva, flags);
563     } else {
564         retval = do_mktuple(&f, &lva, '\0', n, flags);
565     }
566     va_end(lva);
567     return retval;
568 }
569 
570 PyObject **
_Py_VaBuildStack(PyObject ** small_stack,Py_ssize_t small_stack_len,const char * format,va_list va,Py_ssize_t * p_nargs)571 _Py_VaBuildStack(PyObject **small_stack, Py_ssize_t small_stack_len,
572                 const char *format, va_list va, Py_ssize_t *p_nargs)
573 {
574     return va_build_stack(small_stack, small_stack_len, format, va, 0, p_nargs);
575 }
576 
577 PyObject **
_Py_VaBuildStack_SizeT(PyObject ** small_stack,Py_ssize_t small_stack_len,const char * format,va_list va,Py_ssize_t * p_nargs)578 _Py_VaBuildStack_SizeT(PyObject **small_stack, Py_ssize_t small_stack_len,
579                        const char *format, va_list va, Py_ssize_t *p_nargs)
580 {
581     return va_build_stack(small_stack, small_stack_len, format, va, FLAG_SIZE_T, p_nargs);
582 }
583 
584 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)585 va_build_stack(PyObject **small_stack, Py_ssize_t small_stack_len,
586                const char *format, va_list va, int flags, Py_ssize_t *p_nargs)
587 {
588     const char *f;
589     Py_ssize_t n;
590     va_list lva;
591     PyObject **stack;
592     int res;
593 
594     n = countformat(format, '\0');
595     if (n < 0) {
596         *p_nargs = 0;
597         return NULL;
598     }
599 
600     if (n == 0) {
601         *p_nargs = 0;
602         return small_stack;
603     }
604 
605     if (n <= small_stack_len) {
606         stack = small_stack;
607     }
608     else {
609         stack = PyMem_Malloc(n * sizeof(stack[0]));
610         if (stack == NULL) {
611             PyErr_NoMemory();
612             return NULL;
613         }
614     }
615 
616     va_copy(lva, va);
617     f = format;
618     res = do_mkstack(stack, &f, &lva, '\0', n, flags);
619     va_end(lva);
620 
621     if (res < 0) {
622         if (stack != small_stack) {
623             PyMem_Free(stack);
624         }
625         return NULL;
626     }
627 
628     *p_nargs = n;
629     return stack;
630 }
631 
632 
633 int
PyModule_AddObject(PyObject * m,const char * name,PyObject * o)634 PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
635 {
636     PyObject *dict;
637     if (!PyModule_Check(m)) {
638         PyErr_SetString(PyExc_TypeError,
639                     "PyModule_AddObject() needs module as first arg");
640         return -1;
641     }
642     if (!o) {
643         if (!PyErr_Occurred())
644             PyErr_SetString(PyExc_TypeError,
645                             "PyModule_AddObject() needs non-NULL value");
646         return -1;
647     }
648 
649     dict = PyModule_GetDict(m);
650     if (dict == NULL) {
651         /* Internal error -- modules must have a dict! */
652         PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
653                      PyModule_GetName(m));
654         return -1;
655     }
656     if (PyDict_SetItemString(dict, name, o))
657         return -1;
658     Py_DECREF(o);
659     return 0;
660 }
661 
662 int
PyModule_AddIntConstant(PyObject * m,const char * name,long value)663 PyModule_AddIntConstant(PyObject *m, const char *name, long value)
664 {
665     PyObject *o = PyLong_FromLong(value);
666     if (!o)
667         return -1;
668     if (PyModule_AddObject(m, name, o) == 0)
669         return 0;
670     Py_DECREF(o);
671     return -1;
672 }
673 
674 int
PyModule_AddStringConstant(PyObject * m,const char * name,const char * value)675 PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
676 {
677     PyObject *o = PyUnicode_FromString(value);
678     if (!o)
679         return -1;
680     if (PyModule_AddObject(m, name, o) == 0)
681         return 0;
682     Py_DECREF(o);
683     return -1;
684 }
685 
686 int
PyModule_AddType(PyObject * module,PyTypeObject * type)687 PyModule_AddType(PyObject *module, PyTypeObject *type)
688 {
689     if (PyType_Ready(type) < 0) {
690         return -1;
691     }
692 
693     const char *name = _PyType_Name(type);
694     assert(name != NULL);
695 
696     Py_INCREF(type);
697     if (PyModule_AddObject(module, name, (PyObject *)type) < 0) {
698         Py_DECREF(type);
699         return -1;
700     }
701 
702     return 0;
703 }
704