2 /* Module support implementation */
9 #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
10 typedef extended va_double
;
12 typedef double va_double
;
15 /* Package context -- the full module name for package imports */
16 char *_Py_PackageContext
= NULL
;
18 /* Py_InitModule4() parameters:
19 - name is the module name
20 - methods is the list of top-level functions
21 - doc is the documentation string
22 - passthrough is passed as self to functions defined in the module
23 - api_version is the value of PYTHON_API_VERSION at the time the
26 Return value is a borrowed reference to the module object; or NULL
27 if an error occurred (in Python 1.4 and before, errors were fatal).
28 Errors may still leak memory.
31 static char api_version_warning
[] =
32 "WARNING: Python C API version mismatch for module %s:\n\
33 This Python has API version %d, module %s has version %d.\n";
36 Py_InitModule4(char *name
, PyMethodDef
*methods
, char *doc
,
37 PyObject
*passthrough
, int module_api_version
)
41 if (!Py_IsInitialized())
42 Py_FatalError("Interpreter not initialized (version mismatch?)");
43 if (module_api_version
!= PYTHON_API_VERSION
)
44 fprintf(stderr
, api_version_warning
,
45 name
, PYTHON_API_VERSION
, name
, module_api_version
);
46 if (_Py_PackageContext
!= NULL
) {
47 char *p
= strrchr(_Py_PackageContext
, '.');
48 if (p
!= NULL
&& strcmp(name
, p
+1) == 0) {
49 name
= _Py_PackageContext
;
50 _Py_PackageContext
= NULL
;
53 if ((m
= PyImport_AddModule(name
)) == NULL
)
55 d
= PyModule_GetDict(m
);
56 for (ml
= methods
; ml
->ml_name
!= NULL
; ml
++) {
57 v
= PyCFunction_New(ml
, passthrough
);
60 if (PyDict_SetItemString(d
, ml
->ml_name
, v
) != 0)
65 v
= PyString_FromString(doc
);
66 if (v
== NULL
|| PyDict_SetItemString(d
, "__doc__", v
) != 0)
74 /* Helper for mkvalue() to scan the length of a format */
76 static int countformat(char *format
, int endchar
)
80 while (level
> 0 || *format
!= endchar
) {
84 PyErr_SetString(PyExc_SystemError
,
85 "unmatched paren in format");
116 /* Generic function to create a value -- the inverse of getargs() */
117 /* After an original idea and first implementation by Steven Miale */
119 static PyObject
*do_mktuple(char**, va_list *, int, int);
120 static PyObject
*do_mklist(char**, va_list *, int, int);
121 static PyObject
*do_mkdict(char**, va_list *, int, int);
122 static PyObject
*do_mkvalue(char**, va_list *);
126 do_mkdict(char **p_format
, va_list *p_va
, int endchar
, int n
)
132 if ((d
= PyDict_New()) == NULL
)
134 for (i
= 0; i
< n
; i
+= 2) {
137 k
= do_mkvalue(p_format
, p_va
);
142 v
= do_mkvalue(p_format
, p_va
);
148 err
= PyDict_SetItem(d
, k
, v
);
156 if (d
!= NULL
&& **p_format
!= endchar
) {
159 PyErr_SetString(PyExc_SystemError
,
160 "Unmatched paren in format");
168 do_mklist(char **p_format
, va_list *p_va
, int endchar
, int n
)
174 if ((v
= PyList_New(n
)) == NULL
)
176 for (i
= 0; i
< n
; i
++) {
177 PyObject
*w
= do_mkvalue(p_format
, p_va
);
182 PyList_SetItem(v
, i
, w
);
184 if (v
!= NULL
&& **p_format
!= endchar
) {
187 PyErr_SetString(PyExc_SystemError
,
188 "Unmatched paren in format");
196 _ustrlen(Py_UNICODE
*u
)
200 while (*v
!= 0) { i
++; v
++; }
205 do_mktuple(char **p_format
, va_list *p_va
, int endchar
, int n
)
211 if ((v
= PyTuple_New(n
)) == NULL
)
213 for (i
= 0; i
< n
; i
++) {
214 PyObject
*w
= do_mkvalue(p_format
, p_va
);
219 PyTuple_SetItem(v
, i
, w
);
221 if (v
!= NULL
&& **p_format
!= endchar
) {
224 PyErr_SetString(PyExc_SystemError
,
225 "Unmatched paren in format");
233 do_mkvalue(char **p_format
, va_list *p_va
)
236 switch (*(*p_format
)++) {
238 return do_mktuple(p_format
, p_va
, ')',
239 countformat(*p_format
, ')'));
242 return do_mklist(p_format
, p_va
, ']',
243 countformat(*p_format
, ']'));
246 return do_mkdict(p_format
, p_va
, '}',
247 countformat(*p_format
, '}'));
253 return PyInt_FromLong((long)va_arg(*p_va
, int));
256 return PyInt_FromLong((long)va_arg(*p_va
, unsigned int));
259 return PyInt_FromLong((long)va_arg(*p_va
, long));
261 #ifdef HAVE_LONG_LONG
263 return PyLong_FromLongLong((LONG_LONG
)va_arg(*p_va
, LONG_LONG
));
268 Py_UNICODE
*u
= va_arg(*p_va
, Py_UNICODE
*);
270 if (**p_format
== '#') {
272 n
= va_arg(*p_va
, int);
283 v
= PyUnicode_FromUnicode(u
, n
);
289 return PyFloat_FromDouble(
290 (double)va_arg(*p_va
, va_double
));
295 p
[0] = va_arg(*p_va
, int);
296 return PyString_FromStringAndSize(p
, 1);
303 char *str
= va_arg(*p_va
, char *);
305 if (**p_format
== '#') {
307 n
= va_arg(*p_va
, int);
317 size_t m
= strlen(str
);
319 PyErr_SetString(PyExc_OverflowError
,
320 "string too long for Python string");
325 v
= PyString_FromStringAndSize(str
, n
);
333 if (**p_format
== '&') {
334 typedef PyObject
*(*converter
)(void *);
335 converter func
= va_arg(*p_va
, converter
);
336 void *arg
= va_arg(*p_va
, void *);
342 v
= va_arg(*p_va
, PyObject
*);
344 if (*(*p_format
- 1) != 'N')
347 else if (!PyErr_Occurred())
348 /* If a NULL was passed
349 * because a call that should
350 * have constructed a value
351 * failed, that's OK, and we
352 * pass the error on; but if
353 * no error occurred it's not
354 * clear that the caller knew
355 * what she was doing. */
356 PyErr_SetString(PyExc_SystemError
,
357 "NULL object passed to Py_BuildValue");
368 PyErr_SetString(PyExc_SystemError
,
369 "bad format char passed to Py_BuildValue");
377 PyObject
*Py_BuildValue(char *format
, ...)
381 va_start(va
, format
);
382 retval
= Py_VaBuildValue(format
, va
);
388 Py_VaBuildValue(char *format
, va_list va
)
391 int n
= countformat(f
, '\0');
394 #ifdef VA_LIST_IS_ARRAY
395 memcpy(lva
, va
, sizeof(va_list));
407 return do_mkvalue(&f
, &lva
);
408 return do_mktuple(&f
, &lva
, '\0', n
);
413 PyEval_CallFunction(PyObject
*obj
, char *format
, ...)
419 va_start(vargs
, format
);
421 args
= Py_VaBuildValue(format
, vargs
);
427 res
= PyEval_CallObject(obj
, args
);
435 PyEval_CallMethod(PyObject
*obj
, char *methodname
, char *format
, ...)
442 meth
= PyObject_GetAttrString(obj
, methodname
);
446 va_start(vargs
, format
);
448 args
= Py_VaBuildValue(format
, vargs
);
456 res
= PyEval_CallObject(meth
, args
);
464 PyModule_AddObject(PyObject
*m
, char *name
, PyObject
*o
)
467 if (!PyModule_Check(m
) || o
== NULL
)
469 dict
= PyModule_GetDict(m
);
472 if (PyDict_SetItemString(dict
, name
, o
))
479 PyModule_AddIntConstant(PyObject
*m
, char *name
, long value
)
481 return PyModule_AddObject(m
, name
, PyInt_FromLong(value
));
485 PyModule_AddStringConstant(PyObject
*m
, char *name
, char *value
)
487 return PyModule_AddObject(m
, name
, PyString_FromString(value
));