Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Python / modsupport.c
blobf86ca912d732fbd1553ee069e4f3c2b9fc7731f6
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 #ifdef 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 'N':
336 case 'S':
337 case 'O':
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 *);
342 ++*p_format;
343 return (*func)(arg);
345 else {
346 PyObject *v;
347 v = va_arg(*p_va, PyObject *);
348 if (v != NULL) {
349 if (*(*p_format - 1) != 'N')
350 Py_INCREF(v);
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");
363 return v;
366 case ':':
367 case ',':
368 case ' ':
369 case '\t':
370 break;
372 default:
373 PyErr_SetString(PyExc_SystemError,
374 "bad format char passed to Py_BuildValue");
375 return NULL;
382 #ifdef HAVE_STDARG_PROTOTYPES
383 /* VARARGS 2 */
384 PyObject *Py_BuildValue(char *format, ...)
385 #else
386 /* VARARGS */
387 PyObject *Py_BuildValue(va_alist) va_dcl
388 #endif
390 va_list va;
391 PyObject* retval;
392 #ifdef HAVE_STDARG_PROTOTYPES
393 va_start(va, format);
394 #else
395 char *format;
396 va_start(va);
397 format = va_arg(va, char *);
398 #endif
399 retval = Py_VaBuildValue(format, va);
400 va_end(va);
401 return retval;
404 PyObject *
405 Py_VaBuildValue(format, va)
406 char *format;
407 va_list va;
409 char *f = format;
410 int n = countformat(f, '\0');
411 va_list lva;
413 #ifdef VA_LIST_IS_ARRAY
414 memcpy(lva, va, sizeof(va_list));
415 #else
416 lva = va;
417 #endif
419 if (n < 0)
420 return NULL;
421 if (n == 0) {
422 Py_INCREF(Py_None);
423 return Py_None;
425 if (n == 1)
426 return do_mkvalue(&f, &lva);
427 return do_mktuple(&f, &lva, '\0', n);
431 #ifdef HAVE_STDARG_PROTOTYPES
432 PyObject *
433 PyEval_CallFunction(PyObject *obj, char *format, ...)
434 #else
435 PyObject *
436 PyEval_CallFunction(obj, format, va_alist)
437 PyObject *obj;
438 char *format;
439 va_dcl
440 #endif
442 va_list vargs;
443 PyObject *args;
444 PyObject *res;
446 #ifdef HAVE_STDARG_PROTOTYPES
447 va_start(vargs, format);
448 #else
449 va_start(vargs);
450 #endif
452 args = Py_VaBuildValue(format, vargs);
453 va_end(vargs);
455 if (args == NULL)
456 return NULL;
458 res = PyEval_CallObject(obj, args);
459 Py_DECREF(args);
461 return res;
465 #ifdef HAVE_STDARG_PROTOTYPES
466 PyObject *
467 PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
468 #else
469 PyObject *
470 PyEval_CallMethod(obj, methodname, format, va_alist)
471 PyObject *obj;
472 char *methodname;
473 char *format;
474 va_dcl
475 #endif
477 va_list vargs;
478 PyObject *meth;
479 PyObject *args;
480 PyObject *res;
482 meth = PyObject_GetAttrString(obj, methodname);
483 if (meth == NULL)
484 return NULL;
486 #ifdef HAVE_STDARG_PROTOTYPES
487 va_start(vargs, format);
488 #else
489 va_start(vargs);
490 #endif
492 args = Py_VaBuildValue(format, vargs);
493 va_end(vargs);
495 if (args == NULL) {
496 Py_DECREF(meth);
497 return NULL;
500 res = PyEval_CallObject(meth, args);
501 Py_DECREF(meth);
502 Py_DECREF(args);
504 return res;