AddressList.__str__(): Get rid of useless, and broken method. Closes
[python/dscho.git] / Python / modsupport.c
blob12f3f17b487011561c13568f2aaa3e23eda89ca7
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 "Python C API version mismatch for module %.100s:\
30 This Python has API version %d, module %.100s has version %d.";
32 PyObject *
33 Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
34 PyObject *passthrough, int module_api_version)
36 PyObject *m, *d, *v, *n;
37 PyMethodDef *ml;
38 if (!Py_IsInitialized())
39 Py_FatalError("Interpreter not initialized (version mismatch?)");
40 if (module_api_version != PYTHON_API_VERSION) {
41 char message[512];
42 PyOS_snprintf(message, sizeof(message),
43 api_version_warning, name,
44 PYTHON_API_VERSION, name,
45 module_api_version);
46 if (PyErr_Warn(PyExc_RuntimeWarning, message))
47 return NULL;
49 /* Make sure name is fully qualified.
51 This is a bit of a hack: when the shared library is loaded,
52 the module name is "package.module", but the module calls
53 Py_InitModule*() with just "module" for the name. The shared
54 library loader squirrels away the true name of the module in
55 _Py_PackageContext, and Py_InitModule*() will substitute this
56 (if the name actually matches).
58 if (_Py_PackageContext != NULL) {
59 char *p = strrchr(_Py_PackageContext, '.');
60 if (p != NULL && strcmp(name, p+1) == 0) {
61 name = _Py_PackageContext;
62 _Py_PackageContext = NULL;
65 if ((m = PyImport_AddModule(name)) == NULL)
66 return NULL;
67 d = PyModule_GetDict(m);
68 if (methods != NULL) {
69 n = PyString_FromString(name);
70 if (n == NULL)
71 return NULL;
72 for (ml = methods; ml->ml_name != NULL; ml++) {
73 if ((ml->ml_flags & METH_CLASS) ||
74 (ml->ml_flags & METH_STATIC)) {
75 PyErr_SetString(PyExc_ValueError,
76 "module functions cannot set"
77 " METH_CLASS or METH_STATIC");
78 return NULL;
80 v = PyCFunction_NewEx(ml, passthrough, n);
81 if (v == NULL)
82 return NULL;
83 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
84 Py_DECREF(v);
85 return NULL;
87 Py_DECREF(v);
90 if (doc != NULL) {
91 v = PyString_FromString(doc);
92 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
93 Py_XDECREF(v);
94 return NULL;
96 Py_DECREF(v);
98 return m;
102 /* Helper for mkvalue() to scan the length of a format */
104 static int
105 countformat(char *format, int endchar)
107 int count = 0;
108 int level = 0;
109 while (level > 0 || *format != endchar) {
110 switch (*format) {
111 case '\0':
112 /* Premature end */
113 PyErr_SetString(PyExc_SystemError,
114 "unmatched paren in format");
115 return -1;
116 case '(':
117 case '[':
118 case '{':
119 if (level == 0)
120 count++;
121 level++;
122 break;
123 case ')':
124 case ']':
125 case '}':
126 level--;
127 break;
128 case '#':
129 case '&':
130 case ',':
131 case ':':
132 case ' ':
133 case '\t':
134 break;
135 default:
136 if (level == 0)
137 count++;
139 format++;
141 return count;
145 /* Generic function to create a value -- the inverse of getargs() */
146 /* After an original idea and first implementation by Steven Miale */
148 static PyObject *do_mktuple(char**, va_list *, int, int);
149 static PyObject *do_mklist(char**, va_list *, int, int);
150 static PyObject *do_mkdict(char**, va_list *, int, int);
151 static PyObject *do_mkvalue(char**, va_list *);
154 static PyObject *
155 do_mkdict(char **p_format, va_list *p_va, int endchar, int n)
157 PyObject *d;
158 int i;
159 if (n < 0)
160 return NULL;
161 if ((d = PyDict_New()) == NULL)
162 return NULL;
163 for (i = 0; i < n; i+= 2) {
164 PyObject *k, *v;
165 int err;
166 k = do_mkvalue(p_format, p_va);
167 if (k == NULL) {
168 Py_DECREF(d);
169 return NULL;
171 v = do_mkvalue(p_format, p_va);
172 if (v == NULL) {
173 Py_DECREF(k);
174 Py_DECREF(d);
175 return NULL;
177 err = PyDict_SetItem(d, k, v);
178 Py_DECREF(k);
179 Py_DECREF(v);
180 if (err < 0) {
181 Py_DECREF(d);
182 return NULL;
185 if (d != NULL && **p_format != endchar) {
186 Py_DECREF(d);
187 d = NULL;
188 PyErr_SetString(PyExc_SystemError,
189 "Unmatched paren in format");
191 else if (endchar)
192 ++*p_format;
193 return d;
196 static PyObject *
197 do_mklist(char **p_format, va_list *p_va, int endchar, int n)
199 PyObject *v;
200 int i;
201 if (n < 0)
202 return NULL;
203 if ((v = PyList_New(n)) == NULL)
204 return NULL;
205 for (i = 0; i < n; i++) {
206 PyObject *w = do_mkvalue(p_format, p_va);
207 if (w == NULL) {
208 Py_DECREF(v);
209 return NULL;
211 PyList_SetItem(v, i, w);
213 if (v != NULL && **p_format != endchar) {
214 Py_DECREF(v);
215 v = NULL;
216 PyErr_SetString(PyExc_SystemError,
217 "Unmatched paren in format");
219 else if (endchar)
220 ++*p_format;
221 return v;
224 #ifdef Py_USING_UNICODE
225 static int
226 _ustrlen(Py_UNICODE *u)
228 int i = 0;
229 Py_UNICODE *v = u;
230 while (*v != 0) { i++; v++; }
231 return i;
233 #endif
235 static PyObject *
236 do_mktuple(char **p_format, va_list *p_va, int endchar, int n)
238 PyObject *v;
239 int i;
240 if (n < 0)
241 return NULL;
242 if ((v = PyTuple_New(n)) == NULL)
243 return NULL;
244 for (i = 0; i < n; i++) {
245 PyObject *w = do_mkvalue(p_format, p_va);
246 if (w == NULL) {
247 Py_DECREF(v);
248 return NULL;
250 PyTuple_SetItem(v, i, w);
252 if (v != NULL && **p_format != endchar) {
253 Py_DECREF(v);
254 v = NULL;
255 PyErr_SetString(PyExc_SystemError,
256 "Unmatched paren in format");
258 else if (endchar)
259 ++*p_format;
260 return v;
263 static PyObject *
264 do_mkvalue(char **p_format, va_list *p_va)
266 for (;;) {
267 switch (*(*p_format)++) {
268 case '(':
269 return do_mktuple(p_format, p_va, ')',
270 countformat(*p_format, ')'));
272 case '[':
273 return do_mklist(p_format, p_va, ']',
274 countformat(*p_format, ']'));
276 case '{':
277 return do_mkdict(p_format, p_va, '}',
278 countformat(*p_format, '}'));
280 case 'b':
281 case 'B':
282 case 'h':
283 case 'i':
284 return PyInt_FromLong((long)va_arg(*p_va, int));
286 case 'H':
287 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
289 case 'l':
290 return PyInt_FromLong((long)va_arg(*p_va, long));
292 case 'k':
293 return PyInt_FromLong((long)va_arg(*p_va, unsigned long));
295 #ifdef HAVE_LONG_LONG
296 case 'L':
297 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
299 case 'K':
300 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
301 #endif
302 #ifdef Py_USING_UNICODE
303 case 'u':
305 PyObject *v;
306 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
307 int n;
308 if (**p_format == '#') {
309 ++*p_format;
310 n = va_arg(*p_va, int);
312 else
313 n = -1;
314 if (u == NULL) {
315 v = Py_None;
316 Py_INCREF(v);
318 else {
319 if (n < 0)
320 n = _ustrlen(u);
321 v = PyUnicode_FromUnicode(u, n);
323 return v;
325 #endif
326 case 'f':
327 case 'd':
328 return PyFloat_FromDouble(
329 (double)va_arg(*p_va, va_double));
331 #ifndef WITHOUT_COMPLEX
332 case 'D':
333 return PyComplex_FromCComplex(
334 *((Py_complex *)va_arg(*p_va, Py_complex *)));
335 #endif /* WITHOUT_COMPLEX */
337 case 'c':
339 char p[1];
340 p[0] = va_arg(*p_va, int);
341 return PyString_FromStringAndSize(p, 1);
344 case 's':
345 case 'z':
347 PyObject *v;
348 char *str = va_arg(*p_va, char *);
349 int n;
350 if (**p_format == '#') {
351 ++*p_format;
352 n = va_arg(*p_va, int);
354 else
355 n = -1;
356 if (str == NULL) {
357 v = Py_None;
358 Py_INCREF(v);
360 else {
361 if (n < 0) {
362 size_t m = strlen(str);
363 if (m > INT_MAX) {
364 PyErr_SetString(PyExc_OverflowError,
365 "string too long for Python string");
366 return NULL;
368 n = (int)m;
370 v = PyString_FromStringAndSize(str, n);
372 return v;
375 case 'N':
376 case 'S':
377 case 'O':
378 if (**p_format == '&') {
379 typedef PyObject *(*converter)(void *);
380 converter func = va_arg(*p_va, converter);
381 void *arg = va_arg(*p_va, void *);
382 ++*p_format;
383 return (*func)(arg);
385 else {
386 PyObject *v;
387 v = va_arg(*p_va, PyObject *);
388 if (v != NULL) {
389 if (*(*p_format - 1) != 'N')
390 Py_INCREF(v);
392 else if (!PyErr_Occurred())
393 /* If a NULL was passed
394 * because a call that should
395 * have constructed a value
396 * failed, that's OK, and we
397 * pass the error on; but if
398 * no error occurred it's not
399 * clear that the caller knew
400 * what she was doing. */
401 PyErr_SetString(PyExc_SystemError,
402 "NULL object passed to Py_BuildValue");
403 return v;
406 case ':':
407 case ',':
408 case ' ':
409 case '\t':
410 break;
412 default:
413 PyErr_SetString(PyExc_SystemError,
414 "bad format char passed to Py_BuildValue");
415 return NULL;
422 PyObject *
423 Py_BuildValue(char *format, ...)
425 va_list va;
426 PyObject* retval;
427 va_start(va, format);
428 retval = Py_VaBuildValue(format, va);
429 va_end(va);
430 return retval;
433 PyObject *
434 Py_VaBuildValue(char *format, va_list va)
436 char *f = format;
437 int n = countformat(f, '\0');
438 va_list lva;
440 #ifdef VA_LIST_IS_ARRAY
441 memcpy(lva, va, sizeof(va_list));
442 #else
443 #ifdef __va_copy
444 __va_copy(lva, va);
445 #else
446 lva = va;
447 #endif
448 #endif
450 if (n < 0)
451 return NULL;
452 if (n == 0) {
453 Py_INCREF(Py_None);
454 return Py_None;
456 if (n == 1)
457 return do_mkvalue(&f, &lva);
458 return do_mktuple(&f, &lva, '\0', n);
462 PyObject *
463 PyEval_CallFunction(PyObject *obj, char *format, ...)
465 va_list vargs;
466 PyObject *args;
467 PyObject *res;
469 va_start(vargs, format);
471 args = Py_VaBuildValue(format, vargs);
472 va_end(vargs);
474 if (args == NULL)
475 return NULL;
477 res = PyEval_CallObject(obj, args);
478 Py_DECREF(args);
480 return res;
484 PyObject *
485 PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
487 va_list vargs;
488 PyObject *meth;
489 PyObject *args;
490 PyObject *res;
492 meth = PyObject_GetAttrString(obj, methodname);
493 if (meth == NULL)
494 return NULL;
496 va_start(vargs, format);
498 args = Py_VaBuildValue(format, vargs);
499 va_end(vargs);
501 if (args == NULL) {
502 Py_DECREF(meth);
503 return NULL;
506 res = PyEval_CallObject(meth, args);
507 Py_DECREF(meth);
508 Py_DECREF(args);
510 return res;
514 PyModule_AddObject(PyObject *m, char *name, PyObject *o)
516 PyObject *dict;
517 if (!PyModule_Check(m) || o == NULL) {
518 PyErr_SetString(PyExc_TypeError,
519 "PyModule_AddObject() needs module as first arg");
520 return -1;
522 dict = PyModule_GetDict(m);
523 if (dict == NULL) {
524 /* Internal error -- modules must have a dict! */
525 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
526 PyModule_GetName(m));
527 return -1;
529 if (PyDict_SetItemString(dict, name, o))
530 return -1;
531 Py_DECREF(o);
532 return 0;
535 int
536 PyModule_AddIntConstant(PyObject *m, char *name, long value)
538 return PyModule_AddObject(m, name, PyInt_FromLong(value));
541 int
542 PyModule_AddStringConstant(PyObject *m, char *name, char *value)
544 return PyModule_AddObject(m, name, PyString_FromString(value));