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

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

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