1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Module support implementation */
36 #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
37 typedef extended va_double
;
39 typedef double va_double
;
42 /* Package context -- the full module name for package imports */
43 char *_Py_PackageContext
= NULL
;
45 /* Py_InitModule4() parameters:
46 - name is the module name
47 - methods is the list of top-level functions
48 - doc is the documentation string
49 - passthrough is passed as self to functions defined in the module
50 - api_version is the value of PYTHON_API_VERSION at the time the
53 Return value is a borrowed reference to the module object; or NULL
54 if an error occurred (in Python 1.4 and before, errors were fatal).
55 Errors may still leak memory.
58 static char api_version_warning
[] =
59 "WARNING: Python C API version mismatch for module %s:\n\
60 This Python has API version %d, module %s has version %d.\n";
63 Py_InitModule4(name
, methods
, doc
, passthrough
, module_api_version
)
67 PyObject
*passthrough
;
68 int module_api_version
;
72 if (module_api_version
!= PYTHON_API_VERSION
)
73 fprintf(stderr
, api_version_warning
,
74 name
, PYTHON_API_VERSION
, name
, module_api_version
);
75 if (_Py_PackageContext
!= NULL
) {
76 char *p
= strrchr(_Py_PackageContext
, '.');
77 if (p
!= NULL
&& strcmp(name
, p
+1) == 0) {
78 name
= _Py_PackageContext
;
79 _Py_PackageContext
= NULL
;
82 if ((m
= PyImport_AddModule(name
)) == NULL
)
84 d
= PyModule_GetDict(m
);
85 for (ml
= methods
; ml
->ml_name
!= NULL
; ml
++) {
86 v
= PyCFunction_New(ml
, passthrough
);
89 if (PyDict_SetItemString(d
, ml
->ml_name
, v
) != 0)
94 v
= PyString_FromString(doc
);
95 if (v
== NULL
|| PyDict_SetItemString(d
, "__doc__", v
) != 0)
103 /* Helper for mkvalue() to scan the length of a format */
105 static int countformat
Py_PROTO((char *format
, int endchar
));
106 static int countformat(format
, endchar
)
112 while (level
> 0 || *format
!= endchar
) {
116 PyErr_SetString(PyExc_SystemError
,
117 "unmatched paren in format");
148 /* Generic function to create a value -- the inverse of getargs() */
149 /* After an original idea and first implementation by Steven Miale */
151 static PyObject
*do_mktuple
Py_PROTO((char**, va_list *, int, int));
152 static PyObject
*do_mklist
Py_PROTO((char**, va_list *, int, int));
153 static PyObject
*do_mkdict
Py_PROTO((char**, va_list *, int, int));
154 static PyObject
*do_mkvalue
Py_PROTO((char**, va_list *));
158 do_mkdict(p_format
, p_va
, endchar
, n
)
168 if ((d
= PyDict_New()) == NULL
)
170 for (i
= 0; i
< n
; i
+= 2) {
173 k
= do_mkvalue(p_format
, p_va
);
178 v
= do_mkvalue(p_format
, p_va
);
184 err
= PyDict_SetItem(d
, k
, v
);
192 if (d
!= NULL
&& **p_format
!= endchar
) {
195 PyErr_SetString(PyExc_SystemError
,
196 "Unmatched paren in format");
204 do_mklist(p_format
, p_va
, endchar
, n
)
214 if ((v
= PyList_New(n
)) == NULL
)
216 for (i
= 0; i
< n
; i
++) {
217 PyObject
*w
= do_mkvalue(p_format
, p_va
);
222 PyList_SetItem(v
, i
, w
);
224 if (v
!= NULL
&& **p_format
!= endchar
) {
227 PyErr_SetString(PyExc_SystemError
,
228 "Unmatched paren in format");
236 do_mktuple(p_format
, p_va
, endchar
, n
)
246 if ((v
= PyTuple_New(n
)) == NULL
)
248 for (i
= 0; i
< n
; i
++) {
249 PyObject
*w
= do_mkvalue(p_format
, p_va
);
254 PyTuple_SetItem(v
, i
, w
);
256 if (v
!= NULL
&& **p_format
!= endchar
) {
259 PyErr_SetString(PyExc_SystemError
,
260 "Unmatched paren in format");
268 do_mkvalue(p_format
, p_va
)
273 switch (*(*p_format
)++) {
275 return do_mktuple(p_format
, p_va
, ')',
276 countformat(*p_format
, ')'));
279 return do_mklist(p_format
, p_va
, ']',
280 countformat(*p_format
, ']'));
283 return do_mkdict(p_format
, p_va
, '}',
284 countformat(*p_format
, '}'));
289 return PyInt_FromLong((long)va_arg(*p_va
, int));
292 return PyInt_FromLong((long)va_arg(*p_va
, long));
294 #ifdef HAVE_LONG_LONG
296 return PyLong_FromLongLong((LONG_LONG
)va_arg(*p_va
, LONG_LONG
));
301 return PyFloat_FromDouble(
302 (double)va_arg(*p_va
, va_double
));
307 p
[0] = va_arg(*p_va
, int);
308 return PyString_FromStringAndSize(p
, 1);
315 char *str
= va_arg(*p_va
, char *);
317 if (**p_format
== '#') {
319 n
= va_arg(*p_va
, int);
330 v
= PyString_FromStringAndSize(str
, n
);
338 if (**p_format
== '&') {
339 typedef PyObject
*(*converter
) Py_PROTO((void *));
340 converter func
= va_arg(*p_va
, converter
);
341 void *arg
= va_arg(*p_va
, void *);
347 v
= va_arg(*p_va
, PyObject
*);
349 if (*(*p_format
- 1) != 'N')
352 else if (!PyErr_Occurred())
353 /* If a NULL was passed
354 * because a call that should
355 * have constructed a value
356 * failed, that's OK, and we
357 * pass the error on; but if
358 * no error occurred it's not
359 * clear that the caller knew
360 * what she was doing. */
361 PyErr_SetString(PyExc_SystemError
,
362 "NULL object passed to Py_BuildValue");
373 PyErr_SetString(PyExc_SystemError
,
374 "bad format char passed to Py_BuildValue");
382 #ifdef HAVE_STDARG_PROTOTYPES
384 PyObject
*Py_BuildValue(char *format
, ...)
387 PyObject
*Py_BuildValue(va_alist
) va_dcl
392 #ifdef HAVE_STDARG_PROTOTYPES
393 va_start(va
, format
);
397 format
= va_arg(va
, char *);
399 retval
= Py_VaBuildValue(format
, va
);
405 Py_VaBuildValue(format
, va
)
410 int n
= countformat(f
, '\0');
413 #ifdef VA_LIST_IS_ARRAY
414 memcpy(lva
, va
, sizeof(va_list));
426 return do_mkvalue(&f
, &lva
);
427 return do_mktuple(&f
, &lva
, '\0', n
);
431 #ifdef HAVE_STDARG_PROTOTYPES
433 PyEval_CallFunction(PyObject
*obj
, char *format
, ...)
436 PyEval_CallFunction(obj
, format
, va_alist
)
446 #ifdef HAVE_STDARG_PROTOTYPES
447 va_start(vargs
, format
);
452 args
= Py_VaBuildValue(format
, vargs
);
458 res
= PyEval_CallObject(obj
, args
);
465 #ifdef HAVE_STDARG_PROTOTYPES
467 PyEval_CallMethod(PyObject
*obj
, char *methodname
, char *format
, ...)
470 PyEval_CallMethod(obj
, methodname
, format
, va_alist
)
482 meth
= PyObject_GetAttrString(obj
, methodname
);
486 #ifdef HAVE_STDARG_PROTOTYPES
487 va_start(vargs
, format
);
492 args
= Py_VaBuildValue(format
, vargs
);
500 res
= PyEval_CallObject(meth
, args
);