PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/stub/Python/modsupport.c

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