misc_ops.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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. // Allocate the type and then copy the main stuff in.
  165. t = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0);
  166. if (!t)
  167. goto error;
  168. memcpy((char *)t + sizeof(PyVarObject),
  169. (char *)template_ + sizeof(PyVarObject),
  170. sizeof(PyTypeObject) - sizeof(PyVarObject));
  171. if (bases != orig_bases) {
  172. if (PyObject_SetAttrString((PyObject *)t, "__orig_bases__", orig_bases) < 0)
  173. goto error;
  174. }
  175. // Having tp_base set is I think required for stuff to get
  176. // inherited in PyType_Ready, which we needed for subclassing
  177. // BaseException. XXX: Taking the first element is wrong I think though.
  178. if (bases) {
  179. t->ht_type.tp_base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0);
  180. Py_INCREF((PyObject *)t->ht_type.tp_base);
  181. }
  182. t->ht_name = name;
  183. Py_INCREF(name);
  184. t->ht_qualname = name;
  185. t->ht_type.tp_bases = bases;
  186. // references stolen so NULL these out
  187. bases = name = NULL;
  188. if (PyType_Ready((PyTypeObject *)t) < 0)
  189. goto error;
  190. assert(t->ht_type.tp_base != NULL);
  191. // XXX: This is a terrible hack to work around a cpython check on
  192. // the mro. It was needed for mypy.stats. I need to investigate
  193. // what is actually going on here.
  194. Py_INCREF(metaclass);
  195. Py_SET_TYPE(t, metaclass);
  196. if (dummy_class) {
  197. if (PyDict_Merge(t->ht_type.tp_dict, dummy_class->tp_dict, 0) != 0)
  198. goto error;
  199. // This is the *really* tasteless bit. GenericMeta's __new__
  200. // in certain versions of typing sets _gorg to point back to
  201. // the class. We need to override it to keep it from pointing
  202. // to the proxy.
  203. if (PyDict_SetItemString(t->ht_type.tp_dict, "_gorg", (PyObject *)t) < 0)
  204. goto error;
  205. }
  206. // Reject anything that would give us a nontrivial __slots__,
  207. // because the layout will conflict
  208. slots = PyObject_GetAttrString((PyObject *)t, "__slots__");
  209. if (slots) {
  210. // don't fail on an empty __slots__
  211. int is_true = PyObject_IsTrue(slots);
  212. Py_DECREF(slots);
  213. if (is_true > 0)
  214. PyErr_SetString(PyExc_TypeError, "mypyc classes can't have __slots__");
  215. if (is_true != 0)
  216. goto error;
  217. } else {
  218. PyErr_Clear();
  219. }
  220. if (PyObject_SetAttrString((PyObject *)t, "__module__", modname) < 0)
  221. goto error;
  222. if (init_subclass((PyTypeObject *)t, NULL))
  223. goto error;
  224. Py_XDECREF(dummy_class);
  225. #if PY_MINOR_VERSION == 11
  226. // This is a hack. Python 3.11 doesn't include good public APIs to work with managed
  227. // dicts, which are the default for heap types. So we try to opt-out until Python 3.12.
  228. t->ht_type.tp_flags &= ~Py_TPFLAGS_MANAGED_DICT;
  229. #endif
  230. return (PyObject *)t;
  231. error:
  232. Py_XDECREF(t);
  233. Py_XDECREF(bases);
  234. Py_XDECREF(dummy_class);
  235. Py_XDECREF(name);
  236. return NULL;
  237. }
  238. static int _CPy_UpdateObjFromDict(PyObject *obj, PyObject *dict)
  239. {
  240. Py_ssize_t pos = 0;
  241. PyObject *key, *value;
  242. while (PyDict_Next(dict, &pos, &key, &value)) {
  243. if (PyObject_SetAttr(obj, key, value) != 0) {
  244. return -1;
  245. }
  246. }
  247. return 0;
  248. }
  249. /* Support for our partial built-in support for dataclasses.
  250. *
  251. * Take a class we want to make a dataclass, remove any descriptors
  252. * for annotated attributes, swap in the actual values of the class
  253. * variables invoke dataclass, and then restore all of the
  254. * descriptors.
  255. *
  256. * The purpose of all this is that dataclasses uses the values of
  257. * class variables to drive which attributes are required and what the
  258. * default values/factories are for optional attributes. This means
  259. * that the class dict needs to contain those values instead of getset
  260. * descriptors for the attributes when we invoke dataclass.
  261. *
  262. * We need to remove descriptors for attributes even when there is no
  263. * default value for them, or else dataclass will think the descriptor
  264. * is the default value. We remove only the attributes, since we don't
  265. * want dataclasses to try generating functions when they are already
  266. * implemented.
  267. *
  268. * Args:
  269. * dataclass_dec: The decorator to apply
  270. * tp: The class we are making a dataclass
  271. * dict: The dictionary containing values that dataclasses needs
  272. * annotations: The type annotation dictionary
  273. */
  274. int
  275. CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp,
  276. PyObject *dict, PyObject *annotations) {
  277. PyTypeObject *ttp = (PyTypeObject *)tp;
  278. Py_ssize_t pos;
  279. PyObject *res;
  280. /* Make a copy of the original class __dict__ */
  281. PyObject *orig_dict = PyDict_Copy(ttp->tp_dict);
  282. if (!orig_dict) {
  283. goto fail;
  284. }
  285. /* Delete anything that had an annotation */
  286. pos = 0;
  287. PyObject *key;
  288. while (PyDict_Next(annotations, &pos, &key, NULL)) {
  289. if (PyObject_DelAttr(tp, key) != 0) {
  290. goto fail;
  291. }
  292. }
  293. /* Copy in all the attributes that we want dataclass to see */
  294. if (_CPy_UpdateObjFromDict(tp, dict) != 0) {
  295. goto fail;
  296. }
  297. /* Run the @dataclass descriptor */
  298. res = PyObject_CallOneArg(dataclass_dec, tp);
  299. if (!res) {
  300. goto fail;
  301. }
  302. Py_DECREF(res);
  303. /* Copy back the original contents of the dict */
  304. if (_CPy_UpdateObjFromDict(tp, orig_dict) != 0) {
  305. goto fail;
  306. }
  307. Py_DECREF(orig_dict);
  308. return 1;
  309. fail:
  310. Py_XDECREF(orig_dict);
  311. return 0;
  312. }
  313. // Support for pickling; reusable getstate and setstate functions
  314. PyObject *
  315. CPyPickle_SetState(PyObject *obj, PyObject *state)
  316. {
  317. if (_CPy_UpdateObjFromDict(obj, state) != 0) {
  318. return NULL;
  319. }
  320. Py_RETURN_NONE;
  321. }
  322. PyObject *
  323. CPyPickle_GetState(PyObject *obj)
  324. {
  325. PyObject *attrs = NULL, *state = NULL;
  326. attrs = PyObject_GetAttrString((PyObject *)Py_TYPE(obj), "__mypyc_attrs__");
  327. if (!attrs) {
  328. goto fail;
  329. }
  330. if (!PyTuple_Check(attrs)) {
  331. PyErr_SetString(PyExc_TypeError, "__mypyc_attrs__ is not a tuple");
  332. goto fail;
  333. }
  334. state = PyDict_New();
  335. if (!state) {
  336. goto fail;
  337. }
  338. // Collect all the values of attributes in __mypyc_attrs__
  339. // Attributes that are missing we just ignore
  340. int i;
  341. for (i = 0; i < PyTuple_GET_SIZE(attrs); i++) {
  342. PyObject *key = PyTuple_GET_ITEM(attrs, i);
  343. PyObject *value = PyObject_GetAttr(obj, key);
  344. if (!value) {
  345. if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
  346. PyErr_Clear();
  347. continue;
  348. }
  349. goto fail;
  350. }
  351. int result = PyDict_SetItem(state, key, value);
  352. Py_DECREF(value);
  353. if (result != 0) {
  354. goto fail;
  355. }
  356. }
  357. Py_DECREF(attrs);
  358. return state;
  359. fail:
  360. Py_XDECREF(attrs);
  361. Py_XDECREF(state);
  362. return NULL;
  363. }
  364. CPyTagged CPyTagged_Id(PyObject *o) {
  365. return CPyTagged_FromVoidPtr(o);
  366. }
  367. #define MAX_INT_CHARS 22
  368. #define _PyUnicode_LENGTH(op) \
  369. (((PyASCIIObject *)(op))->length)
  370. // using snprintf or PyUnicode_FromFormat was way slower than
  371. // boxing the int and calling PyObject_Str on it, so we implement our own
  372. static int fmt_ssize_t(char *out, Py_ssize_t n) {
  373. bool neg = n < 0;
  374. if (neg) n = -n;
  375. // buf gets filled backward and then we copy it forward
  376. char buf[MAX_INT_CHARS];
  377. int i = 0;
  378. do {
  379. buf[i] = (n % 10) + '0';
  380. n /= 10;
  381. i++;
  382. } while (n);
  383. int len = i;
  384. int j = 0;
  385. if (neg) {
  386. out[j++] = '-';
  387. len++;
  388. }
  389. for (; j < len; j++, i--) {
  390. out[j] = buf[i-1];
  391. }
  392. out[j] = '\0';
  393. return len;
  394. }
  395. static PyObject *CPyTagged_ShortToStr(Py_ssize_t n) {
  396. PyObject *obj = PyUnicode_New(MAX_INT_CHARS, 127);
  397. if (!obj) return NULL;
  398. int len = fmt_ssize_t((char *)PyUnicode_1BYTE_DATA(obj), n);
  399. _PyUnicode_LENGTH(obj) = len;
  400. return obj;
  401. }
  402. PyObject *CPyTagged_Str(CPyTagged n) {
  403. if (CPyTagged_CheckShort(n)) {
  404. return CPyTagged_ShortToStr(CPyTagged_ShortAsSsize_t(n));
  405. } else {
  406. return PyObject_Str(CPyTagged_AsObject(n));
  407. }
  408. }
  409. void CPyDebug_Print(const char *msg) {
  410. printf("%s\n", msg);
  411. fflush(stdout);
  412. }
  413. int CPySequence_CheckUnpackCount(PyObject *sequence, Py_ssize_t expected) {
  414. Py_ssize_t actual = Py_SIZE(sequence);
  415. if (unlikely(actual != expected)) {
  416. if (actual < expected) {
  417. PyErr_Format(PyExc_ValueError, "not enough values to unpack (expected %zd, got %zd)",
  418. expected, actual);
  419. } else {
  420. PyErr_Format(PyExc_ValueError, "too many values to unpack (expected %zd)", expected);
  421. }
  422. return -1;
  423. }
  424. return 0;
  425. }
  426. // Parse an integer (size_t) encoded as a variable-length binary sequence.
  427. static const char *parse_int(const char *s, size_t *len) {
  428. Py_ssize_t n = 0;
  429. while ((unsigned char)*s >= 0x80) {
  430. n = (n << 7) + (*s & 0x7f);
  431. s++;
  432. }
  433. n = (n << 7) | *s++;
  434. *len = n;
  435. return s;
  436. }
  437. // Initialize static constant array of literal values
  438. int CPyStatics_Initialize(PyObject **statics,
  439. const char * const *strings,
  440. const char * const *bytestrings,
  441. const char * const *ints,
  442. const double *floats,
  443. const double *complex_numbers,
  444. const int *tuples,
  445. const int *frozensets) {
  446. PyObject **result = statics;
  447. // Start with some hard-coded values
  448. *result++ = Py_None;
  449. Py_INCREF(Py_None);
  450. *result++ = Py_False;
  451. Py_INCREF(Py_False);
  452. *result++ = Py_True;
  453. Py_INCREF(Py_True);
  454. if (strings) {
  455. for (; **strings != '\0'; strings++) {
  456. size_t num;
  457. const char *data = *strings;
  458. data = parse_int(data, &num);
  459. while (num-- > 0) {
  460. size_t len;
  461. data = parse_int(data, &len);
  462. PyObject *obj = PyUnicode_FromStringAndSize(data, len);
  463. if (obj == NULL) {
  464. return -1;
  465. }
  466. PyUnicode_InternInPlace(&obj);
  467. *result++ = obj;
  468. data += len;
  469. }
  470. }
  471. }
  472. if (bytestrings) {
  473. for (; **bytestrings != '\0'; bytestrings++) {
  474. size_t num;
  475. const char *data = *bytestrings;
  476. data = parse_int(data, &num);
  477. while (num-- > 0) {
  478. size_t len;
  479. data = parse_int(data, &len);
  480. PyObject *obj = PyBytes_FromStringAndSize(data, len);
  481. if (obj == NULL) {
  482. return -1;
  483. }
  484. *result++ = obj;
  485. data += len;
  486. }
  487. }
  488. }
  489. if (ints) {
  490. for (; **ints != '\0'; ints++) {
  491. size_t num;
  492. const char *data = *ints;
  493. data = parse_int(data, &num);
  494. while (num-- > 0) {
  495. char *end;
  496. PyObject *obj = PyLong_FromString(data, &end, 10);
  497. if (obj == NULL) {
  498. return -1;
  499. }
  500. data = end;
  501. data++;
  502. *result++ = obj;
  503. }
  504. }
  505. }
  506. if (floats) {
  507. size_t num = (size_t)*floats++;
  508. while (num-- > 0) {
  509. PyObject *obj = PyFloat_FromDouble(*floats++);
  510. if (obj == NULL) {
  511. return -1;
  512. }
  513. *result++ = obj;
  514. }
  515. }
  516. if (complex_numbers) {
  517. size_t num = (size_t)*complex_numbers++;
  518. while (num-- > 0) {
  519. double real = *complex_numbers++;
  520. double imag = *complex_numbers++;
  521. PyObject *obj = PyComplex_FromDoubles(real, imag);
  522. if (obj == NULL) {
  523. return -1;
  524. }
  525. *result++ = obj;
  526. }
  527. }
  528. if (tuples) {
  529. int num = *tuples++;
  530. while (num-- > 0) {
  531. int num_items = *tuples++;
  532. PyObject *obj = PyTuple_New(num_items);
  533. if (obj == NULL) {
  534. return -1;
  535. }
  536. int i;
  537. for (i = 0; i < num_items; i++) {
  538. PyObject *item = statics[*tuples++];
  539. Py_INCREF(item);
  540. PyTuple_SET_ITEM(obj, i, item);
  541. }
  542. *result++ = obj;
  543. }
  544. }
  545. if (frozensets) {
  546. int num = *frozensets++;
  547. while (num-- > 0) {
  548. int num_items = *frozensets++;
  549. PyObject *obj = PyFrozenSet_New(NULL);
  550. if (obj == NULL) {
  551. return -1;
  552. }
  553. for (int i = 0; i < num_items; i++) {
  554. PyObject *item = statics[*frozensets++];
  555. Py_INCREF(item);
  556. if (PySet_Add(obj, item) == -1) {
  557. return -1;
  558. }
  559. }
  560. *result++ = obj;
  561. }
  562. }
  563. return 0;
  564. }
  565. // Call super(type(self), self)
  566. PyObject *
  567. CPy_Super(PyObject *builtins, PyObject *self) {
  568. PyObject *super_type = PyObject_GetAttrString(builtins, "super");
  569. if (!super_type)
  570. return NULL;
  571. PyObject *result = PyObject_CallFunctionObjArgs(
  572. super_type, (PyObject*)Py_TYPE(self), self, NULL);
  573. Py_DECREF(super_type);
  574. return result;
  575. }
  576. static bool import_single(PyObject *mod_id, PyObject **mod_static,
  577. PyObject *globals_id, PyObject *globals_name, PyObject *globals) {
  578. if (*mod_static == Py_None) {
  579. CPyModule *mod = PyImport_Import(mod_id);
  580. if (mod == NULL) {
  581. return false;
  582. }
  583. *mod_static = mod;
  584. }
  585. PyObject *mod_dict = PyImport_GetModuleDict();
  586. CPyModule *globals_mod = CPyDict_GetItem(mod_dict, globals_id);
  587. if (globals_mod == NULL) {
  588. return false;
  589. }
  590. int ret = CPyDict_SetItem(globals, globals_name, globals_mod);
  591. Py_DECREF(globals_mod);
  592. if (ret < 0) {
  593. return false;
  594. }
  595. return true;
  596. }
  597. // Table-driven import helper. See transform_import() in irbuild for the details.
  598. bool CPyImport_ImportMany(PyObject *modules, CPyModule **statics[], PyObject *globals,
  599. PyObject *tb_path, PyObject *tb_function, Py_ssize_t *tb_lines) {
  600. for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(modules); i++) {
  601. PyObject *module = PyTuple_GET_ITEM(modules, i);
  602. PyObject *mod_id = PyTuple_GET_ITEM(module, 0);
  603. PyObject *globals_id = PyTuple_GET_ITEM(module, 1);
  604. PyObject *globals_name = PyTuple_GET_ITEM(module, 2);
  605. if (!import_single(mod_id, statics[i], globals_id, globals_name, globals)) {
  606. assert(PyErr_Occurred() && "error indicator should be set on bad import!");
  607. PyObject *typ, *val, *tb;
  608. PyErr_Fetch(&typ, &val, &tb);
  609. const char *path = PyUnicode_AsUTF8(tb_path);
  610. if (path == NULL) {
  611. path = "<unable to display>";
  612. }
  613. const char *function = PyUnicode_AsUTF8(tb_function);
  614. if (function == NULL) {
  615. function = "<unable to display>";
  616. }
  617. PyErr_Restore(typ, val, tb);
  618. CPy_AddTraceback(path, function, tb_lines[i], globals);
  619. return false;
  620. }
  621. }
  622. return true;
  623. }
  624. // This helper function is a simplification of cpython/ceval.c/import_from()
  625. static PyObject *CPyImport_ImportFrom(PyObject *module, PyObject *package_name,
  626. PyObject *import_name, PyObject *as_name) {
  627. // check if the imported module has an attribute by that name
  628. PyObject *x = PyObject_GetAttr(module, import_name);
  629. if (x == NULL) {
  630. // if not, attempt to import a submodule with that name
  631. PyObject *fullmodname = PyUnicode_FromFormat("%U.%U", package_name, import_name);
  632. if (fullmodname == NULL) {
  633. goto fail;
  634. }
  635. // The following code is a simplification of cpython/import.c/PyImport_GetModule()
  636. x = PyObject_GetItem(module, fullmodname);
  637. Py_DECREF(fullmodname);
  638. if (x == NULL) {
  639. goto fail;
  640. }
  641. }
  642. return x;
  643. fail:
  644. PyErr_Clear();
  645. PyObject *package_path = PyModule_GetFilenameObject(module);
  646. PyObject *errmsg = PyUnicode_FromFormat("cannot import name %R from %R (%S)",
  647. import_name, package_name, package_path);
  648. // NULL checks for errmsg and package_name done by PyErr_SetImportError.
  649. PyErr_SetImportError(errmsg, package_name, package_path);
  650. Py_DECREF(package_path);
  651. Py_DECREF(errmsg);
  652. return NULL;
  653. }
  654. PyObject *CPyImport_ImportFromMany(PyObject *mod_id, PyObject *names, PyObject *as_names,
  655. PyObject *globals) {
  656. PyObject *mod = PyImport_ImportModuleLevelObject(mod_id, globals, 0, names, 0);
  657. if (mod == NULL) {
  658. return NULL;
  659. }
  660. for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(names); i++) {
  661. PyObject *name = PyTuple_GET_ITEM(names, i);
  662. PyObject *as_name = PyTuple_GET_ITEM(as_names, i);
  663. PyObject *obj = CPyImport_ImportFrom(mod, mod_id, name, as_name);
  664. if (obj == NULL) {
  665. Py_DECREF(mod);
  666. return NULL;
  667. }
  668. int ret = CPyDict_SetItem(globals, as_name, obj);
  669. Py_DECREF(obj);
  670. if (ret < 0) {
  671. Py_DECREF(mod);
  672. return NULL;
  673. }
  674. }
  675. return mod;
  676. }
  677. // From CPython
  678. static PyObject *
  679. CPy_BinopTypeError(PyObject *left, PyObject *right, const char *op) {
  680. PyErr_Format(PyExc_TypeError,
  681. "unsupported operand type(s) for %.100s: "
  682. "'%.100s' and '%.100s'",
  683. op,
  684. Py_TYPE(left)->tp_name,
  685. Py_TYPE(right)->tp_name);
  686. return NULL;
  687. }
  688. PyObject *
  689. CPy_CallReverseOpMethod(PyObject *left,
  690. PyObject *right,
  691. const char *op,
  692. _Py_Identifier *method) {
  693. // Look up reverse method
  694. PyObject *m = _PyObject_GetAttrId(right, method);
  695. if (m == NULL) {
  696. // If reverse method not defined, generate TypeError instead AttributeError
  697. if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
  698. CPy_BinopTypeError(left, right, op);
  699. }
  700. return NULL;
  701. }
  702. // Call reverse method
  703. PyObject *result = PyObject_CallOneArg(m, left);
  704. Py_DECREF(m);
  705. return result;
  706. }
  707. PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func,
  708. PyObject *cls,
  709. PyObject *func) {
  710. PyObject *registry = PyObject_GetAttrString(singledispatch_func, "registry");
  711. PyObject *register_func = NULL;
  712. PyObject *typing = NULL;
  713. PyObject *get_type_hints = NULL;
  714. PyObject *type_hints = NULL;
  715. if (registry == NULL) goto fail;
  716. if (func == NULL) {
  717. // one argument case
  718. if (PyType_Check(cls)) {
  719. // passed a class
  720. // bind cls to the first argument so that register gets called again with both the
  721. // class and the function
  722. register_func = PyObject_GetAttrString(singledispatch_func, "register");
  723. if (register_func == NULL) goto fail;
  724. return PyMethod_New(register_func, cls);
  725. }
  726. // passed a function
  727. PyObject *annotations = PyFunction_GetAnnotations(cls);
  728. const char *invalid_first_arg_msg =
  729. "Invalid first argument to `register()`: %R. "
  730. "Use either `@register(some_class)` or plain `@register` "
  731. "on an annotated function.";
  732. if (annotations == NULL) {
  733. PyErr_Format(PyExc_TypeError, invalid_first_arg_msg, cls);
  734. goto fail;
  735. }
  736. Py_INCREF(annotations);
  737. func = cls;
  738. typing = PyImport_ImportModule("typing");
  739. if (typing == NULL) goto fail;
  740. get_type_hints = PyObject_GetAttrString(typing, "get_type_hints");
  741. type_hints = PyObject_CallOneArg(get_type_hints, func);
  742. PyObject *argname;
  743. Py_ssize_t pos = 0;
  744. if (!PyDict_Next(type_hints, &pos, &argname, &cls)) {
  745. // the functools implementation raises the same type error if annotations is an empty dict
  746. PyErr_Format(PyExc_TypeError, invalid_first_arg_msg, cls);
  747. goto fail;
  748. }
  749. if (!PyType_Check(cls)) {
  750. const char *invalid_annotation_msg = "Invalid annotation for %R. %R is not a class.";
  751. PyErr_Format(PyExc_TypeError, invalid_annotation_msg, argname, cls);
  752. goto fail;
  753. }
  754. }
  755. if (PyDict_SetItem(registry, cls, func) == -1) {
  756. goto fail;
  757. }
  758. // clear the cache so we consider the newly added function when dispatching
  759. PyObject *dispatch_cache = PyObject_GetAttrString(singledispatch_func, "dispatch_cache");
  760. if (dispatch_cache == NULL) goto fail;
  761. PyDict_Clear(dispatch_cache);
  762. Py_INCREF(func);
  763. return func;
  764. fail:
  765. Py_XDECREF(registry);
  766. Py_XDECREF(register_func);
  767. Py_XDECREF(typing);
  768. Py_XDECREF(get_type_hints);
  769. Py_XDECREF(type_hints);
  770. return NULL;
  771. }
  772. // Adapated from ceval.c GET_AITER
  773. PyObject *CPy_GetAIter(PyObject *obj)
  774. {
  775. unaryfunc getter = NULL;
  776. PyTypeObject *type = Py_TYPE(obj);
  777. if (type->tp_as_async != NULL) {
  778. getter = type->tp_as_async->am_aiter;
  779. }
  780. if (getter == NULL) {
  781. PyErr_Format(PyExc_TypeError,
  782. "'async for' requires an object with "
  783. "__aiter__ method, got %.100s",
  784. type->tp_name);
  785. Py_DECREF(obj);
  786. return NULL;
  787. }
  788. PyObject *iter = (*getter)(obj);
  789. if (!iter) {
  790. return NULL;
  791. }
  792. if (Py_TYPE(iter)->tp_as_async == NULL ||
  793. Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
  794. PyErr_Format(PyExc_TypeError,
  795. "'async for' received an object from __aiter__ "
  796. "that does not implement __anext__: %.100s",
  797. Py_TYPE(iter)->tp_name);
  798. Py_DECREF(iter);
  799. return NULL;
  800. }
  801. return iter;
  802. }
  803. // Adapated from ceval.c GET_ANEXT
  804. PyObject *CPy_GetANext(PyObject *aiter)
  805. {
  806. unaryfunc getter = NULL;
  807. PyObject *next_iter = NULL;
  808. PyObject *awaitable = NULL;
  809. PyTypeObject *type = Py_TYPE(aiter);
  810. if (PyAsyncGen_CheckExact(aiter)) {
  811. awaitable = type->tp_as_async->am_anext(aiter);
  812. if (awaitable == NULL) {
  813. goto error;
  814. }
  815. } else {
  816. if (type->tp_as_async != NULL){
  817. getter = type->tp_as_async->am_anext;
  818. }
  819. if (getter != NULL) {
  820. next_iter = (*getter)(aiter);
  821. if (next_iter == NULL) {
  822. goto error;
  823. }
  824. }
  825. else {
  826. PyErr_Format(PyExc_TypeError,
  827. "'async for' requires an iterator with "
  828. "__anext__ method, got %.100s",
  829. type->tp_name);
  830. goto error;
  831. }
  832. awaitable = CPyCoro_GetAwaitableIter(next_iter);
  833. if (awaitable == NULL) {
  834. _PyErr_FormatFromCause(
  835. PyExc_TypeError,
  836. "'async for' received an invalid object "
  837. "from __anext__: %.100s",
  838. Py_TYPE(next_iter)->tp_name);
  839. Py_DECREF(next_iter);
  840. goto error;
  841. } else {
  842. Py_DECREF(next_iter);
  843. }
  844. }
  845. return awaitable;
  846. error:
  847. return NULL;
  848. }