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