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.

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