CPy.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. // Mypyc C API
  2. #ifndef CPY_CPY_H
  3. #define CPY_CPY_H
  4. #include <stdbool.h>
  5. #include <Python.h>
  6. #include <frameobject.h>
  7. #include <structmember.h>
  8. #include <assert.h>
  9. #include <stdint.h>
  10. #include "pythonsupport.h"
  11. #include "mypyc_util.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #if 0
  16. } // why isn't emacs smart enough to not indent this
  17. #endif
  18. #define CPYTHON_LARGE_INT_ERRMSG "Python int too large to convert to C ssize_t"
  19. // Naming conventions:
  20. //
  21. // Tagged: tagged int
  22. // Long: tagged long int (pointer)
  23. // Short: tagged short int (unboxed)
  24. // Ssize_t: A Py_ssize_t, which ought to be the same width as pointers
  25. // Object: CPython object (PyObject *)
  26. // Tuple type definitions needed for API functions
  27. #ifndef MYPYC_DECLARED_tuple_T3OOO
  28. #define MYPYC_DECLARED_tuple_T3OOO
  29. typedef struct tuple_T3OOO {
  30. PyObject *f0;
  31. PyObject *f1;
  32. PyObject *f2;
  33. } tuple_T3OOO;
  34. static tuple_T3OOO tuple_undefined_T3OOO = { NULL, NULL, NULL };
  35. #endif
  36. // Our return tuple wrapper for dictionary iteration helper.
  37. #ifndef MYPYC_DECLARED_tuple_T3CIO
  38. #define MYPYC_DECLARED_tuple_T3CIO
  39. typedef struct tuple_T3CIO {
  40. char f0; // Should continue?
  41. CPyTagged f1; // Last dict offset
  42. PyObject *f2; // Next dictionary key or value
  43. } tuple_T3CIO;
  44. static tuple_T3CIO tuple_undefined_T3CIO = { 2, CPY_INT_TAG, NULL };
  45. #endif
  46. // Same as above but for both key and value.
  47. #ifndef MYPYC_DECLARED_tuple_T4CIOO
  48. #define MYPYC_DECLARED_tuple_T4CIOO
  49. typedef struct tuple_T4CIOO {
  50. char f0; // Should continue?
  51. CPyTagged f1; // Last dict offset
  52. PyObject *f2; // Next dictionary key
  53. PyObject *f3; // Next dictionary value
  54. } tuple_T4CIOO;
  55. static tuple_T4CIOO tuple_undefined_T4CIOO = { 2, CPY_INT_TAG, NULL, NULL };
  56. #endif
  57. // Native object operations
  58. // Search backwards through the trait part of a vtable (which sits *before*
  59. // the start of the vtable proper) looking for the subvtable describing a trait
  60. // implementation. We don't do any bounds checking so we'd better be pretty sure
  61. // we know that it is there.
  62. static inline CPyVTableItem *CPy_FindTraitVtable(PyTypeObject *trait, CPyVTableItem *vtable) {
  63. int i;
  64. for (i = -3; ; i -= 3) {
  65. if ((PyTypeObject *)vtable[i] == trait) {
  66. return (CPyVTableItem *)vtable[i + 1];
  67. }
  68. }
  69. }
  70. // Use the same logic for offset table.
  71. static inline size_t CPy_FindAttrOffset(PyTypeObject *trait, CPyVTableItem *vtable, size_t index) {
  72. int i;
  73. for (i = -3; ; i -= 3) {
  74. if ((PyTypeObject *)vtable[i] == trait) {
  75. return ((size_t *)vtable[i + 2])[index];
  76. }
  77. }
  78. }
  79. // Get attribute value using vtable (may return an undefined value)
  80. #define CPY_GET_ATTR(obj, type, vtable_index, object_type, attr_type) \
  81. ((attr_type (*)(object_type *))((object_type *)obj)->vtable[vtable_index])((object_type *)obj)
  82. #define CPY_GET_ATTR_TRAIT(obj, trait, vtable_index, object_type, attr_type) \
  83. ((attr_type (*)(object_type *))(CPy_FindTraitVtable(trait, ((object_type *)obj)->vtable))[vtable_index])((object_type *)obj)
  84. // Set attribute value using vtable
  85. #define CPY_SET_ATTR(obj, type, vtable_index, value, object_type, attr_type) \
  86. ((bool (*)(object_type *, attr_type))((object_type *)obj)->vtable[vtable_index])( \
  87. (object_type *)obj, value)
  88. #define CPY_SET_ATTR_TRAIT(obj, trait, vtable_index, value, object_type, attr_type) \
  89. ((bool (*)(object_type *, attr_type))(CPy_FindTraitVtable(trait, ((object_type *)obj)->vtable))[vtable_index])( \
  90. (object_type *)obj, value)
  91. #define CPY_GET_METHOD(obj, type, vtable_index, object_type, method_type) \
  92. ((method_type)(((object_type *)obj)->vtable[vtable_index]))
  93. #define CPY_GET_METHOD_TRAIT(obj, trait, vtable_index, object_type, method_type) \
  94. ((method_type)(CPy_FindTraitVtable(trait, ((object_type *)obj)->vtable)[vtable_index]))
  95. // Int operations
  96. CPyTagged CPyTagged_FromSsize_t(Py_ssize_t value);
  97. CPyTagged CPyTagged_FromVoidPtr(void *ptr);
  98. CPyTagged CPyTagged_FromInt64(int64_t value);
  99. CPyTagged CPyTagged_FromObject(PyObject *object);
  100. CPyTagged CPyTagged_StealFromObject(PyObject *object);
  101. CPyTagged CPyTagged_BorrowFromObject(PyObject *object);
  102. PyObject *CPyTagged_AsObject(CPyTagged x);
  103. PyObject *CPyTagged_StealAsObject(CPyTagged x);
  104. Py_ssize_t CPyTagged_AsSsize_t(CPyTagged x);
  105. void CPyTagged_IncRef(CPyTagged x);
  106. void CPyTagged_DecRef(CPyTagged x);
  107. void CPyTagged_XDecRef(CPyTagged x);
  108. CPyTagged CPyTagged_Negate(CPyTagged num);
  109. CPyTagged CPyTagged_Invert(CPyTagged num);
  110. CPyTagged CPyTagged_Add(CPyTagged left, CPyTagged right);
  111. CPyTagged CPyTagged_Subtract(CPyTagged left, CPyTagged right);
  112. CPyTagged CPyTagged_Multiply(CPyTagged left, CPyTagged right);
  113. CPyTagged CPyTagged_FloorDivide(CPyTagged left, CPyTagged right);
  114. CPyTagged CPyTagged_Remainder(CPyTagged left, CPyTagged right);
  115. CPyTagged CPyTagged_And(CPyTagged left, CPyTagged right);
  116. CPyTagged CPyTagged_Or(CPyTagged left, CPyTagged right);
  117. CPyTagged CPyTagged_Xor(CPyTagged left, CPyTagged right);
  118. CPyTagged CPyTagged_Rshift(CPyTagged left, CPyTagged right);
  119. CPyTagged CPyTagged_Lshift(CPyTagged left, CPyTagged right);
  120. bool CPyTagged_IsEq_(CPyTagged left, CPyTagged right);
  121. bool CPyTagged_IsLt_(CPyTagged left, CPyTagged right);
  122. PyObject *CPyTagged_Str(CPyTagged n);
  123. CPyTagged CPyTagged_FromFloat(double f);
  124. PyObject *CPyLong_FromStrWithBase(PyObject *o, CPyTagged base);
  125. PyObject *CPyLong_FromStr(PyObject *o);
  126. PyObject *CPyBool_Str(bool b);
  127. int64_t CPyLong_AsInt64(PyObject *o);
  128. int64_t CPyInt64_Divide(int64_t x, int64_t y);
  129. int64_t CPyInt64_Remainder(int64_t x, int64_t y);
  130. int32_t CPyLong_AsInt32(PyObject *o);
  131. int32_t CPyInt32_Divide(int32_t x, int32_t y);
  132. int32_t CPyInt32_Remainder(int32_t x, int32_t y);
  133. void CPyInt32_Overflow(void);
  134. double CPyTagged_TrueDivide(CPyTagged x, CPyTagged y);
  135. static inline int CPyTagged_CheckLong(CPyTagged x) {
  136. return x & CPY_INT_TAG;
  137. }
  138. static inline int CPyTagged_CheckShort(CPyTagged x) {
  139. return !CPyTagged_CheckLong(x);
  140. }
  141. static inline void CPyTagged_INCREF(CPyTagged x) {
  142. if (unlikely(CPyTagged_CheckLong(x))) {
  143. CPyTagged_IncRef(x);
  144. }
  145. }
  146. static inline void CPyTagged_DECREF(CPyTagged x) {
  147. if (unlikely(CPyTagged_CheckLong(x))) {
  148. CPyTagged_DecRef(x);
  149. }
  150. }
  151. static inline void CPyTagged_XDECREF(CPyTagged x) {
  152. if (unlikely(CPyTagged_CheckLong(x))) {
  153. CPyTagged_XDecRef(x);
  154. }
  155. }
  156. static inline Py_ssize_t CPyTagged_ShortAsSsize_t(CPyTagged x) {
  157. // NOTE: Assume that we sign extend.
  158. return (Py_ssize_t)x >> 1;
  159. }
  160. static inline PyObject *CPyTagged_LongAsObject(CPyTagged x) {
  161. // NOTE: Assume target is not a short int.
  162. return (PyObject *)(x & ~CPY_INT_TAG);
  163. }
  164. static inline bool CPyTagged_TooBig(Py_ssize_t value) {
  165. // Micro-optimized for the common case where it fits.
  166. return (size_t)value > CPY_TAGGED_MAX
  167. && (value >= 0 || value < CPY_TAGGED_MIN);
  168. }
  169. static inline bool CPyTagged_TooBigInt64(int64_t value) {
  170. // Micro-optimized for the common case where it fits.
  171. return (uint64_t)value > CPY_TAGGED_MAX
  172. && (value >= 0 || value < CPY_TAGGED_MIN);
  173. }
  174. static inline bool CPyTagged_IsAddOverflow(CPyTagged sum, CPyTagged left, CPyTagged right) {
  175. // This check was copied from some of my old code I believe that it works :-)
  176. return (Py_ssize_t)(sum ^ left) < 0 && (Py_ssize_t)(sum ^ right) < 0;
  177. }
  178. static inline bool CPyTagged_IsSubtractOverflow(CPyTagged diff, CPyTagged left, CPyTagged right) {
  179. // This check was copied from some of my old code I believe that it works :-)
  180. return (Py_ssize_t)(diff ^ left) < 0 && (Py_ssize_t)(diff ^ right) >= 0;
  181. }
  182. static inline bool CPyTagged_IsMultiplyOverflow(CPyTagged left, CPyTagged right) {
  183. // This is conservative -- return false only in a small number of all non-overflow cases
  184. return left >= (1U << (CPY_INT_BITS/2 - 1)) || right >= (1U << (CPY_INT_BITS/2 - 1));
  185. }
  186. static inline bool CPyTagged_MaybeFloorDivideFault(CPyTagged left, CPyTagged right) {
  187. return right == 0 || left == -((size_t)1 << (CPY_INT_BITS-1));
  188. }
  189. static inline bool CPyTagged_MaybeRemainderFault(CPyTagged left, CPyTagged right) {
  190. // Division/modulus can fault when dividing INT_MIN by -1, but we
  191. // do our mods on still-tagged integers with the low-bit clear, so
  192. // -1 is actually represented as -2 and can't overflow.
  193. // Mod by 0 can still fault though.
  194. return right == 0;
  195. }
  196. static inline bool CPyTagged_IsEq(CPyTagged left, CPyTagged right) {
  197. if (CPyTagged_CheckShort(left)) {
  198. return left == right;
  199. } else {
  200. return CPyTagged_IsEq_(left, right);
  201. }
  202. }
  203. static inline bool CPyTagged_IsNe(CPyTagged left, CPyTagged right) {
  204. if (CPyTagged_CheckShort(left)) {
  205. return left != right;
  206. } else {
  207. return !CPyTagged_IsEq_(left, right);
  208. }
  209. }
  210. static inline bool CPyTagged_IsLt(CPyTagged left, CPyTagged right) {
  211. if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right)) {
  212. return (Py_ssize_t)left < (Py_ssize_t)right;
  213. } else {
  214. return CPyTagged_IsLt_(left, right);
  215. }
  216. }
  217. static inline bool CPyTagged_IsGe(CPyTagged left, CPyTagged right) {
  218. if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right)) {
  219. return (Py_ssize_t)left >= (Py_ssize_t)right;
  220. } else {
  221. return !CPyTagged_IsLt_(left, right);
  222. }
  223. }
  224. static inline bool CPyTagged_IsGt(CPyTagged left, CPyTagged right) {
  225. if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right)) {
  226. return (Py_ssize_t)left > (Py_ssize_t)right;
  227. } else {
  228. return CPyTagged_IsLt_(right, left);
  229. }
  230. }
  231. static inline bool CPyTagged_IsLe(CPyTagged left, CPyTagged right) {
  232. if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right)) {
  233. return (Py_ssize_t)left <= (Py_ssize_t)right;
  234. } else {
  235. return !CPyTagged_IsLt_(right, left);
  236. }
  237. }
  238. // Float operations
  239. double CPyFloat_FloorDivide(double x, double y);
  240. double CPyFloat_Pow(double x, double y);
  241. double CPyFloat_Sin(double x);
  242. double CPyFloat_Cos(double x);
  243. double CPyFloat_Tan(double x);
  244. double CPyFloat_Sqrt(double x);
  245. double CPyFloat_Exp(double x);
  246. double CPyFloat_Log(double x);
  247. CPyTagged CPyFloat_Floor(double x);
  248. CPyTagged CPyFloat_Ceil(double x);
  249. double CPyFloat_FromTagged(CPyTagged x);
  250. bool CPyFloat_IsInf(double x);
  251. bool CPyFloat_IsNaN(double x);
  252. // Generic operations (that work with arbitrary types)
  253. /* We use intentionally non-inlined decrefs in rarely executed code
  254. * paths since it pretty substantially speeds up compile time. We have
  255. * our own copies both to avoid the null check in Py_DecRef and to avoid
  256. * making an indirect PIC call. */
  257. CPy_NOINLINE
  258. static void CPy_DecRef(PyObject *p) {
  259. CPy_DECREF(p);
  260. }
  261. CPy_NOINLINE
  262. static void CPy_XDecRef(PyObject *p) {
  263. CPy_XDECREF(p);
  264. }
  265. static inline CPyTagged CPyObject_Size(PyObject *obj) {
  266. Py_ssize_t s = PyObject_Size(obj);
  267. if (s < 0) {
  268. return CPY_INT_TAG;
  269. } else {
  270. // Technically __len__ could return a really big number, so we
  271. // should allow this to produce a boxed int. In practice it
  272. // shouldn't ever if the data structure actually contains all
  273. // the elements, but...
  274. return CPyTagged_FromSsize_t(s);
  275. }
  276. }
  277. #ifdef MYPYC_LOG_GETATTR
  278. static void CPy_LogGetAttr(const char *method, PyObject *obj, PyObject *attr) {
  279. PyObject *module = PyImport_ImportModule("getattr_hook");
  280. if (module) {
  281. PyObject *res = PyObject_CallMethodObjArgs(module, method, obj, attr, NULL);
  282. Py_XDECREF(res);
  283. Py_DECREF(module);
  284. }
  285. PyErr_Clear();
  286. }
  287. #else
  288. #define CPy_LogGetAttr(method, obj, attr) (void)0
  289. #endif
  290. // Intercept a method call and log it. This needs to be a macro
  291. // because there is no API that accepts va_args for making a
  292. // call. Worse, it needs to use the comma operator to return the right
  293. // value.
  294. #define CPyObject_CallMethodObjArgs(obj, attr, ...) \
  295. (CPy_LogGetAttr("log_method", (obj), (attr)), \
  296. PyObject_CallMethodObjArgs((obj), (attr), __VA_ARGS__))
  297. // This one is a macro for consistency with the above, I guess.
  298. #define CPyObject_GetAttr(obj, attr) \
  299. (CPy_LogGetAttr("log", (obj), (attr)), \
  300. PyObject_GetAttr((obj), (attr)))
  301. CPyTagged CPyObject_Hash(PyObject *o);
  302. PyObject *CPyObject_GetAttr3(PyObject *v, PyObject *name, PyObject *defl);
  303. PyObject *CPyIter_Next(PyObject *iter);
  304. PyObject *CPyNumber_Power(PyObject *base, PyObject *index);
  305. PyObject *CPyNumber_InPlacePower(PyObject *base, PyObject *index);
  306. PyObject *CPyObject_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end);
  307. // List operations
  308. PyObject *CPyList_Build(Py_ssize_t len, ...);
  309. PyObject *CPyList_GetItem(PyObject *list, CPyTagged index);
  310. PyObject *CPyList_GetItemUnsafe(PyObject *list, CPyTagged index);
  311. PyObject *CPyList_GetItemShort(PyObject *list, CPyTagged index);
  312. PyObject *CPyList_GetItemBorrow(PyObject *list, CPyTagged index);
  313. PyObject *CPyList_GetItemShortBorrow(PyObject *list, CPyTagged index);
  314. PyObject *CPyList_GetItemInt64(PyObject *list, int64_t index);
  315. PyObject *CPyList_GetItemInt64Borrow(PyObject *list, int64_t index);
  316. bool CPyList_SetItem(PyObject *list, CPyTagged index, PyObject *value);
  317. bool CPyList_SetItemUnsafe(PyObject *list, CPyTagged index, PyObject *value);
  318. bool CPyList_SetItemInt64(PyObject *list, int64_t index, PyObject *value);
  319. PyObject *CPyList_PopLast(PyObject *obj);
  320. PyObject *CPyList_Pop(PyObject *obj, CPyTagged index);
  321. CPyTagged CPyList_Count(PyObject *obj, PyObject *value);
  322. int CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value);
  323. PyObject *CPyList_Extend(PyObject *o1, PyObject *o2);
  324. int CPyList_Remove(PyObject *list, PyObject *obj);
  325. CPyTagged CPyList_Index(PyObject *list, PyObject *obj);
  326. PyObject *CPySequence_Multiply(PyObject *seq, CPyTagged t_size);
  327. PyObject *CPySequence_RMultiply(CPyTagged t_size, PyObject *seq);
  328. PyObject *CPyList_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end);
  329. int CPySequence_Check(PyObject *obj);
  330. // Dict operations
  331. PyObject *CPyDict_GetItem(PyObject *dict, PyObject *key);
  332. int CPyDict_SetItem(PyObject *dict, PyObject *key, PyObject *value);
  333. PyObject *CPyDict_Get(PyObject *dict, PyObject *key, PyObject *fallback);
  334. PyObject *CPyDict_GetWithNone(PyObject *dict, PyObject *key);
  335. PyObject *CPyDict_SetDefault(PyObject *dict, PyObject *key, PyObject *value);
  336. PyObject *CPyDict_SetDefaultWithNone(PyObject *dict, PyObject *key);
  337. PyObject *CPyDict_SetDefaultWithEmptyDatatype(PyObject *dict, PyObject *key, int data_type);
  338. PyObject *CPyDict_Build(Py_ssize_t size, ...);
  339. int CPyDict_Update(PyObject *dict, PyObject *stuff);
  340. int CPyDict_UpdateInDisplay(PyObject *dict, PyObject *stuff);
  341. int CPyDict_UpdateFromAny(PyObject *dict, PyObject *stuff);
  342. PyObject *CPyDict_FromAny(PyObject *obj);
  343. PyObject *CPyDict_KeysView(PyObject *dict);
  344. PyObject *CPyDict_ValuesView(PyObject *dict);
  345. PyObject *CPyDict_ItemsView(PyObject *dict);
  346. PyObject *CPyDict_Keys(PyObject *dict);
  347. PyObject *CPyDict_Values(PyObject *dict);
  348. PyObject *CPyDict_Items(PyObject *dict);
  349. char CPyDict_Clear(PyObject *dict);
  350. PyObject *CPyDict_Copy(PyObject *dict);
  351. PyObject *CPyDict_GetKeysIter(PyObject *dict);
  352. PyObject *CPyDict_GetItemsIter(PyObject *dict);
  353. PyObject *CPyDict_GetValuesIter(PyObject *dict);
  354. tuple_T3CIO CPyDict_NextKey(PyObject *dict_or_iter, CPyTagged offset);
  355. tuple_T3CIO CPyDict_NextValue(PyObject *dict_or_iter, CPyTagged offset);
  356. tuple_T4CIOO CPyDict_NextItem(PyObject *dict_or_iter, CPyTagged offset);
  357. int CPyMapping_Check(PyObject *obj);
  358. // Check that dictionary didn't change size during iteration.
  359. static inline char CPyDict_CheckSize(PyObject *dict, CPyTagged size) {
  360. if (!PyDict_CheckExact(dict)) {
  361. // Dict subclasses will be checked by Python runtime.
  362. return 1;
  363. }
  364. Py_ssize_t py_size = CPyTagged_AsSsize_t(size);
  365. Py_ssize_t dict_size = PyDict_Size(dict);
  366. if (py_size != dict_size) {
  367. PyErr_SetString(PyExc_RuntimeError, "dictionary changed size during iteration");
  368. return 0;
  369. }
  370. return 1;
  371. }
  372. // Str operations
  373. PyObject *CPyStr_Build(Py_ssize_t len, ...);
  374. PyObject *CPyStr_GetItem(PyObject *str, CPyTagged index);
  375. PyObject *CPyStr_Split(PyObject *str, PyObject *sep, CPyTagged max_split);
  376. PyObject *CPyStr_Replace(PyObject *str, PyObject *old_substr, PyObject *new_substr, CPyTagged max_replace);
  377. PyObject *CPyStr_Append(PyObject *o1, PyObject *o2);
  378. PyObject *CPyStr_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end);
  379. bool CPyStr_Startswith(PyObject *self, PyObject *subobj);
  380. bool CPyStr_Endswith(PyObject *self, PyObject *subobj);
  381. bool CPyStr_IsTrue(PyObject *obj);
  382. Py_ssize_t CPyStr_Size_size_t(PyObject *str);
  383. PyObject *CPy_Decode(PyObject *obj, PyObject *encoding, PyObject *errors);
  384. PyObject *CPy_Encode(PyObject *obj, PyObject *encoding, PyObject *errors);
  385. // Bytes operations
  386. PyObject *CPyBytes_Build(Py_ssize_t len, ...);
  387. PyObject *CPyBytes_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end);
  388. CPyTagged CPyBytes_GetItem(PyObject *o, CPyTagged index);
  389. PyObject *CPyBytes_Concat(PyObject *a, PyObject *b);
  390. PyObject *CPyBytes_Join(PyObject *sep, PyObject *iter);
  391. int CPyBytes_Compare(PyObject *left, PyObject *right);
  392. // Set operations
  393. bool CPySet_Remove(PyObject *set, PyObject *key);
  394. // Tuple operations
  395. PyObject *CPySequenceTuple_GetItem(PyObject *tuple, CPyTagged index);
  396. PyObject *CPySequenceTuple_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end);
  397. bool CPySequenceTuple_SetItemUnsafe(PyObject *tuple, CPyTagged index, PyObject *value);
  398. // Exception operations
  399. // mypyc is not very good at dealing with refcount management of
  400. // pointers that might be NULL. As a workaround for this, the
  401. // exception APIs that might want to return NULL pointers instead
  402. // return properly refcounted pointers to this dummy object.
  403. struct ExcDummyStruct { PyObject_HEAD };
  404. extern struct ExcDummyStruct _CPy_ExcDummyStruct;
  405. extern PyObject *_CPy_ExcDummy;
  406. static inline void _CPy_ToDummy(PyObject **p) {
  407. if (*p == NULL) {
  408. Py_INCREF(_CPy_ExcDummy);
  409. *p = _CPy_ExcDummy;
  410. }
  411. }
  412. static inline PyObject *_CPy_FromDummy(PyObject *p) {
  413. if (p == _CPy_ExcDummy) return NULL;
  414. Py_INCREF(p);
  415. return p;
  416. }
  417. static int CPy_NoErrOccured(void) {
  418. return PyErr_Occurred() == NULL;
  419. }
  420. static inline bool CPy_KeepPropagating(void) {
  421. return 0;
  422. }
  423. // We want to avoid the public PyErr_GetExcInfo API for these because
  424. // it requires a bunch of spurious refcount traffic on the parts of
  425. // the triple we don't care about.
  426. #define CPy_ExcState() PyThreadState_GET()->exc_info
  427. void CPy_Raise(PyObject *exc);
  428. void CPy_Reraise(void);
  429. void CPyErr_SetObjectAndTraceback(PyObject *type, PyObject *value, PyObject *traceback);
  430. tuple_T3OOO CPy_CatchError(void);
  431. void CPy_RestoreExcInfo(tuple_T3OOO info);
  432. bool CPy_ExceptionMatches(PyObject *type);
  433. PyObject *CPy_GetExcValue(void);
  434. tuple_T3OOO CPy_GetExcInfo(void);
  435. void _CPy_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback);
  436. void CPyError_OutOfMemory(void);
  437. void CPy_TypeError(const char *expected, PyObject *value);
  438. void CPy_AddTraceback(const char *filename, const char *funcname, int line, PyObject *globals);
  439. void CPy_TypeErrorTraceback(const char *filename, const char *funcname, int line,
  440. PyObject *globals, const char *expected, PyObject *value);
  441. void CPy_AttributeError(const char *filename, const char *funcname, const char *classname,
  442. const char *attrname, int line, PyObject *globals);
  443. // Misc operations
  444. #if PY_VERSION_HEX >= 0x03080000
  445. #define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_BEGIN(op, dealloc)
  446. #define CPy_TRASHCAN_END(op) Py_TRASHCAN_END
  447. #else
  448. #define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_SAFE_BEGIN(op)
  449. #define CPy_TRASHCAN_END(op) Py_TRASHCAN_SAFE_END(op)
  450. #endif
  451. // Tweaked version of _PyArg_Parser in CPython
  452. typedef struct CPyArg_Parser {
  453. const char *format;
  454. const char * const *keywords;
  455. const char *fname;
  456. const char *custom_msg;
  457. int pos; /* number of positional-only arguments */
  458. int min; /* minimal number of arguments */
  459. int max; /* maximal number of positional arguments */
  460. int has_required_kws; /* are there any keyword-only arguments? */
  461. int required_kwonly_start;
  462. int varargs; /* does the function accept *args or **kwargs? */
  463. PyObject *kwtuple; /* tuple of keyword parameter names */
  464. struct CPyArg_Parser *next;
  465. } CPyArg_Parser;
  466. // mypy lets ints silently coerce to floats, so a mypyc runtime float
  467. // might be an int also
  468. static inline bool CPyFloat_Check(PyObject *o) {
  469. return PyFloat_Check(o) || PyLong_Check(o);
  470. }
  471. // TODO: find an unified way to avoid inline functions in non-C back ends that can not
  472. // use inline functions
  473. static inline bool CPy_TypeCheck(PyObject *o, PyObject *type) {
  474. return PyObject_TypeCheck(o, (PyTypeObject *)type);
  475. }
  476. static inline PyObject *CPy_CalculateMetaclass(PyObject *type, PyObject *o) {
  477. return (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)type, o);
  478. }
  479. PyObject *CPy_GetCoro(PyObject *obj);
  480. PyObject *CPyIter_Send(PyObject *iter, PyObject *val);
  481. int CPy_YieldFromErrorHandle(PyObject *iter, PyObject **outp);
  482. PyObject *CPy_FetchStopIterationValue(void);
  483. PyObject *CPyType_FromTemplate(PyObject *template_,
  484. PyObject *orig_bases,
  485. PyObject *modname);
  486. PyObject *CPyType_FromTemplateWarpper(PyObject *template_,
  487. PyObject *orig_bases,
  488. PyObject *modname);
  489. int CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp,
  490. PyObject *dict, PyObject *annotations);
  491. PyObject *CPyPickle_SetState(PyObject *obj, PyObject *state);
  492. PyObject *CPyPickle_GetState(PyObject *obj);
  493. CPyTagged CPyTagged_Id(PyObject *o);
  494. void CPyDebug_Print(const char *msg);
  495. void CPy_Init(void);
  496. int CPyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
  497. const char *, const char *, const char * const *, ...);
  498. int CPyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
  499. CPyArg_Parser *parser, ...);
  500. int CPyArg_ParseStackAndKeywordsNoArgs(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
  501. CPyArg_Parser *parser, ...);
  502. int CPyArg_ParseStackAndKeywordsOneArg(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
  503. CPyArg_Parser *parser, ...);
  504. int CPyArg_ParseStackAndKeywordsSimple(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
  505. CPyArg_Parser *parser, ...);
  506. int CPySequence_CheckUnpackCount(PyObject *sequence, Py_ssize_t expected);
  507. int CPyStatics_Initialize(PyObject **statics,
  508. const char * const *strings,
  509. const char * const *bytestrings,
  510. const char * const *ints,
  511. const double *floats,
  512. const double *complex_numbers,
  513. const int *tuples,
  514. const int *frozensets);
  515. PyObject *CPy_Super(PyObject *builtins, PyObject *self);
  516. PyObject *CPy_CallReverseOpMethod(PyObject *left, PyObject *right, const char *op,
  517. _Py_Identifier *method);
  518. bool CPyImport_ImportMany(PyObject *modules, CPyModule **statics[], PyObject *globals,
  519. PyObject *tb_path, PyObject *tb_function, Py_ssize_t *tb_lines);
  520. PyObject *CPyImport_ImportFromMany(PyObject *mod_id, PyObject *names, PyObject *as_names,
  521. PyObject *globals);
  522. PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func, PyObject *cls,
  523. PyObject *func);
  524. PyObject *CPy_GetAIter(PyObject *obj);
  525. PyObject *CPy_GetANext(PyObject *aiter);
  526. #ifdef __cplusplus
  527. }
  528. #endif
  529. #endif // CPY_CPY_H