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

/edk2/AppPkg/Applications/Python/Python-2.7.2/Python/modsupport.c

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