/xbmc/lib/libPython/Python/Python/modsupport.c
C | 590 lines | 489 code | 59 blank | 42 comment | 124 complexity | 0358a7392cc7c587e56feee2f47a8edf MD5 | raw file
1
2/* Module support implementation */
3
4#include "Python.h"
5
6typedef double va_double;
7
8/* Package context -- the full module name for package imports */
9char *_Py_PackageContext = NULL;
10
11/* Py_InitModule4() parameters:
12 - 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
18
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.
22*/
23
24static char api_version_warning[] =
25"Python C API version mismatch for module %.100s:\
26 This Python has API version %d, module %.100s has version %d.";
27
28PyObject *
29Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
30 PyObject *passthrough, int module_api_version)
31{
32 PyObject *m, *d, *v, *n;
33 PyMethodDef *ml;
34 if (!Py_IsInitialized())
35 Py_FatalError("Interpreter not initialized (version mismatch?)");
36 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 }
45 /* 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 */
54 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 }
61 if ((m = PyImport_AddModule(name)) == NULL)
62 return NULL;
63 d = PyModule_GetDict(m);
64 if (methods != NULL) {
65 n = PyString_FromString(name);
66 if (n == NULL)
67 return NULL;
68 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 Py_DECREF(n);
75 return NULL;
76 }
77 v = PyCFunction_NewEx(ml, passthrough, n);
78 if (v == NULL) {
79 Py_DECREF(n);
80 return NULL;
81 }
82 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
83 Py_DECREF(v);
84 Py_DECREF(n);
85 return NULL;
86 }
87 Py_DECREF(v);
88 }
89 Py_DECREF(n);
90 }
91 if (doc != NULL) {
92 v = PyString_FromString(doc);
93 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
94 Py_XDECREF(v);
95 return NULL;
96 }
97 Py_DECREF(v);
98 }
99 return m;
100}
101
102
103/* Helper for mkvalue() to scan the length of a format */
104
105static int
106countformat(char *format, int endchar)
107{
108 int count = 0;
109 int level = 0;
110 while (level > 0 || *format != endchar) {
111 switch (*format) {
112 case '\0':
113 /* Premature end */
114 PyErr_SetString(PyExc_SystemError,
115 "unmatched paren in format");
116 return -1;
117 case '(':
118 case '[':
119 case '{':
120 if (level == 0)
121 count++;
122 level++;
123 break;
124 case ')':
125 case ']':
126 case '}':
127 level--;
128 break;
129 case '#':
130 case '&':
131 case ',':
132 case ':':
133 case ' ':
134 case '\t':
135 break;
136 default:
137 if (level == 0)
138 count++;
139 }
140 format++;
141 }
142 return count;
143}
144
145
146/* Generic function to create a value -- the inverse of getargs() */
147/* After an original idea and first implementation by Steven Miale */
148
149static PyObject *do_mktuple(char**, va_list *, int, int);
150static PyObject *do_mklist(char**, va_list *, int, int);
151static PyObject *do_mkdict(char**, va_list *, int, int);
152static PyObject *do_mkvalue(char**, va_list *);
153
154
155static PyObject *
156do_mkdict(char **p_format, va_list *p_va, int endchar, int n)
157{
158 PyObject *d;
159 int i;
160 int itemfailed = 0;
161 if (n < 0)
162 return NULL;
163 if ((d = PyDict_New()) == NULL)
164 return NULL;
165 /* Note that we can't bail immediately on error as this will leak
166 refcounts on any 'N' arguments. */
167 for (i = 0; i < n; i+= 2) {
168 PyObject *k, *v;
169 int err;
170 k = do_mkvalue(p_format, p_va);
171 if (k == NULL) {
172 itemfailed = 1;
173 Py_INCREF(Py_None);
174 k = Py_None;
175 }
176 v = do_mkvalue(p_format, p_va);
177 if (v == NULL) {
178 itemfailed = 1;
179 Py_INCREF(Py_None);
180 v = Py_None;
181 }
182 err = PyDict_SetItem(d, k, v);
183 Py_DECREF(k);
184 Py_DECREF(v);
185 if (err < 0 || itemfailed) {
186 Py_DECREF(d);
187 return NULL;
188 }
189 }
190 if (d != NULL && **p_format != endchar) {
191 Py_DECREF(d);
192 d = NULL;
193 PyErr_SetString(PyExc_SystemError,
194 "Unmatched paren in format");
195 }
196 else if (endchar)
197 ++*p_format;
198 return d;
199}
200
201static PyObject *
202do_mklist(char **p_format, va_list *p_va, int endchar, int n)
203{
204 PyObject *v;
205 int i;
206 int itemfailed = 0;
207 if (n < 0)
208 return NULL;
209 if ((v = PyList_New(n)) == NULL)
210 return NULL;
211 /* Note that we can't bail immediately on error as this will leak
212 refcounts on any 'N' arguments. */
213 for (i = 0; i < n; i++) {
214 PyObject *w = do_mkvalue(p_format, p_va);
215 if (w == NULL) {
216 itemfailed = 1;
217 Py_INCREF(Py_None);
218 w = Py_None;
219 }
220 PyList_SetItem(v, i, w);
221 }
222 if (v != NULL && **p_format != endchar) {
223 Py_DECREF(v);
224 v = NULL;
225 PyErr_SetString(PyExc_SystemError,
226 "Unmatched paren in format");
227 }
228 else if (endchar)
229 ++*p_format;
230 if (itemfailed) {
231 Py_DECREF(v);
232 v = NULL;
233 }
234 return v;
235}
236
237#ifdef Py_USING_UNICODE
238static int
239_ustrlen(Py_UNICODE *u)
240{
241 int i = 0;
242 Py_UNICODE *v = u;
243 while (*v != 0) { i++; v++; }
244 return i;
245}
246#endif
247
248static PyObject *
249do_mktuple(char **p_format, va_list *p_va, int endchar, int n)
250{
251 PyObject *v;
252 int i;
253 int itemfailed = 0;
254 if (n < 0)
255 return NULL;
256 if ((v = PyTuple_New(n)) == NULL)
257 return NULL;
258 /* Note that we can't bail immediately on error as this will leak
259 refcounts on any 'N' arguments. */
260 for (i = 0; i < n; i++) {
261 PyObject *w = do_mkvalue(p_format, p_va);
262 if (w == NULL) {
263 itemfailed = 1;
264 Py_INCREF(Py_None);
265 w = Py_None;
266 }
267 PyTuple_SetItem(v, i, w);
268 }
269 if (v != NULL && **p_format != endchar) {
270 Py_DECREF(v);
271 v = NULL;
272 PyErr_SetString(PyExc_SystemError,
273 "Unmatched paren in format");
274 }
275 else if (endchar)
276 ++*p_format;
277 if (itemfailed) {
278 Py_DECREF(v);
279 v = NULL;
280 }
281 return v;
282}
283
284static PyObject *
285do_mkvalue(char **p_format, va_list *p_va)
286{
287 for (;;) {
288 switch (*(*p_format)++) {
289 case '(':
290 return do_mktuple(p_format, p_va, ')',
291 countformat(*p_format, ')'));
292
293 case '[':
294 return do_mklist(p_format, p_va, ']',
295 countformat(*p_format, ']'));
296
297 case '{':
298 return do_mkdict(p_format, p_va, '}',
299 countformat(*p_format, '}'));
300
301 case 'b':
302 case 'B':
303 case 'h':
304 case 'i':
305 return PyInt_FromLong((long)va_arg(*p_va, int));
306
307 case 'H':
308 return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
309
310 case 'I':
311 {
312 unsigned int n;
313 n = va_arg(*p_va, unsigned int);
314 if (n > (unsigned long)PyInt_GetMax())
315 return PyLong_FromUnsignedLong((unsigned long)n);
316 else
317 return PyInt_FromLong(n);
318 }
319
320 case 'l':
321 return PyInt_FromLong(va_arg(*p_va, long));
322
323 case 'k':
324 {
325 unsigned long n;
326 n = va_arg(*p_va, unsigned long);
327 if (n > (unsigned long)PyInt_GetMax())
328 return PyLong_FromUnsignedLong(n);
329 else
330 return PyInt_FromLong(n);
331 }
332
333#ifdef HAVE_LONG_LONG
334 case 'L':
335 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
336
337 case 'K':
338 return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
339#endif
340#ifdef Py_USING_UNICODE
341 case 'u':
342 {
343 PyObject *v;
344 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
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 (u == NULL) {
353 v = Py_None;
354 Py_INCREF(v);
355 }
356 else {
357 if (n < 0)
358 n = _ustrlen(u);
359 v = PyUnicode_FromUnicode(u, n);
360 }
361 return v;
362 }
363#endif
364 case 'f':
365 case 'd':
366 return PyFloat_FromDouble(
367 (double)va_arg(*p_va, va_double));
368
369#ifndef WITHOUT_COMPLEX
370 case 'D':
371 return PyComplex_FromCComplex(
372 *((Py_complex *)va_arg(*p_va, Py_complex *)));
373#endif /* WITHOUT_COMPLEX */
374
375 case 'c':
376 {
377 char p[1];
378 p[0] = va_arg(*p_va, int);
379 return PyString_FromStringAndSize(p, 1);
380 }
381
382 case 's':
383 case 'z':
384 {
385 PyObject *v;
386 char *str = va_arg(*p_va, char *);
387 int n;
388 if (**p_format == '#') {
389 ++*p_format;
390 n = va_arg(*p_va, int);
391 }
392 else
393 n = -1;
394 if (str == NULL) {
395 v = Py_None;
396 Py_INCREF(v);
397 }
398 else {
399 if (n < 0) {
400 size_t m = strlen(str);
401 if (m > INT_MAX) {
402 PyErr_SetString(PyExc_OverflowError,
403 "string too long for Python string");
404 return NULL;
405 }
406 n = (int)m;
407 }
408 v = PyString_FromStringAndSize(str, n);
409 }
410 return v;
411 }
412
413 case 'N':
414 case 'S':
415 case 'O':
416 if (**p_format == '&') {
417 typedef PyObject *(*converter)(void *);
418 converter func = va_arg(*p_va, converter);
419 void *arg = va_arg(*p_va, void *);
420 ++*p_format;
421 return (*func)(arg);
422 }
423 else {
424 PyObject *v;
425 v = va_arg(*p_va, PyObject *);
426 if (v != NULL) {
427 if (*(*p_format - 1) != 'N')
428 Py_INCREF(v);
429 }
430 else if (!PyErr_Occurred())
431 /* If a NULL was passed
432 * because a call that should
433 * have constructed a value
434 * failed, that's OK, and we
435 * pass the error on; but if
436 * no error occurred it's not
437 * clear that the caller knew
438 * what she was doing. */
439 PyErr_SetString(PyExc_SystemError,
440 "NULL object passed to Py_BuildValue");
441 return v;
442 }
443
444 case ':':
445 case ',':
446 case ' ':
447 case '\t':
448 break;
449
450 default:
451 PyErr_SetString(PyExc_SystemError,
452 "bad format char passed to Py_BuildValue");
453 return NULL;
454
455 }
456 }
457}
458
459
460PyObject *
461Py_BuildValue(char *format, ...)
462{
463 va_list va;
464 PyObject* retval;
465 va_start(va, format);
466 retval = Py_VaBuildValue(format, va);
467 va_end(va);
468 return retval;
469}
470
471PyObject *
472Py_VaBuildValue(char *format, va_list va)
473{
474 char *f = format;
475 int n = countformat(f, '\0');
476 va_list lva;
477
478#ifdef VA_LIST_IS_ARRAY
479 memcpy(lva, va, sizeof(va_list));
480#else
481#ifdef __va_copy
482 __va_copy(lva, va);
483#else
484 lva = va;
485#endif
486#endif
487
488 if (n < 0)
489 return NULL;
490 if (n == 0) {
491 Py_INCREF(Py_None);
492 return Py_None;
493 }
494 if (n == 1)
495 return do_mkvalue(&f, &lva);
496 return do_mktuple(&f, &lva, '\0', n);
497}
498
499
500PyObject *
501PyEval_CallFunction(PyObject *obj, char *format, ...)
502{
503 va_list vargs;
504 PyObject *args;
505 PyObject *res;
506
507 va_start(vargs, format);
508
509 args = Py_VaBuildValue(format, vargs);
510 va_end(vargs);
511
512 if (args == NULL)
513 return NULL;
514
515 res = PyEval_CallObject(obj, args);
516 Py_DECREF(args);
517
518 return res;
519}
520
521
522PyObject *
523PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
524{
525 va_list vargs;
526 PyObject *meth;
527 PyObject *args;
528 PyObject *res;
529
530 meth = PyObject_GetAttrString(obj, methodname);
531 if (meth == NULL)
532 return NULL;
533
534 va_start(vargs, format);
535
536 args = Py_VaBuildValue(format, vargs);
537 va_end(vargs);
538
539 if (args == NULL) {
540 Py_DECREF(meth);
541 return NULL;
542 }
543
544 res = PyEval_CallObject(meth, args);
545 Py_DECREF(meth);
546 Py_DECREF(args);
547
548 return res;
549}
550
551int
552PyModule_AddObject(PyObject *m, char *name, PyObject *o)
553{
554 PyObject *dict;
555 if (!PyModule_Check(m)) {
556 PyErr_SetString(PyExc_TypeError,
557 "PyModule_AddObject() needs module as first arg");
558 return -1;
559 }
560 if (!o) {
561 if (!PyErr_Occurred())
562 PyErr_SetString(PyExc_TypeError,
563 "PyModule_AddObject() needs non-NULL value");
564 return -1;
565 }
566
567 dict = PyModule_GetDict(m);
568 if (dict == NULL) {
569 /* Internal error -- modules must have a dict! */
570 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
571 PyModule_GetName(m));
572 return -1;
573 }
574 if (PyDict_SetItemString(dict, name, o))
575 return -1;
576 Py_DECREF(o);
577 return 0;
578}
579
580int
581PyModule_AddIntConstant(PyObject *m, char *name, long value)
582{
583 return PyModule_AddObject(m, name, PyInt_FromLong(value));
584}
585
586int
587PyModule_AddStringConstant(PyObject *m, char *name, char *value)
588{
589 return PyModule_AddObject(m, name, PyString_FromString(value));
590}