Clarify portability and main program.
[python/dscho.git] / Python / modsupport.c
blob6066aa860e13d17e18980bf9b77d8a7512d1e3da
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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
15 permission.
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 */
34 #include "Python.h"
36 #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
37 typedef extended va_double;
38 #else
39 typedef double va_double;
40 #endif
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
51 module was compiled
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";
62 PyObject *
63 Py_InitModule4(name, methods, doc, passthrough, module_api_version)
64 char *name;
65 PyMethodDef *methods;
66 char *doc;
67 PyObject *passthrough;
68 int module_api_version;
70 PyObject *m, *d, *v;
71 PyMethodDef *ml;
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)
83 return NULL;
84 d = PyModule_GetDict(m);
85 for (ml = methods; ml->ml_name != NULL; ml++) {
86 v = PyCFunction_New(ml, passthrough);
87 if (v == NULL)
88 return NULL;
89 if (PyDict_SetItemString(d, ml->ml_name, v) != 0)
90 return NULL;
91 Py_DECREF(v);
93 if (doc != NULL) {
94 v = PyString_FromString(doc);
95 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0)
96 return NULL;
97 Py_DECREF(v);
99 return m;
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)
107 char *format;
108 int endchar;
110 int count = 0;
111 int level = 0;
112 while (level > 0 || *format != endchar) {
113 switch (*format) {
114 case '\0':
115 /* Premature end */
116 PyErr_SetString(PyExc_SystemError,
117 "unmatched paren in format");
118 return -1;
119 case '(':
120 case '[':
121 case '{':
122 if (level == 0)
123 count++;
124 level++;
125 break;
126 case ')':
127 case ']':
128 case '}':
129 level--;
130 break;
131 case '#':
132 case '&':
133 case ',':
134 case ':':
135 case ' ':
136 case '\t':
137 break;
138 default:
139 if (level == 0)
140 count++;
142 format++;
144 return count;
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 *));
157 static PyObject *
158 do_mkdict(p_format, p_va, endchar, n)
159 char **p_format;
160 va_list *p_va;
161 int endchar;
162 int n;
164 PyObject *d;
165 int i;
166 if (n < 0)
167 return NULL;
168 if ((d = PyDict_New()) == NULL)
169 return NULL;
170 for (i = 0; i < n; i+= 2) {
171 PyObject *k, *v;
172 int err;
173 k = do_mkvalue(p_format, p_va);
174 if (k == NULL) {
175 Py_DECREF(d);
176 return NULL;
178 v = do_mkvalue(p_format, p_va);
179 if (v == NULL) {
180 Py_DECREF(k);
181 Py_DECREF(d);
182 return NULL;
184 err = PyDict_SetItem(d, k, v);
185 Py_DECREF(k);
186 Py_DECREF(v);
187 if (err < 0) {
188 Py_DECREF(d);
189 return NULL;
192 if (d != NULL && **p_format != endchar) {
193 Py_DECREF(d);
194 d = NULL;
195 PyErr_SetString(PyExc_SystemError,
196 "Unmatched paren in format");
198 else if (endchar)
199 ++*p_format;
200 return d;
203 static PyObject *
204 do_mklist(p_format, p_va, endchar, n)
205 char **p_format;
206 va_list *p_va;
207 int endchar;
208 int n;
210 PyObject *v;
211 int i;
212 if (n < 0)
213 return NULL;
214 if ((v = PyList_New(n)) == NULL)
215 return NULL;
216 for (i = 0; i < n; i++) {
217 PyObject *w = do_mkvalue(p_format, p_va);
218 if (w == NULL) {
219 Py_DECREF(v);
220 return NULL;
222 PyList_SetItem(v, i, w);
224 if (v != NULL && **p_format != endchar) {
225 Py_DECREF(v);
226 v = NULL;
227 PyErr_SetString(PyExc_SystemError,
228 "Unmatched paren in format");
230 else if (endchar)
231 ++*p_format;
232 return v;
235 static PyObject *
236 do_mktuple(p_format, p_va, endchar, n)
237 char **p_format;
238 va_list *p_va;
239 int endchar;
240 int n;
242 PyObject *v;
243 int i;
244 if (n < 0)
245 return NULL;
246 if ((v = PyTuple_New(n)) == NULL)
247 return NULL;
248 for (i = 0; i < n; i++) {
249 PyObject *w = do_mkvalue(p_format, p_va);
250 if (w == NULL) {
251 Py_DECREF(v);
252 return NULL;
254 PyTuple_SetItem(v, i, w);
256 if (v != NULL && **p_format != endchar) {
257 Py_DECREF(v);
258 v = NULL;
259 PyErr_SetString(PyExc_SystemError,
260 "Unmatched paren in format");
262 else if (endchar)
263 ++*p_format;
264 return v;
267 static PyObject *
268 do_mkvalue(p_format, p_va)
269 char **p_format;
270 va_list *p_va;
272 for (;;) {
273 switch (*(*p_format)++) {
274 case '(':
275 return do_mktuple(p_format, p_va, ')',
276 countformat(*p_format, ')'));
278 case '[':
279 return do_mklist(p_format, p_va, ']',
280 countformat(*p_format, ']'));
282 case '{':
283 return do_mkdict(p_format, p_va, '}',
284 countformat(*p_format, '}'));
286 case 'b':
287 case 'h':
288 case 'i':
289 return PyInt_FromLong((long)va_arg(*p_va, int));
291 case 'l':
292 return PyInt_FromLong((long)va_arg(*p_va, long));
294 #if HAVE_LONG_LONG
295 case 'L':
296 return PyLong_FromLongLong((LONG_LONG)va_arg(*p_va, LONG_LONG));
297 #endif
299 case 'f':
300 case 'd':
301 return PyFloat_FromDouble(
302 (double)va_arg(*p_va, va_double));
304 case 'c':
306 char p[1];
307 p[0] = va_arg(*p_va, int);
308 return PyString_FromStringAndSize(p, 1);
311 case 's':
312 case 'z':
314 PyObject *v;
315 char *str = va_arg(*p_va, char *);
316 int n;
317 if (**p_format == '#') {
318 ++*p_format;
319 n = va_arg(*p_va, int);
321 else
322 n = -1;
323 if (str == NULL) {
324 v = Py_None;
325 Py_INCREF(v);
327 else {
328 if (n < 0)
329 n = strlen(str);
330 v = PyString_FromStringAndSize(str, n);
332 return v;
335 case 'S':
336 case 'O':
337 if (**p_format == '&') {
338 typedef PyObject *(*converter) Py_PROTO((void *));
339 converter func = va_arg(*p_va, converter);
340 void *arg = va_arg(*p_va, void *);
341 ++*p_format;
342 return (*func)(arg);
344 else {
345 PyObject *v;
346 v = va_arg(*p_va, PyObject *);
347 if (v != NULL)
348 Py_INCREF(v);
349 else if (!PyErr_Occurred())
350 /* If a NULL was passed
351 * because a call that should
352 * have constructed a value
353 * failed, that's OK, and we
354 * pass the error on; but if
355 * no error occurred it's not
356 * clear that the caller knew
357 * what she was doing. */
358 PyErr_SetString(PyExc_SystemError,
359 "NULL object passed to Py_BuildValue");
360 return v;
363 case ':':
364 case ',':
365 case ' ':
366 case '\t':
367 break;
369 default:
370 PyErr_SetString(PyExc_SystemError,
371 "bad format char passed to Py_BuildValue");
372 return NULL;
379 #ifdef HAVE_STDARG_PROTOTYPES
380 /* VARARGS 2 */
381 PyObject *Py_BuildValue(char *format, ...)
382 #else
383 /* VARARGS */
384 PyObject *Py_BuildValue(va_alist) va_dcl
385 #endif
387 va_list va;
388 PyObject* retval;
389 #ifdef HAVE_STDARG_PROTOTYPES
390 va_start(va, format);
391 #else
392 char *format;
393 va_start(va);
394 format = va_arg(va, char *);
395 #endif
396 retval = Py_VaBuildValue(format, va);
397 va_end(va);
398 return retval;
401 PyObject *
402 Py_VaBuildValue(format, va)
403 char *format;
404 va_list va;
406 char *f = format;
407 int n = countformat(f, '\0');
408 va_list lva;
410 #ifdef VA_LIST_IS_ARRAY
411 memcpy(lva, va, sizeof(va_list));
412 #else
413 lva = va;
414 #endif
416 if (n < 0)
417 return NULL;
418 if (n == 0) {
419 Py_INCREF(Py_None);
420 return Py_None;
422 if (n == 1)
423 return do_mkvalue(&f, &lva);
424 return do_mktuple(&f, &lva, '\0', n);
428 #ifdef HAVE_STDARG_PROTOTYPES
429 PyObject *
430 PyEval_CallFunction(PyObject *obj, char *format, ...)
431 #else
432 PyObject *
433 PyEval_CallFunction(obj, format, va_alist)
434 PyObject *obj;
435 char *format;
436 va_dcl
437 #endif
439 va_list vargs;
440 PyObject *args;
441 PyObject *res;
443 #ifdef HAVE_STDARG_PROTOTYPES
444 va_start(vargs, format);
445 #else
446 va_start(vargs);
447 #endif
449 args = Py_VaBuildValue(format, vargs);
450 va_end(vargs);
452 if (args == NULL)
453 return NULL;
455 res = PyEval_CallObject(obj, args);
456 Py_DECREF(args);
458 return res;
462 #ifdef HAVE_STDARG_PROTOTYPES
463 PyObject *
464 PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
465 #else
466 PyObject *
467 PyEval_CallMethod(obj, methodname, format, va_alist)
468 PyObject *obj;
469 char *methodname;
470 char *format;
471 va_dcl
472 #endif
474 va_list vargs;
475 PyObject *meth;
476 PyObject *args;
477 PyObject *res;
479 meth = PyObject_GetAttrString(obj, methodname);
480 if (meth == NULL)
481 return NULL;
483 #ifdef HAVE_STDARG_PROTOTYPES
484 va_start(vargs, format);
485 #else
486 va_start(vargs);
487 #endif
489 args = Py_VaBuildValue(format, vargs);
490 va_end(vargs);
492 if (args == NULL) {
493 Py_DECREF(meth);
494 return NULL;
497 res = PyEval_CallObject(meth, args);
498 Py_DECREF(meth);
499 Py_DECREF(args);
501 return res;