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.

587 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. case 'L':
  238. return PyLong_FromLongLong((long long)va_arg(*p_va, long long));
  239. case 'K':
  240. return PyLong_FromUnsignedLongLong((long long)va_arg(*p_va, unsigned long long));
  241. case 'u':
  242. {
  243. PyObject *v;
  244. Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
  245. Py_ssize_t n;
  246. if (**p_format == '#') {
  247. ++*p_format;
  248. if (flags & FLAG_SIZE_T)
  249. n = va_arg(*p_va, Py_ssize_t);
  250. else
  251. n = va_arg(*p_va, int);
  252. }
  253. else
  254. n = -1;
  255. if (u == NULL) {
  256. v = Py_None;
  257. Py_INCREF(v);
  258. }
  259. else {
  260. if (n < 0)
  261. n = Py_UNICODE_strlen(u);
  262. v = PyUnicode_FromUnicode(u, n);
  263. }
  264. return v;
  265. }
  266. case 'f':
  267. case 'd':
  268. return PyFloat_FromDouble(
  269. (double)va_arg(*p_va, va_double));
  270. case 'D':
  271. return PyComplex_FromCComplex(
  272. *((Py_complex *)va_arg(*p_va, Py_complex *)));
  273. case 'c':
  274. {
  275. char p[1];
  276. p[0] = (char)va_arg(*p_va, int);
  277. return PyBytes_FromStringAndSize(p, 1);
  278. }
  279. case 'C':
  280. {
  281. int i = va_arg(*p_va, int);
  282. return PyUnicode_FromOrdinal(i);
  283. }
  284. case 's':
  285. case 'z':
  286. case 'U': /* XXX deprecated alias */
  287. {
  288. PyObject *v;
  289. const char *str = va_arg(*p_va, const char *);
  290. Py_ssize_t n;
  291. if (**p_format == '#') {
  292. ++*p_format;
  293. if (flags & FLAG_SIZE_T)
  294. n = va_arg(*p_va, Py_ssize_t);
  295. else
  296. n = va_arg(*p_va, int);
  297. }
  298. else
  299. n = -1;
  300. if (str == NULL) {
  301. v = Py_None;
  302. Py_INCREF(v);
  303. }
  304. else {
  305. if (n < 0) {
  306. size_t m = strlen(str);
  307. if (m > PY_SSIZE_T_MAX) {
  308. PyErr_SetString(PyExc_OverflowError,
  309. "string too long for Python string");
  310. return NULL;
  311. }
  312. n = (Py_ssize_t)m;
  313. }
  314. v = PyUnicode_FromStringAndSize(str, n);
  315. }
  316. return v;
  317. }
  318. case 'y':
  319. {
  320. PyObject *v;
  321. const char *str = va_arg(*p_va, const char *);
  322. Py_ssize_t n;
  323. if (**p_format == '#') {
  324. ++*p_format;
  325. if (flags & FLAG_SIZE_T)
  326. n = va_arg(*p_va, Py_ssize_t);
  327. else
  328. n = va_arg(*p_va, int);
  329. }
  330. else
  331. n = -1;
  332. if (str == NULL) {
  333. v = Py_None;
  334. Py_INCREF(v);
  335. }
  336. else {
  337. if (n < 0) {
  338. size_t m = strlen(str);
  339. if (m > PY_SSIZE_T_MAX) {
  340. PyErr_SetString(PyExc_OverflowError,
  341. "string too long for Python bytes");
  342. return NULL;
  343. }
  344. n = (Py_ssize_t)m;
  345. }
  346. v = PyBytes_FromStringAndSize(str, n);
  347. }
  348. return v;
  349. }
  350. case 'N':
  351. case 'S':
  352. case 'O':
  353. if (**p_format == '&') {
  354. typedef PyObject *(*converter)(void *);
  355. converter func = va_arg(*p_va, converter);
  356. void *arg = va_arg(*p_va, void *);
  357. ++*p_format;
  358. return (*func)(arg);
  359. }
  360. else {
  361. PyObject *v;
  362. v = va_arg(*p_va, PyObject *);
  363. if (v != NULL) {
  364. if (*(*p_format - 1) != 'N')
  365. Py_INCREF(v);
  366. }
  367. else if (!PyErr_Occurred())
  368. /* If a NULL was passed
  369. * because a call that should
  370. * have constructed a value
  371. * failed, that's OK, and we
  372. * pass the error on; but if
  373. * no error occurred it's not
  374. * clear that the caller knew
  375. * what she was doing. */
  376. PyErr_SetString(PyExc_SystemError,
  377. "NULL object passed to Py_BuildValue");
  378. return v;
  379. }
  380. case ':':
  381. case ',':
  382. case ' ':
  383. case '\t':
  384. break;
  385. default:
  386. PyErr_SetString(PyExc_SystemError,
  387. "bad format char passed to Py_BuildValue");
  388. return NULL;
  389. }
  390. }
  391. }
  392. PyObject *
  393. Py_BuildValue(const char *format, ...)
  394. {
  395. va_list va;
  396. PyObject* retval;
  397. va_start(va, format);
  398. retval = va_build_value(format, va, 0);
  399. va_end(va);
  400. return retval;
  401. }
  402. PyObject *
  403. _Py_BuildValue_SizeT(const char *format, ...)
  404. {
  405. va_list va;
  406. PyObject* retval;
  407. va_start(va, format);
  408. retval = va_build_value(format, va, FLAG_SIZE_T);
  409. va_end(va);
  410. return retval;
  411. }
  412. PyObject *
  413. Py_VaBuildValue(const char *format, va_list va)
  414. {
  415. return va_build_value(format, va, 0);
  416. }
  417. PyObject *
  418. _Py_VaBuildValue_SizeT(const char *format, va_list va)
  419. {
  420. return va_build_value(format, va, FLAG_SIZE_T);
  421. }
  422. static PyObject *
  423. va_build_value(const char *format, va_list va, int flags)
  424. {
  425. const char *f = format;
  426. int n = countformat(f, '\0');
  427. va_list lva;
  428. Py_VA_COPY(lva, va);
  429. if (n < 0)
  430. return NULL;
  431. if (n == 0) {
  432. Py_INCREF(Py_None);
  433. return Py_None;
  434. }
  435. if (n == 1)
  436. return do_mkvalue(&f, &lva, flags);
  437. return do_mktuple(&f, &lva, '\0', n, flags);
  438. }
  439. PyObject *
  440. PyEval_CallFunction(PyObject *obj, const char *format, ...)
  441. {
  442. va_list vargs;
  443. PyObject *args;
  444. PyObject *res;
  445. va_start(vargs, format);
  446. args = Py_VaBuildValue(format, vargs);
  447. va_end(vargs);
  448. if (args == NULL)
  449. return NULL;
  450. res = PyEval_CallObject(obj, args);
  451. Py_DECREF(args);
  452. return res;
  453. }
  454. PyObject *
  455. PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
  456. {
  457. va_list vargs;
  458. PyObject *meth;
  459. PyObject *args;
  460. PyObject *res;
  461. meth = PyObject_GetAttrString(obj, methodname);
  462. if (meth == NULL)
  463. return NULL;
  464. va_start(vargs, format);
  465. args = Py_VaBuildValue(format, vargs);
  466. va_end(vargs);
  467. if (args == NULL) {
  468. Py_DECREF(meth);
  469. return NULL;
  470. }
  471. res = PyEval_CallObject(meth, args);
  472. Py_DECREF(meth);
  473. Py_DECREF(args);
  474. return res;
  475. }
  476. int
  477. PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
  478. {
  479. PyObject *dict;
  480. if (!PyModule_Check(m)) {
  481. PyErr_SetString(PyExc_TypeError,
  482. "PyModule_AddObject() needs module as first arg");
  483. return -1;
  484. }
  485. if (!o) {
  486. if (!PyErr_Occurred())
  487. PyErr_SetString(PyExc_TypeError,
  488. "PyModule_AddObject() needs non-NULL value");
  489. return -1;
  490. }
  491. dict = PyModule_GetDict(m);
  492. if (dict == NULL) {
  493. /* Internal error -- modules must have a dict! */
  494. PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
  495. PyModule_GetName(m));
  496. return -1;
  497. }
  498. if (PyDict_SetItemString(dict, name, o))
  499. return -1;
  500. Py_DECREF(o);
  501. return 0;
  502. }
  503. int
  504. PyModule_AddIntConstant(PyObject *m, const char *name, long value)
  505. {
  506. PyObject *o = PyLong_FromLong(value);
  507. if (!o)
  508. return -1;
  509. if (PyModule_AddObject(m, name, o) == 0)
  510. return 0;
  511. Py_DECREF(o);
  512. return -1;
  513. }
  514. int
  515. PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
  516. {
  517. PyObject *o = PyUnicode_FromString(value);
  518. if (!o)
  519. return -1;
  520. if (PyModule_AddObject(m, name, o) == 0)
  521. return 0;
  522. Py_DECREF(o);
  523. return -1;
  524. }