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
)
36 PyObject
*m
, *d
, *v
, *n
;
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 /* Make sure name is fully qualified.
51 This is a bit of a hack: when the shared library is loaded,
52 the module name is "package.module", but the module calls
53 Py_InitModule*() with just "module" for the name. The shared
54 library loader squirrels away the true name of the module in
55 _Py_PackageContext, and Py_InitModule*() will substitute this
56 (if the name actually matches).
58 if (_Py_PackageContext
!= NULL
) {
59 char *p
= strrchr(_Py_PackageContext
, '.');
60 if (p
!= NULL
&& strcmp(name
, p
+1) == 0) {
61 name
= _Py_PackageContext
;
62 _Py_PackageContext
= NULL
;
65 if ((m
= PyImport_AddModule(name
)) == NULL
)
67 d
= PyModule_GetDict(m
);
68 if (methods
!= NULL
) {
69 n
= PyString_FromString(name
);
72 for (ml
= methods
; ml
->ml_name
!= NULL
; ml
++) {
73 if ((ml
->ml_flags
& METH_CLASS
) ||
74 (ml
->ml_flags
& METH_STATIC
)) {
75 PyErr_SetString(PyExc_ValueError
,
76 "module functions cannot set"
77 " METH_CLASS or METH_STATIC");
80 v
= PyCFunction_NewEx(ml
, passthrough
, n
);
83 if (PyDict_SetItemString(d
, ml
->ml_name
, v
) != 0) {
91 v
= PyString_FromString(doc
);
92 if (v
== NULL
|| PyDict_SetItemString(d
, "__doc__", v
) != 0) {
102 /* Helper for mkvalue() to scan the length of a format */
105 countformat(char *format
, int endchar
)
109 while (level
> 0 || *format
!= endchar
) {
113 PyErr_SetString(PyExc_SystemError
,
114 "unmatched paren in format");
145 /* Generic function to create a value -- the inverse of getargs() */
146 /* After an original idea and first implementation by Steven Miale */
148 static PyObject
*do_mktuple(char**, va_list *, int, int);
149 static PyObject
*do_mklist(char**, va_list *, int, int);
150 static PyObject
*do_mkdict(char**, va_list *, int, int);
151 static PyObject
*do_mkvalue(char**, va_list *);
155 do_mkdict(char **p_format
, va_list *p_va
, int endchar
, int n
)
161 if ((d
= PyDict_New()) == NULL
)
163 for (i
= 0; i
< n
; i
+= 2) {
166 k
= do_mkvalue(p_format
, p_va
);
171 v
= do_mkvalue(p_format
, p_va
);
177 err
= PyDict_SetItem(d
, k
, v
);
185 if (d
!= NULL
&& **p_format
!= endchar
) {
188 PyErr_SetString(PyExc_SystemError
,
189 "Unmatched paren in format");
197 do_mklist(char **p_format
, va_list *p_va
, int endchar
, int n
)
203 if ((v
= PyList_New(n
)) == NULL
)
205 for (i
= 0; i
< n
; i
++) {
206 PyObject
*w
= do_mkvalue(p_format
, p_va
);
211 PyList_SetItem(v
, i
, w
);
213 if (v
!= NULL
&& **p_format
!= endchar
) {
216 PyErr_SetString(PyExc_SystemError
,
217 "Unmatched paren in format");
224 #ifdef Py_USING_UNICODE
226 _ustrlen(Py_UNICODE
*u
)
230 while (*v
!= 0) { i
++; v
++; }
236 do_mktuple(char **p_format
, va_list *p_va
, int endchar
, int n
)
242 if ((v
= PyTuple_New(n
)) == NULL
)
244 for (i
= 0; i
< n
; i
++) {
245 PyObject
*w
= do_mkvalue(p_format
, p_va
);
250 PyTuple_SetItem(v
, i
, w
);
252 if (v
!= NULL
&& **p_format
!= endchar
) {
255 PyErr_SetString(PyExc_SystemError
,
256 "Unmatched paren in format");
264 do_mkvalue(char **p_format
, va_list *p_va
)
267 switch (*(*p_format
)++) {
269 return do_mktuple(p_format
, p_va
, ')',
270 countformat(*p_format
, ')'));
273 return do_mklist(p_format
, p_va
, ']',
274 countformat(*p_format
, ']'));
277 return do_mkdict(p_format
, p_va
, '}',
278 countformat(*p_format
, '}'));
284 return PyInt_FromLong((long)va_arg(*p_va
, int));
287 return PyInt_FromLong((long)va_arg(*p_va
, unsigned int));
290 return PyInt_FromLong((long)va_arg(*p_va
, long));
293 return PyInt_FromLong((long)va_arg(*p_va
, unsigned long));
295 #ifdef HAVE_LONG_LONG
297 return PyLong_FromLongLong((PY_LONG_LONG
)va_arg(*p_va
, PY_LONG_LONG
));
300 return PyLong_FromLongLong((PY_LONG_LONG
)va_arg(*p_va
, unsigned PY_LONG_LONG
));
302 #ifdef Py_USING_UNICODE
306 Py_UNICODE
*u
= va_arg(*p_va
, Py_UNICODE
*);
308 if (**p_format
== '#') {
310 n
= va_arg(*p_va
, int);
321 v
= PyUnicode_FromUnicode(u
, n
);
328 return PyFloat_FromDouble(
329 (double)va_arg(*p_va
, va_double
));
331 #ifndef WITHOUT_COMPLEX
333 return PyComplex_FromCComplex(
334 *((Py_complex
*)va_arg(*p_va
, Py_complex
*)));
335 #endif /* WITHOUT_COMPLEX */
340 p
[0] = va_arg(*p_va
, int);
341 return PyString_FromStringAndSize(p
, 1);
348 char *str
= va_arg(*p_va
, char *);
350 if (**p_format
== '#') {
352 n
= va_arg(*p_va
, int);
362 size_t m
= strlen(str
);
364 PyErr_SetString(PyExc_OverflowError
,
365 "string too long for Python string");
370 v
= PyString_FromStringAndSize(str
, n
);
378 if (**p_format
== '&') {
379 typedef PyObject
*(*converter
)(void *);
380 converter func
= va_arg(*p_va
, converter
);
381 void *arg
= va_arg(*p_va
, void *);
387 v
= va_arg(*p_va
, PyObject
*);
389 if (*(*p_format
- 1) != 'N')
392 else if (!PyErr_Occurred())
393 /* If a NULL was passed
394 * because a call that should
395 * have constructed a value
396 * failed, that's OK, and we
397 * pass the error on; but if
398 * no error occurred it's not
399 * clear that the caller knew
400 * what she was doing. */
401 PyErr_SetString(PyExc_SystemError
,
402 "NULL object passed to Py_BuildValue");
413 PyErr_SetString(PyExc_SystemError
,
414 "bad format char passed to Py_BuildValue");
423 Py_BuildValue(char *format
, ...)
427 va_start(va
, format
);
428 retval
= Py_VaBuildValue(format
, va
);
434 Py_VaBuildValue(char *format
, va_list va
)
437 int n
= countformat(f
, '\0');
440 #ifdef VA_LIST_IS_ARRAY
441 memcpy(lva
, va
, sizeof(va_list));
457 return do_mkvalue(&f
, &lva
);
458 return do_mktuple(&f
, &lva
, '\0', n
);
463 PyEval_CallFunction(PyObject
*obj
, char *format
, ...)
469 va_start(vargs
, format
);
471 args
= Py_VaBuildValue(format
, vargs
);
477 res
= PyEval_CallObject(obj
, args
);
485 PyEval_CallMethod(PyObject
*obj
, char *methodname
, char *format
, ...)
492 meth
= PyObject_GetAttrString(obj
, methodname
);
496 va_start(vargs
, format
);
498 args
= Py_VaBuildValue(format
, vargs
);
506 res
= PyEval_CallObject(meth
, args
);
514 PyModule_AddObject(PyObject
*m
, char *name
, PyObject
*o
)
517 if (!PyModule_Check(m
) || o
== NULL
) {
518 PyErr_SetString(PyExc_TypeError
,
519 "PyModule_AddObject() needs module as first arg");
522 dict
= PyModule_GetDict(m
);
524 /* Internal error -- modules must have a dict! */
525 PyErr_Format(PyExc_SystemError
, "module '%s' has no __dict__",
526 PyModule_GetName(m
));
529 if (PyDict_SetItemString(dict
, name
, o
))
536 PyModule_AddIntConstant(PyObject
*m
, char *name
, long value
)
538 return PyModule_AddObject(m
, name
, PyInt_FromLong(value
));
542 PyModule_AddStringConstant(PyObject
*m
, char *name
, char *value
)
544 return PyModule_AddObject(m
, name
, PyString_FromString(value
));