blob: 65383233712a2070d635b17c493c0e5be14d305d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Module support implementation */
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035#include "import.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
Guido van Rossum1d5735e1994-08-30 08:27:36 +000037#ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
38typedef extended va_double;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000039#else
Guido van Rossum1d5735e1994-08-30 08:27:36 +000040typedef double va_double;
41#endif
42
Guido van Rossum970a0a21995-01-09 17:47:20 +000043/* initmodule4() parameters:
44 - name is the module name
45 - methods is the list of top-level functions
46 - doc is the documentation string
47 - passthrough is passed as self to functions defined in the module
48 - api_version is the value of PYTHON_API_VERSION at the time the
49 module was compiled
Guido van Rossum50620fa1995-01-07 12:43:18 +000050*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +000051
Guido van Rossum970a0a21995-01-09 17:47:20 +000052static char api_version_warning[] =
53"WARNING: Python C API version mismatch for module %s:\n\
Sjoerd Mullender996e6dc1996-01-23 16:07:29 +000054 This Python has API version %d, module %s has version %d.\n";
Guido van Rossum970a0a21995-01-09 17:47:20 +000055
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056object *
Guido van Rossum970a0a21995-01-09 17:47:20 +000057initmodule4(name, methods, doc, passthrough, module_api_version)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 char *name;
59 struct methodlist *methods;
Guido van Rossum50620fa1995-01-07 12:43:18 +000060 char *doc;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061 object *passthrough;
Guido van Rossum970a0a21995-01-09 17:47:20 +000062 int module_api_version;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063{
64 object *m, *d, *v;
65 struct methodlist *ml;
Guido van Rossum970a0a21995-01-09 17:47:20 +000066 if (module_api_version != PYTHON_API_VERSION)
67 fprintf(stderr, api_version_warning,
68 name, PYTHON_API_VERSION, name, module_api_version);
Guido van Rossum3f5da241990-12-20 15:06:42 +000069 if ((m = add_module(name)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 fprintf(stderr, "initializing module: %s\n", name);
71 fatal("can't create a module");
72 }
73 d = getmoduledict(m);
74 for (ml = methods; ml->ml_name != NULL; ml++) {
Guido van Rossum50620fa1995-01-07 12:43:18 +000075 v = newmethodobject(ml, passthrough);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 if (v == NULL || dictinsert(d, ml->ml_name, v) != 0) {
77 fprintf(stderr, "initializing module: %s\n", name);
78 fatal("can't initialize module");
79 }
80 DECREF(v);
81 }
Guido van Rossum50620fa1995-01-07 12:43:18 +000082 if (doc != NULL) {
83 v = newstringobject(doc);
84 if (v == NULL || dictinsert(d, "__doc__", v) != 0)
85 fatal("can't add doc string");
86 DECREF(v);
87 }
Guido van Rossum3f5da241990-12-20 15:06:42 +000088 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089}
90
91
Guido van Rossumbf80e541993-02-08 15:49:17 +000092/* Helper for mkvalue() to scan the length of a format */
Guido van Rossumfc61adb1992-04-13 15:53:41 +000093
94static int countformat PROTO((char *format, int endchar));
95static int countformat(format, endchar)
96 char *format;
97 int endchar;
98{
99 int count = 0;
100 int level = 0;
101 while (level > 0 || *format != endchar) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000102 switch (*format) {
103 case '\0':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000104 /* Premature end */
105 err_setstr(SystemError, "unmatched paren in format");
106 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000107 case '(':
108 case '[':
109 case '{':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000110 if (level == 0)
111 count++;
112 level++;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000113 break;
114 case ')':
115 case ']':
116 case '}':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000117 level--;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000118 break;
119 case '#':
Guido van Rossumd1b93931995-01-20 16:56:02 +0000120 case '&':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000121 case ',':
122 case ':':
123 case ' ':
124 case '\t':
125 break;
126 default:
127 if (level == 0)
128 count++;
129 }
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000130 format++;
131 }
132 return count;
133}
134
135
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000136/* Generic function to create a value -- the inverse of getargs() */
137/* After an original idea and first implementation by Steven Miale */
138
139static object *do_mktuple PROTO((char**, va_list *, int, int));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000140static object *do_mklist PROTO((char**, va_list *, int, int));
141static object *do_mkdict PROTO((char**, va_list *, int, int));
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000142static object *do_mkvalue PROTO((char**, va_list *));
143
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000144
145static object *
146do_mkdict(p_format, p_va, endchar, n)
147 char **p_format;
148 va_list *p_va;
149 int endchar;
150 int n;
151{
152 object *d;
153 int i;
154 if (n < 0)
155 return NULL;
156 if ((d = newdictobject()) == NULL)
157 return NULL;
158 for (i = 0; i < n; i+= 2) {
159 object *k, *v;
160 k = do_mkvalue(p_format, p_va);
161 if (k == NULL) {
162 DECREF(d);
163 return NULL;
164 }
165 v = do_mkvalue(p_format, p_va);
166 if (v == NULL) {
167 DECREF(k);
168 DECREF(d);
169 return NULL;
170 }
171 if (dict2insert(d, k, v) < 0) {
172 DECREF(k);
173 DECREF(v);
174 DECREF(d);
175 return NULL;
176 }
177 }
178 if (d != NULL && **p_format != endchar) {
179 DECREF(d);
180 d = NULL;
181 err_setstr(SystemError, "Unmatched paren in format");
182 }
183 else if (endchar)
184 ++*p_format;
185 return d;
186}
187
188static object *
189do_mklist(p_format, p_va, endchar, n)
190 char **p_format;
191 va_list *p_va;
192 int endchar;
193 int n;
194{
195 object *v;
196 int i;
197 if (n < 0)
198 return NULL;
199 if ((v = newlistobject(n)) == NULL)
200 return NULL;
201 for (i = 0; i < n; i++) {
202 object *w = do_mkvalue(p_format, p_va);
203 if (w == NULL) {
204 DECREF(v);
205 return NULL;
206 }
207 setlistitem(v, i, w);
208 }
209 if (v != NULL && **p_format != endchar) {
210 DECREF(v);
211 v = NULL;
212 err_setstr(SystemError, "Unmatched paren in format");
213 }
214 else if (endchar)
215 ++*p_format;
216 return v;
217}
218
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000219static object *
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000220do_mktuple(p_format, p_va, endchar, n)
221 char **p_format;
222 va_list *p_va;
223 int endchar;
224 int n;
225{
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000226 object *v;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000227 int i;
228 if (n < 0)
229 return NULL;
230 if ((v = newtupleobject(n)) == NULL)
231 return NULL;
232 for (i = 0; i < n; i++) {
233 object *w = do_mkvalue(p_format, p_va);
234 if (w == NULL) {
235 DECREF(v);
236 return NULL;
237 }
238 settupleitem(v, i, w);
239 }
240 if (v != NULL && **p_format != endchar) {
241 DECREF(v);
242 v = NULL;
243 err_setstr(SystemError, "Unmatched paren in format");
244 }
245 else if (endchar)
246 ++*p_format;
247 return v;
248}
249
250static object *
Guido van Rossum899dcf31992-05-15 11:04:59 +0000251do_mkvalue(p_format, p_va)
252 char **p_format;
253 va_list *p_va;
254{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000255 for (;;) {
256 switch (*(*p_format)++) {
257 case '(':
258 return do_mktuple(p_format, p_va, ')',
259 countformat(*p_format, ')'));
260
261 case '[':
262 return do_mklist(p_format, p_va, ']',
263 countformat(*p_format, ']'));
264
265 case '{':
266 return do_mkdict(p_format, p_va, '}',
267 countformat(*p_format, '}'));
268
269 case 'b':
270 case 'h':
271 case 'i':
272 return newintobject((long)va_arg(*p_va, int));
273
274 case 'l':
275 return newintobject((long)va_arg(*p_va, long));
276
277 case 'f':
278 case 'd':
Guido van Rossumd1b93931995-01-20 16:56:02 +0000279 return newfloatobject((double)va_arg(*p_va, va_double));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000280
281 case 'c':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000282 {
283 char p[1];
284 p[0] = va_arg(*p_va, int);
285 return newsizedstringobject(p, 1);
286 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000287
288 case 's':
289 case 'z':
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000290 {
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000291 object *v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000292 char *str = va_arg(*p_va, char *);
293 int n;
294 if (**p_format == '#') {
295 ++*p_format;
296 n = va_arg(*p_va, int);
297 }
298 else
299 n = -1;
300 if (str == NULL) {
301 v = None;
302 INCREF(v);
303 }
304 else {
305 if (n < 0)
306 n = strlen(str);
307 v = newsizedstringobject(str, n);
308 }
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000309 return v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000310 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000311
312 case 'S':
313 case 'O':
Guido van Rossumd1b93931995-01-20 16:56:02 +0000314 if (**p_format == '&') {
Guido van Rossum59461c71995-03-09 12:10:50 +0000315 typedef object *(*converter) PROTO((void *));
Guido van Rossumd1b93931995-01-20 16:56:02 +0000316 converter func = va_arg(*p_va, converter);
317 void *arg = va_arg(*p_va, void *);
318 ++*p_format;
319 return (*func)(arg);
320 }
321 else {
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000322 object *v;
323 v = va_arg(*p_va, object *);
324 if (v != NULL)
325 INCREF(v);
326 else if (!err_occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000327 /* If a NULL was passed
328 * because a call that should
329 * have constructed a value
330 * failed, that's OK, and we
331 * pass the error on; but if
332 * no error occurred it's not
333 * clear that the caller knew
334 * what she was doing. */
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000335 err_setstr(SystemError,
336 "NULL object passed to mkvalue");
337 return v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000338 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000339
340 case ':':
341 case ',':
342 case ' ':
343 case '\t':
344 break;
345
346 default:
347 err_setstr(SystemError,
348 "bad format char passed to mkvalue");
349 return NULL;
350
351 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000352 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000353}
354
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000355
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000356#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000357/* VARARGS 2 */
358object *mkvalue(char *format, ...)
359#else
360/* VARARGS */
361object *mkvalue(va_alist) va_dcl
362#endif
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000363{
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000364 va_list va;
365 object* retval;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000366#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000367 va_start(va, format);
368#else
369 char *format;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000370 va_start(va);
371 format = va_arg(va, char *);
372#endif
Guido van Rossume5372401993-03-16 12:15:04 +0000373 retval = vmkvalue(format, va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000374 va_end(va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000375 return retval;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000376}
Guido van Rossume5372401993-03-16 12:15:04 +0000377
378object *
379vmkvalue(format, va)
380 char *format;
381 va_list va;
382{
383 char *f = format;
384 int n = countformat(f, '\0');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000385 va_list lva;
386
387#ifdef VA_LIST_IS_ARRAY
388 memcpy(lva, va, sizeof(va_list));
389#else
390 lva = va;
391#endif
392
Guido van Rossume5372401993-03-16 12:15:04 +0000393 if (n < 0)
394 return NULL;
395 if (n == 0) {
396 INCREF(None);
397 return None;
398 }
399 if (n == 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000400 return do_mkvalue(&f, &lva);
401 return do_mktuple(&f, &lva, '\0', n);
402}
403
404
405#ifdef HAVE_STDARG_PROTOTYPES
406object *
407PyEval_CallFunction(object *obj, char *format, ...)
408#else
409object *
410PyEval_CallFunction(obj, format, va_alist)
411 object *obj;
412 char *format;
413 va_dcl
414#endif
415{
416 va_list vargs;
417 object *args;
418 object *res;
419
420#ifdef HAVE_STDARG_PROTOTYPES
421 va_start(vargs, format);
422#else
423 va_start(vargs);
424#endif
425
426 args = vmkvalue(format, vargs);
427 va_end(vargs);
428
429 if (args == NULL)
430 return NULL;
431
432 res = call_object(obj, args);
433 DECREF(args);
434
435 return res;
436}
437
438
439#ifdef HAVE_STDARG_PROTOTYPES
440object *
Guido van Rossum2ac3bc21995-01-17 16:31:01 +0000441PyEval_CallMethod(object *obj, char *methonname, char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000442#else
443object *
Guido van Rossum2ac3bc21995-01-17 16:31:01 +0000444PyEval_CallMethod(obj, methonname, format, va_alist)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000445 object *obj;
Guido van Rossum2ac3bc21995-01-17 16:31:01 +0000446 char *methonname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000447 char *format;
448 va_dcl
449#endif
450{
451 va_list vargs;
452 object *meth;
453 object *args;
454 object *res;
455
Guido van Rossum2ac3bc21995-01-17 16:31:01 +0000456 meth = getattr(obj, methonname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000457 if (meth == NULL)
458 return NULL;
459
460#ifdef HAVE_STDARG_PROTOTYPES
461 va_start(vargs, format);
462#else
463 va_start(vargs);
464#endif
465
466 args = vmkvalue(format, vargs);
467 va_end(vargs);
468
469 if (args == NULL) {
470 DECREF(meth);
471 return NULL;
472 }
473
474 res = call_object(meth, args);
475 DECREF(meth);
476 DECREF(args);
477
478 return res;
Guido van Rossume5372401993-03-16 12:15:04 +0000479}