2 /* Module support implementation */
7 typedef double va_double;
9 static PyObject *va_build_value(const char *, va_list, int);
11 /* Package context -- the full module name for package imports */
12 char *_Py_PackageContext = NULL;
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
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.
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.";
32 Py_InitModule4(const char *name, PyMethodDef *methods, const char *doc,
33 PyObject *passthrough, int module_api_version)
35 PyObject *m, *d, *v, *n;
37 PyInterpreterState *interp = PyThreadState_Get()->interp;
38 if (interp->modules == NULL)
39 Py_FatalError("Python import machinery not initialized");
40 if (module_api_version != PYTHON_API_VERSION) {
42 PyOS_snprintf(message, sizeof(message),
43 api_version_warning, name,
44 PYTHON_API_VERSION, name,
46 if (PyErr_Warn(PyExc_RuntimeWarning, message))
49 /* Make sure name is fully qualified.
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).
58 if (_Py_PackageContext != NULL) {
59 char *p = strrchr(_Py_PackageContext, '.');
60 if (p != NULL && strcmp(name, p+1) == 0) {
61 name = _Py_PackageContext;
62 _Py_PackageContext = NULL;
65 if ((m = PyImport_AddModule(name)) == NULL)
67 d = PyModule_GetDict(m);
68 if (methods != NULL) {
69 n = PyString_FromString(name);
72 for (ml = methods; ml->ml_name != NULL; ml++) {
73 if ((ml->ml_flags & METH_CLASS) ||
74 (ml->ml_flags & METH_STATIC)) {
75 PyErr_SetString(PyExc_ValueError,
76 "module functions cannot set"
77 " METH_CLASS or METH_STATIC");
81 v = PyCFunction_NewEx(ml, passthrough, n);
86 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
96 v = PyString_FromString(doc);
97 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
107 /* Helper for mkvalue() to scan the length of a format */
110 countformat(const char *format, int endchar)
114 while (level > 0 || *format != endchar) {
118 PyErr_SetString(PyExc_SystemError,
119 "unmatched paren in format");
150 /* Generic function to create a value -- the inverse of getargs() */
151 /* After an original idea and first implementation by Steven Miale */
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);
160 do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags)
167 if ((d = PyDict_New()) == NULL)
169 /* Note that we can't bail immediately on error as this will leak
170 refcounts on any 'N' arguments. */
171 for (i = 0; i < n; i+= 2) {
174 k = do_mkvalue(p_format, p_va, flags);
180 v = do_mkvalue(p_format, p_va, flags);
186 err = PyDict_SetItem(d, k, v);
189 if (err < 0 || itemfailed) {
194 if (d != NULL && **p_format != endchar) {
197 PyErr_SetString(PyExc_SystemError,
198 "Unmatched paren in format");
206 do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
216 /* Note that we can't bail immediately on error as this will leak
217 refcounts on any 'N' arguments. */
218 for (i = 0; i < n; i++) {
219 PyObject *w = do_mkvalue(p_format, p_va, flags);
225 PyList_SET_ITEM(v, i, w);
229 /* do_mkvalue() should have already set an error */
233 if (**p_format != endchar) {
235 PyErr_SetString(PyExc_SystemError,
236 "Unmatched paren in format");
244 #ifdef Py_USING_UNICODE
246 _ustrlen(Py_UNICODE *u)
250 while (*v != 0) { i++; v++; }
256 do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
263 if ((v = PyTuple_New(n)) == NULL)
265 /* Note that we can't bail immediately on error as this will leak
266 refcounts on any 'N' arguments. */
267 for (i = 0; i < n; i++) {
268 PyObject *w = do_mkvalue(p_format, p_va, flags);
274 PyTuple_SET_ITEM(v, i, w);
277 /* do_mkvalue() should have already set an error */
281 if (**p_format != endchar) {
283 PyErr_SetString(PyExc_SystemError,
284 "Unmatched paren in format");
293 do_mkvalue(const char **p_format, va_list *p_va, int flags)
296 switch (*(*p_format)++) {
298 return do_mktuple(p_format, p_va, ')',
299 countformat(*p_format, ')'), flags);
302 return do_mklist(p_format, p_va, ']',
303 countformat(*p_format, ']'), flags);
306 return do_mkdict(p_format, p_va, '}',
307 countformat(*p_format, '}'), flags);
313 return PyInt_FromLong((long)va_arg(*p_va, int));
316 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
321 n = va_arg(*p_va, unsigned int);
322 if (n > (unsigned long)PyInt_GetMax())
323 return PyLong_FromUnsignedLong((unsigned long)n);
325 return PyInt_FromLong(n);
329 #if SIZEOF_SIZE_T!=SIZEOF_LONG
330 return PyInt_FromSsize_t(va_arg(*p_va, Py_ssize_t));
332 /* Fall through from 'n' to 'l' if Py_ssize_t is long */
334 return PyInt_FromLong(va_arg(*p_va, long));
339 n = va_arg(*p_va, unsigned long);
340 if (n > (unsigned long)PyInt_GetMax())
341 return PyLong_FromUnsignedLong(n);
343 return PyInt_FromLong(n);
346 #ifdef HAVE_LONG_LONG
348 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
351 return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
353 #ifdef Py_USING_UNICODE
357 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
359 if (**p_format == '#') {
361 if (flags & FLAG_SIZE_T)
362 n = va_arg(*p_va, Py_ssize_t);
364 n = va_arg(*p_va, int);
375 v = PyUnicode_FromUnicode(u, n);
382 return PyFloat_FromDouble(
383 (double)va_arg(*p_va, va_double));
385 #ifndef WITHOUT_COMPLEX
387 return PyComplex_FromCComplex(
388 *((Py_complex *)va_arg(*p_va, Py_complex *)));
389 #endif /* WITHOUT_COMPLEX */
394 p[0] = (char)va_arg(*p_va, int);
395 return PyString_FromStringAndSize(p, 1);
402 char *str = va_arg(*p_va, char *);
404 if (**p_format == '#') {
406 if (flags & FLAG_SIZE_T)
407 n = va_arg(*p_va, Py_ssize_t);
409 n = va_arg(*p_va, int);
419 size_t m = strlen(str);
420 if (m > PY_SSIZE_T_MAX) {
421 PyErr_SetString(PyExc_OverflowError,
422 "string too long for Python string");
427 v = PyString_FromStringAndSize(str, n);
435 if (**p_format == '&') {
436 typedef PyObject *(*converter)(void *);
437 converter func = va_arg(*p_va, converter);
438 void *arg = va_arg(*p_va, void *);
444 v = va_arg(*p_va, PyObject *);
446 if (*(*p_format - 1) != 'N')
449 else if (!PyErr_Occurred())
450 /* If a NULL was passed
451 * because a call that should
452 * have constructed a value
453 * failed, that's OK, and we
454 * pass the error on; but if
455 * no error occurred it's not
456 * clear that the caller knew
457 * what she was doing. */
458 PyErr_SetString(PyExc_SystemError,
459 "NULL object passed to Py_BuildValue");
470 PyErr_SetString(PyExc_SystemError,
471 "bad format char passed to Py_BuildValue");
480 Py_BuildValue(const char *format, ...)
484 va_start(va, format);
485 retval = va_build_value(format, va, 0);
491 _Py_BuildValue_SizeT(const char *format, ...)
495 va_start(va, format);
496 retval = va_build_value(format, va, FLAG_SIZE_T);
502 Py_VaBuildValue(const char *format, va_list va)
504 return va_build_value(format, va, 0);
508 _Py_VaBuildValue_SizeT(const char *format, va_list va)
510 return va_build_value(format, va, FLAG_SIZE_T);
514 va_build_value(const char *format, va_list va, int flags)
516 const char *f = format;
517 int n = countformat(f, '\0');
520 #ifdef VA_LIST_IS_ARRAY
521 memcpy(lva, va, sizeof(va_list));
537 return do_mkvalue(&f, &lva, flags);
538 return do_mktuple(&f, &lva, '\0', n, flags);
543 PyEval_CallFunction(PyObject *obj, const char *format, ...)
549 va_start(vargs, format);
551 args = Py_VaBuildValue(format, vargs);
557 res = PyEval_CallObject(obj, args);
565 PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
572 meth = PyObject_GetAttrString(obj, methodname);
576 va_start(vargs, format);
578 args = Py_VaBuildValue(format, vargs);
586 res = PyEval_CallObject(meth, args);
594 PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
597 if (!PyModule_Check(m)) {
598 PyErr_SetString(PyExc_TypeError,
599 "PyModule_AddObject() needs module as first arg");
603 if (!PyErr_Occurred())
604 PyErr_SetString(PyExc_TypeError,
605 "PyModule_AddObject() needs non-NULL value");
609 dict = PyModule_GetDict(m);
611 /* Internal error -- modules must have a dict! */
612 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
613 PyModule_GetName(m));
616 if (PyDict_SetItemString(dict, name, o))
623 PyModule_AddIntConstant(PyObject *m, const char *name, long value)
625 PyObject *o = PyInt_FromLong(value);
628 if (PyModule_AddObject(m, name, o) == 0)
635 PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
637 PyObject *o = PyString_FromString(value);
640 if (PyModule_AddObject(m, name, o) == 0)