Updated for 2.1a3
[python/dscho.git] / Python / modsupport.c
blobef36d108f5b7085021f6932c837bbacbea3a6fc3
2 /* Module support implementation */
4 #include "Python.h"
6 #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
7 typedef extended va_double;
8 #else
9 typedef double va_double;
10 #endif
12 /* Package context -- the full module name for package imports */
13 char *_Py_PackageContext = NULL;
15 /* Py_InitModule4() parameters:
16 - name is the module name
17 - methods is the list of top-level functions
18 - doc is the documentation string
19 - passthrough is passed as self to functions defined in the module
20 - api_version is the value of PYTHON_API_VERSION at the time the
21 module was compiled
23 Return value is a borrowed reference to the module object; or NULL
24 if an error occurred (in Python 1.4 and before, errors were fatal).
25 Errors may still leak memory.
28 static char api_version_warning[] =
29 "WARNING: Python C API version mismatch for module %s:\n\
30 This Python has API version %d, module %s has version %d.\n";
32 PyObject *
33 Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
34 PyObject *passthrough, int module_api_version)
36 PyObject *m, *d, *v;
37 PyMethodDef *ml;
38 if (!Py_IsInitialized())
39 Py_FatalError("Interpreter not initialized (version mismatch?)");
40 if (module_api_version != PYTHON_API_VERSION)
41 fprintf(stderr, api_version_warning,
42 name, PYTHON_API_VERSION, name, module_api_version);
43 if (_Py_PackageContext != NULL) {
44 char *p = strrchr(_Py_PackageContext, '.');
45 if (p != NULL && strcmp(name, p+1) == 0) {
46 name = _Py_PackageContext;
47 _Py_PackageContext = NULL;
50 if ((m = PyImport_AddModule(name)) == NULL)
51 return NULL;
52 d = PyModule_GetDict(m);
53 for (ml = methods; ml->ml_name != NULL; ml++) {
54 v = PyCFunction_New(ml, passthrough);
55 if (v == NULL)
56 return NULL;
57 if (PyDict_SetItemString(d, ml->ml_name, v) != 0)
58 return NULL;
59 Py_DECREF(v);
61 if (doc != NULL) {
62 v = PyString_FromString(doc);
63 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0)
64 return NULL;
65 Py_DECREF(v);
67 return m;
71 /* Helper for mkvalue() to scan the length of a format */
73 static int countformat(char *format, int endchar)
75 int count = 0;
76 int level = 0;
77 while (level > 0 || *format != endchar) {
78 switch (*format) {
79 case '\0':
80 /* Premature end */
81 PyErr_SetString(PyExc_SystemError,
82 "unmatched paren in format");
83 return -1;
84 case '(':
85 case '[':
86 case '{':
87 if (level == 0)
88 count++;
89 level++;
90 break;
91 case ')':
92 case ']':
93 case '}':
94 level--;
95 break;
96 case '#':
97 case '&':
98 case ',':
99 case ':':
100 case ' ':
101 case '\t':
102 break;
103 default:
104 if (level == 0)
105 count++;
107 format++;
109 return count;
113 /* Generic function to create a value -- the inverse of getargs() */
114 /* After an original idea and first implementation by Steven Miale */
116 static PyObject *do_mktuple(char**, va_list *, int, int);
117 static PyObject *do_mklist(char**, va_list *, int, int);
118 static PyObject *do_mkdict(char**, va_list *, int, int);
119 static PyObject *do_mkvalue(char**, va_list *);
122 static PyObject *
123 do_mkdict(char **p_format, va_list *p_va, int endchar, int n)
125 PyObject *d;
126 int i;
127 if (n < 0)
128 return NULL;
129 if ((d = PyDict_New()) == NULL)
130 return NULL;
131 for (i = 0; i < n; i+= 2) {
132 PyObject *k, *v;
133 int err;
134 k = do_mkvalue(p_format, p_va);
135 if (k == NULL) {
136 Py_DECREF(d);
137 return NULL;
139 v = do_mkvalue(p_format, p_va);
140 if (v == NULL) {
141 Py_DECREF(k);
142 Py_DECREF(d);
143 return NULL;
145 err = PyDict_SetItem(d, k, v);
146 Py_DECREF(k);
147 Py_DECREF(v);
148 if (err < 0) {
149 Py_DECREF(d);
150 return NULL;
153 if (d != NULL && **p_format != endchar) {
154 Py_DECREF(d);
155 d = NULL;
156 PyErr_SetString(PyExc_SystemError,
157 "Unmatched paren in format");
159 else if (endchar)
160 ++*p_format;
161 return d;
164 static PyObject *
165 do_mklist(char **p_format, va_list *p_va, int endchar, int n)
167 PyObject *v;
168 int i;
169 if (n < 0)
170 return NULL;
171 if ((v = PyList_New(n)) == NULL)
172 return NULL;
173 for (i = 0; i < n; i++) {
174 PyObject *w = do_mkvalue(p_format, p_va);
175 if (w == NULL) {
176 Py_DECREF(v);
177 return NULL;
179 PyList_SetItem(v, i, w);
181 if (v != NULL && **p_format != endchar) {
182 Py_DECREF(v);
183 v = NULL;
184 PyErr_SetString(PyExc_SystemError,
185 "Unmatched paren in format");
187 else if (endchar)
188 ++*p_format;
189 return v;
192 static int
193 _ustrlen(Py_UNICODE *u)
195 int i = 0;
196 Py_UNICODE *v = u;
197 while (*v != 0) { i++; v++; }
198 return i;
201 static PyObject *
202 do_mktuple(char **p_format, va_list *p_va, int endchar, int n)
204 PyObject *v;
205 int i;
206 if (n < 0)
207 return NULL;
208 if ((v = PyTuple_New(n)) == NULL)
209 return NULL;
210 for (i = 0; i < n; i++) {
211 PyObject *w = do_mkvalue(p_format, p_va);
212 if (w == NULL) {
213 Py_DECREF(v);
214 return NULL;
216 PyTuple_SetItem(v, i, w);
218 if (v != NULL && **p_format != endchar) {
219 Py_DECREF(v);
220 v = NULL;
221 PyErr_SetString(PyExc_SystemError,
222 "Unmatched paren in format");
224 else if (endchar)
225 ++*p_format;
226 return v;
229 static PyObject *
230 do_mkvalue(char **p_format, va_list *p_va)
232 for (;;) {
233 switch (*(*p_format)++) {
234 case '(':
235 return do_mktuple(p_format, p_va, ')',
236 countformat(*p_format, ')'));
238 case '[':
239 return do_mklist(p_format, p_va, ']',
240 countformat(*p_format, ']'));
242 case '{':
243 return do_mkdict(p_format, p_va, '}',
244 countformat(*p_format, '}'));
246 case 'b':
247 case 'B':
248 case 'h':
249 case 'i':
250 return PyInt_FromLong((long)va_arg(*p_va, int));
252 case 'H':
253 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
255 case 'l':
256 return PyInt_FromLong((long)va_arg(*p_va, long));
258 #ifdef HAVE_LONG_LONG
259 case 'L':
260 return PyLong_FromLongLong((LONG_LONG)va_arg(*p_va, LONG_LONG));
261 #endif
262 case 'u':
264 PyObject *v;
265 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
266 int n;
267 if (**p_format == '#') {
268 ++*p_format;
269 n = va_arg(*p_va, int);
271 else
272 n = -1;
273 if (u == NULL) {
274 v = Py_None;
275 Py_INCREF(v);
277 else {
278 if (n < 0)
279 n = _ustrlen(u);
280 v = PyUnicode_FromUnicode(u, n);
282 return v;
284 case 'f':
285 case 'd':
286 return PyFloat_FromDouble(
287 (double)va_arg(*p_va, va_double));
289 case 'c':
291 char p[1];
292 p[0] = va_arg(*p_va, int);
293 return PyString_FromStringAndSize(p, 1);
296 case 's':
297 case 'z':
299 PyObject *v;
300 char *str = va_arg(*p_va, char *);
301 int n;
302 if (**p_format == '#') {
303 ++*p_format;
304 n = va_arg(*p_va, int);
306 else
307 n = -1;
308 if (str == NULL) {
309 v = Py_None;
310 Py_INCREF(v);
312 else {
313 if (n < 0) {
314 size_t m = strlen(str);
315 if (m > INT_MAX) {
316 PyErr_SetString(PyExc_OverflowError,
317 "string too long for Python string");
318 return NULL;
320 n = (int)m;
322 v = PyString_FromStringAndSize(str, n);
324 return v;
327 case 'N':
328 case 'S':
329 case 'O':
330 if (**p_format == '&') {
331 typedef PyObject *(*converter)(void *);
332 converter func = va_arg(*p_va, converter);
333 void *arg = va_arg(*p_va, void *);
334 ++*p_format;
335 return (*func)(arg);
337 else {
338 PyObject *v;
339 v = va_arg(*p_va, PyObject *);
340 if (v != NULL) {
341 if (*(*p_format - 1) != 'N')
342 Py_INCREF(v);
344 else if (!PyErr_Occurred())
345 /* If a NULL was passed
346 * because a call that should
347 * have constructed a value
348 * failed, that's OK, and we
349 * pass the error on; but if
350 * no error occurred it's not
351 * clear that the caller knew
352 * what she was doing. */
353 PyErr_SetString(PyExc_SystemError,
354 "NULL object passed to Py_BuildValue");
355 return v;
358 case ':':
359 case ',':
360 case ' ':
361 case '\t':
362 break;
364 default:
365 PyErr_SetString(PyExc_SystemError,
366 "bad format char passed to Py_BuildValue");
367 return NULL;
374 PyObject *Py_BuildValue(char *format, ...)
376 va_list va;
377 PyObject* retval;
378 va_start(va, format);
379 retval = Py_VaBuildValue(format, va);
380 va_end(va);
381 return retval;
384 PyObject *
385 Py_VaBuildValue(char *format, va_list va)
387 char *f = format;
388 int n = countformat(f, '\0');
389 va_list lva;
391 #ifdef VA_LIST_IS_ARRAY
392 memcpy(lva, va, sizeof(va_list));
393 #else
394 lva = va;
395 #endif
397 if (n < 0)
398 return NULL;
399 if (n == 0) {
400 Py_INCREF(Py_None);
401 return Py_None;
403 if (n == 1)
404 return do_mkvalue(&f, &lva);
405 return do_mktuple(&f, &lva, '\0', n);
409 PyObject *
410 PyEval_CallFunction(PyObject *obj, char *format, ...)
412 va_list vargs;
413 PyObject *args;
414 PyObject *res;
416 va_start(vargs, format);
418 args = Py_VaBuildValue(format, vargs);
419 va_end(vargs);
421 if (args == NULL)
422 return NULL;
424 res = PyEval_CallObject(obj, args);
425 Py_DECREF(args);
427 return res;
431 PyObject *
432 PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
434 va_list vargs;
435 PyObject *meth;
436 PyObject *args;
437 PyObject *res;
439 meth = PyObject_GetAttrString(obj, methodname);
440 if (meth == NULL)
441 return NULL;
443 va_start(vargs, format);
445 args = Py_VaBuildValue(format, vargs);
446 va_end(vargs);
448 if (args == NULL) {
449 Py_DECREF(meth);
450 return NULL;
453 res = PyEval_CallObject(meth, args);
454 Py_DECREF(meth);
455 Py_DECREF(args);
457 return res;
461 PyModule_AddObject(PyObject *m, char *name, PyObject *o)
463 PyObject *dict;
464 if (!PyModule_Check(m) || o == NULL)
465 return -1;
466 dict = PyModule_GetDict(m);
467 if (dict == NULL)
468 return -1;
469 if (PyDict_SetItemString(dict, name, o))
470 return -1;
471 Py_DECREF(o);
472 return 0;
475 int
476 PyModule_AddIntConstant(PyObject *m, char *name, long value)
478 return PyModule_AddObject(m, name, PyInt_FromLong(value));
481 int
482 PyModule_AddStringConstant(PyObject *m, char *name, char *value)
484 return PyModule_AddObject(m, name, PyString_FromString(value));