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.

561 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 PyObject *
  137. do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
  138. {
  139. PyObject *v;
  140. int i;
  141. int itemfailed = 0;
  142. if (n < 0)
  143. return NULL;
  144. if ((v = PyTuple_New(n)) == NULL)
  145. return NULL;
  146. /* Note that we can't bail immediately on error as this will leak
  147. refcounts on any 'N' arguments. */
  148. for (i = 0; i < n; i++) {
  149. PyObject *w = do_mkvalue(p_format, p_va, flags);
  150. if (w == NULL) {
  151. itemfailed = 1;
  152. Py_INCREF(Py_None);
  153. w = Py_None;
  154. }
  155. PyTuple_SET_ITEM(v, i, w);
  156. }
  157. if (itemfailed) {
  158. /* do_mkvalue() should have already set an error */
  159. Py_DECREF(v);
  160. return NULL;
  161. }
  162. if (**p_format != endchar) {
  163. Py_DECREF(v);
  164. PyErr_SetString(PyExc_SystemError,
  165. "Unmatched paren in format");
  166. return NULL;
  167. }
  168. if (endchar)
  169. ++*p_format;
  170. return v;
  171. }
  172. static PyObject *
  173. do_mkvalue(const char **p_format, va_list *p_va, int flags)
  174. {
  175. for (;;) {
  176. switch (*(*p_format)++) {
  177. case '(':
  178. return do_mktuple(p_format, p_va, ')',
  179. countformat(*p_format, ')'), flags);
  180. case '[':
  181. return do_mklist(p_format, p_va, ']',
  182. countformat(*p_format, ']'), flags);
  183. case '{':
  184. return do_mkdict(p_format, p_va, '}',
  185. countformat(*p_format, '}'), flags);
  186. case 'b':
  187. case 'B':
  188. case 'h':
  189. case 'i':
  190. return PyLong_FromLong((long)va_arg(*p_va, int));
  191. case 'H':
  192. return PyLong_FromLong((long)va_arg(*p_va, unsigned int));
  193. case 'I':
  194. {
  195. unsigned int n;
  196. n = va_arg(*p_va, unsigned int);
  197. return PyLong_FromUnsignedLong(n);
  198. }
  199. case 'n':
  200. #if SIZEOF_SIZE_T!=SIZEOF_LONG
  201. return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t));
  202. #endif
  203. /* Fall through from 'n' to 'l' if Py_ssize_t is long */
  204. case 'l':
  205. return PyLong_FromLong(va_arg(*p_va, long));
  206. case 'k':
  207. {
  208. unsigned long n;
  209. n = va_arg(*p_va, unsigned long);
  210. return PyLong_FromUnsignedLong(n);
  211. }
  212. #ifdef HAVE_LONG_LONG
  213. case 'L':
  214. return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
  215. case 'K':
  216. return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
  217. #endif
  218. case 'u':
  219. {
  220. PyObject *v;
  221. Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
  222. Py_ssize_t n;
  223. if (**p_format == '#') {
  224. ++*p_format;
  225. if (flags & FLAG_SIZE_T)
  226. n = va_arg(*p_va, Py_ssize_t);
  227. else
  228. n = va_arg(*p_va, int);
  229. }
  230. else
  231. n = -1;
  232. if (u == NULL) {
  233. v = Py_None;
  234. Py_INCREF(v);
  235. }
  236. else {
  237. if (n < 0)
  238. n = Py_UNICODE_strlen(u);
  239. v = PyUnicode_FromUnicode(u, n);
  240. }
  241. return v;
  242. }
  243. case 'f':
  244. case 'd':
  245. return PyFloat_FromDouble(
  246. (double)va_arg(*p_va, va_double));
  247. case 'D':
  248. return PyComplex_FromCComplex(
  249. *((Py_complex *)va_arg(*p_va, Py_complex *)));
  250. case 'c':
  251. {
  252. char p[1];
  253. p[0] = (char)va_arg(*p_va, int);
  254. return PyBytes_FromStringAndSize(p, 1);
  255. }
  256. case 'C':
  257. {
  258. int i = va_arg(*p_va, int);
  259. return PyUnicode_FromOrdinal(i);
  260. }
  261. case 's':
  262. case 'z':
  263. case 'U': /* XXX deprecated alias */
  264. {
  265. PyObject *v;
  266. char *str = va_arg(*p_va, char *);
  267. Py_ssize_t n;
  268. if (**p_format == '#') {
  269. ++*p_format;
  270. if (flags & FLAG_SIZE_T)
  271. n = va_arg(*p_va, Py_ssize_t);
  272. else
  273. n = va_arg(*p_va, int);
  274. }
  275. else
  276. n = -1;
  277. if (str == NULL) {
  278. v = Py_None;
  279. Py_INCREF(v);
  280. }
  281. else {
  282. if (n < 0) {
  283. size_t m = strlen(str);
  284. if (m > PY_SSIZE_T_MAX) {
  285. PyErr_SetString(PyExc_OverflowError,
  286. "string too long for Python string");
  287. return NULL;
  288. }
  289. n = (Py_ssize_t)m;
  290. }
  291. v = PyUnicode_FromStringAndSize(str, n);
  292. }
  293. return v;
  294. }
  295. case 'y':
  296. {
  297. PyObject *v;
  298. char *str = va_arg(*p_va, char *);
  299. Py_ssize_t n;
  300. if (**p_format == '#') {
  301. ++*p_format;
  302. if (flags & FLAG_SIZE_T)
  303. n = va_arg(*p_va, Py_ssize_t);
  304. else
  305. n = va_arg(*p_va, int);
  306. }
  307. else
  308. n = -1;
  309. if (str == NULL) {
  310. v = Py_None;
  311. Py_INCREF(v);
  312. }
  313. else {
  314. if (n < 0) {
  315. size_t m = strlen(str);
  316. if (m > PY_SSIZE_T_MAX) {
  317. PyErr_SetString(PyExc_OverflowError,
  318. "string too long for Python bytes");
  319. return NULL;
  320. }
  321. n = (Py_ssize_t)m;
  322. }
  323. v = PyBytes_FromStringAndSize(str, n);
  324. }
  325. return v;
  326. }
  327. case 'N':
  328. case 'S':
  329. case 'O':
  330. if (**p_format == '&') {
  331. typedef PyObject *(*converter)(void *);
  332. converter func = va_arg(*p_va, converter);
  333. void *arg = va_arg(*p_va, void *);
  334. ++*p_format;
  335. return (*func)(arg);
  336. }
  337. else {
  338. PyObject *v;
  339. v = va_arg(*p_va, PyObject *);
  340. if (v != NULL) {
  341. if (*(*p_format - 1) != 'N')
  342. Py_INCREF(v);
  343. }
  344. else if (!PyErr_Occurred())
  345. /* If a NULL was passed
  346. * because a call that should
  347. * have constructed a value
  348. * failed, that's OK, and we
  349. * pass the error on; but if
  350. * no error occurred it's not
  351. * clear that the caller knew
  352. * what she was doing. */
  353. PyErr_SetString(PyExc_SystemError,
  354. "NULL object passed to Py_BuildValue");
  355. return v;
  356. }
  357. case ':':
  358. case ',':
  359. case ' ':
  360. case '\t':
  361. break;
  362. default:
  363. PyErr_SetString(PyExc_SystemError,
  364. "bad format char passed to Py_BuildValue");
  365. return NULL;
  366. }
  367. }
  368. }
  369. PyObject *
  370. Py_BuildValue(const char *format, ...)
  371. {
  372. va_list va;
  373. PyObject* retval;
  374. va_start(va, format);
  375. retval = va_build_value(format, va, 0);
  376. va_end(va);
  377. return retval;
  378. }
  379. PyObject *
  380. _Py_BuildValue_SizeT(const char *format, ...)
  381. {
  382. va_list va;
  383. PyObject* retval;
  384. va_start(va, format);
  385. retval = va_build_value(format, va, FLAG_SIZE_T);
  386. va_end(va);
  387. return retval;
  388. }
  389. PyObject *
  390. Py_VaBuildValue(const char *format, va_list va)
  391. {
  392. return va_build_value(format, va, 0);
  393. }
  394. PyObject *
  395. _Py_VaBuildValue_SizeT(const char *format, va_list va)
  396. {
  397. return va_build_value(format, va, FLAG_SIZE_T);
  398. }
  399. static PyObject *
  400. va_build_value(const char *format, va_list va, int flags)
  401. {
  402. const char *f = format;
  403. int n = countformat(f, '\0');
  404. va_list lva;
  405. Py_VA_COPY(lva, va);
  406. if (n < 0)
  407. return NULL;
  408. if (n == 0) {
  409. Py_INCREF(Py_None);
  410. return Py_None;
  411. }
  412. if (n == 1)
  413. return do_mkvalue(&f, &lva, flags);
  414. return do_mktuple(&f, &lva, '\0', n, flags);
  415. }
  416. PyObject *
  417. PyEval_CallFunction(PyObject *obj, const char *format, ...)
  418. {
  419. va_list vargs;
  420. PyObject *args;
  421. PyObject *res;
  422. va_start(vargs, format);
  423. args = Py_VaBuildValue(format, vargs);
  424. va_end(vargs);
  425. if (args == NULL)
  426. return NULL;
  427. res = PyEval_CallObject(obj, args);
  428. Py_DECREF(args);
  429. return res;
  430. }
  431. PyObject *
  432. PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
  433. {
  434. va_list vargs;
  435. PyObject *meth;
  436. PyObject *args;
  437. PyObject *res;
  438. meth = PyObject_GetAttrString(obj, methodname);
  439. if (meth == NULL)
  440. return NULL;
  441. va_start(vargs, format);
  442. args = Py_VaBuildValue(format, vargs);
  443. va_end(vargs);
  444. if (args == NULL) {
  445. Py_DECREF(meth);
  446. return NULL;
  447. }
  448. res = PyEval_CallObject(meth, args);
  449. Py_DECREF(meth);
  450. Py_DECREF(args);
  451. return res;
  452. }
  453. int
  454. PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
  455. {
  456. PyObject *dict;
  457. if (!PyModule_Check(m)) {
  458. PyErr_SetString(PyExc_TypeError,
  459. "PyModule_AddObject() needs module as first arg");
  460. return -1;
  461. }
  462. if (!o) {
  463. if (!PyErr_Occurred())
  464. PyErr_SetString(PyExc_TypeError,
  465. "PyModule_AddObject() needs non-NULL value");
  466. return -1;
  467. }
  468. dict = PyModule_GetDict(m);
  469. if (dict == NULL) {
  470. /* Internal error -- modules must have a dict! */
  471. PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
  472. PyModule_GetName(m));
  473. return -1;
  474. }
  475. if (PyDict_SetItemString(dict, name, o))
  476. return -1;
  477. Py_DECREF(o);
  478. return 0;
  479. }
  480. int
  481. PyModule_AddIntConstant(PyObject *m, const char *name, long value)
  482. {
  483. PyObject *o = PyLong_FromLong(value);
  484. if (!o)
  485. return -1;
  486. if (PyModule_AddObject(m, name, o) == 0)
  487. return 0;
  488. Py_DECREF(o);
  489. return -1;
  490. }
  491. int
  492. PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
  493. {
  494. PyObject *o = PyUnicode_FromString(value);
  495. if (!o)
  496. return -1;
  497. if (PyModule_AddObject(m, name, o) == 0)
  498. return 0;
  499. Py_DECREF(o);
  500. return -1;
  501. }