You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

588 lines
15 KiB

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