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.

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