2 /* Module support implementation */
6 #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
7 typedef extended va_double
;
9 typedef double va_double
;
12 /* Package context -- the full module name for package imports */
13 char *_Py_PackageContext
= NULL
;
15 /* Py_InitModule4() parameters:
16 - name is the module name
17 - methods is the list of top-level functions
18 - doc is the documentation string
19 - passthrough is passed as self to functions defined in the module
20 - api_version is the value of PYTHON_API_VERSION at the time the
23 Return value is a borrowed reference to the module object; or NULL
24 if an error occurred (in Python 1.4 and before, errors were fatal).
25 Errors may still leak memory.
28 static char api_version_warning
[] =
29 "Python C API version mismatch for module %.100s:\
30 This Python has API version %d, module %.100s has version %d.";
33 Py_InitModule4(char *name
, PyMethodDef
*methods
, char *doc
,
34 PyObject
*passthrough
, int module_api_version
)
38 if (!Py_IsInitialized())
39 Py_FatalError("Interpreter not initialized (version mismatch?)");
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 if (_Py_PackageContext
!= NULL
) {
50 char *p
= strrchr(_Py_PackageContext
, '.');
51 if (p
!= NULL
&& strcmp(name
, p
+1) == 0) {
52 name
= _Py_PackageContext
;
53 _Py_PackageContext
= NULL
;
56 if ((m
= PyImport_AddModule(name
)) == NULL
)
58 d
= PyModule_GetDict(m
);
59 for (ml
= methods
; ml
->ml_name
!= NULL
; ml
++) {
60 v
= PyCFunction_New(ml
, passthrough
);
63 if (PyDict_SetItemString(d
, ml
->ml_name
, v
) != 0) {
70 v
= PyString_FromString(doc
);
71 if (v
== NULL
|| PyDict_SetItemString(d
, "__doc__", v
) != 0) {
81 /* Helper for mkvalue() to scan the length of a format */
83 static int countformat(char *format
, int endchar
)
87 while (level
> 0 || *format
!= endchar
) {
91 PyErr_SetString(PyExc_SystemError
,
92 "unmatched paren in format");
123 /* Generic function to create a value -- the inverse of getargs() */
124 /* After an original idea and first implementation by Steven Miale */
126 static PyObject
*do_mktuple(char**, va_list *, int, int);
127 static PyObject
*do_mklist(char**, va_list *, int, int);
128 static PyObject
*do_mkdict(char**, va_list *, int, int);
129 static PyObject
*do_mkvalue(char**, va_list *);
133 do_mkdict(char **p_format
, va_list *p_va
, int endchar
, int n
)
139 if ((d
= PyDict_New()) == NULL
)
141 for (i
= 0; i
< n
; i
+= 2) {
144 k
= do_mkvalue(p_format
, p_va
);
149 v
= do_mkvalue(p_format
, p_va
);
155 err
= PyDict_SetItem(d
, k
, v
);
163 if (d
!= NULL
&& **p_format
!= endchar
) {
166 PyErr_SetString(PyExc_SystemError
,
167 "Unmatched paren in format");
175 do_mklist(char **p_format
, va_list *p_va
, int endchar
, int n
)
181 if ((v
= PyList_New(n
)) == NULL
)
183 for (i
= 0; i
< n
; i
++) {
184 PyObject
*w
= do_mkvalue(p_format
, p_va
);
189 PyList_SetItem(v
, i
, w
);
191 if (v
!= NULL
&& **p_format
!= endchar
) {
194 PyErr_SetString(PyExc_SystemError
,
195 "Unmatched paren in format");
202 #ifdef Py_USING_UNICODE
204 _ustrlen(Py_UNICODE
*u
)
208 while (*v
!= 0) { i
++; v
++; }
214 do_mktuple(char **p_format
, va_list *p_va
, int endchar
, int n
)
220 if ((v
= PyTuple_New(n
)) == NULL
)
222 for (i
= 0; i
< n
; i
++) {
223 PyObject
*w
= do_mkvalue(p_format
, p_va
);
228 PyTuple_SetItem(v
, i
, w
);
230 if (v
!= NULL
&& **p_format
!= endchar
) {
233 PyErr_SetString(PyExc_SystemError
,
234 "Unmatched paren in format");
242 do_mkvalue(char **p_format
, va_list *p_va
)
245 switch (*(*p_format
)++) {
247 return do_mktuple(p_format
, p_va
, ')',
248 countformat(*p_format
, ')'));
251 return do_mklist(p_format
, p_va
, ']',
252 countformat(*p_format
, ']'));
255 return do_mkdict(p_format
, p_va
, '}',
256 countformat(*p_format
, '}'));
262 return PyInt_FromLong((long)va_arg(*p_va
, int));
265 return PyInt_FromLong((long)va_arg(*p_va
, unsigned int));
268 return PyInt_FromLong((long)va_arg(*p_va
, long));
270 #ifdef HAVE_LONG_LONG
272 return PyLong_FromLongLong((LONG_LONG
)va_arg(*p_va
, LONG_LONG
));
274 #ifdef Py_USING_UNICODE
278 Py_UNICODE
*u
= va_arg(*p_va
, Py_UNICODE
*);
280 if (**p_format
== '#') {
282 n
= va_arg(*p_va
, int);
293 v
= PyUnicode_FromUnicode(u
, n
);
300 return PyFloat_FromDouble(
301 (double)va_arg(*p_va
, va_double
));
303 #ifndef WITHOUT_COMPLEX
305 return PyComplex_FromCComplex(
306 *((Py_complex
*)va_arg(*p_va
, Py_complex
*)));
307 #endif /* WITHOUT_COMPLEX */
312 p
[0] = va_arg(*p_va
, int);
313 return PyString_FromStringAndSize(p
, 1);
320 char *str
= va_arg(*p_va
, char *);
322 if (**p_format
== '#') {
324 n
= va_arg(*p_va
, int);
334 size_t m
= strlen(str
);
336 PyErr_SetString(PyExc_OverflowError
,
337 "string too long for Python string");
342 v
= PyString_FromStringAndSize(str
, n
);
350 if (**p_format
== '&') {
351 typedef PyObject
*(*converter
)(void *);
352 converter func
= va_arg(*p_va
, converter
);
353 void *arg
= va_arg(*p_va
, void *);
359 v
= va_arg(*p_va
, PyObject
*);
361 if (*(*p_format
- 1) != 'N')
364 else if (!PyErr_Occurred())
365 /* If a NULL was passed
366 * because a call that should
367 * have constructed a value
368 * failed, that's OK, and we
369 * pass the error on; but if
370 * no error occurred it's not
371 * clear that the caller knew
372 * what she was doing. */
373 PyErr_SetString(PyExc_SystemError
,
374 "NULL object passed to Py_BuildValue");
385 PyErr_SetString(PyExc_SystemError
,
386 "bad format char passed to Py_BuildValue");
394 PyObject
*Py_BuildValue(char *format
, ...)
398 va_start(va
, format
);
399 retval
= Py_VaBuildValue(format
, va
);
405 Py_VaBuildValue(char *format
, va_list va
)
408 int n
= countformat(f
, '\0');
411 #ifdef VA_LIST_IS_ARRAY
412 memcpy(lva
, va
, sizeof(va_list));
424 return do_mkvalue(&f
, &lva
);
425 return do_mktuple(&f
, &lva
, '\0', n
);
430 PyEval_CallFunction(PyObject
*obj
, char *format
, ...)
436 va_start(vargs
, format
);
438 args
= Py_VaBuildValue(format
, vargs
);
444 res
= PyEval_CallObject(obj
, args
);
452 PyEval_CallMethod(PyObject
*obj
, char *methodname
, char *format
, ...)
459 meth
= PyObject_GetAttrString(obj
, methodname
);
463 va_start(vargs
, format
);
465 args
= Py_VaBuildValue(format
, vargs
);
473 res
= PyEval_CallObject(meth
, args
);
481 PyModule_AddObject(PyObject
*m
, char *name
, PyObject
*o
)
484 if (!PyModule_Check(m
) || o
== NULL
)
486 dict
= PyModule_GetDict(m
);
489 if (PyDict_SetItemString(dict
, name
, o
))
496 PyModule_AddIntConstant(PyObject
*m
, char *name
, long value
)
498 return PyModule_AddObject(m
, name
, PyInt_FromLong(value
));
502 PyModule_AddStringConstant(PyObject
*m
, char *name
, char *value
)
504 return PyModule_AddObject(m
, name
, PyString_FromString(value
));