PageRenderTime 22ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/MOULOpenSourceClientPlugin/Plasma20/SDKs/XPlatform/Cypython-2.3.3/Python/modsupport.c

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