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 "WARNING: Python C API version mismatch for module %s:\n\
30 This Python has API version %d, module %s has version %d.\n";
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
)
41 fprintf(stderr
, api_version_warning
,
42 name
, PYTHON_API_VERSION
, name
, module_api_version
);
43 if (_Py_PackageContext
!= NULL
) {
44 char *p
= strrchr(_Py_PackageContext
, '.');
45 if (p
!= NULL
&& strcmp(name
, p
+1) == 0) {
46 name
= _Py_PackageContext
;
47 _Py_PackageContext
= NULL
;
50 if ((m
= PyImport_AddModule(name
)) == NULL
)
52 d
= PyModule_GetDict(m
);
53 for (ml
= methods
; ml
->ml_name
!= NULL
; ml
++) {
54 v
= PyCFunction_New(ml
, passthrough
);
57 if (PyDict_SetItemString(d
, ml
->ml_name
, v
) != 0)
62 v
= PyString_FromString(doc
);
63 if (v
== NULL
|| PyDict_SetItemString(d
, "__doc__", v
) != 0)
71 /* Helper for mkvalue() to scan the length of a format */
73 static int countformat(char *format
, int endchar
)
77 while (level
> 0 || *format
!= endchar
) {
81 PyErr_SetString(PyExc_SystemError
,
82 "unmatched paren in format");
113 /* Generic function to create a value -- the inverse of getargs() */
114 /* After an original idea and first implementation by Steven Miale */
116 static PyObject
*do_mktuple(char**, va_list *, int, int);
117 static PyObject
*do_mklist(char**, va_list *, int, int);
118 static PyObject
*do_mkdict(char**, va_list *, int, int);
119 static PyObject
*do_mkvalue(char**, va_list *);
123 do_mkdict(char **p_format
, va_list *p_va
, int endchar
, int n
)
129 if ((d
= PyDict_New()) == NULL
)
131 for (i
= 0; i
< n
; i
+= 2) {
134 k
= do_mkvalue(p_format
, p_va
);
139 v
= do_mkvalue(p_format
, p_va
);
145 err
= PyDict_SetItem(d
, k
, v
);
153 if (d
!= NULL
&& **p_format
!= endchar
) {
156 PyErr_SetString(PyExc_SystemError
,
157 "Unmatched paren in format");
165 do_mklist(char **p_format
, va_list *p_va
, int endchar
, int n
)
171 if ((v
= PyList_New(n
)) == NULL
)
173 for (i
= 0; i
< n
; i
++) {
174 PyObject
*w
= do_mkvalue(p_format
, p_va
);
179 PyList_SetItem(v
, i
, w
);
181 if (v
!= NULL
&& **p_format
!= endchar
) {
184 PyErr_SetString(PyExc_SystemError
,
185 "Unmatched paren in format");
193 _ustrlen(Py_UNICODE
*u
)
197 while (*v
!= 0) { i
++; v
++; }
202 do_mktuple(char **p_format
, va_list *p_va
, int endchar
, int n
)
208 if ((v
= PyTuple_New(n
)) == NULL
)
210 for (i
= 0; i
< n
; i
++) {
211 PyObject
*w
= do_mkvalue(p_format
, p_va
);
216 PyTuple_SetItem(v
, i
, w
);
218 if (v
!= NULL
&& **p_format
!= endchar
) {
221 PyErr_SetString(PyExc_SystemError
,
222 "Unmatched paren in format");
230 do_mkvalue(char **p_format
, va_list *p_va
)
233 switch (*(*p_format
)++) {
235 return do_mktuple(p_format
, p_va
, ')',
236 countformat(*p_format
, ')'));
239 return do_mklist(p_format
, p_va
, ']',
240 countformat(*p_format
, ']'));
243 return do_mkdict(p_format
, p_va
, '}',
244 countformat(*p_format
, '}'));
250 return PyInt_FromLong((long)va_arg(*p_va
, int));
253 return PyInt_FromLong((long)va_arg(*p_va
, unsigned int));
256 return PyInt_FromLong((long)va_arg(*p_va
, long));
258 #ifdef HAVE_LONG_LONG
260 return PyLong_FromLongLong((LONG_LONG
)va_arg(*p_va
, LONG_LONG
));
265 Py_UNICODE
*u
= va_arg(*p_va
, Py_UNICODE
*);
267 if (**p_format
== '#') {
269 n
= va_arg(*p_va
, int);
280 v
= PyUnicode_FromUnicode(u
, n
);
286 return PyFloat_FromDouble(
287 (double)va_arg(*p_va
, va_double
));
292 p
[0] = va_arg(*p_va
, int);
293 return PyString_FromStringAndSize(p
, 1);
300 char *str
= va_arg(*p_va
, char *);
302 if (**p_format
== '#') {
304 n
= va_arg(*p_va
, int);
314 size_t m
= strlen(str
);
316 PyErr_SetString(PyExc_OverflowError
,
317 "string too long for Python string");
322 v
= PyString_FromStringAndSize(str
, n
);
330 if (**p_format
== '&') {
331 typedef PyObject
*(*converter
)(void *);
332 converter func
= va_arg(*p_va
, converter
);
333 void *arg
= va_arg(*p_va
, void *);
339 v
= va_arg(*p_va
, PyObject
*);
341 if (*(*p_format
- 1) != 'N')
344 else if (!PyErr_Occurred())
345 /* If a NULL was passed
346 * because a call that should
347 * have constructed a value
348 * failed, that's OK, and we
349 * pass the error on; but if
350 * no error occurred it's not
351 * clear that the caller knew
352 * what she was doing. */
353 PyErr_SetString(PyExc_SystemError
,
354 "NULL object passed to Py_BuildValue");
365 PyErr_SetString(PyExc_SystemError
,
366 "bad format char passed to Py_BuildValue");
374 PyObject
*Py_BuildValue(char *format
, ...)
378 va_start(va
, format
);
379 retval
= Py_VaBuildValue(format
, va
);
385 Py_VaBuildValue(char *format
, va_list va
)
388 int n
= countformat(f
, '\0');
391 #ifdef VA_LIST_IS_ARRAY
392 memcpy(lva
, va
, sizeof(va_list));
404 return do_mkvalue(&f
, &lva
);
405 return do_mktuple(&f
, &lva
, '\0', n
);
410 PyEval_CallFunction(PyObject
*obj
, char *format
, ...)
416 va_start(vargs
, format
);
418 args
= Py_VaBuildValue(format
, vargs
);
424 res
= PyEval_CallObject(obj
, args
);
432 PyEval_CallMethod(PyObject
*obj
, char *methodname
, char *format
, ...)
439 meth
= PyObject_GetAttrString(obj
, methodname
);
443 va_start(vargs
, format
);
445 args
= Py_VaBuildValue(format
, vargs
);
453 res
= PyEval_CallObject(meth
, args
);
461 PyModule_AddObject(PyObject
*m
, char *name
, PyObject
*o
)
464 if (!PyModule_Check(m
) || o
== NULL
)
466 dict
= PyModule_GetDict(m
);
469 if (PyDict_SetItemString(dict
, name
, o
))
476 PyModule_AddIntConstant(PyObject
*m
, char *name
, long value
)
478 return PyModule_AddObject(m
, name
, PyInt_FromLong(value
));
482 PyModule_AddStringConstant(PyObject
*m
, char *name
, char *value
)
484 return PyModule_AddObject(m
, name
, PyString_FromString(value
));