PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/lib/libPython/Python/Python/modsupport.c

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