list_ops.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // List primitive operations
  2. //
  3. // These are registered in mypyc.primitives.list_ops.
  4. #include <Python.h>
  5. #include "CPy.h"
  6. #ifndef Py_TPFLAGS_SEQUENCE
  7. #define Py_TPFLAGS_SEQUENCE (1 << 5)
  8. #endif
  9. PyObject *CPyList_Build(Py_ssize_t len, ...) {
  10. Py_ssize_t i;
  11. PyObject *res = PyList_New(len);
  12. if (res == NULL) {
  13. return NULL;
  14. }
  15. va_list args;
  16. va_start(args, len);
  17. for (i = 0; i < len; i++) {
  18. // Steals the reference
  19. PyObject *value = va_arg(args, PyObject *);
  20. PyList_SET_ITEM(res, i, value);
  21. }
  22. va_end(args);
  23. return res;
  24. }
  25. PyObject *CPyList_GetItemUnsafe(PyObject *list, CPyTagged index) {
  26. Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
  27. PyObject *result = PyList_GET_ITEM(list, n);
  28. Py_INCREF(result);
  29. return result;
  30. }
  31. PyObject *CPyList_GetItemShort(PyObject *list, CPyTagged index) {
  32. Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
  33. Py_ssize_t size = PyList_GET_SIZE(list);
  34. if (n >= 0) {
  35. if (n >= size) {
  36. PyErr_SetString(PyExc_IndexError, "list index out of range");
  37. return NULL;
  38. }
  39. } else {
  40. n += size;
  41. if (n < 0) {
  42. PyErr_SetString(PyExc_IndexError, "list index out of range");
  43. return NULL;
  44. }
  45. }
  46. PyObject *result = PyList_GET_ITEM(list, n);
  47. Py_INCREF(result);
  48. return result;
  49. }
  50. PyObject *CPyList_GetItemShortBorrow(PyObject *list, CPyTagged index) {
  51. Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
  52. Py_ssize_t size = PyList_GET_SIZE(list);
  53. if (n >= 0) {
  54. if (n >= size) {
  55. PyErr_SetString(PyExc_IndexError, "list index out of range");
  56. return NULL;
  57. }
  58. } else {
  59. n += size;
  60. if (n < 0) {
  61. PyErr_SetString(PyExc_IndexError, "list index out of range");
  62. return NULL;
  63. }
  64. }
  65. return PyList_GET_ITEM(list, n);
  66. }
  67. PyObject *CPyList_GetItem(PyObject *list, CPyTagged index) {
  68. if (CPyTagged_CheckShort(index)) {
  69. Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
  70. Py_ssize_t size = PyList_GET_SIZE(list);
  71. if (n >= 0) {
  72. if (n >= size) {
  73. PyErr_SetString(PyExc_IndexError, "list index out of range");
  74. return NULL;
  75. }
  76. } else {
  77. n += size;
  78. if (n < 0) {
  79. PyErr_SetString(PyExc_IndexError, "list index out of range");
  80. return NULL;
  81. }
  82. }
  83. PyObject *result = PyList_GET_ITEM(list, n);
  84. Py_INCREF(result);
  85. return result;
  86. } else {
  87. PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
  88. return NULL;
  89. }
  90. }
  91. PyObject *CPyList_GetItemBorrow(PyObject *list, CPyTagged index) {
  92. if (CPyTagged_CheckShort(index)) {
  93. Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
  94. Py_ssize_t size = PyList_GET_SIZE(list);
  95. if (n >= 0) {
  96. if (n >= size) {
  97. PyErr_SetString(PyExc_IndexError, "list index out of range");
  98. return NULL;
  99. }
  100. } else {
  101. n += size;
  102. if (n < 0) {
  103. PyErr_SetString(PyExc_IndexError, "list index out of range");
  104. return NULL;
  105. }
  106. }
  107. return PyList_GET_ITEM(list, n);
  108. } else {
  109. PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
  110. return NULL;
  111. }
  112. }
  113. PyObject *CPyList_GetItemInt64(PyObject *list, int64_t index) {
  114. size_t size = PyList_GET_SIZE(list);
  115. if (likely((uint64_t)index < size)) {
  116. PyObject *result = PyList_GET_ITEM(list, index);
  117. Py_INCREF(result);
  118. return result;
  119. }
  120. if (index >= 0) {
  121. PyErr_SetString(PyExc_IndexError, "list index out of range");
  122. return NULL;
  123. }
  124. index += size;
  125. if (index < 0) {
  126. PyErr_SetString(PyExc_IndexError, "list index out of range");
  127. return NULL;
  128. }
  129. PyObject *result = PyList_GET_ITEM(list, index);
  130. Py_INCREF(result);
  131. return result;
  132. }
  133. PyObject *CPyList_GetItemInt64Borrow(PyObject *list, int64_t index) {
  134. size_t size = PyList_GET_SIZE(list);
  135. if (likely((uint64_t)index < size)) {
  136. return PyList_GET_ITEM(list, index);
  137. }
  138. if (index >= 0) {
  139. PyErr_SetString(PyExc_IndexError, "list index out of range");
  140. return NULL;
  141. }
  142. index += size;
  143. if (index < 0) {
  144. PyErr_SetString(PyExc_IndexError, "list index out of range");
  145. return NULL;
  146. }
  147. return PyList_GET_ITEM(list, index);
  148. }
  149. bool CPyList_SetItem(PyObject *list, CPyTagged index, PyObject *value) {
  150. if (CPyTagged_CheckShort(index)) {
  151. Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
  152. Py_ssize_t size = PyList_GET_SIZE(list);
  153. if (n >= 0) {
  154. if (n >= size) {
  155. PyErr_SetString(PyExc_IndexError, "list assignment index out of range");
  156. return false;
  157. }
  158. } else {
  159. n += size;
  160. if (n < 0) {
  161. PyErr_SetString(PyExc_IndexError, "list assignment index out of range");
  162. return false;
  163. }
  164. }
  165. // PyList_SET_ITEM doesn't decref the old element, so we do
  166. Py_DECREF(PyList_GET_ITEM(list, n));
  167. // N.B: Steals reference
  168. PyList_SET_ITEM(list, n, value);
  169. return true;
  170. } else {
  171. PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
  172. return false;
  173. }
  174. }
  175. bool CPyList_SetItemInt64(PyObject *list, int64_t index, PyObject *value) {
  176. size_t size = PyList_GET_SIZE(list);
  177. if (unlikely((uint64_t)index >= size)) {
  178. if (index > 0) {
  179. PyErr_SetString(PyExc_IndexError, "list assignment index out of range");
  180. return false;
  181. }
  182. index += size;
  183. if (index < 0) {
  184. PyErr_SetString(PyExc_IndexError, "list assignment index out of range");
  185. return false;
  186. }
  187. }
  188. // PyList_SET_ITEM doesn't decref the old element, so we do
  189. Py_DECREF(PyList_GET_ITEM(list, index));
  190. // N.B: Steals reference
  191. PyList_SET_ITEM(list, index, value);
  192. return true;
  193. }
  194. // This function should only be used to fill in brand new lists.
  195. bool CPyList_SetItemUnsafe(PyObject *list, CPyTagged index, PyObject *value) {
  196. if (CPyTagged_CheckShort(index)) {
  197. Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
  198. PyList_SET_ITEM(list, n, value);
  199. return true;
  200. } else {
  201. PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
  202. return false;
  203. }
  204. }
  205. PyObject *CPyList_PopLast(PyObject *obj)
  206. {
  207. // I tried a specalized version of pop_impl for just removing the
  208. // last element and it wasn't any faster in microbenchmarks than
  209. // the generic one so I ditched it.
  210. return list_pop_impl((PyListObject *)obj, -1);
  211. }
  212. PyObject *CPyList_Pop(PyObject *obj, CPyTagged index)
  213. {
  214. if (CPyTagged_CheckShort(index)) {
  215. Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
  216. return list_pop_impl((PyListObject *)obj, n);
  217. } else {
  218. PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
  219. return NULL;
  220. }
  221. }
  222. CPyTagged CPyList_Count(PyObject *obj, PyObject *value)
  223. {
  224. return list_count((PyListObject *)obj, value);
  225. }
  226. int CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value)
  227. {
  228. if (CPyTagged_CheckShort(index)) {
  229. Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
  230. return PyList_Insert(list, n, value);
  231. }
  232. // The max range doesn't exactly coincide with ssize_t, but we still
  233. // want to keep the error message compatible with CPython.
  234. PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
  235. return -1;
  236. }
  237. PyObject *CPyList_Extend(PyObject *o1, PyObject *o2) {
  238. return _PyList_Extend((PyListObject *)o1, o2);
  239. }
  240. // Return -2 or error, -1 if not found, or index of first match otherwise.
  241. static Py_ssize_t _CPyList_Find(PyObject *list, PyObject *obj) {
  242. Py_ssize_t i;
  243. for (i = 0; i < Py_SIZE(list); i++) {
  244. PyObject *item = PyList_GET_ITEM(list, i);
  245. Py_INCREF(item);
  246. int cmp = PyObject_RichCompareBool(item, obj, Py_EQ);
  247. Py_DECREF(item);
  248. if (cmp != 0) {
  249. if (cmp > 0) {
  250. return i;
  251. } else {
  252. return -2;
  253. }
  254. }
  255. }
  256. return -1;
  257. }
  258. int CPyList_Remove(PyObject *list, PyObject *obj) {
  259. Py_ssize_t index = _CPyList_Find(list, obj);
  260. if (index == -2) {
  261. return -1;
  262. }
  263. if (index == -1) {
  264. PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
  265. return -1;
  266. }
  267. return PyList_SetSlice(list, index, index + 1, NULL);
  268. }
  269. CPyTagged CPyList_Index(PyObject *list, PyObject *obj) {
  270. Py_ssize_t index = _CPyList_Find(list, obj);
  271. if (index == -2) {
  272. return CPY_INT_TAG;
  273. }
  274. if (index == -1) {
  275. PyErr_SetString(PyExc_ValueError, "value is not in list");
  276. return CPY_INT_TAG;
  277. }
  278. return index << 1;
  279. }
  280. PyObject *CPySequence_Multiply(PyObject *seq, CPyTagged t_size) {
  281. Py_ssize_t size = CPyTagged_AsSsize_t(t_size);
  282. if (size == -1 && PyErr_Occurred()) {
  283. return NULL;
  284. }
  285. return PySequence_Repeat(seq, size);
  286. }
  287. PyObject *CPySequence_RMultiply(CPyTagged t_size, PyObject *seq) {
  288. return CPySequence_Multiply(seq, t_size);
  289. }
  290. PyObject *CPyList_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end) {
  291. if (likely(PyList_CheckExact(obj)
  292. && CPyTagged_CheckShort(start) && CPyTagged_CheckShort(end))) {
  293. Py_ssize_t startn = CPyTagged_ShortAsSsize_t(start);
  294. Py_ssize_t endn = CPyTagged_ShortAsSsize_t(end);
  295. if (startn < 0) {
  296. startn += PyList_GET_SIZE(obj);
  297. }
  298. if (endn < 0) {
  299. endn += PyList_GET_SIZE(obj);
  300. }
  301. return PyList_GetSlice(obj, startn, endn);
  302. }
  303. return CPyObject_GetSlice(obj, start, end);
  304. }
  305. int CPySequence_Check(PyObject *obj) {
  306. return Py_TYPE(obj)->tp_flags & Py_TPFLAGS_SEQUENCE;
  307. }