Remove a ?? in the description of Mac OS support.
[python/dscho.git] / Python / modsupport.c
blob9c2dc18a4516e029f7847a98393fc9d1dde5e3d3
2 /* Module support implementation */
4 #include "Python.h"
5 #ifdef HAVE_LIMITS_H
6 #include <limits.h>
7 #endif
9 #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
10 typedef extended va_double;
11 #else
12 typedef double va_double;
13 #endif
15 /* Package context -- the full module name for package imports */
16 char *_Py_PackageContext = NULL;
18 /* Py_InitModule4() parameters:
19 - 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
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.
31 static char api_version_warning[] =
32 "WARNING: Python C API version mismatch for module %s:\n\
33 This Python has API version %d, module %s has version %d.\n";
35 PyObject *
36 Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
37 PyObject *passthrough, int module_api_version)
39 PyObject *m, *d, *v;
40 PyMethodDef *ml;
41 if (!Py_IsInitialized())
42 Py_FatalError("Interpreter not initialized (version mismatch?)");
43 if (module_api_version != PYTHON_API_VERSION)
44 fprintf(stderr, api_version_warning,
45 name, PYTHON_API_VERSION, name, module_api_version);
46 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;
53 if ((m = PyImport_AddModule(name)) == NULL)
54 return NULL;
55 d = PyModule_GetDict(m);
56 for (ml = methods; ml->ml_name != NULL; ml++) {
57 v = PyCFunction_New(ml, passthrough);
58 if (v == NULL)
59 return NULL;
60 if (PyDict_SetItemString(d, ml->ml_name, v) != 0)
61 return NULL;
62 Py_DECREF(v);
64 if (doc != NULL) {
65 v = PyString_FromString(doc);
66 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0)
67 return NULL;
68 Py_DECREF(v);
70 return m;
74 /* Helper for mkvalue() to scan the length of a format */
76 static int countformat(char *format, int endchar)
78 int count = 0;
79 int level = 0;
80 while (level > 0 || *format != endchar) {
81 switch (*format) {
82 case '\0':
83 /* Premature end */
84 PyErr_SetString(PyExc_SystemError,
85 "unmatched paren in format");
86 return -1;
87 case '(':
88 case '[':
89 case '{':
90 if (level == 0)
91 count++;
92 level++;
93 break;
94 case ')':
95 case ']':
96 case '}':
97 level--;
98 break;
99 case '#':
100 case '&':
101 case ',':
102 case ':':
103 case ' ':
104 case '\t':
105 break;
106 default:
107 if (level == 0)
108 count++;
110 format++;
112 return count;
116 /* Generic function to create a value -- the inverse of getargs() */
117 /* After an original idea and first implementation by Steven Miale */
119 static PyObject *do_mktuple(char**, va_list *, int, int);
120 static PyObject *do_mklist(char**, va_list *, int, int);
121 static PyObject *do_mkdict(char**, va_list *, int, int);
122 static PyObject *do_mkvalue(char**, va_list *);
125 static PyObject *
126 do_mkdict(char **p_format, va_list *p_va, int endchar, int n)
128 PyObject *d;
129 int i;
130 if (n < 0)
131 return NULL;
132 if ((d = PyDict_New()) == NULL)
133 return NULL;
134 for (i = 0; i < n; i+= 2) {
135 PyObject *k, *v;
136 int err;
137 k = do_mkvalue(p_format, p_va);
138 if (k == NULL) {
139 Py_DECREF(d);
140 return NULL;
142 v = do_mkvalue(p_format, p_va);
143 if (v == NULL) {
144 Py_DECREF(k);
145 Py_DECREF(d);
146 return NULL;
148 err = PyDict_SetItem(d, k, v);
149 Py_DECREF(k);
150 Py_DECREF(v);
151 if (err < 0) {
152 Py_DECREF(d);
153 return NULL;
156 if (d != NULL && **p_format != endchar) {
157 Py_DECREF(d);
158 d = NULL;
159 PyErr_SetString(PyExc_SystemError,
160 "Unmatched paren in format");
162 else if (endchar)
163 ++*p_format;
164 return d;
167 static PyObject *
168 do_mklist(char **p_format, va_list *p_va, int endchar, int n)
170 PyObject *v;
171 int i;
172 if (n < 0)
173 return NULL;
174 if ((v = PyList_New(n)) == NULL)
175 return NULL;
176 for (i = 0; i < n; i++) {
177 PyObject *w = do_mkvalue(p_format, p_va);
178 if (w == NULL) {
179 Py_DECREF(v);
180 return NULL;
182 PyList_SetItem(v, i, w);
184 if (v != NULL && **p_format != endchar) {
185 Py_DECREF(v);
186 v = NULL;
187 PyErr_SetString(PyExc_SystemError,
188 "Unmatched paren in format");
190 else if (endchar)
191 ++*p_format;
192 return v;
195 static int
196 _ustrlen(Py_UNICODE *u)
198 int i = 0;
199 Py_UNICODE *v = u;
200 while (*v != 0) { i++; v++; }
201 return i;
204 static PyObject *
205 do_mktuple(char **p_format, va_list *p_va, int endchar, int n)
207 PyObject *v;
208 int i;
209 if (n < 0)
210 return NULL;
211 if ((v = PyTuple_New(n)) == NULL)
212 return NULL;
213 for (i = 0; i < n; i++) {
214 PyObject *w = do_mkvalue(p_format, p_va);
215 if (w == NULL) {
216 Py_DECREF(v);
217 return NULL;
219 PyTuple_SetItem(v, i, w);
221 if (v != NULL && **p_format != endchar) {
222 Py_DECREF(v);
223 v = NULL;
224 PyErr_SetString(PyExc_SystemError,
225 "Unmatched paren in format");
227 else if (endchar)
228 ++*p_format;
229 return v;
232 static PyObject *
233 do_mkvalue(char **p_format, va_list *p_va)
235 for (;;) {
236 switch (*(*p_format)++) {
237 case '(':
238 return do_mktuple(p_format, p_va, ')',
239 countformat(*p_format, ')'));
241 case '[':
242 return do_mklist(p_format, p_va, ']',
243 countformat(*p_format, ']'));
245 case '{':
246 return do_mkdict(p_format, p_va, '}',
247 countformat(*p_format, '}'));
249 case 'b':
250 case 'B':
251 case 'h':
252 case 'i':
253 return PyInt_FromLong((long)va_arg(*p_va, int));
255 case 'H':
256 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
258 case 'l':
259 return PyInt_FromLong((long)va_arg(*p_va, long));
261 #ifdef HAVE_LONG_LONG
262 case 'L':
263 return PyLong_FromLongLong((LONG_LONG)va_arg(*p_va, LONG_LONG));
264 #endif
265 case 'u':
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);
274 else
275 n = -1;
276 if (u == NULL) {
277 v = Py_None;
278 Py_INCREF(v);
280 else {
281 if (n < 0)
282 n = _ustrlen(u);
283 v = PyUnicode_FromUnicode(u, n);
285 return v;
287 case 'f':
288 case 'd':
289 return PyFloat_FromDouble(
290 (double)va_arg(*p_va, va_double));
292 case 'c':
294 char p[1];
295 p[0] = va_arg(*p_va, int);
296 return PyString_FromStringAndSize(p, 1);
299 case 's':
300 case 'z':
302 PyObject *v;
303 char *str = va_arg(*p_va, char *);
304 int n;
305 if (**p_format == '#') {
306 ++*p_format;
307 n = va_arg(*p_va, int);
309 else
310 n = -1;
311 if (str == NULL) {
312 v = Py_None;
313 Py_INCREF(v);
315 else {
316 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;
323 n = (int)m;
325 v = PyString_FromStringAndSize(str, n);
327 return v;
330 case 'N':
331 case 'S':
332 case 'O':
333 if (**p_format == '&') {
334 typedef PyObject *(*converter)(void *);
335 converter func = va_arg(*p_va, converter);
336 void *arg = va_arg(*p_va, void *);
337 ++*p_format;
338 return (*func)(arg);
340 else {
341 PyObject *v;
342 v = va_arg(*p_va, PyObject *);
343 if (v != NULL) {
344 if (*(*p_format - 1) != 'N')
345 Py_INCREF(v);
347 else if (!PyErr_Occurred())
348 /* 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. */
356 PyErr_SetString(PyExc_SystemError,
357 "NULL object passed to Py_BuildValue");
358 return v;
361 case ':':
362 case ',':
363 case ' ':
364 case '\t':
365 break;
367 default:
368 PyErr_SetString(PyExc_SystemError,
369 "bad format char passed to Py_BuildValue");
370 return NULL;
377 PyObject *Py_BuildValue(char *format, ...)
379 va_list va;
380 PyObject* retval;
381 va_start(va, format);
382 retval = Py_VaBuildValue(format, va);
383 va_end(va);
384 return retval;
387 PyObject *
388 Py_VaBuildValue(char *format, va_list va)
390 char *f = format;
391 int n = countformat(f, '\0');
392 va_list lva;
394 #ifdef VA_LIST_IS_ARRAY
395 memcpy(lva, va, sizeof(va_list));
396 #else
397 lva = va;
398 #endif
400 if (n < 0)
401 return NULL;
402 if (n == 0) {
403 Py_INCREF(Py_None);
404 return Py_None;
406 if (n == 1)
407 return do_mkvalue(&f, &lva);
408 return do_mktuple(&f, &lva, '\0', n);
412 PyObject *
413 PyEval_CallFunction(PyObject *obj, char *format, ...)
415 va_list vargs;
416 PyObject *args;
417 PyObject *res;
419 va_start(vargs, format);
421 args = Py_VaBuildValue(format, vargs);
422 va_end(vargs);
424 if (args == NULL)
425 return NULL;
427 res = PyEval_CallObject(obj, args);
428 Py_DECREF(args);
430 return res;
434 PyObject *
435 PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
437 va_list vargs;
438 PyObject *meth;
439 PyObject *args;
440 PyObject *res;
442 meth = PyObject_GetAttrString(obj, methodname);
443 if (meth == NULL)
444 return NULL;
446 va_start(vargs, format);
448 args = Py_VaBuildValue(format, vargs);
449 va_end(vargs);
451 if (args == NULL) {
452 Py_DECREF(meth);
453 return NULL;
456 res = PyEval_CallObject(meth, args);
457 Py_DECREF(meth);
458 Py_DECREF(args);
460 return res;
464 PyModule_AddObject(PyObject *m, char *name, PyObject *o)
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;
478 int
479 PyModule_AddIntConstant(PyObject *m, char *name, long value)
481 return PyModule_AddObject(m, name, PyInt_FromLong(value));
484 int
485 PyModule_AddStringConstant(PyObject *m, char *name, char *value)
487 return PyModule_AddObject(m, name, PyString_FromString(value));