blob: f26d7b8eca2a181852cb67c6c3495353b9dd8eb9 [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"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum1d5735e1994-08-30 08:27:36 +00006typedef double va_double;
Guido van Rossum1d5735e1994-08-30 08:27:36 +00007
Guido van Rossum2e58ff31997-11-19 18:53:33 +00008/* Package context -- the full module name for package imports */
9char *_Py_PackageContext = NULL;
10
Guido van Rossum40b33c61997-08-02 03:07:46 +000011/* Py_InitModule4() parameters:
Guido van Rossum970a0a21995-01-09 17:47:20 +000012 - name is the module name
13 - methods is the list of top-level functions
14 - doc is the documentation string
15 - passthrough is passed as self to functions defined in the module
16 - api_version is the value of PYTHON_API_VERSION at the time the
17 module was compiled
Guido van Rossum40b33c61997-08-02 03:07:46 +000018
19 Return value is a borrowed reference to the module object; or NULL
20 if an error occurred (in Python 1.4 and before, errors were fatal).
21 Errors may still leak memory.
Guido van Rossum50620fa1995-01-07 12:43:18 +000022*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +000023
Guido van Rossum970a0a21995-01-09 17:47:20 +000024static char api_version_warning[] =
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000025"Python C API version mismatch for module %.100s:\
26 This Python has API version %d, module %.100s has version %d.";
Guido van Rossum970a0a21995-01-09 17:47:20 +000027
Guido van Rossum79f25d91997-04-29 20:08:16 +000028PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000029Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
30 PyObject *passthrough, int module_api_version)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000032 PyObject *m, *d, *v, *n;
Guido van Rossum79f25d91997-04-29 20:08:16 +000033 PyMethodDef *ml;
Guido van Rossum413407f2000-08-04 14:00:14 +000034 if (!Py_IsInitialized())
35 Py_FatalError("Interpreter not initialized (version mismatch?)");
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000036 if (module_api_version != PYTHON_API_VERSION) {
37 char message[512];
38 PyOS_snprintf(message, sizeof(message),
39 api_version_warning, name,
40 PYTHON_API_VERSION, name,
41 module_api_version);
42 if (PyErr_Warn(PyExc_RuntimeWarning, message))
43 return NULL;
44 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000045 /* Make sure name is fully qualified.
46
47 This is a bit of a hack: when the shared library is loaded,
48 the module name is "package.module", but the module calls
49 Py_InitModule*() with just "module" for the name. The shared
50 library loader squirrels away the true name of the module in
51 _Py_PackageContext, and Py_InitModule*() will substitute this
52 (if the name actually matches).
53 */
Guido van Rossum2e58ff31997-11-19 18:53:33 +000054 if (_Py_PackageContext != NULL) {
55 char *p = strrchr(_Py_PackageContext, '.');
56 if (p != NULL && strcmp(name, p+1) == 0) {
57 name = _Py_PackageContext;
58 _Py_PackageContext = NULL;
59 }
60 }
Guido van Rossum40b33c61997-08-02 03:07:46 +000061 if ((m = PyImport_AddModule(name)) == NULL)
62 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +000063 d = PyModule_GetDict(m);
Fred Drake233cc592002-08-14 20:57:56 +000064 if (methods != NULL) {
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000065 n = PyString_FromString(name);
66 if (n == NULL)
67 return NULL;
Fred Drake233cc592002-08-14 20:57:56 +000068 for (ml = methods; ml->ml_name != NULL; ml++) {
69 if ((ml->ml_flags & METH_CLASS) ||
70 (ml->ml_flags & METH_STATIC)) {
71 PyErr_SetString(PyExc_ValueError,
72 "module functions cannot set"
73 " METH_CLASS or METH_STATIC");
74 return NULL;
75 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000076 v = PyCFunction_NewEx(ml, passthrough, n);
Fred Drake233cc592002-08-14 20:57:56 +000077 if (v == NULL)
78 return NULL;
79 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
80 Py_DECREF(v);
81 return NULL;
82 }
Fred Drake289898c2001-08-04 03:11:25 +000083 Py_DECREF(v);
Fred Drake289898c2001-08-04 03:11:25 +000084 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 }
Guido van Rossum50620fa1995-01-07 12:43:18 +000086 if (doc != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +000087 v = PyString_FromString(doc);
Fred Drake289898c2001-08-04 03:11:25 +000088 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
Just van Rossumbbfd8592002-12-15 13:45:32 +000089 Py_XDECREF(v);
Guido van Rossum40b33c61997-08-02 03:07:46 +000090 return NULL;
Fred Drake289898c2001-08-04 03:11:25 +000091 }
Guido van Rossum79f25d91997-04-29 20:08:16 +000092 Py_DECREF(v);
Guido van Rossum50620fa1995-01-07 12:43:18 +000093 }
Guido van Rossum3f5da241990-12-20 15:06:42 +000094 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095}
96
97
Guido van Rossumbf80e541993-02-08 15:49:17 +000098/* Helper for mkvalue() to scan the length of a format */
Guido van Rossumfc61adb1992-04-13 15:53:41 +000099
Fred Drakeceead6d2003-01-30 15:08:25 +0000100static int
101countformat(char *format, int endchar)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000102{
103 int count = 0;
104 int level = 0;
105 while (level > 0 || *format != endchar) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000106 switch (*format) {
107 case '\0':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000108 /* Premature end */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000109 PyErr_SetString(PyExc_SystemError,
110 "unmatched paren in format");
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000111 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000112 case '(':
113 case '[':
114 case '{':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000115 if (level == 0)
116 count++;
117 level++;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000118 break;
119 case ')':
120 case ']':
121 case '}':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000122 level--;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000123 break;
124 case '#':
Guido van Rossumd1b93931995-01-20 16:56:02 +0000125 case '&':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000126 case ',':
127 case ':':
128 case ' ':
129 case '\t':
130 break;
131 default:
132 if (level == 0)
133 count++;
134 }
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000135 format++;
136 }
137 return count;
138}
139
140
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000141/* Generic function to create a value -- the inverse of getargs() */
142/* After an original idea and first implementation by Steven Miale */
143
Tim Petersdbd9ba62000-07-09 03:09:57 +0000144static PyObject *do_mktuple(char**, va_list *, int, int);
145static PyObject *do_mklist(char**, va_list *, int, int);
146static PyObject *do_mkdict(char**, va_list *, int, int);
147static PyObject *do_mkvalue(char**, va_list *);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000148
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000149
Guido van Rossum79f25d91997-04-29 20:08:16 +0000150static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000151do_mkdict(char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000152{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000153 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000154 int i;
155 if (n < 0)
156 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000157 if ((d = PyDict_New()) == NULL)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000158 return NULL;
159 for (i = 0; i < n; i+= 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000160 PyObject *k, *v;
Guido van Rossumdb847bd1997-11-20 20:35:45 +0000161 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000162 k = do_mkvalue(p_format, p_va);
163 if (k == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000165 return NULL;
166 }
167 v = do_mkvalue(p_format, p_va);
168 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000169 Py_DECREF(k);
170 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000171 return NULL;
172 }
Guido van Rossumdb847bd1997-11-20 20:35:45 +0000173 err = PyDict_SetItem(d, k, v);
174 Py_DECREF(k);
175 Py_DECREF(v);
176 if (err < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000177 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000178 return NULL;
179 }
180 }
181 if (d != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000182 Py_DECREF(d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000183 d = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000184 PyErr_SetString(PyExc_SystemError,
185 "Unmatched paren in format");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000186 }
187 else if (endchar)
188 ++*p_format;
189 return d;
190}
191
Guido van Rossum79f25d91997-04-29 20:08:16 +0000192static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000193do_mklist(char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000194{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000195 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000196 int i;
197 if (n < 0)
198 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000199 if ((v = PyList_New(n)) == NULL)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000200 return NULL;
201 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202 PyObject *w = do_mkvalue(p_format, p_va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000203 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000204 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000205 return NULL;
206 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000207 PyList_SetItem(v, i, w);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000208 }
209 if (v != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000210 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000211 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000212 PyErr_SetString(PyExc_SystemError,
213 "Unmatched paren in format");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000214 }
215 else if (endchar)
216 ++*p_format;
217 return v;
218}
219
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000220#ifdef Py_USING_UNICODE
Fred Drake25d34472000-04-28 14:42:37 +0000221static int
222_ustrlen(Py_UNICODE *u)
223{
224 int i = 0;
225 Py_UNICODE *v = u;
226 while (*v != 0) { i++; v++; }
227 return i;
228}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000229#endif
Fred Drake25d34472000-04-28 14:42:37 +0000230
Guido van Rossum79f25d91997-04-29 20:08:16 +0000231static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000232do_mktuple(char **p_format, va_list *p_va, int endchar, int n)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000233{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000234 PyObject *v;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000235 int i;
236 if (n < 0)
237 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000238 if ((v = PyTuple_New(n)) == NULL)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000239 return NULL;
240 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000241 PyObject *w = do_mkvalue(p_format, p_va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000242 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243 Py_DECREF(v);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000244 return NULL;
245 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000246 PyTuple_SetItem(v, i, w);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000247 }
248 if (v != NULL && **p_format != endchar) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000249 Py_DECREF(v);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000250 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000251 PyErr_SetString(PyExc_SystemError,
252 "Unmatched paren in format");
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000253 }
254 else if (endchar)
255 ++*p_format;
256 return v;
257}
258
Guido van Rossum79f25d91997-04-29 20:08:16 +0000259static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000260do_mkvalue(char **p_format, va_list *p_va)
Guido van Rossum899dcf31992-05-15 11:04:59 +0000261{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000262 for (;;) {
263 switch (*(*p_format)++) {
264 case '(':
265 return do_mktuple(p_format, p_va, ')',
266 countformat(*p_format, ')'));
267
268 case '[':
269 return do_mklist(p_format, p_va, ']',
270 countformat(*p_format, ']'));
271
272 case '{':
273 return do_mkdict(p_format, p_va, '}',
274 countformat(*p_format, '}'));
275
276 case 'b':
Jack Jansenb763b9d2000-09-15 12:51:01 +0000277 case 'B':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000278 case 'h':
279 case 'i':
Guido van Rossum79f25d91997-04-29 20:08:16 +0000280 return PyInt_FromLong((long)va_arg(*p_va, int));
Jack Jansend50338f2000-07-06 12:22:00 +0000281
282 case 'H':
283 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000284
285 case 'l':
Guido van Rossum79f25d91997-04-29 20:08:16 +0000286 return PyInt_FromLong((long)va_arg(*p_va, long));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000287
Jack Jansendbd65032003-04-17 22:01:10 +0000288 case 'k':
289 return PyInt_FromLong((long)va_arg(*p_va, unsigned long));
290
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000291#ifdef HAVE_LONG_LONG
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000292 case 'L':
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000293 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
Jack Jansendbd65032003-04-17 22:01:10 +0000294
295 case 'K':
296 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000297#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000298#ifdef Py_USING_UNICODE
Fred Drake25d34472000-04-28 14:42:37 +0000299 case 'u':
300 {
301 PyObject *v;
302 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
303 int n;
304 if (**p_format == '#') {
305 ++*p_format;
306 n = va_arg(*p_va, int);
307 }
308 else
309 n = -1;
310 if (u == NULL) {
311 v = Py_None;
312 Py_INCREF(v);
313 }
314 else {
315 if (n < 0)
316 n = _ustrlen(u);
317 v = PyUnicode_FromUnicode(u, n);
318 }
319 return v;
320 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000321#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000322 case 'f':
323 case 'd':
Guido van Rossum79f25d91997-04-29 20:08:16 +0000324 return PyFloat_FromDouble(
325 (double)va_arg(*p_va, va_double));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000326
Fred Drakeaec79242001-03-12 21:03:26 +0000327#ifndef WITHOUT_COMPLEX
328 case 'D':
329 return PyComplex_FromCComplex(
330 *((Py_complex *)va_arg(*p_va, Py_complex *)));
331#endif /* WITHOUT_COMPLEX */
332
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000333 case 'c':
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000334 {
335 char p[1];
336 p[0] = va_arg(*p_va, int);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 return PyString_FromStringAndSize(p, 1);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000338 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000339
340 case 's':
341 case 'z':
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000342 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000343 PyObject *v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000344 char *str = va_arg(*p_va, char *);
345 int n;
346 if (**p_format == '#') {
347 ++*p_format;
348 n = va_arg(*p_va, int);
349 }
350 else
351 n = -1;
352 if (str == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000353 v = Py_None;
354 Py_INCREF(v);
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000355 }
356 else {
Guido van Rossum582acec2000-06-28 22:07:35 +0000357 if (n < 0) {
358 size_t m = strlen(str);
359 if (m > INT_MAX) {
360 PyErr_SetString(PyExc_OverflowError,
361 "string too long for Python string");
362 return NULL;
363 }
364 n = (int)m;
365 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000366 v = PyString_FromStringAndSize(str, n);
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000367 }
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000368 return v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000369 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000370
Guido van Rossumd3415001998-12-23 05:01:38 +0000371 case 'N':
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000372 case 'S':
373 case 'O':
Guido van Rossumd1b93931995-01-20 16:56:02 +0000374 if (**p_format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000375 typedef PyObject *(*converter)(void *);
Guido van Rossumd1b93931995-01-20 16:56:02 +0000376 converter func = va_arg(*p_va, converter);
377 void *arg = va_arg(*p_va, void *);
378 ++*p_format;
379 return (*func)(arg);
380 }
381 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000382 PyObject *v;
383 v = va_arg(*p_va, PyObject *);
Guido van Rossumc38e7d41998-12-23 19:53:45 +0000384 if (v != NULL) {
Guido van Rossumd3415001998-12-23 05:01:38 +0000385 if (*(*p_format - 1) != 'N')
386 Py_INCREF(v);
Guido van Rossumc38e7d41998-12-23 19:53:45 +0000387 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000388 else if (!PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000389 /* If a NULL was passed
390 * because a call that should
391 * have constructed a value
392 * failed, that's OK, and we
393 * pass the error on; but if
394 * no error occurred it's not
395 * clear that the caller knew
396 * what she was doing. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397 PyErr_SetString(PyExc_SystemError,
Guido van Rossum233f4b51998-07-07 22:32:19 +0000398 "NULL object passed to Py_BuildValue");
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000399 return v;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000400 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000401
402 case ':':
403 case ',':
404 case ' ':
405 case '\t':
406 break;
407
408 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000409 PyErr_SetString(PyExc_SystemError,
Guido van Rossum233f4b51998-07-07 22:32:19 +0000410 "bad format char passed to Py_BuildValue");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000411 return NULL;
412
413 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000414 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000415}
416
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000417
Fred Drakeceead6d2003-01-30 15:08:25 +0000418PyObject *
419Py_BuildValue(char *format, ...)
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000420{
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000421 va_list va;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000422 PyObject* retval;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000423 va_start(va, format);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000424 retval = Py_VaBuildValue(format, va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000425 va_end(va);
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000426 return retval;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000427}
Guido van Rossume5372401993-03-16 12:15:04 +0000428
Guido van Rossum79f25d91997-04-29 20:08:16 +0000429PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000430Py_VaBuildValue(char *format, va_list va)
Guido van Rossume5372401993-03-16 12:15:04 +0000431{
432 char *f = format;
433 int n = countformat(f, '\0');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000434 va_list lva;
435
436#ifdef VA_LIST_IS_ARRAY
437 memcpy(lva, va, sizeof(va_list));
438#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000439#ifdef __va_copy
440 __va_copy(lva, va);
441#else
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000442 lva = va;
443#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000444#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000445
Guido van Rossume5372401993-03-16 12:15:04 +0000446 if (n < 0)
447 return NULL;
448 if (n == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000449 Py_INCREF(Py_None);
450 return Py_None;
Guido van Rossume5372401993-03-16 12:15:04 +0000451 }
452 if (n == 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000453 return do_mkvalue(&f, &lva);
454 return do_mktuple(&f, &lva, '\0', n);
455}
456
457
Guido van Rossum79f25d91997-04-29 20:08:16 +0000458PyObject *
459PyEval_CallFunction(PyObject *obj, char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000460{
461 va_list vargs;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000462 PyObject *args;
463 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000464
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000465 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000466
Guido van Rossum79f25d91997-04-29 20:08:16 +0000467 args = Py_VaBuildValue(format, vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000468 va_end(vargs);
469
470 if (args == NULL)
471 return NULL;
472
Guido van Rossum79f25d91997-04-29 20:08:16 +0000473 res = PyEval_CallObject(obj, args);
474 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000475
476 return res;
477}
478
479
Guido van Rossum79f25d91997-04-29 20:08:16 +0000480PyObject *
Guido van Rossumc4099e61998-08-08 20:51:26 +0000481PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000482{
483 va_list vargs;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000484 PyObject *meth;
485 PyObject *args;
486 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000487
Guido van Rossumc4099e61998-08-08 20:51:26 +0000488 meth = PyObject_GetAttrString(obj, methodname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000489 if (meth == NULL)
490 return NULL;
491
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000492 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000493
Guido van Rossum79f25d91997-04-29 20:08:16 +0000494 args = Py_VaBuildValue(format, vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000495 va_end(vargs);
496
497 if (args == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000498 Py_DECREF(meth);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000499 return NULL;
500 }
501
Guido van Rossum79f25d91997-04-29 20:08:16 +0000502 res = PyEval_CallObject(meth, args);
503 Py_DECREF(meth);
504 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000505
506 return res;
Guido van Rossume5372401993-03-16 12:15:04 +0000507}
Fred Drake9e285152000-09-23 03:24:27 +0000508
509int
510PyModule_AddObject(PyObject *m, char *name, PyObject *o)
511{
512 PyObject *dict;
Jeremy Hyltonc44dbc42003-06-21 21:35:25 +0000513 if (!PyModule_Check(m)) {
Fred Drake83115182002-06-17 17:16:57 +0000514 PyErr_SetString(PyExc_TypeError,
515 "PyModule_AddObject() needs module as first arg");
Fred Drake9e285152000-09-23 03:24:27 +0000516 return -1;
Fred Drake83115182002-06-17 17:16:57 +0000517 }
Jeremy Hyltonc44dbc42003-06-21 21:35:25 +0000518 if (!o) {
Martin v. Löwisf8d59d22003-10-24 20:05:32 +0000519 if (!PyErr_Occurred())
520 PyErr_SetString(PyExc_TypeError,
521 "PyModule_AddObject() needs non-NULL value");
Jeremy Hyltonc44dbc42003-06-21 21:35:25 +0000522 return -1;
523 }
524
Fred Drake83115182002-06-17 17:16:57 +0000525 dict = PyModule_GetDict(m);
526 if (dict == NULL) {
527 /* Internal error -- modules must have a dict! */
528 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
529 PyModule_GetName(m));
530 return -1;
531 }
532 if (PyDict_SetItemString(dict, name, o))
533 return -1;
534 Py_DECREF(o);
535 return 0;
Fred Drake9e285152000-09-23 03:24:27 +0000536}
537
538int
539PyModule_AddIntConstant(PyObject *m, char *name, long value)
540{
541 return PyModule_AddObject(m, name, PyInt_FromLong(value));
542}
543
544int
545PyModule_AddStringConstant(PyObject *m, char *name, char *value)
546{
547 return PyModule_AddObject(m, name, PyString_FromString(value));
548}