blob: 9c2dc18a4516e029f7847a98393fc9d1dde5e3d3 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module support implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Jack Jansen41aa8e52000-07-03 21:39:47 +00005#ifdef HAVE_LIMITS_H
6#include <limits.h>
7#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Guido van Rossum1d5735e1994-08-30 08:27:36 +00009#ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
10typedef extended va_double;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000011#else
Guido van Rossum1d5735e1994-08-30 08:27:36 +000012typedef double va_double;
13#endif
14
Guido van Rossum2e58ff31997-11-19 18:53:33 +000015/* Package context -- the full module name for package imports */
16char *_Py_PackageContext = NULL;
17
Guido van Rossum40b33c61997-08-02 03:07:46 +000018/* Py_InitModule4() parameters:
Guido van Rossum970a0a21995-01-09 17:47:20 +000019 - name is the module name
20 - methods is the list of top-level functions
21 - doc is the documentation string
22 - passthrough is passed as self to functions defined in the module
23 - api_version is the value of PYTHON_API_VERSION at the time the
24 module was compiled
Guido van Rossum40b33c61997-08-02 03:07:46 +000025
26 Return value is a borrowed reference to the module object; or NULL
27 if an error occurred (in Python 1.4 and before, errors were fatal).
28 Errors may still leak memory.
Guido van Rossum50620fa1995-01-07 12:43:18 +000029*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +000030
Guido van Rossum970a0a21995-01-09 17:47:20 +000031static char api_version_warning[] =
32"WARNING: Python C API version mismatch for module %s:\n\
Sjoerd Mullender996e6dc1996-01-23 16:07:29 +000033 This Python has API version %d, module %s has version %d.\n";
Guido van Rossum970a0a21995-01-09 17:47:20 +000034
Guido van Rossum79f25d91997-04-29 20:08:16 +000035PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000036Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
37 PyObject *passthrough, int module_api_version)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038{
Guido van Rossum79f25d91997-04-29 20:08:16 +000039 PyObject *m, *d, *v;
40 PyMethodDef *ml;
Guido van Rossum413407f2000-08-04 14:00:14 +000041 if (!Py_IsInitialized())
42 Py_FatalError("Interpreter not initialized (version mismatch?)");
Guido van Rossum970a0a21995-01-09 17:47:20 +000043 if (module_api_version != PYTHON_API_VERSION)
44 fprintf(stderr, api_version_warning,
45 name, PYTHON_API_VERSION, name, module_api_version);
Guido van Rossum2e58ff31997-11-19 18:53:33 +000046 if (_Py_PackageContext != NULL) {
47 char *p = strrchr(_Py_PackageContext, '.');
48 if (p != NULL && strcmp(name, p+1) == 0) {
49 name = _Py_PackageContext;
50 _Py_PackageContext = NULL;
51 }
52 }
Guido van Rossum40b33c61997-08-02 03:07:46 +000053 if ((m = PyImport_AddModule(name)) == NULL)
54 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +000055 d = PyModule_GetDict(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056 for (ml = methods; ml->ml_name != NULL; ml++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +000057 v = PyCFunction_New(ml, passthrough);
Guido van Rossum40b33c61997-08-02 03:07:46 +000058 if (v == NULL)
59 return NULL;
60 if (PyDict_SetItemString(d, ml->ml_name, v) != 0)
61 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +000062 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063 }
Guido van Rossum50620fa1995-01-07 12:43:18 +000064 if (doc != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +000065 v = PyString_FromString(doc);
66 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0)
Guido van Rossum40b33c61997-08-02 03:07:46 +000067 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +000068 Py_DECREF(v);
Guido van Rossum50620fa1995-01-07 12:43:18 +000069 }
Guido van Rossum3f5da241990-12-20 15:06:42 +000070 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071}
72
73
Guido van Rossumbf80e541993-02-08 15:49:17 +000074/* Helper for mkvalue() to scan the length of a format */
Guido van Rossumfc61adb1992-04-13 15:53:41 +000075
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000076static int countformat(char *format, int endchar)
Guido van Rossumfc61adb1992-04-13 15:53:41 +000077{
78 int count = 0;
79 int level = 0;
80 while (level > 0 || *format != endchar) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +000081 switch (*format) {
82 case '\0':
Guido van Rossumfc61adb1992-04-13 15:53:41 +000083 /* Premature end */
Guido van Rossum79f25d91997-04-29 20:08:16 +000084 PyErr_SetString(PyExc_SystemError,
85 "unmatched paren in format");
Guido van Rossumfc61adb1992-04-13 15:53:41 +000086 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000087 case '(':
88 case '[':
89 case '{':
Guido van Rossumfc61adb1992-04-13 15:53:41 +000090 if (level == 0)
91 count++;
92 level++;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000093 break;
94 case ')':
95 case ']':
96 case '}':
Guido van Rossumfc61adb1992-04-13 15:53:41 +000097 level--;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000098 break;
99 case '#':
Guido van Rossumd1b93931995-01-20 16:56:02 +0000100 case '&':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000101 case ',':
102 case ':':
103 case ' ':
104 case '\t':
105 break;
106 default:
107 if (level == 0)
108 count++;
109 }
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000110 format++;
111 }
112 return count;
113}
114
115
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000116/* Generic function to create a value -- the inverse of getargs() */
117/* After an original idea and first implementation by Steven Miale */
118
Tim Petersdbd9ba62000-07-09 03:09:57 +0000119static PyObject *do_mktuple(char**, va_list *, int, int);
120static PyObject *do_mklist(char**, va_list *, int, int);
121static PyObject *do_mkdict(char**, va_list *, int, int);
122static PyObject *do_mkvalue(char**, va_list *);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000123
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000124
Guido van Rossum79f25d91997-04-29 20:08:16 +0000125static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000126do_mkdict(char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000127{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000128 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000129 int i;
130 if (n < 0)
131 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000132 if ((d = PyDict_New()) == NULL)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000133 return NULL;
134 for (i = 0; i < n; i+= 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000135 PyObject *k, *v;
Guido van Rossumdb847bd1997-11-20 20:35:45 +0000136 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000137 k = do_mkvalue(p_format, p_va);
138 if (k == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000139 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000140 return NULL;
141 }
142 v = do_mkvalue(p_format, p_va);
143 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000144 Py_DECREF(k);
145 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000146 return NULL;
147 }
Guido van Rossumdb847bd1997-11-20 20:35:45 +0000148 err = PyDict_SetItem(d, k, v);
149 Py_DECREF(k);
150 Py_DECREF(v);
151 if (err < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000152 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000153 return NULL;
154 }
155 }
156 if (d != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000157 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000158 d = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000159 PyErr_SetString(PyExc_SystemError,
160 "Unmatched paren in format");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000161 }
162 else if (endchar)
163 ++*p_format;
164 return d;
165}
166
Guido van Rossum79f25d91997-04-29 20:08:16 +0000167static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000168do_mklist(char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000169{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000170 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000171 int i;
172 if (n < 0)
173 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000174 if ((v = PyList_New(n)) == NULL)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000175 return NULL;
176 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000177 PyObject *w = do_mkvalue(p_format, p_va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000178 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000179 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000180 return NULL;
181 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000182 PyList_SetItem(v, i, w);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000183 }
184 if (v != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000185 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000186 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000187 PyErr_SetString(PyExc_SystemError,
188 "Unmatched paren in format");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000189 }
190 else if (endchar)
191 ++*p_format;
192 return v;
193}
194
Fred Drake25d34472000-04-28 14:42:37 +0000195static int
196_ustrlen(Py_UNICODE *u)
197{
198 int i = 0;
199 Py_UNICODE *v = u;
200 while (*v != 0) { i++; v++; }
201 return i;
202}
203
Guido van Rossum79f25d91997-04-29 20:08:16 +0000204static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000205do_mktuple(char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000206{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000207 PyObject *v;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000208 int i;
209 if (n < 0)
210 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000211 if ((v = PyTuple_New(n)) == NULL)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000212 return NULL;
213 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000214 PyObject *w = do_mkvalue(p_format, p_va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000215 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000216 Py_DECREF(v);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000217 return NULL;
218 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219 PyTuple_SetItem(v, i, w);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000220 }
221 if (v != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000222 Py_DECREF(v);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000223 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 PyErr_SetString(PyExc_SystemError,
225 "Unmatched paren in format");
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000226 }
227 else if (endchar)
228 ++*p_format;
229 return v;
230}
231
Guido van Rossum79f25d91997-04-29 20:08:16 +0000232static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000233do_mkvalue(char **p_format, va_list *p_va)
Guido van Rossum899dcf31992-05-15 11:04:59 +0000234{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000235 for (;;) {
236 switch (*(*p_format)++) {
237 case '(':
238 return do_mktuple(p_format, p_va, ')',
239 countformat(*p_format, ')'));
240
241 case '[':
242 return do_mklist(p_format, p_va, ']',
243 countformat(*p_format, ']'));
244
245 case '{':
246 return do_mkdict(p_format, p_va, '}',
247 countformat(*p_format, '}'));
248
249 case 'b':
Jack Jansenb763b9d2000-09-15 12:51:01 +0000250 case 'B':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000251 case 'h':
252 case 'i':
Guido van Rossum79f25d91997-04-29 20:08:16 +0000253 return PyInt_FromLong((long)va_arg(*p_va, int));
Jack Jansend50338f2000-07-06 12:22:00 +0000254
255 case 'H':
256 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000257
258 case 'l':
Guido van Rossum79f25d91997-04-29 20:08:16 +0000259 return PyInt_FromLong((long)va_arg(*p_va, long));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000260
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000261#ifdef HAVE_LONG_LONG
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000262 case 'L':
Guido van Rossum3293b071998-08-25 16:07:15 +0000263 return PyLong_FromLongLong((LONG_LONG)va_arg(*p_va, LONG_LONG));
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000264#endif
Fred Drake25d34472000-04-28 14:42:37 +0000265 case 'u':
266 {
267 PyObject *v;
268 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
269 int n;
270 if (**p_format == '#') {
271 ++*p_format;
272 n = va_arg(*p_va, int);
273 }
274 else
275 n = -1;
276 if (u == NULL) {
277 v = Py_None;
278 Py_INCREF(v);
279 }
280 else {
281 if (n < 0)
282 n = _ustrlen(u);
283 v = PyUnicode_FromUnicode(u, n);
284 }
285 return v;
286 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000287 case 'f':
288 case 'd':
Guido van Rossum79f25d91997-04-29 20:08:16 +0000289 return PyFloat_FromDouble(
290 (double)va_arg(*p_va, va_double));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000291
292 case 'c':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000293 {
294 char p[1];
295 p[0] = va_arg(*p_va, int);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000296 return PyString_FromStringAndSize(p, 1);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000297 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000298
299 case 's':
300 case 'z':
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000301 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000302 PyObject *v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000303 char *str = va_arg(*p_va, char *);
304 int n;
305 if (**p_format == '#') {
306 ++*p_format;
307 n = va_arg(*p_va, int);
308 }
309 else
310 n = -1;
311 if (str == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000312 v = Py_None;
313 Py_INCREF(v);
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000314 }
315 else {
Guido van Rossum582acec2000-06-28 22:07:35 +0000316 if (n < 0) {
317 size_t m = strlen(str);
318 if (m > INT_MAX) {
319 PyErr_SetString(PyExc_OverflowError,
320 "string too long for Python string");
321 return NULL;
322 }
323 n = (int)m;
324 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000325 v = PyString_FromStringAndSize(str, n);
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000326 }
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000327 return v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000328 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000329
Guido van Rossumd3415001998-12-23 05:01:38 +0000330 case 'N':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000331 case 'S':
332 case 'O':
Guido van Rossumd1b93931995-01-20 16:56:02 +0000333 if (**p_format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000334 typedef PyObject *(*converter)(void *);
Guido van Rossumd1b93931995-01-20 16:56:02 +0000335 converter func = va_arg(*p_va, converter);
336 void *arg = va_arg(*p_va, void *);
337 ++*p_format;
338 return (*func)(arg);
339 }
340 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000341 PyObject *v;
342 v = va_arg(*p_va, PyObject *);
Guido van Rossumc38e7d41998-12-23 19:53:45 +0000343 if (v != NULL) {
Guido van Rossumd3415001998-12-23 05:01:38 +0000344 if (*(*p_format - 1) != 'N')
345 Py_INCREF(v);
Guido van Rossumc38e7d41998-12-23 19:53:45 +0000346 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000347 else if (!PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000348 /* If a NULL was passed
349 * because a call that should
350 * have constructed a value
351 * failed, that's OK, and we
352 * pass the error on; but if
353 * no error occurred it's not
354 * clear that the caller knew
355 * what she was doing. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000356 PyErr_SetString(PyExc_SystemError,
Guido van Rossum233f4b51998-07-07 22:32:19 +0000357 "NULL object passed to Py_BuildValue");
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000358 return v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000359 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000360
361 case ':':
362 case ',':
363 case ' ':
364 case '\t':
365 break;
366
367 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000368 PyErr_SetString(PyExc_SystemError,
Guido van Rossum233f4b51998-07-07 22:32:19 +0000369 "bad format char passed to Py_BuildValue");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000370 return NULL;
371
372 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000373 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000374}
375
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000376
Guido van Rossum79f25d91997-04-29 20:08:16 +0000377PyObject *Py_BuildValue(char *format, ...)
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000378{
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000379 va_list va;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000380 PyObject* retval;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000381 va_start(va, format);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000382 retval = Py_VaBuildValue(format, va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000383 va_end(va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000384 return retval;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000385}
Guido van Rossume5372401993-03-16 12:15:04 +0000386
Guido van Rossum79f25d91997-04-29 20:08:16 +0000387PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000388Py_VaBuildValue(char *format, va_list va)
Guido van Rossume5372401993-03-16 12:15:04 +0000389{
390 char *f = format;
391 int n = countformat(f, '\0');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000392 va_list lva;
393
394#ifdef VA_LIST_IS_ARRAY
395 memcpy(lva, va, sizeof(va_list));
396#else
397 lva = va;
398#endif
399
Guido van Rossume5372401993-03-16 12:15:04 +0000400 if (n < 0)
401 return NULL;
402 if (n == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000403 Py_INCREF(Py_None);
404 return Py_None;
Guido van Rossume5372401993-03-16 12:15:04 +0000405 }
406 if (n == 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000407 return do_mkvalue(&f, &lva);
408 return do_mktuple(&f, &lva, '\0', n);
409}
410
411
Guido van Rossum79f25d91997-04-29 20:08:16 +0000412PyObject *
413PyEval_CallFunction(PyObject *obj, char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000414{
415 va_list vargs;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000416 PyObject *args;
417 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000418
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000419 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000420
Guido van Rossum79f25d91997-04-29 20:08:16 +0000421 args = Py_VaBuildValue(format, vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000422 va_end(vargs);
423
424 if (args == NULL)
425 return NULL;
426
Guido van Rossum79f25d91997-04-29 20:08:16 +0000427 res = PyEval_CallObject(obj, args);
428 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000429
430 return res;
431}
432
433
Guido van Rossum79f25d91997-04-29 20:08:16 +0000434PyObject *
Guido van Rossumc4099e61998-08-08 20:51:26 +0000435PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000436{
437 va_list vargs;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000438 PyObject *meth;
439 PyObject *args;
440 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000441
Guido van Rossumc4099e61998-08-08 20:51:26 +0000442 meth = PyObject_GetAttrString(obj, methodname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000443 if (meth == NULL)
444 return NULL;
445
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000446 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000447
Guido van Rossum79f25d91997-04-29 20:08:16 +0000448 args = Py_VaBuildValue(format, vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000449 va_end(vargs);
450
451 if (args == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000452 Py_DECREF(meth);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000453 return NULL;
454 }
455
Guido van Rossum79f25d91997-04-29 20:08:16 +0000456 res = PyEval_CallObject(meth, args);
457 Py_DECREF(meth);
458 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000459
460 return res;
Guido van Rossume5372401993-03-16 12:15:04 +0000461}
Fred Drake9e285152000-09-23 03:24:27 +0000462
463int
464PyModule_AddObject(PyObject *m, char *name, PyObject *o)
465{
466 PyObject *dict;
467 if (!PyModule_Check(m) || o == NULL)
468 return -1;
469 dict = PyModule_GetDict(m);
470 if (dict == NULL)
471 return -1;
472 if (PyDict_SetItemString(dict, name, o))
473 return -1;
474 Py_DECREF(o);
475 return 0;
476}
477
478int
479PyModule_AddIntConstant(PyObject *m, char *name, long value)
480{
481 return PyModule_AddObject(m, name, PyInt_FromLong(value));
482}
483
484int
485PyModule_AddStringConstant(PyObject *m, char *name, char *value)
486{
487 return PyModule_AddObject(m, name, PyString_FromString(value));
488}