Apparently the code to forestall Tk eating events was too aggressive (Tk user input...
[python/dscho.git] / Python / modsupport.c
blob08685898da222abc3bb06f06a92595661eb2df78
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 #ifndef WITHOUT_COMPLEX
290 case 'D':
291 return PyComplex_FromCComplex(
292 *((Py_complex *)va_arg(*p_va, Py_complex *)));
293 #endif /* WITHOUT_COMPLEX */
295 case 'c':
297 char p[1];
298 p[0] = va_arg(*p_va, int);
299 return PyString_FromStringAndSize(p, 1);
302 case 's':
303 case 'z':
305 PyObject *v;
306 char *str = va_arg(*p_va, char *);
307 int n;
308 if (**p_format == '#') {
309 ++*p_format;
310 n = va_arg(*p_va, int);
312 else
313 n = -1;
314 if (str == NULL) {
315 v = Py_None;
316 Py_INCREF(v);
318 else {
319 if (n < 0) {
320 size_t m = strlen(str);
321 if (m > INT_MAX) {
322 PyErr_SetString(PyExc_OverflowError,
323 "string too long for Python string");
324 return NULL;
326 n = (int)m;
328 v = PyString_FromStringAndSize(str, n);
330 return v;
333 case 'N':
334 case 'S':
335 case 'O':
336 if (**p_format == '&') {
337 typedef PyObject *(*converter)(void *);
338 converter func = va_arg(*p_va, converter);
339 void *arg = va_arg(*p_va, void *);
340 ++*p_format;
341 return (*func)(arg);
343 else {
344 PyObject *v;
345 v = va_arg(*p_va, PyObject *);
346 if (v != NULL) {
347 if (*(*p_format - 1) != 'N')
348 Py_INCREF(v);
350 else if (!PyErr_Occurred())
351 /* If a NULL was passed
352 * because a call that should
353 * have constructed a value
354 * failed, that's OK, and we
355 * pass the error on; but if
356 * no error occurred it's not
357 * clear that the caller knew
358 * what she was doing. */
359 PyErr_SetString(PyExc_SystemError,
360 "NULL object passed to Py_BuildValue");
361 return v;
364 case ':':
365 case ',':
366 case ' ':
367 case '\t':
368 break;
370 default:
371 PyErr_SetString(PyExc_SystemError,
372 "bad format char passed to Py_BuildValue");
373 return NULL;
380 PyObject *Py_BuildValue(char *format, ...)
382 va_list va;
383 PyObject* retval;
384 va_start(va, format);
385 retval = Py_VaBuildValue(format, va);
386 va_end(va);
387 return retval;
390 PyObject *
391 Py_VaBuildValue(char *format, va_list va)
393 char *f = format;
394 int n = countformat(f, '\0');
395 va_list lva;
397 #ifdef VA_LIST_IS_ARRAY
398 memcpy(lva, va, sizeof(va_list));
399 #else
400 lva = va;
401 #endif
403 if (n < 0)
404 return NULL;
405 if (n == 0) {
406 Py_INCREF(Py_None);
407 return Py_None;
409 if (n == 1)
410 return do_mkvalue(&f, &lva);
411 return do_mktuple(&f, &lva, '\0', n);
415 PyObject *
416 PyEval_CallFunction(PyObject *obj, char *format, ...)
418 va_list vargs;
419 PyObject *args;
420 PyObject *res;
422 va_start(vargs, format);
424 args = Py_VaBuildValue(format, vargs);
425 va_end(vargs);
427 if (args == NULL)
428 return NULL;
430 res = PyEval_CallObject(obj, args);
431 Py_DECREF(args);
433 return res;
437 PyObject *
438 PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
440 va_list vargs;
441 PyObject *meth;
442 PyObject *args;
443 PyObject *res;
445 meth = PyObject_GetAttrString(obj, methodname);
446 if (meth == NULL)
447 return NULL;
449 va_start(vargs, format);
451 args = Py_VaBuildValue(format, vargs);
452 va_end(vargs);
454 if (args == NULL) {
455 Py_DECREF(meth);
456 return NULL;
459 res = PyEval_CallObject(meth, args);
460 Py_DECREF(meth);
461 Py_DECREF(args);
463 return res;
467 PyModule_AddObject(PyObject *m, char *name, PyObject *o)
469 PyObject *dict;
470 if (!PyModule_Check(m) || o == NULL)
471 return -1;
472 dict = PyModule_GetDict(m);
473 if (dict == NULL)
474 return -1;
475 if (PyDict_SetItemString(dict, name, o))
476 return -1;
477 Py_DECREF(o);
478 return 0;
481 int
482 PyModule_AddIntConstant(PyObject *m, char *name, long value)
484 return PyModule_AddObject(m, name, PyInt_FromLong(value));
487 int
488 PyModule_AddStringConstant(PyObject *m, char *name, char *value)
490 return PyModule_AddObject(m, name, PyString_FromString(value));