Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
| 20 | |
| 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. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Module support implementation */ |
| 33 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 34 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | #include "import.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 37 | #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */ |
| 38 | typedef extended va_double; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 39 | #else |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 40 | typedef double va_double; |
| 41 | #endif |
| 42 | |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 43 | /* 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 Rossum | 50620fa | 1995-01-07 12:43:18 +0000 | [diff] [blame] | 50 | */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 52 | static char api_version_warning[] = |
| 53 | "WARNING: Python C API version mismatch for module %s:\n\ |
Sjoerd Mullender | 996e6dc | 1996-01-23 16:07:29 +0000 | [diff] [blame] | 54 | This Python has API version %d, module %s has version %d.\n"; |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | object * |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 57 | initmodule4(name, methods, doc, passthrough, module_api_version) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | char *name; |
| 59 | struct methodlist *methods; |
Guido van Rossum | 50620fa | 1995-01-07 12:43:18 +0000 | [diff] [blame] | 60 | char *doc; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 61 | object *passthrough; |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 62 | int module_api_version; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 63 | { |
| 64 | object *m, *d, *v; |
| 65 | struct methodlist *ml; |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 66 | if (module_api_version != PYTHON_API_VERSION) |
| 67 | fprintf(stderr, api_version_warning, |
| 68 | name, PYTHON_API_VERSION, name, module_api_version); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 69 | if ((m = add_module(name)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | 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 Rossum | 50620fa | 1995-01-07 12:43:18 +0000 | [diff] [blame] | 75 | v = newmethodobject(ml, passthrough); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | 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 Rossum | 50620fa | 1995-01-07 12:43:18 +0000 | [diff] [blame] | 82 | 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 88 | return m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 92 | /* Helper for mkvalue() to scan the length of a format */ |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 93 | |
| 94 | static int countformat PROTO((char *format, int endchar)); |
| 95 | static 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 Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 102 | switch (*format) { |
| 103 | case '\0': |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 104 | /* Premature end */ |
| 105 | err_setstr(SystemError, "unmatched paren in format"); |
| 106 | return -1; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 107 | case '(': |
| 108 | case '[': |
| 109 | case '{': |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 110 | if (level == 0) |
| 111 | count++; |
| 112 | level++; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 113 | break; |
| 114 | case ')': |
| 115 | case ']': |
| 116 | case '}': |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 117 | level--; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 118 | break; |
| 119 | case '#': |
Guido van Rossum | d1b9393 | 1995-01-20 16:56:02 +0000 | [diff] [blame] | 120 | case '&': |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 121 | case ',': |
| 122 | case ':': |
| 123 | case ' ': |
| 124 | case '\t': |
| 125 | break; |
| 126 | default: |
| 127 | if (level == 0) |
| 128 | count++; |
| 129 | } |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 130 | format++; |
| 131 | } |
| 132 | return count; |
| 133 | } |
| 134 | |
| 135 | |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 136 | /* Generic function to create a value -- the inverse of getargs() */ |
| 137 | /* After an original idea and first implementation by Steven Miale */ |
| 138 | |
| 139 | static object *do_mktuple PROTO((char**, va_list *, int, int)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 140 | static object *do_mklist PROTO((char**, va_list *, int, int)); |
| 141 | static object *do_mkdict PROTO((char**, va_list *, int, int)); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 142 | static object *do_mkvalue PROTO((char**, va_list *)); |
| 143 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 144 | |
| 145 | static object * |
| 146 | do_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 | |
| 188 | static object * |
| 189 | do_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 Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 219 | static object * |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 220 | do_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 Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 226 | object *v; |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 227 | 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 | |
| 250 | static object * |
Guido van Rossum | 899dcf3 | 1992-05-15 11:04:59 +0000 | [diff] [blame] | 251 | do_mkvalue(p_format, p_va) |
| 252 | char **p_format; |
| 253 | va_list *p_va; |
| 254 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 255 | 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 Rossum | d1b9393 | 1995-01-20 16:56:02 +0000 | [diff] [blame] | 279 | return newfloatobject((double)va_arg(*p_va, va_double)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 280 | |
| 281 | case 'c': |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 282 | { |
| 283 | char p[1]; |
| 284 | p[0] = va_arg(*p_va, int); |
| 285 | return newsizedstringobject(p, 1); |
| 286 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 287 | |
| 288 | case 's': |
| 289 | case 'z': |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 290 | { |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 291 | object *v; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 292 | 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 Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 309 | return v; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 310 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 311 | |
| 312 | case 'S': |
| 313 | case 'O': |
Guido van Rossum | d1b9393 | 1995-01-20 16:56:02 +0000 | [diff] [blame] | 314 | if (**p_format == '&') { |
Guido van Rossum | 59461c7 | 1995-03-09 12:10:50 +0000 | [diff] [blame] | 315 | typedef object *(*converter) PROTO((void *)); |
Guido van Rossum | d1b9393 | 1995-01-20 16:56:02 +0000 | [diff] [blame] | 316 | 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 Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 322 | object *v; |
| 323 | v = va_arg(*p_va, object *); |
| 324 | if (v != NULL) |
| 325 | INCREF(v); |
| 326 | else if (!err_occurred()) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 327 | /* 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 Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 335 | err_setstr(SystemError, |
| 336 | "NULL object passed to mkvalue"); |
| 337 | return v; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 338 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 339 | |
| 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 Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 352 | } |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 356 | #ifdef HAVE_STDARG_PROTOTYPES |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 357 | /* VARARGS 2 */ |
| 358 | object *mkvalue(char *format, ...) |
| 359 | #else |
| 360 | /* VARARGS */ |
| 361 | object *mkvalue(va_alist) va_dcl |
| 362 | #endif |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 363 | { |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 364 | va_list va; |
| 365 | object* retval; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 366 | #ifdef HAVE_STDARG_PROTOTYPES |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 367 | va_start(va, format); |
| 368 | #else |
| 369 | char *format; |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 370 | va_start(va); |
| 371 | format = va_arg(va, char *); |
| 372 | #endif |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 373 | retval = vmkvalue(format, va); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 374 | va_end(va); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 375 | return retval; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 376 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 377 | |
| 378 | object * |
| 379 | vmkvalue(format, va) |
| 380 | char *format; |
| 381 | va_list va; |
| 382 | { |
| 383 | char *f = format; |
| 384 | int n = countformat(f, '\0'); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 385 | 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 Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 393 | if (n < 0) |
| 394 | return NULL; |
| 395 | if (n == 0) { |
| 396 | INCREF(None); |
| 397 | return None; |
| 398 | } |
| 399 | if (n == 1) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 400 | return do_mkvalue(&f, &lva); |
| 401 | return do_mktuple(&f, &lva, '\0', n); |
| 402 | } |
| 403 | |
| 404 | |
| 405 | #ifdef HAVE_STDARG_PROTOTYPES |
| 406 | object * |
| 407 | PyEval_CallFunction(object *obj, char *format, ...) |
| 408 | #else |
| 409 | object * |
| 410 | PyEval_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 |
| 440 | object * |
Guido van Rossum | 2ac3bc2 | 1995-01-17 16:31:01 +0000 | [diff] [blame] | 441 | PyEval_CallMethod(object *obj, char *methonname, char *format, ...) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 442 | #else |
| 443 | object * |
Guido van Rossum | 2ac3bc2 | 1995-01-17 16:31:01 +0000 | [diff] [blame] | 444 | PyEval_CallMethod(obj, methonname, format, va_alist) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 445 | object *obj; |
Guido van Rossum | 2ac3bc2 | 1995-01-17 16:31:01 +0000 | [diff] [blame] | 446 | char *methonname; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 447 | char *format; |
| 448 | va_dcl |
| 449 | #endif |
| 450 | { |
| 451 | va_list vargs; |
| 452 | object *meth; |
| 453 | object *args; |
| 454 | object *res; |
| 455 | |
Guido van Rossum | 2ac3bc2 | 1995-01-17 16:31:01 +0000 | [diff] [blame] | 456 | meth = getattr(obj, methonname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 457 | 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 Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 479 | } |