PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/necco/python/Python/modsupport.c

https://github.com/brosner/cleese
C | 405 lines | 357 code | 44 blank | 4 comment | 91 complexity | 9b53b90b5af7c53be900e5b5abf5a53e MD5 | raw file
  1. /* Module support implementation */
  2. #include "Python.h"
  3. typedef double va_double;
  4. PyObject *
  5. Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
  6. PyObject *passthrough, int module_api_version)
  7. {
  8. PyObject *m, *d, *v, *n;
  9. PyMethodDef *ml;
  10. if ((m = PyImport_AddModule(name)) == NULL)
  11. return NULL;
  12. d = PyModule_GetDict(m);
  13. if (methods != NULL) {
  14. n = PyString_FromString(name);
  15. if (n == NULL)
  16. return NULL;
  17. for (ml = methods; ml->ml_name != NULL; ml++) {
  18. v = PyCFunction_NewEx(ml, passthrough, n);
  19. if (v == NULL)
  20. return NULL;
  21. if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
  22. Py_DECREF(v);
  23. return NULL;
  24. }
  25. Py_DECREF(v);
  26. }
  27. }
  28. return m;
  29. }
  30. static int
  31. countformat(char *format, int endchar)
  32. {
  33. int count = 0;
  34. int level = 0;
  35. while (level > 0 || *format != endchar) {
  36. switch (*format) {
  37. case '\0':
  38. /* Premature end */
  39. printf("unmatched paren in format\n");
  40. return -1;
  41. case '(':
  42. case '[':
  43. case '{':
  44. if (level == 0)
  45. count++;
  46. level++;
  47. break;
  48. case ')':
  49. case ']':
  50. case '}':
  51. level--;
  52. break;
  53. case '#':
  54. case '&':
  55. case ',':
  56. case ':':
  57. case ' ':
  58. case '\t':
  59. break;
  60. default:
  61. if (level == 0)
  62. count++;
  63. }
  64. format++;
  65. }
  66. return count;
  67. }
  68. /* forward reference */
  69. static PyObject *do_mklist(char**, va_list *, int, int);
  70. static PyObject *do_mkdict(char**, va_list *, int, int);
  71. static PyObject *do_mkvalue(char**, va_list *);
  72. static PyObject *
  73. do_mkdict(char **p_format, va_list *p_va, int endchar, int n)
  74. {
  75. PyObject *d;
  76. int i;
  77. if (n < 0)
  78. return NULL;
  79. if ((d = PyDict_New()) == NULL)
  80. return NULL;
  81. for (i = 0; i < n; i+= 2) {
  82. PyObject *k, *v;
  83. int err;
  84. k = do_mkvalue(p_format, p_va);
  85. if (k == NULL) {
  86. Py_DECREF(d);
  87. return NULL;
  88. }
  89. v = do_mkvalue(p_format, p_va);
  90. if (v == NULL) {
  91. Py_DECREF(k);
  92. Py_DECREF(d);
  93. return NULL;
  94. }
  95. err = PyDict_SetItem(d, k, v);
  96. Py_DECREF(k);
  97. Py_DECREF(v);
  98. if (err < 0) {
  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(char **p_format, va_list *p_va, int endchar, int n)
  115. {
  116. PyObject *v;
  117. int i;
  118. if (n < 0)
  119. return NULL;
  120. if ((v = PyList_New(n)) == NULL)
  121. return NULL;
  122. for (i = 0; i < n; i++) {
  123. PyObject *w = do_mkvalue(p_format, p_va);
  124. if (w == NULL) {
  125. Py_DECREF(v);
  126. return NULL;
  127. }
  128. PyList_SetItem(v, i, w);
  129. }
  130. if (v != NULL && **p_format != endchar) {
  131. Py_DECREF(v);
  132. v = NULL;
  133. PyErr_SetString(PyExc_SystemError,
  134. "Unmatched paren in format");
  135. }
  136. else if (endchar)
  137. ++*p_format;
  138. return v;
  139. }
  140. static PyObject *
  141. do_mktuple(char **p_format, va_list *p_va, int endchar, int n)
  142. {
  143. PyObject *v;
  144. int i;
  145. if (n < 0)
  146. return NULL;
  147. if ((v = PyTuple_New(n)) == NULL)
  148. return NULL;
  149. for (i = 0; i < n; i++) {
  150. PyObject *w = do_mkvalue(p_format, p_va);
  151. if (w == NULL) {
  152. Py_DECREF(v);
  153. return NULL;
  154. }
  155. PyTuple_SetItem(v, i, w);
  156. }
  157. if (v != NULL && **p_format != endchar) {
  158. Py_DECREF(v);
  159. v = NULL;
  160. printf("Unmatched paren in format");
  161. }
  162. else if (endchar)
  163. ++*p_format;
  164. return v;
  165. }
  166. static PyObject *
  167. do_mkvalue(char **p_format, va_list *p_va)
  168. {
  169. for (;;) {
  170. switch (*(*p_format)++) {
  171. case '(':
  172. return do_mktuple(p_format, p_va, ')',
  173. countformat(*p_format, ')'));
  174. case '[':
  175. return do_mklist(p_format, p_va, ']',
  176. countformat(*p_format, ']'));
  177. case '{':
  178. return do_mkdict(p_format, p_va, '}',
  179. countformat(*p_format, '}'));
  180. case 'b':
  181. case 'B':
  182. case 'h':
  183. case 'i':
  184. return PyInt_FromLong((long)va_arg(*p_va, int));
  185. case 'H':
  186. return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
  187. case 'l':
  188. return PyInt_FromLong((long)va_arg(*p_va, long));
  189. case 'k':
  190. return PyInt_FromLong((long)va_arg(*p_va, unsigned long));
  191. case 'f':
  192. case 'd':
  193. return PyFloat_FromDouble(
  194. (double)va_arg(*p_va, va_double));
  195. case 'c':
  196. {
  197. char p[1];
  198. p[0] = va_arg(*p_va, int);
  199. return PyString_FromStringAndSize(p, 1);
  200. }
  201. case 's':
  202. case 'z':
  203. {
  204. PyObject *v;
  205. char *str = va_arg(*p_va, char *);
  206. int n;
  207. if (**p_format == '#') {
  208. ++*p_format;
  209. n = va_arg(*p_va, int);
  210. }
  211. else
  212. n = -1;
  213. if (str == NULL) {
  214. v = Py_None;
  215. Py_INCREF(v);
  216. }
  217. else {
  218. if (n < 0) {
  219. size_t m = strlen(str);
  220. if (m > INT_MAX) {
  221. printf("string too long for Python string");
  222. return NULL;
  223. }
  224. n = (int)m;
  225. }
  226. v = PyString_FromStringAndSize(str, n);
  227. }
  228. return v;
  229. }
  230. case 'N':
  231. case 'S':
  232. case 'O':
  233. if (**p_format == '&') {
  234. typedef PyObject *(*converter)(void *);
  235. converter func = va_arg(*p_va, converter);
  236. void *arg = va_arg(*p_va, void *);
  237. ++*p_format;
  238. return (*func)(arg);
  239. }
  240. else {
  241. PyObject *v;
  242. v = va_arg(*p_va, PyObject *);
  243. if (v != NULL) {
  244. if (*(*p_format - 1) != 'N')
  245. Py_INCREF(v);
  246. }
  247. else
  248. printf("NULL object passed to Py_BuildValue");
  249. return v;
  250. }
  251. case ':':
  252. case ',':
  253. case ' ':
  254. case '\t':
  255. break;
  256. default:
  257. printf("bad format char passed to Py_BuildValue\n");
  258. return NULL;
  259. }
  260. }
  261. }
  262. PyObject *
  263. Py_VaBuildValue(char *format, va_list va)
  264. {
  265. char *f = format;
  266. int n = countformat(f, '\0');
  267. va_list lva;
  268. lva = va;
  269. if (n < 0)
  270. return NULL;
  271. if (n == 0) {
  272. Py_INCREF(Py_None);
  273. return Py_None;
  274. }
  275. if (n == 1)
  276. return do_mkvalue(&f, &lva);
  277. return do_mktuple(&f, &lva, '\0', n);
  278. }
  279. PyObject *
  280. Py_BuildValue(char *format, ...)
  281. {
  282. va_list va;
  283. PyObject* retval;
  284. va_start(va, format);
  285. retval = Py_VaBuildValue(format, va);
  286. va_end(va);
  287. return retval;
  288. }
  289. PyObject *
  290. PyEval_CallFunction(PyObject *obj, char *format, ...)
  291. {
  292. va_list vargs;
  293. PyObject *args;
  294. PyObject *res;
  295. va_start(vargs, format);
  296. args = Py_VaBuildValue(format, vargs);
  297. va_end(vargs);
  298. if (args == NULL)
  299. return NULL;
  300. res = PyEval_CallObject(obj, args);
  301. Py_DECREF(args);
  302. return res;
  303. }
  304. PyObject *
  305. PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
  306. {
  307. va_list vargs;
  308. PyObject *meth;
  309. PyObject *args;
  310. PyObject *res;
  311. meth = PyObject_GetAttrString(obj, methodname);
  312. if (meth == NULL)
  313. return NULL;
  314. va_start(vargs, format);
  315. args = Py_VaBuildValue(format, vargs);
  316. va_end(vargs);
  317. if (args == NULL) {
  318. Py_DECREF(meth);
  319. return NULL;
  320. }
  321. res = PyEval_CallObject(meth, args);
  322. Py_DECREF(meth);
  323. Py_DECREF(args);
  324. return res;
  325. }
  326. int
  327. PyModule_AddObject(PyObject *m, char *name, PyObject *o)
  328. {
  329. PyObject *dict;
  330. if (!PyModule_Check(m)) {
  331. PyErr_SetString(PyExc_TypeError,
  332. "PyModule_AddObject() needs module as first arg");
  333. return -1;
  334. }
  335. if (!o) {
  336. PyErr_SetString(PyExc_TypeError,
  337. "PyModule_AddObject() needs non-NULL value");
  338. return -1;
  339. }
  340. dict = PyModule_GetDict(m);
  341. if (dict == NULL) {
  342. /* Internal error -- modules must have a dict! */
  343. PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
  344. PyModule_GetName(m));
  345. return -1;
  346. }
  347. if (PyDict_SetItemString(dict, name, o))
  348. return -1;
  349. Py_DECREF(o);
  350. return 0;
  351. }
  352. int
  353. PyModule_AddIntConstant(PyObject *m, char *name, long value)
  354. {
  355. return PyModule_AddObject(m, name, PyInt_FromLong(value));
  356. }
  357. int
  358. PyModule_AddStringConstant(PyObject *m, char *name, char *value)
  359. {
  360. return PyModule_AddObject(m, name, PyString_FromString(value));
  361. }