PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/cpyext/src/modsupport.c

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