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");
76 v
= PyCFunction_NewEx(ml
, passthrough
, n
);
79 if (PyDict_SetItemString(d
, ml
->ml_name
, v
) != 0) {
88 v
= PyString_FromString(doc
);
89 if (v
== NULL
|| PyDict_SetItemString(d
, "__doc__", v
) != 0) {
99 /* Helper for mkvalue() to scan the length of a format */
102 countformat(const char *format
, int endchar
)
106 while (level
> 0 || *format
!= endchar
) {
110 PyErr_SetString(PyExc_SystemError
,
111 "unmatched paren in format");
142 /* Generic function to create a value -- the inverse of getargs() */
143 /* After an original idea and first implementation by Steven Miale */
145 static PyObject
*do_mktuple(const char**, va_list *, int, int);
146 static PyObject
*do_mklist(const char**, va_list *, int, int);
147 static PyObject
*do_mkdict(const char**, va_list *, int, int);
148 static PyObject
*do_mkvalue(const char**, va_list *);
152 do_mkdict(const char **p_format
, va_list *p_va
, int endchar
, int n
)
159 if ((d
= PyDict_New()) == NULL
)
161 /* Note that we can't bail immediately on error as this will leak
162 refcounts on any 'N' arguments. */
163 for (i
= 0; i
< n
; i
+= 2) {
166 k
= do_mkvalue(p_format
, p_va
);
172 v
= do_mkvalue(p_format
, p_va
);
178 err
= PyDict_SetItem(d
, k
, v
);
181 if (err
< 0 || itemfailed
) {
186 if (d
!= NULL
&& **p_format
!= endchar
) {
189 PyErr_SetString(PyExc_SystemError
,
190 "Unmatched paren in format");
198 do_mklist(const char **p_format
, va_list *p_va
, int endchar
, int n
)
205 if ((v
= PyList_New(n
)) == NULL
)
207 /* Note that we can't bail immediately on error as this will leak
208 refcounts on any 'N' arguments. */
209 for (i
= 0; i
< n
; i
++) {
210 PyObject
*w
= do_mkvalue(p_format
, p_va
);
216 PyList_SetItem(v
, i
, w
);
218 if (v
!= NULL
&& **p_format
!= endchar
) {
221 PyErr_SetString(PyExc_SystemError
,
222 "Unmatched paren in format");
233 #ifdef Py_USING_UNICODE
235 _ustrlen(Py_UNICODE
*u
)
239 while (*v
!= 0) { i
++; v
++; }
245 do_mktuple(const char **p_format
, va_list *p_va
, int endchar
, int n
)
252 if ((v
= PyTuple_New(n
)) == NULL
)
254 /* Note that we can't bail immediately on error as this will leak
255 refcounts on any 'N' arguments. */
256 for (i
= 0; i
< n
; i
++) {
257 PyObject
*w
= do_mkvalue(p_format
, p_va
);
263 PyTuple_SetItem(v
, i
, w
);
265 if (v
!= NULL
&& **p_format
!= endchar
) {
268 PyErr_SetString(PyExc_SystemError
,
269 "Unmatched paren in format");
281 do_mkvalue(const char **p_format
, va_list *p_va
)
284 switch (*(*p_format
)++) {
286 return do_mktuple(p_format
, p_va
, ')',
287 countformat(*p_format
, ')'));
290 return do_mklist(p_format
, p_va
, ']',
291 countformat(*p_format
, ']'));
294 return do_mkdict(p_format
, p_va
, '}',
295 countformat(*p_format
, '}'));
301 return PyInt_FromLong((long)va_arg(*p_va
, int));
304 return PyInt_FromLong((long)va_arg(*p_va
, unsigned int));
309 n
= va_arg(*p_va
, unsigned int);
310 if (n
> (unsigned long)PyInt_GetMax())
311 return PyLong_FromUnsignedLong((unsigned long)n
);
313 return PyInt_FromLong(n
);
317 return PyInt_FromLong(va_arg(*p_va
, long));
322 n
= va_arg(*p_va
, unsigned long);
323 if (n
> (unsigned long)PyInt_GetMax())
324 return PyLong_FromUnsignedLong(n
);
326 return PyInt_FromLong(n
);
329 #ifdef HAVE_LONG_LONG
331 return PyLong_FromLongLong((PY_LONG_LONG
)va_arg(*p_va
, PY_LONG_LONG
));
334 return PyLong_FromUnsignedLongLong((PY_LONG_LONG
)va_arg(*p_va
, unsigned PY_LONG_LONG
));
336 #ifdef Py_USING_UNICODE
340 Py_UNICODE
*u
= va_arg(*p_va
, Py_UNICODE
*);
342 if (**p_format
== '#') {
344 n
= va_arg(*p_va
, int);
355 v
= PyUnicode_FromUnicode(u
, n
);
362 return PyFloat_FromDouble(
363 (double)va_arg(*p_va
, va_double
));
365 #ifndef WITHOUT_COMPLEX
367 return PyComplex_FromCComplex(
368 *((Py_complex
*)va_arg(*p_va
, Py_complex
*)));
369 #endif /* WITHOUT_COMPLEX */
374 p
[0] = va_arg(*p_va
, int);
375 return PyString_FromStringAndSize(p
, 1);
382 char *str
= va_arg(*p_va
, char *);
384 if (**p_format
== '#') {
386 n
= va_arg(*p_va
, int);
396 size_t m
= strlen(str
);
398 PyErr_SetString(PyExc_OverflowError
,
399 "string too long for Python string");
404 v
= PyString_FromStringAndSize(str
, n
);
412 if (**p_format
== '&') {
413 typedef PyObject
*(*converter
)(void *);
414 converter func
= va_arg(*p_va
, converter
);
415 void *arg
= va_arg(*p_va
, void *);
421 v
= va_arg(*p_va
, PyObject
*);
423 if (*(*p_format
- 1) != 'N')
426 else if (!PyErr_Occurred())
427 /* If a NULL was passed
428 * because a call that should
429 * have constructed a value
430 * failed, that's OK, and we
431 * pass the error on; but if
432 * no error occurred it's not
433 * clear that the caller knew
434 * what she was doing. */
435 PyErr_SetString(PyExc_SystemError
,
436 "NULL object passed to Py_BuildValue");
447 PyErr_SetString(PyExc_SystemError
,
448 "bad format char passed to Py_BuildValue");
457 Py_BuildValue(const char *format
, ...)
461 va_start(va
, format
);
462 retval
= Py_VaBuildValue(format
, va
);
468 Py_VaBuildValue(const char *format
, va_list va
)
470 const char *f
= format
;
471 int n
= countformat(f
, '\0');
474 #ifdef VA_LIST_IS_ARRAY
475 memcpy(lva
, va
, sizeof(va_list));
491 return do_mkvalue(&f
, &lva
);
492 return do_mktuple(&f
, &lva
, '\0', n
);
497 PyEval_CallFunction(PyObject
*obj
, const char *format
, ...)
503 va_start(vargs
, format
);
505 args
= Py_VaBuildValue(format
, vargs
);
511 res
= PyEval_CallObject(obj
, args
);
519 PyEval_CallMethod(PyObject
*obj
, const char *methodname
, const char *format
, ...)
526 meth
= PyObject_GetAttrString(obj
, methodname
);
530 va_start(vargs
, format
);
532 args
= Py_VaBuildValue(format
, vargs
);
540 res
= PyEval_CallObject(meth
, args
);
548 PyModule_AddObject(PyObject
*m
, const char *name
, PyObject
*o
)
551 if (!PyModule_Check(m
)) {
552 PyErr_SetString(PyExc_TypeError
,
553 "PyModule_AddObject() needs module as first arg");
557 if (!PyErr_Occurred())
558 PyErr_SetString(PyExc_TypeError
,
559 "PyModule_AddObject() needs non-NULL value");
563 dict
= PyModule_GetDict(m
);
565 /* Internal error -- modules must have a dict! */
566 PyErr_Format(PyExc_SystemError
, "module '%s' has no __dict__",
567 PyModule_GetName(m
));
570 if (PyDict_SetItemString(dict
, name
, o
))
577 PyModule_AddIntConstant(PyObject
*m
, const char *name
, long value
)
579 return PyModule_AddObject(m
, name
, PyInt_FromLong(value
));
583 PyModule_AddStringConstant(PyObject
*m
, const char *name
, const char *value
)
585 return PyModule_AddObject(m
, name
, PyString_FromString(value
));