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