2 /* Module support implementation */
6 typedef double va_double
;
8 /* Package context -- the full module name for package imports */
9 char *_Py_PackageContext
= NULL
;
11 /* Py_InitModule4() parameters:
12 - name is the module name
13 - methods is the list of top-level functions
14 - doc is the documentation string
15 - passthrough is passed as self to functions defined in the module
16 - api_version is the value of PYTHON_API_VERSION at the time the
19 Return value is a borrowed reference to the module object; or NULL
20 if an error occurred (in Python 1.4 and before, errors were fatal).
21 Errors may still leak memory.
24 static char api_version_warning
[] =
25 "Python C API version mismatch for module %.100s:\
26 This Python has API version %d, module %.100s has version %d.";
29 Py_InitModule4(const char *name
, PyMethodDef
*methods
, const char *doc
,
30 PyObject
*passthrough
, int module_api_version
)
32 PyObject
*m
, *d
, *v
, *n
;
34 if (!Py_IsInitialized())
35 Py_FatalError("Interpreter not initialized (version mismatch?)");
36 if (module_api_version
!= PYTHON_API_VERSION
) {
38 PyOS_snprintf(message
, sizeof(message
),
39 api_version_warning
, name
,
40 PYTHON_API_VERSION
, name
,
42 if (PyErr_Warn(PyExc_RuntimeWarning
, message
))
45 /* Make sure name is fully qualified.
47 This is a bit of a hack: when the shared library is loaded,
48 the module name is "package.module", but the module calls
49 Py_InitModule*() with just "module" for the name. The shared
50 library loader squirrels away the true name of the module in
51 _Py_PackageContext, and Py_InitModule*() will substitute this
52 (if the name actually matches).
54 if (_Py_PackageContext
!= NULL
) {
55 char *p
= strrchr(_Py_PackageContext
, '.');
56 if (p
!= NULL
&& strcmp(name
, p
+1) == 0) {
57 name
= _Py_PackageContext
;
58 _Py_PackageContext
= NULL
;
61 if ((m
= PyImport_AddModule(name
)) == NULL
)
63 d
= PyModule_GetDict(m
);
64 if (methods
!= NULL
) {
65 n
= PyString_FromString(name
);
68 for (ml
= methods
; ml
->ml_name
!= NULL
; ml
++) {
69 if ((ml
->ml_flags
& METH_CLASS
) ||
70 (ml
->ml_flags
& METH_STATIC
)) {
71 PyErr_SetString(PyExc_ValueError
,
72 "module functions cannot set"
73 " METH_CLASS or METH_STATIC");
77 v
= PyCFunction_NewEx(ml
, passthrough
, n
);
82 if (PyDict_SetItemString(d
, ml
->ml_name
, v
) != 0) {
92 v
= PyString_FromString(doc
);
93 if (v
== NULL
|| PyDict_SetItemString(d
, "__doc__", v
) != 0) {
103 /* Helper for mkvalue() to scan the length of a format */
106 countformat(const char *format
, int endchar
)
110 while (level
> 0 || *format
!= endchar
) {
114 PyErr_SetString(PyExc_SystemError
,
115 "unmatched paren in format");
146 /* Generic function to create a value -- the inverse of getargs() */
147 /* After an original idea and first implementation by Steven Miale */
149 static PyObject
*do_mktuple(const char**, va_list *, int, int);
150 static PyObject
*do_mklist(const char**, va_list *, int, int);
151 static PyObject
*do_mkdict(const char**, va_list *, int, int);
152 static PyObject
*do_mkvalue(const char**, va_list *);
156 do_mkdict(const char **p_format
, va_list *p_va
, int endchar
, int n
)
163 if ((d
= PyDict_New()) == NULL
)
165 /* Note that we can't bail immediately on error as this will leak
166 refcounts on any 'N' arguments. */
167 for (i
= 0; i
< n
; i
+= 2) {
170 k
= do_mkvalue(p_format
, p_va
);
176 v
= do_mkvalue(p_format
, p_va
);
182 err
= PyDict_SetItem(d
, k
, v
);
185 if (err
< 0 || itemfailed
) {
190 if (d
!= NULL
&& **p_format
!= endchar
) {
193 PyErr_SetString(PyExc_SystemError
,
194 "Unmatched paren in format");
202 do_mklist(const char **p_format
, va_list *p_va
, int endchar
, int n
)
212 /* Note that we can't bail immediately on error as this will leak
213 refcounts on any 'N' arguments. */
214 for (i
= 0; i
< n
; i
++) {
215 PyObject
*w
= do_mkvalue(p_format
, p_va
);
221 PyList_SetItem(v
, i
, w
);
225 /* do_mkvalue() should have already set an error */
229 if (**p_format
!= endchar
) {
231 PyErr_SetString(PyExc_SystemError
,
232 "Unmatched paren in format");
241 #ifdef Py_USING_UNICODE
243 _ustrlen(Py_UNICODE
*u
)
247 while (*v
!= 0) { i
++; v
++; }
253 do_mktuple(const char **p_format
, va_list *p_va
, int endchar
, int n
)
260 if ((v
= PyTuple_New(n
)) == NULL
)
262 /* Note that we can't bail immediately on error as this will leak
263 refcounts on any 'N' arguments. */
264 for (i
= 0; i
< n
; i
++) {
265 PyObject
*w
= do_mkvalue(p_format
, p_va
);
271 PyTuple_SetItem(v
, i
, w
);
273 if (v
!= NULL
&& **p_format
!= endchar
) {
276 PyErr_SetString(PyExc_SystemError
,
277 "Unmatched paren in format");
289 do_mkvalue(const char **p_format
, va_list *p_va
)
292 switch (*(*p_format
)++) {
294 return do_mktuple(p_format
, p_va
, ')',
295 countformat(*p_format
, ')'));
298 return do_mklist(p_format
, p_va
, ']',
299 countformat(*p_format
, ']'));
302 return do_mkdict(p_format
, p_va
, '}',
303 countformat(*p_format
, '}'));
309 return PyInt_FromLong((long)va_arg(*p_va
, int));
312 return PyInt_FromLong((long)va_arg(*p_va
, unsigned int));
317 n
= va_arg(*p_va
, unsigned int);
318 if (n
> (unsigned long)PyInt_GetMax())
319 return PyLong_FromUnsignedLong((unsigned long)n
);
321 return PyInt_FromLong(n
);
325 #if SIZEOF_SIZE_T!=SIZEOF_LONG
326 return PyInt_FromSsize_t(va_arg(*p_va
, Py_ssize_t
));
328 /* Fall through from 'n' to 'l' if Py_ssize_t is long */
330 return PyInt_FromLong(va_arg(*p_va
, long));
335 n
= va_arg(*p_va
, unsigned long);
336 if (n
> (unsigned long)PyInt_GetMax())
337 return PyLong_FromUnsignedLong(n
);
339 return PyInt_FromLong(n
);
342 #ifdef HAVE_LONG_LONG
344 return PyLong_FromLongLong((PY_LONG_LONG
)va_arg(*p_va
, PY_LONG_LONG
));
347 return PyLong_FromUnsignedLongLong((PY_LONG_LONG
)va_arg(*p_va
, unsigned PY_LONG_LONG
));
349 #ifdef Py_USING_UNICODE
353 Py_UNICODE
*u
= va_arg(*p_va
, Py_UNICODE
*);
355 if (**p_format
== '#') {
357 n
= va_arg(*p_va
, int);
368 v
= PyUnicode_FromUnicode(u
, n
);
375 return PyFloat_FromDouble(
376 (double)va_arg(*p_va
, va_double
));
378 #ifndef WITHOUT_COMPLEX
380 return PyComplex_FromCComplex(
381 *((Py_complex
*)va_arg(*p_va
, Py_complex
*)));
382 #endif /* WITHOUT_COMPLEX */
387 p
[0] = (char)va_arg(*p_va
, int);
388 return PyString_FromStringAndSize(p
, 1);
395 char *str
= va_arg(*p_va
, char *);
397 if (**p_format
== '#') {
399 n
= va_arg(*p_va
, int);
409 size_t m
= strlen(str
);
411 PyErr_SetString(PyExc_OverflowError
,
412 "string too long for Python string");
417 v
= PyString_FromStringAndSize(str
, n
);
425 if (**p_format
== '&') {
426 typedef PyObject
*(*converter
)(void *);
427 converter func
= va_arg(*p_va
, converter
);
428 void *arg
= va_arg(*p_va
, void *);
434 v
= va_arg(*p_va
, PyObject
*);
436 if (*(*p_format
- 1) != 'N')
439 else if (!PyErr_Occurred())
440 /* If a NULL was passed
441 * because a call that should
442 * have constructed a value
443 * failed, that's OK, and we
444 * pass the error on; but if
445 * no error occurred it's not
446 * clear that the caller knew
447 * what she was doing. */
448 PyErr_SetString(PyExc_SystemError
,
449 "NULL object passed to Py_BuildValue");
460 PyErr_SetString(PyExc_SystemError
,
461 "bad format char passed to Py_BuildValue");
470 Py_BuildValue(const char *format
, ...)
474 va_start(va
, format
);
475 retval
= Py_VaBuildValue(format
, va
);
481 Py_VaBuildValue(const char *format
, va_list va
)
483 const char *f
= format
;
484 int n
= countformat(f
, '\0');
487 #ifdef VA_LIST_IS_ARRAY
488 memcpy(lva
, va
, sizeof(va_list));
504 return do_mkvalue(&f
, &lva
);
505 return do_mktuple(&f
, &lva
, '\0', n
);
510 PyEval_CallFunction(PyObject
*obj
, const char *format
, ...)
516 va_start(vargs
, format
);
518 args
= Py_VaBuildValue(format
, vargs
);
524 res
= PyEval_CallObject(obj
, args
);
532 PyEval_CallMethod(PyObject
*obj
, const char *methodname
, const char *format
, ...)
539 meth
= PyObject_GetAttrString(obj
, methodname
);
543 va_start(vargs
, format
);
545 args
= Py_VaBuildValue(format
, vargs
);
553 res
= PyEval_CallObject(meth
, args
);
561 PyModule_AddObject(PyObject
*m
, const char *name
, PyObject
*o
)
564 if (!PyModule_Check(m
)) {
565 PyErr_SetString(PyExc_TypeError
,
566 "PyModule_AddObject() needs module as first arg");
570 if (!PyErr_Occurred())
571 PyErr_SetString(PyExc_TypeError
,
572 "PyModule_AddObject() needs non-NULL value");
576 dict
= PyModule_GetDict(m
);
578 /* Internal error -- modules must have a dict! */
579 PyErr_Format(PyExc_SystemError
, "module '%s' has no __dict__",
580 PyModule_GetName(m
));
583 if (PyDict_SetItemString(dict
, name
, o
))
590 PyModule_AddIntConstant(PyObject
*m
, const char *name
, long value
)
592 return PyModule_AddObject(m
, name
, PyInt_FromLong(value
));
596 PyModule_AddStringConstant(PyObject
*m
, const char *name
, const char *value
)
598 return PyModule_AddObject(m
, name
, PyString_FromString(value
));