modsupport.c 18 KB

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