misc_ops.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. // Misc primitive operations + C helpers
  2. //
  3. // These are registered in mypyc.primitives.misc_ops.
  4. #include <Python.h>
  5. #include <patchlevel.h>
  6. #include "CPy.h"
  7. PyObject *CPy_GetCoro(PyObject *obj)
  8. {
  9. // If the type has an __await__ method, call it,
  10. // otherwise, fallback to calling __iter__.
  11. PyAsyncMethods* async_struct = Py_TYPE(obj)->tp_as_async;
  12. if (async_struct != NULL && async_struct->am_await != NULL) {
  13. return (async_struct->am_await)(obj);
  14. } else {
  15. // TODO: We should check that the type is a generator decorated with
  16. // asyncio.coroutine
  17. return PyObject_GetIter(obj);
  18. }
  19. }
  20. PyObject *CPyIter_Send(PyObject *iter, PyObject *val)
  21. {
  22. // Do a send, or a next if second arg is None.
  23. // (This behavior is to match the PEP 380 spec for yield from.)
  24. _Py_IDENTIFIER(send);
  25. if (Py_IsNone(val)) {
  26. return CPyIter_Next(iter);
  27. } else {
  28. return _PyObject_CallMethodIdOneArg(iter, &PyId_send, val);
  29. }
  30. }
  31. // A somewhat hairy implementation of specifically most of the error handling
  32. // in `yield from` error handling. The point here is to reduce code size.
  33. //
  34. // This implements most of the bodies of the `except` blocks in the
  35. // pseudocode in PEP 380.
  36. //
  37. // Returns true (1) if a StopIteration was received and we should return.
  38. // Returns false (0) if a value should be yielded.
  39. // In both cases the value is stored in outp.
  40. // Signals an error (2) if the an exception should be propagated.
  41. int CPy_YieldFromErrorHandle(PyObject *iter, PyObject **outp)
  42. {
  43. _Py_IDENTIFIER(close);
  44. _Py_IDENTIFIER(throw);
  45. PyObject *exc_type = (PyObject *)Py_TYPE(CPy_ExcState()->exc_value);
  46. PyObject *type, *value, *traceback;
  47. PyObject *_m;
  48. PyObject *res;
  49. *outp = NULL;
  50. if (PyErr_GivenExceptionMatches(exc_type, PyExc_GeneratorExit)) {
  51. _m = _PyObject_GetAttrId(iter, &PyId_close);
  52. if (_m) {
  53. res = PyObject_CallNoArgs(_m);
  54. Py_DECREF(_m);
  55. if (!res)
  56. return 2;
  57. Py_DECREF(res);
  58. } else if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
  59. PyErr_Clear();
  60. } else {
  61. return 2;
  62. }
  63. } else {
  64. _m = _PyObject_GetAttrId(iter, &PyId_throw);
  65. if (_m) {
  66. _CPy_GetExcInfo(&type, &value, &traceback);
  67. res = PyObject_CallFunctionObjArgs(_m, type, value, traceback, NULL);
  68. Py_DECREF(type);
  69. Py_DECREF(value);
  70. Py_DECREF(traceback);
  71. Py_DECREF(_m);
  72. if (res) {
  73. *outp = res;
  74. return 0;
  75. } else {
  76. res = CPy_FetchStopIterationValue();
  77. if (res) {
  78. *outp = res;
  79. return 1;
  80. }
  81. }
  82. } else if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
  83. PyErr_Clear();
  84. } else {
  85. return 2;
  86. }
  87. }
  88. CPy_Reraise();
  89. return 2;
  90. }
  91. PyObject *CPy_FetchStopIterationValue(void)
  92. {
  93. PyObject *val = NULL;
  94. _PyGen_FetchStopIterationValue(&val);
  95. return val;
  96. }
  97. static bool _CPy_IsSafeMetaClass(PyTypeObject *metaclass) {
  98. // mypyc classes can't work with metaclasses in
  99. // general. Through some various nasty hacks we *do*
  100. // manage to work with TypingMeta and its friends.
  101. if (metaclass == &PyType_Type)
  102. return true;
  103. PyObject *module = PyObject_GetAttrString((PyObject *)metaclass, "__module__");
  104. if (!module) {
  105. PyErr_Clear();
  106. return false;
  107. }
  108. bool matches = false;
  109. if (PyUnicode_CompareWithASCIIString(module, "typing") == 0 &&
  110. (strcmp(metaclass->tp_name, "TypingMeta") == 0
  111. || strcmp(metaclass->tp_name, "GenericMeta") == 0
  112. || strcmp(metaclass->tp_name, "_ProtocolMeta") == 0)) {
  113. matches = true;
  114. } else if (PyUnicode_CompareWithASCIIString(module, "typing_extensions") == 0 &&
  115. strcmp(metaclass->tp_name, "_ProtocolMeta") == 0) {
  116. matches = true;
  117. } else if (PyUnicode_CompareWithASCIIString(module, "abc") == 0 &&
  118. strcmp(metaclass->tp_name, "ABCMeta") == 0) {
  119. matches = true;
  120. }
  121. Py_DECREF(module);
  122. return matches;
  123. }
  124. // Create a heap type based on a template non-heap type.
  125. // This is super hacky and maybe we should suck it up and use PyType_FromSpec instead.
  126. // We allow bases to be NULL to represent just inheriting from object.
  127. // We don't support NULL bases and a non-type metaclass.
  128. PyObject *CPyType_FromTemplate(PyObject *template,
  129. PyObject *orig_bases,
  130. PyObject *modname) {
  131. PyTypeObject *template_ = (PyTypeObject *)template;
  132. PyHeapTypeObject *t = NULL;
  133. PyTypeObject *dummy_class = NULL;
  134. PyObject *name = NULL;
  135. PyObject *bases = NULL;
  136. PyObject *slots;
  137. // If the type of the class (the metaclass) is NULL, we default it
  138. // to being type. (This allows us to avoid needing to initialize
  139. // it explicitly on windows.)
  140. if (!Py_TYPE(template_)) {
  141. Py_SET_TYPE(template_, &PyType_Type);
  142. }
  143. PyTypeObject *metaclass = Py_TYPE(template_);
  144. if (orig_bases) {
  145. bases = update_bases(orig_bases);
  146. // update_bases doesn't increment the refcount if nothing changes,
  147. // so we do it to make sure we have distinct "references" to both
  148. if (bases == orig_bases)
  149. Py_INCREF(bases);
  150. // Find the appropriate metaclass from our base classes. We
  151. // care about this because Generic uses a metaclass prior to
  152. // Python 3.7.
  153. metaclass = _PyType_CalculateMetaclass(metaclass, bases);
  154. if (!metaclass)
  155. goto error;
  156. if (!_CPy_IsSafeMetaClass(metaclass)) {
  157. PyErr_SetString(PyExc_TypeError, "mypyc classes can't have a metaclass");
  158. goto error;
  159. }
  160. }
  161. name = PyUnicode_FromString(template_->tp_name);
  162. if (!name)
  163. goto error;
  164. // If there is a metaclass other than type, we would like to call
  165. // its __new__ function. Unfortunately there doesn't seem to be a
  166. // good way to mix a C extension class and creating it via a
  167. // metaclass. We need to do it anyways, though, in order to
  168. // support subclassing Generic[T] prior to Python 3.7.
  169. //
  170. // We solve this with a kind of atrocious hack: create a parallel
  171. // class using the metaclass, determine the bases of the real
  172. // class by pulling them out of the parallel class, creating the
  173. // real class, and then merging its dict back into the original
  174. // class. There are lots of cases where this won't really work,
  175. // but for the case of GenericMeta setting a bunch of properties
  176. // on the class we should be fine.
  177. if (metaclass != &PyType_Type) {
  178. assert(bases && "non-type metaclasses require non-NULL bases");
  179. PyObject *ns = PyDict_New();
  180. if (!ns)
  181. goto error;
  182. if (bases != orig_bases) {
  183. if (PyDict_SetItemString(ns, "__orig_bases__", orig_bases) < 0)
  184. goto error;
  185. }
  186. dummy_class = (PyTypeObject *)PyObject_CallFunctionObjArgs(
  187. (PyObject *)metaclass, name, bases, ns, NULL);
  188. Py_DECREF(ns);
  189. if (!dummy_class)
  190. goto error;
  191. Py_DECREF(bases);
  192. bases = dummy_class->tp_bases;
  193. Py_INCREF(bases);
  194. }
  195. // Allocate the type and then copy the main stuff in.
  196. t = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0);
  197. if (!t)
  198. goto error;
  199. memcpy((char *)t + sizeof(PyVarObject),
  200. (char *)template_ + sizeof(PyVarObject),
  201. sizeof(PyTypeObject) - sizeof(PyVarObject));
  202. if (bases != orig_bases) {
  203. if (PyObject_SetAttrString((PyObject *)t, "__orig_bases__", orig_bases) < 0)
  204. goto error;
  205. }
  206. // Having tp_base set is I think required for stuff to get
  207. // inherited in PyType_Ready, which we needed for subclassing
  208. // BaseException. XXX: Taking the first element is wrong I think though.
  209. if (bases) {
  210. t->ht_type.tp_base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0);
  211. Py_INCREF((PyObject *)t->ht_type.tp_base);
  212. }
  213. t->ht_name = name;
  214. Py_INCREF(name);
  215. t->ht_qualname = name;
  216. t->ht_type.tp_bases = bases;
  217. // references stolen so NULL these out
  218. bases = name = NULL;
  219. if (PyType_Ready((PyTypeObject *)t) < 0)
  220. goto error;
  221. assert(t->ht_type.tp_base != NULL);
  222. // XXX: This is a terrible hack to work around a cpython check on
  223. // the mro. It was needed for mypy.stats. I need to investigate
  224. // what is actually going on here.
  225. Py_INCREF(metaclass);
  226. Py_SET_TYPE(t, metaclass);
  227. if (dummy_class) {
  228. if (PyDict_Merge(t->ht_type.tp_dict, dummy_class->tp_dict, 0) != 0)
  229. goto error;
  230. // This is the *really* tasteless bit. GenericMeta's __new__
  231. // in certain versions of typing sets _gorg to point back to
  232. // the class. We need to override it to keep it from pointing
  233. // to the proxy.
  234. if (PyDict_SetItemString(t->ht_type.tp_dict, "_gorg", (PyObject *)t) < 0)
  235. goto error;
  236. }
  237. // Reject anything that would give us a nontrivial __slots__,
  238. // because the layout will conflict
  239. slots = PyObject_GetAttrString((PyObject *)t, "__slots__");
  240. if (slots) {
  241. // don't fail on an empty __slots__
  242. int is_true = PyObject_IsTrue(slots);
  243. Py_DECREF(slots);
  244. if (is_true > 0)
  245. PyErr_SetString(PyExc_TypeError, "mypyc classes can't have __slots__");
  246. if (is_true != 0)
  247. goto error;
  248. } else {
  249. PyErr_Clear();
  250. }
  251. if (PyObject_SetAttrString((PyObject *)t, "__module__", modname) < 0)
  252. goto error;
  253. if (init_subclass((PyTypeObject *)t, NULL))
  254. goto error;
  255. Py_XDECREF(dummy_class);
  256. #if PY_MINOR_VERSION == 11
  257. // This is a hack. Python 3.11 doesn't include good public APIs to work with managed
  258. // dicts, which are the default for heap types. So we try to opt-out until Python 3.12.
  259. t->ht_type.tp_flags &= ~Py_TPFLAGS_MANAGED_DICT;
  260. #endif
  261. return (PyObject *)t;
  262. error:
  263. Py_XDECREF(t);
  264. Py_XDECREF(bases);
  265. Py_XDECREF(dummy_class);
  266. Py_XDECREF(name);
  267. return NULL;
  268. }
  269. static int _CPy_UpdateObjFromDict(PyObject *obj, PyObject *dict)
  270. {
  271. Py_ssize_t pos = 0;
  272. PyObject *key, *value;
  273. while (PyDict_Next(dict, &pos, &key, &value)) {
  274. if (PyObject_SetAttr(obj, key, value) != 0) {
  275. return -1;
  276. }
  277. }
  278. return 0;
  279. }
  280. /* Support for our partial built-in support for dataclasses.
  281. *
  282. * Take a class we want to make a dataclass, remove any descriptors
  283. * for annotated attributes, swap in the actual values of the class
  284. * variables invoke dataclass, and then restore all of the
  285. * descriptors.
  286. *
  287. * The purpose of all this is that dataclasses uses the values of
  288. * class variables to drive which attributes are required and what the
  289. * default values/factories are for optional attributes. This means
  290. * that the class dict needs to contain those values instead of getset
  291. * descriptors for the attributes when we invoke dataclass.
  292. *
  293. * We need to remove descriptors for attributes even when there is no
  294. * default value for them, or else dataclass will think the descriptor
  295. * is the default value. We remove only the attributes, since we don't
  296. * want dataclasses to try generating functions when they are already
  297. * implemented.
  298. *
  299. * Args:
  300. * dataclass_dec: The decorator to apply
  301. * tp: The class we are making a dataclass
  302. * dict: The dictionary containing values that dataclasses needs
  303. * annotations: The type annotation dictionary
  304. */
  305. int
  306. CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp,
  307. PyObject *dict, PyObject *annotations) {
  308. PyTypeObject *ttp = (PyTypeObject *)tp;
  309. Py_ssize_t pos;
  310. PyObject *res;
  311. /* Make a copy of the original class __dict__ */
  312. PyObject *orig_dict = PyDict_Copy(ttp->tp_dict);
  313. if (!orig_dict) {
  314. goto fail;
  315. }
  316. /* Delete anything that had an annotation */
  317. pos = 0;
  318. PyObject *key;
  319. while (PyDict_Next(annotations, &pos, &key, NULL)) {
  320. if (PyObject_DelAttr(tp, key) != 0) {
  321. goto fail;
  322. }
  323. }
  324. /* Copy in all the attributes that we want dataclass to see */
  325. if (_CPy_UpdateObjFromDict(tp, dict) != 0) {
  326. goto fail;
  327. }
  328. /* Run the @dataclass descriptor */
  329. res = PyObject_CallOneArg(dataclass_dec, tp);
  330. if (!res) {
  331. goto fail;
  332. }
  333. Py_DECREF(res);
  334. /* Copy back the original contents of the dict */
  335. if (_CPy_UpdateObjFromDict(tp, orig_dict) != 0) {
  336. goto fail;
  337. }
  338. Py_DECREF(orig_dict);
  339. return 1;
  340. fail:
  341. Py_XDECREF(orig_dict);
  342. return 0;
  343. }
  344. // Support for pickling; reusable getstate and setstate functions
  345. PyObject *
  346. CPyPickle_SetState(PyObject *obj, PyObject *state)
  347. {
  348. if (_CPy_UpdateObjFromDict(obj, state) != 0) {
  349. return NULL;
  350. }
  351. Py_RETURN_NONE;
  352. }
  353. PyObject *
  354. CPyPickle_GetState(PyObject *obj)
  355. {
  356. PyObject *attrs = NULL, *state = NULL;
  357. attrs = PyObject_GetAttrString((PyObject *)Py_TYPE(obj), "__mypyc_attrs__");
  358. if (!attrs) {
  359. goto fail;
  360. }
  361. if (!PyTuple_Check(attrs)) {
  362. PyErr_SetString(PyExc_TypeError, "__mypyc_attrs__ is not a tuple");
  363. goto fail;
  364. }
  365. state = PyDict_New();
  366. if (!state) {
  367. goto fail;
  368. }
  369. // Collect all the values of attributes in __mypyc_attrs__
  370. // Attributes that are missing we just ignore
  371. int i;
  372. for (i = 0; i < PyTuple_GET_SIZE(attrs); i++) {
  373. PyObject *key = PyTuple_GET_ITEM(attrs, i);
  374. PyObject *value = PyObject_GetAttr(obj, key);
  375. if (!value) {
  376. if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
  377. PyErr_Clear();
  378. continue;
  379. }
  380. goto fail;
  381. }
  382. int result = PyDict_SetItem(state, key, value);
  383. Py_DECREF(value);
  384. if (result != 0) {
  385. goto fail;
  386. }
  387. }
  388. Py_DECREF(attrs);
  389. return state;
  390. fail:
  391. Py_XDECREF(attrs);
  392. Py_XDECREF(state);
  393. return NULL;
  394. }
  395. CPyTagged CPyTagged_Id(PyObject *o) {
  396. return CPyTagged_FromVoidPtr(o);
  397. }
  398. #define MAX_INT_CHARS 22
  399. #define _PyUnicode_LENGTH(op) \
  400. (((PyASCIIObject *)(op))->length)
  401. // using snprintf or PyUnicode_FromFormat was way slower than
  402. // boxing the int and calling PyObject_Str on it, so we implement our own
  403. static int fmt_ssize_t(char *out, Py_ssize_t n) {
  404. bool neg = n < 0;
  405. if (neg) n = -n;
  406. // buf gets filled backward and then we copy it forward
  407. char buf[MAX_INT_CHARS];
  408. int i = 0;
  409. do {
  410. buf[i] = (n % 10) + '0';
  411. n /= 10;
  412. i++;
  413. } while (n);
  414. int len = i;
  415. int j = 0;
  416. if (neg) {
  417. out[j++] = '-';
  418. len++;
  419. }
  420. for (; j < len; j++, i--) {
  421. out[j] = buf[i-1];
  422. }
  423. out[j] = '\0';
  424. return len;
  425. }
  426. static PyObject *CPyTagged_ShortToStr(Py_ssize_t n) {
  427. PyObject *obj = PyUnicode_New(MAX_INT_CHARS, 127);
  428. if (!obj) return NULL;
  429. int len = fmt_ssize_t((char *)PyUnicode_1BYTE_DATA(obj), n);
  430. _PyUnicode_LENGTH(obj) = len;
  431. return obj;
  432. }
  433. PyObject *CPyTagged_Str(CPyTagged n) {
  434. if (CPyTagged_CheckShort(n)) {
  435. return CPyTagged_ShortToStr(CPyTagged_ShortAsSsize_t(n));
  436. } else {
  437. return PyObject_Str(CPyTagged_AsObject(n));
  438. }
  439. }
  440. void CPyDebug_Print(const char *msg) {
  441. printf("%s\n", msg);
  442. fflush(stdout);
  443. }
  444. int CPySequence_CheckUnpackCount(PyObject *sequence, Py_ssize_t expected) {
  445. Py_ssize_t actual = Py_SIZE(sequence);
  446. if (unlikely(actual != expected)) {
  447. if (actual < expected) {
  448. PyErr_Format(PyExc_ValueError, "not enough values to unpack (expected %zd, got %zd)",
  449. expected, actual);
  450. } else {
  451. PyErr_Format(PyExc_ValueError, "too many values to unpack (expected %zd)", expected);
  452. }
  453. return -1;
  454. }
  455. return 0;
  456. }
  457. // Parse an integer (size_t) encoded as a variable-length binary sequence.
  458. static const char *parse_int(const char *s, size_t *len) {
  459. Py_ssize_t n = 0;
  460. while ((unsigned char)*s >= 0x80) {
  461. n = (n << 7) + (*s & 0x7f);
  462. s++;
  463. }
  464. n = (n << 7) | *s++;
  465. *len = n;
  466. return s;
  467. }
  468. // Initialize static constant array of literal values
  469. int CPyStatics_Initialize(PyObject **statics,
  470. const char * const *strings,
  471. const char * const *bytestrings,
  472. const char * const *ints,
  473. const double *floats,
  474. const double *complex_numbers,
  475. const int *tuples,
  476. const int *frozensets) {
  477. PyObject **result = statics;
  478. // Start with some hard-coded values
  479. *result++ = Py_None;
  480. Py_INCREF(Py_None);
  481. *result++ = Py_False;
  482. Py_INCREF(Py_False);
  483. *result++ = Py_True;
  484. Py_INCREF(Py_True);
  485. if (strings) {
  486. for (; **strings != '\0'; strings++) {
  487. size_t num;
  488. const char *data = *strings;
  489. data = parse_int(data, &num);
  490. while (num-- > 0) {
  491. size_t len;
  492. data = parse_int(data, &len);
  493. PyObject *obj = PyUnicode_FromStringAndSize(data, len);
  494. if (obj == NULL) {
  495. return -1;
  496. }
  497. PyUnicode_InternInPlace(&obj);
  498. *result++ = obj;
  499. data += len;
  500. }
  501. }
  502. }
  503. if (bytestrings) {
  504. for (; **bytestrings != '\0'; bytestrings++) {
  505. size_t num;
  506. const char *data = *bytestrings;
  507. data = parse_int(data, &num);
  508. while (num-- > 0) {
  509. size_t len;
  510. data = parse_int(data, &len);
  511. PyObject *obj = PyBytes_FromStringAndSize(data, len);
  512. if (obj == NULL) {
  513. return -1;
  514. }
  515. *result++ = obj;
  516. data += len;
  517. }
  518. }
  519. }
  520. if (ints) {
  521. for (; **ints != '\0'; ints++) {
  522. size_t num;
  523. const char *data = *ints;
  524. data = parse_int(data, &num);
  525. while (num-- > 0) {
  526. char *end;
  527. PyObject *obj = PyLong_FromString(data, &end, 10);
  528. if (obj == NULL) {
  529. return -1;
  530. }
  531. data = end;
  532. data++;
  533. *result++ = obj;
  534. }
  535. }
  536. }
  537. if (floats) {
  538. size_t num = (size_t)*floats++;
  539. while (num-- > 0) {
  540. PyObject *obj = PyFloat_FromDouble(*floats++);
  541. if (obj == NULL) {
  542. return -1;
  543. }
  544. *result++ = obj;
  545. }
  546. }
  547. if (complex_numbers) {
  548. size_t num = (size_t)*complex_numbers++;
  549. while (num-- > 0) {
  550. double real = *complex_numbers++;
  551. double imag = *complex_numbers++;
  552. PyObject *obj = PyComplex_FromDoubles(real, imag);
  553. if (obj == NULL) {
  554. return -1;
  555. }
  556. *result++ = obj;
  557. }
  558. }
  559. if (tuples) {
  560. int num = *tuples++;
  561. while (num-- > 0) {
  562. int num_items = *tuples++;
  563. PyObject *obj = PyTuple_New(num_items);
  564. if (obj == NULL) {
  565. return -1;
  566. }
  567. int i;
  568. for (i = 0; i < num_items; i++) {
  569. PyObject *item = statics[*tuples++];
  570. Py_INCREF(item);
  571. PyTuple_SET_ITEM(obj, i, item);
  572. }
  573. *result++ = obj;
  574. }
  575. }
  576. if (frozensets) {
  577. int num = *frozensets++;
  578. while (num-- > 0) {
  579. int num_items = *frozensets++;
  580. PyObject *obj = PyFrozenSet_New(NULL);
  581. if (obj == NULL) {
  582. return -1;
  583. }
  584. for (int i = 0; i < num_items; i++) {
  585. PyObject *item = statics[*frozensets++];
  586. Py_INCREF(item);
  587. if (PySet_Add(obj, item) == -1) {
  588. return -1;
  589. }
  590. }
  591. *result++ = obj;
  592. }
  593. }
  594. return 0;
  595. }
  596. // Call super(type(self), self)
  597. PyObject *
  598. CPy_Super(PyObject *builtins, PyObject *self) {
  599. PyObject *super_type = PyObject_GetAttrString(builtins, "super");
  600. if (!super_type)
  601. return NULL;
  602. PyObject *result = PyObject_CallFunctionObjArgs(
  603. super_type, (PyObject*)Py_TYPE(self), self, NULL);
  604. Py_DECREF(super_type);
  605. return result;
  606. }
  607. static bool import_single(PyObject *mod_id, PyObject **mod_static,
  608. PyObject *globals_id, PyObject *globals_name, PyObject *globals) {
  609. if (*mod_static == Py_None) {
  610. CPyModule *mod = PyImport_Import(mod_id);
  611. if (mod == NULL) {
  612. return false;
  613. }
  614. *mod_static = mod;
  615. }
  616. PyObject *mod_dict = PyImport_GetModuleDict();
  617. CPyModule *globals_mod = CPyDict_GetItem(mod_dict, globals_id);
  618. if (globals_mod == NULL) {
  619. return false;
  620. }
  621. int ret = CPyDict_SetItem(globals, globals_name, globals_mod);
  622. Py_DECREF(globals_mod);
  623. if (ret < 0) {
  624. return false;
  625. }
  626. return true;
  627. }
  628. // Table-driven import helper. See transform_import() in irbuild for the details.
  629. bool CPyImport_ImportMany(PyObject *modules, CPyModule **statics[], PyObject *globals,
  630. PyObject *tb_path, PyObject *tb_function, Py_ssize_t *tb_lines) {
  631. for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(modules); i++) {
  632. PyObject *module = PyTuple_GET_ITEM(modules, i);
  633. PyObject *mod_id = PyTuple_GET_ITEM(module, 0);
  634. PyObject *globals_id = PyTuple_GET_ITEM(module, 1);
  635. PyObject *globals_name = PyTuple_GET_ITEM(module, 2);
  636. if (!import_single(mod_id, statics[i], globals_id, globals_name, globals)) {
  637. assert(PyErr_Occurred() && "error indicator should be set on bad import!");
  638. PyObject *typ, *val, *tb;
  639. PyErr_Fetch(&typ, &val, &tb);
  640. const char *path = PyUnicode_AsUTF8(tb_path);
  641. if (path == NULL) {
  642. path = "<unable to display>";
  643. }
  644. const char *function = PyUnicode_AsUTF8(tb_function);
  645. if (function == NULL) {
  646. function = "<unable to display>";
  647. }
  648. PyErr_Restore(typ, val, tb);
  649. CPy_AddTraceback(path, function, tb_lines[i], globals);
  650. return false;
  651. }
  652. }
  653. return true;
  654. }
  655. // This helper function is a simplification of cpython/ceval.c/import_from()
  656. static PyObject *CPyImport_ImportFrom(PyObject *module, PyObject *package_name,
  657. PyObject *import_name, PyObject *as_name) {
  658. // check if the imported module has an attribute by that name
  659. PyObject *x = PyObject_GetAttr(module, import_name);
  660. if (x == NULL) {
  661. // if not, attempt to import a submodule with that name
  662. PyObject *fullmodname = PyUnicode_FromFormat("%U.%U", package_name, import_name);
  663. if (fullmodname == NULL) {
  664. goto fail;
  665. }
  666. // The following code is a simplification of cpython/import.c/PyImport_GetModule()
  667. x = PyObject_GetItem(module, fullmodname);
  668. Py_DECREF(fullmodname);
  669. if (x == NULL) {
  670. goto fail;
  671. }
  672. }
  673. return x;
  674. fail:
  675. PyErr_Clear();
  676. PyObject *package_path = PyModule_GetFilenameObject(module);
  677. PyObject *errmsg = PyUnicode_FromFormat("cannot import name %R from %R (%S)",
  678. import_name, package_name, package_path);
  679. // NULL checks for errmsg and package_name done by PyErr_SetImportError.
  680. PyErr_SetImportError(errmsg, package_name, package_path);
  681. Py_DECREF(package_path);
  682. Py_DECREF(errmsg);
  683. return NULL;
  684. }
  685. PyObject *CPyImport_ImportFromMany(PyObject *mod_id, PyObject *names, PyObject *as_names,
  686. PyObject *globals) {
  687. PyObject *mod = PyImport_ImportModuleLevelObject(mod_id, globals, 0, names, 0);
  688. if (mod == NULL) {
  689. return NULL;
  690. }
  691. for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(names); i++) {
  692. PyObject *name = PyTuple_GET_ITEM(names, i);
  693. PyObject *as_name = PyTuple_GET_ITEM(as_names, i);
  694. PyObject *obj = CPyImport_ImportFrom(mod, mod_id, name, as_name);
  695. if (obj == NULL) {
  696. Py_DECREF(mod);
  697. return NULL;
  698. }
  699. int ret = CPyDict_SetItem(globals, as_name, obj);
  700. Py_DECREF(obj);
  701. if (ret < 0) {
  702. Py_DECREF(mod);
  703. return NULL;
  704. }
  705. }
  706. return mod;
  707. }
  708. // From CPython
  709. static PyObject *
  710. CPy_BinopTypeError(PyObject *left, PyObject *right, const char *op) {
  711. PyErr_Format(PyExc_TypeError,
  712. "unsupported operand type(s) for %.100s: "
  713. "'%.100s' and '%.100s'",
  714. op,
  715. Py_TYPE(left)->tp_name,
  716. Py_TYPE(right)->tp_name);
  717. return NULL;
  718. }
  719. PyObject *
  720. CPy_CallReverseOpMethod(PyObject *left,
  721. PyObject *right,
  722. const char *op,
  723. _Py_Identifier *method) {
  724. // Look up reverse method
  725. PyObject *m = _PyObject_GetAttrId(right, method);
  726. if (m == NULL) {
  727. // If reverse method not defined, generate TypeError instead AttributeError
  728. if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
  729. CPy_BinopTypeError(left, right, op);
  730. }
  731. return NULL;
  732. }
  733. // Call reverse method
  734. PyObject *result = PyObject_CallOneArg(m, left);
  735. Py_DECREF(m);
  736. return result;
  737. }
  738. PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func,
  739. PyObject *cls,
  740. PyObject *func) {
  741. PyObject *registry = PyObject_GetAttrString(singledispatch_func, "registry");
  742. PyObject *register_func = NULL;
  743. PyObject *typing = NULL;
  744. PyObject *get_type_hints = NULL;
  745. PyObject *type_hints = NULL;
  746. if (registry == NULL) goto fail;
  747. if (func == NULL) {
  748. // one argument case
  749. if (PyType_Check(cls)) {
  750. // passed a class
  751. // bind cls to the first argument so that register gets called again with both the
  752. // class and the function
  753. register_func = PyObject_GetAttrString(singledispatch_func, "register");
  754. if (register_func == NULL) goto fail;
  755. return PyMethod_New(register_func, cls);
  756. }
  757. // passed a function
  758. PyObject *annotations = PyFunction_GetAnnotations(cls);
  759. const char *invalid_first_arg_msg =
  760. "Invalid first argument to `register()`: %R. "
  761. "Use either `@register(some_class)` or plain `@register` "
  762. "on an annotated function.";
  763. if (annotations == NULL) {
  764. PyErr_Format(PyExc_TypeError, invalid_first_arg_msg, cls);
  765. goto fail;
  766. }
  767. Py_INCREF(annotations);
  768. func = cls;
  769. typing = PyImport_ImportModule("typing");
  770. if (typing == NULL) goto fail;
  771. get_type_hints = PyObject_GetAttrString(typing, "get_type_hints");
  772. type_hints = PyObject_CallOneArg(get_type_hints, func);
  773. PyObject *argname;
  774. Py_ssize_t pos = 0;
  775. if (!PyDict_Next(type_hints, &pos, &argname, &cls)) {
  776. // the functools implementation raises the same type error if annotations is an empty dict
  777. PyErr_Format(PyExc_TypeError, invalid_first_arg_msg, cls);
  778. goto fail;
  779. }
  780. if (!PyType_Check(cls)) {
  781. const char *invalid_annotation_msg = "Invalid annotation for %R. %R is not a class.";
  782. PyErr_Format(PyExc_TypeError, invalid_annotation_msg, argname, cls);
  783. goto fail;
  784. }
  785. }
  786. if (PyDict_SetItem(registry, cls, func) == -1) {
  787. goto fail;
  788. }
  789. // clear the cache so we consider the newly added function when dispatching
  790. PyObject *dispatch_cache = PyObject_GetAttrString(singledispatch_func, "dispatch_cache");
  791. if (dispatch_cache == NULL) goto fail;
  792. PyDict_Clear(dispatch_cache);
  793. Py_INCREF(func);
  794. return func;
  795. fail:
  796. Py_XDECREF(registry);
  797. Py_XDECREF(register_func);
  798. Py_XDECREF(typing);
  799. Py_XDECREF(get_type_hints);
  800. Py_XDECREF(type_hints);
  801. return NULL;
  802. }
  803. // Adapated from ceval.c GET_AITER
  804. PyObject *CPy_GetAIter(PyObject *obj)
  805. {
  806. unaryfunc getter = NULL;
  807. PyTypeObject *type = Py_TYPE(obj);
  808. if (type->tp_as_async != NULL) {
  809. getter = type->tp_as_async->am_aiter;
  810. }
  811. if (getter == NULL) {
  812. PyErr_Format(PyExc_TypeError,
  813. "'async for' requires an object with "
  814. "__aiter__ method, got %.100s",
  815. type->tp_name);
  816. Py_DECREF(obj);
  817. return NULL;
  818. }
  819. PyObject *iter = (*getter)(obj);
  820. if (!iter) {
  821. return NULL;
  822. }
  823. if (Py_TYPE(iter)->tp_as_async == NULL ||
  824. Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
  825. PyErr_Format(PyExc_TypeError,
  826. "'async for' received an object from __aiter__ "
  827. "that does not implement __anext__: %.100s",
  828. Py_TYPE(iter)->tp_name);
  829. Py_DECREF(iter);
  830. return NULL;
  831. }
  832. return iter;
  833. }
  834. // Adapated from ceval.c GET_ANEXT
  835. PyObject *CPy_GetANext(PyObject *aiter)
  836. {
  837. unaryfunc getter = NULL;
  838. PyObject *next_iter = NULL;
  839. PyObject *awaitable = NULL;
  840. PyTypeObject *type = Py_TYPE(aiter);
  841. if (PyAsyncGen_CheckExact(aiter)) {
  842. awaitable = type->tp_as_async->am_anext(aiter);
  843. if (awaitable == NULL) {
  844. goto error;
  845. }
  846. } else {
  847. if (type->tp_as_async != NULL){
  848. getter = type->tp_as_async->am_anext;
  849. }
  850. if (getter != NULL) {
  851. next_iter = (*getter)(aiter);
  852. if (next_iter == NULL) {
  853. goto error;
  854. }
  855. }
  856. else {
  857. PyErr_Format(PyExc_TypeError,
  858. "'async for' requires an iterator with "
  859. "__anext__ method, got %.100s",
  860. type->tp_name);
  861. goto error;
  862. }
  863. awaitable = CPyCoro_GetAwaitableIter(next_iter);
  864. if (awaitable == NULL) {
  865. _PyErr_FormatFromCause(
  866. PyExc_TypeError,
  867. "'async for' received an invalid object "
  868. "from __anext__: %.100s",
  869. Py_TYPE(next_iter)->tp_name);
  870. Py_DECREF(next_iter);
  871. goto error;
  872. } else {
  873. Py_DECREF(next_iter);
  874. }
  875. }
  876. return awaitable;
  877. error:
  878. return NULL;
  879. }