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 if (!Py_IsInitialized())
38 Py_FatalError("Interpreter not initialized (version mismatch?)");
39 if (module_api_version
!= PYTHON_API_VERSION
) {
41 PyOS_snprintf(message
, sizeof(message
),
42 api_version_warning
, name
,
43 PYTHON_API_VERSION
, name
,
45 if (PyErr_Warn(PyExc_RuntimeWarning
, message
))
48 /* Make sure name is fully qualified.
50 This is a bit of a hack: when the shared library is loaded,
51 the module name is "package.module", but the module calls
52 Py_InitModule*() with just "module" for the name. The shared
53 library loader squirrels away the true name of the module in
54 _Py_PackageContext, and Py_InitModule*() will substitute this
55 (if the name actually matches).
57 if (_Py_PackageContext
!= NULL
) {
58 char *p
= strrchr(_Py_PackageContext
, '.');
59 if (p
!= NULL
&& strcmp(name
, p
+1) == 0) {
60 name
= _Py_PackageContext
;
61 _Py_PackageContext
= NULL
;
64 if ((m
= PyImport_AddModule(name
)) == NULL
)
66 d
= PyModule_GetDict(m
);
67 if (methods
!= NULL
) {
68 n
= PyString_FromString(name
);
71 for (ml
= methods
; ml
->ml_name
!= NULL
; ml
++) {
72 if ((ml
->ml_flags
& METH_CLASS
) ||
73 (ml
->ml_flags
& METH_STATIC
)) {
74 PyErr_SetString(PyExc_ValueError
,
75 "module functions cannot set"
76 " METH_CLASS or METH_STATIC");
80 v
= PyCFunction_NewEx(ml
, passthrough
, n
);
85 if (PyDict_SetItemString(d
, ml
->ml_name
, v
) != 0) {
95 v
= PyString_FromString(doc
);
96 if (v
== NULL
|| PyDict_SetItemString(d
, "__doc__", v
) != 0) {
106 /* Helper for mkvalue() to scan the length of a format */
109 countformat(const char *format
, int endchar
)
113 while (level
> 0 || *format
!= endchar
) {
117 PyErr_SetString(PyExc_SystemError
,
118 "unmatched paren in format");
149 /* Generic function to create a value -- the inverse of getargs() */
150 /* After an original idea and first implementation by Steven Miale */
152 static PyObject
*do_mktuple(const char**, va_list *, int, int, int);
153 static PyObject
*do_mklist(const char**, va_list *, int, int, int);
154 static PyObject
*do_mkdict(const char**, va_list *, int, int, int);
155 static PyObject
*do_mkvalue(const char**, va_list *, int);
159 do_mkdict(const char **p_format
, va_list *p_va
, int endchar
, int n
, int flags
)
166 if ((d
= PyDict_New()) == NULL
)
168 /* Note that we can't bail immediately on error as this will leak
169 refcounts on any 'N' arguments. */
170 for (i
= 0; i
< n
; i
+= 2) {
173 k
= do_mkvalue(p_format
, p_va
, flags
);
179 v
= do_mkvalue(p_format
, p_va
, flags
);
185 err
= PyDict_SetItem(d
, k
, v
);
188 if (err
< 0 || itemfailed
) {
193 if (d
!= NULL
&& **p_format
!= endchar
) {
196 PyErr_SetString(PyExc_SystemError
,
197 "Unmatched paren in format");
205 do_mklist(const char **p_format
, va_list *p_va
, int endchar
, int n
, int flags
)
215 /* Note that we can't bail immediately on error as this will leak
216 refcounts on any 'N' arguments. */
217 for (i
= 0; i
< n
; i
++) {
218 PyObject
*w
= do_mkvalue(p_format
, p_va
, flags
);
224 PyList_SET_ITEM(v
, i
, w
);
228 /* do_mkvalue() should have already set an error */
232 if (**p_format
!= endchar
) {
234 PyErr_SetString(PyExc_SystemError
,
235 "Unmatched paren in format");
243 #ifdef Py_USING_UNICODE
245 _ustrlen(Py_UNICODE
*u
)
249 while (*v
!= 0) { i
++; v
++; }
255 do_mktuple(const char **p_format
, va_list *p_va
, int endchar
, int n
, int flags
)
262 if ((v
= PyTuple_New(n
)) == NULL
)
264 /* Note that we can't bail immediately on error as this will leak
265 refcounts on any 'N' arguments. */
266 for (i
= 0; i
< n
; i
++) {
267 PyObject
*w
= do_mkvalue(p_format
, p_va
, flags
);
273 PyTuple_SET_ITEM(v
, i
, w
);
276 /* do_mkvalue() should have already set an error */
280 if (**p_format
!= endchar
) {
282 PyErr_SetString(PyExc_SystemError
,
283 "Unmatched paren in format");
292 do_mkvalue(const char **p_format
, va_list *p_va
, int flags
)
295 switch (*(*p_format
)++) {
297 return do_mktuple(p_format
, p_va
, ')',
298 countformat(*p_format
, ')'), flags
);
301 return do_mklist(p_format
, p_va
, ']',
302 countformat(*p_format
, ']'), flags
);
305 return do_mkdict(p_format
, p_va
, '}',
306 countformat(*p_format
, '}'), flags
);
312 return PyInt_FromLong((long)va_arg(*p_va
, int));
315 return PyInt_FromLong((long)va_arg(*p_va
, unsigned int));
320 n
= va_arg(*p_va
, unsigned int);
321 if (n
> (unsigned long)PyInt_GetMax())
322 return PyLong_FromUnsignedLong((unsigned long)n
);
324 return PyInt_FromLong(n
);
328 #if SIZEOF_SIZE_T!=SIZEOF_LONG
329 return PyInt_FromSsize_t(va_arg(*p_va
, Py_ssize_t
));
331 /* Fall through from 'n' to 'l' if Py_ssize_t is long */
333 return PyInt_FromLong(va_arg(*p_va
, long));
338 n
= va_arg(*p_va
, unsigned long);
339 if (n
> (unsigned long)PyInt_GetMax())
340 return PyLong_FromUnsignedLong(n
);
342 return PyInt_FromLong(n
);
345 #ifdef HAVE_LONG_LONG
347 return PyLong_FromLongLong((PY_LONG_LONG
)va_arg(*p_va
, PY_LONG_LONG
));
350 return PyLong_FromUnsignedLongLong((PY_LONG_LONG
)va_arg(*p_va
, unsigned PY_LONG_LONG
));
352 #ifdef Py_USING_UNICODE
356 Py_UNICODE
*u
= va_arg(*p_va
, Py_UNICODE
*);
358 if (**p_format
== '#') {
360 if (flags
& FLAG_SIZE_T
)
361 n
= va_arg(*p_va
, Py_ssize_t
);
363 n
= va_arg(*p_va
, int);
374 v
= PyUnicode_FromUnicode(u
, n
);
381 return PyFloat_FromDouble(
382 (double)va_arg(*p_va
, va_double
));
384 #ifndef WITHOUT_COMPLEX
386 return PyComplex_FromCComplex(
387 *((Py_complex
*)va_arg(*p_va
, Py_complex
*)));
388 #endif /* WITHOUT_COMPLEX */
393 p
[0] = (char)va_arg(*p_va
, int);
394 return PyString_FromStringAndSize(p
, 1);
401 char *str
= va_arg(*p_va
, char *);
403 if (**p_format
== '#') {
405 if (flags
& FLAG_SIZE_T
)
406 n
= va_arg(*p_va
, Py_ssize_t
);
408 n
= va_arg(*p_va
, int);
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");
426 v
= PyString_FromStringAndSize(str
, n
);
434 if (**p_format
== '&') {
435 typedef PyObject
*(*converter
)(void *);
436 converter func
= va_arg(*p_va
, converter
);
437 void *arg
= va_arg(*p_va
, void *);
443 v
= va_arg(*p_va
, PyObject
*);
445 if (*(*p_format
- 1) != 'N')
448 else if (!PyErr_Occurred())
449 /* If a NULL was passed
450 * because a call that should
451 * have constructed a value
452 * failed, that's OK, and we
453 * pass the error on; but if
454 * no error occurred it's not
455 * clear that the caller knew
456 * what she was doing. */
457 PyErr_SetString(PyExc_SystemError
,
458 "NULL object passed to Py_BuildValue");
469 PyErr_SetString(PyExc_SystemError
,
470 "bad format char passed to Py_BuildValue");
479 Py_BuildValue(const char *format
, ...)
483 va_start(va
, format
);
484 retval
= va_build_value(format
, va
, 0);
490 _Py_BuildValue_SizeT(const char *format
, ...)
494 va_start(va
, format
);
495 retval
= va_build_value(format
, va
, FLAG_SIZE_T
);
501 Py_VaBuildValue(const char *format
, va_list va
)
503 return va_build_value(format
, va
, 0);
507 _Py_VaBuildValue_SizeT(const char *format
, va_list va
)
509 return va_build_value(format
, va
, FLAG_SIZE_T
);
513 va_build_value(const char *format
, va_list va
, int flags
)
515 const char *f
= format
;
516 int n
= countformat(f
, '\0');
519 #ifdef VA_LIST_IS_ARRAY
520 memcpy(lva
, va
, sizeof(va_list));
536 return do_mkvalue(&f
, &lva
, flags
);
537 return do_mktuple(&f
, &lva
, '\0', n
, flags
);
542 PyEval_CallFunction(PyObject
*obj
, const char *format
, ...)
548 va_start(vargs
, format
);
550 args
= Py_VaBuildValue(format
, vargs
);
556 res
= PyEval_CallObject(obj
, args
);
564 PyEval_CallMethod(PyObject
*obj
, const char *methodname
, const char *format
, ...)
571 meth
= PyObject_GetAttrString(obj
, methodname
);
575 va_start(vargs
, format
);
577 args
= Py_VaBuildValue(format
, vargs
);
585 res
= PyEval_CallObject(meth
, args
);
593 PyModule_AddObject(PyObject
*m
, const char *name
, PyObject
*o
)
596 if (!PyModule_Check(m
)) {
597 PyErr_SetString(PyExc_TypeError
,
598 "PyModule_AddObject() needs module as first arg");
602 if (!PyErr_Occurred())
603 PyErr_SetString(PyExc_TypeError
,
604 "PyModule_AddObject() needs non-NULL value");
608 dict
= PyModule_GetDict(m
);
610 /* Internal error -- modules must have a dict! */
611 PyErr_Format(PyExc_SystemError
, "module '%s' has no __dict__",
612 PyModule_GetName(m
));
615 if (PyDict_SetItemString(dict
, name
, o
))
622 PyModule_AddIntConstant(PyObject
*m
, const char *name
, long value
)
624 PyObject
*o
= PyInt_FromLong(value
);
627 if (PyModule_AddObject(m
, name
, o
) == 0)
634 PyModule_AddStringConstant(PyObject
*m
, const char *name
, const char *value
)
636 PyObject
*o
= PyString_FromString(value
);
639 if (PyModule_AddObject(m
, name
, o
) == 0)