/Objects/sliceobject.c

http://unladen-swallow.googlecode.com/ · C · 361 lines · 292 code · 47 blank · 22 comment · 75 complexity · b52b82dcd5b5361746d057ee01dcde52 MD5 · raw file

  1. /*
  2. Written by Jim Hugunin and Chris Chase.
  3. This includes both the singular ellipsis object and slice objects.
  4. Guido, feel free to do whatever you want in the way of copyrights
  5. for this file.
  6. */
  7. /*
  8. Py_Ellipsis encodes the '...' rubber index token. It is similar to
  9. the Py_NoneStruct in that there is no way to create other objects of
  10. this type and there is exactly one in existence.
  11. */
  12. #include "Python.h"
  13. #include "structmember.h"
  14. static PyObject *
  15. ellipsis_repr(PyObject *op)
  16. {
  17. return PyString_FromString("Ellipsis");
  18. }
  19. PyTypeObject PyEllipsis_Type = {
  20. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  21. "ellipsis", /* tp_name */
  22. 0, /* tp_basicsize */
  23. 0, /* tp_itemsize */
  24. 0, /*never called*/ /* tp_dealloc */
  25. 0, /* tp_print */
  26. 0, /* tp_getattr */
  27. 0, /* tp_setattr */
  28. 0, /* tp_compare */
  29. ellipsis_repr, /* tp_repr */
  30. 0, /* tp_as_number */
  31. 0, /* tp_as_sequence */
  32. 0, /* tp_as_mapping */
  33. 0, /* tp_hash */
  34. 0, /* tp_call */
  35. 0, /* tp_str */
  36. PyObject_GenericGetAttr, /* tp_getattro */
  37. 0, /* tp_setattro */
  38. 0, /* tp_as_buffer */
  39. Py_TPFLAGS_DEFAULT, /* tp_flags */
  40. };
  41. PyObject _Py_EllipsisObject = {
  42. _PyObject_EXTRA_INIT
  43. 1, &PyEllipsis_Type
  44. };
  45. /* Slice object implementation
  46. start, stop, and step are python objects with None indicating no
  47. index is present.
  48. */
  49. PyObject *
  50. PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
  51. {
  52. PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type);
  53. if (obj == NULL)
  54. return NULL;
  55. if (step == NULL) step = Py_None;
  56. Py_INCREF(step);
  57. if (start == NULL) start = Py_None;
  58. Py_INCREF(start);
  59. if (stop == NULL) stop = Py_None;
  60. Py_INCREF(stop);
  61. obj->step = step;
  62. obj->start = start;
  63. obj->stop = stop;
  64. return (PyObject *) obj;
  65. }
  66. PyObject *
  67. _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop)
  68. {
  69. PyObject *start, *end, *slice;
  70. start = PyInt_FromSsize_t(istart);
  71. if (!start)
  72. return NULL;
  73. end = PyInt_FromSsize_t(istop);
  74. if (!end) {
  75. Py_DECREF(start);
  76. return NULL;
  77. }
  78. slice = PySlice_New(start, end, NULL);
  79. Py_DECREF(start);
  80. Py_DECREF(end);
  81. return slice;
  82. }
  83. int
  84. PySlice_GetIndices(PySliceObject *r, Py_ssize_t length,
  85. Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
  86. {
  87. /* XXX support long ints */
  88. if (r->step == Py_None) {
  89. *step = 1;
  90. } else {
  91. if (!PyInt_Check(r->step) && !PyLong_Check(r->step)) return -1;
  92. *step = PyInt_AsSsize_t(r->step);
  93. }
  94. if (r->start == Py_None) {
  95. *start = *step < 0 ? length-1 : 0;
  96. } else {
  97. if (!PyInt_Check(r->start) && !PyLong_Check(r->step)) return -1;
  98. *start = PyInt_AsSsize_t(r->start);
  99. if (*start < 0) *start += length;
  100. }
  101. if (r->stop == Py_None) {
  102. *stop = *step < 0 ? -1 : length;
  103. } else {
  104. if (!PyInt_Check(r->stop) && !PyLong_Check(r->step)) return -1;
  105. *stop = PyInt_AsSsize_t(r->stop);
  106. if (*stop < 0) *stop += length;
  107. }
  108. if (*stop > length) return -1;
  109. if (*start >= length) return -1;
  110. if (*step == 0) return -1;
  111. return 0;
  112. }
  113. int
  114. PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length,
  115. Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength)
  116. {
  117. /* this is harder to get right than you might think */
  118. Py_ssize_t defstart, defstop;
  119. if (r->step == Py_None) {
  120. *step = 1;
  121. }
  122. else {
  123. if (!_PyEval_SliceIndex(r->step, step)) return -1;
  124. if (*step == 0) {
  125. PyErr_SetString(PyExc_ValueError,
  126. "slice step cannot be zero");
  127. return -1;
  128. }
  129. }
  130. defstart = *step < 0 ? length-1 : 0;
  131. defstop = *step < 0 ? -1 : length;
  132. if (r->start == Py_None) {
  133. *start = defstart;
  134. }
  135. else {
  136. if (!_PyEval_SliceIndex(r->start, start)) return -1;
  137. if (*start < 0) *start += length;
  138. if (*start < 0) *start = (*step < 0) ? -1 : 0;
  139. if (*start >= length)
  140. *start = (*step < 0) ? length - 1 : length;
  141. }
  142. if (r->stop == Py_None) {
  143. *stop = defstop;
  144. }
  145. else {
  146. if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
  147. if (*stop < 0) *stop += length;
  148. if (*stop < 0) *stop = (*step < 0) ? -1 : 0;
  149. if (*stop >= length)
  150. *stop = (*step < 0) ? length - 1 : length;
  151. }
  152. if ((*step < 0 && *stop >= *start)
  153. || (*step > 0 && *start >= *stop)) {
  154. *slicelength = 0;
  155. }
  156. else if (*step < 0) {
  157. *slicelength = (*stop-*start+1)/(*step)+1;
  158. }
  159. else {
  160. *slicelength = (*stop-*start-1)/(*step)+1;
  161. }
  162. return 0;
  163. }
  164. static PyObject *
  165. slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
  166. {
  167. PyObject *start, *stop, *step;
  168. start = stop = step = NULL;
  169. if (!_PyArg_NoKeywords("slice()", kw))
  170. return NULL;
  171. if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
  172. return NULL;
  173. /* This swapping of stop and start is to maintain similarity with
  174. range(). */
  175. if (stop == NULL) {
  176. stop = start;
  177. start = NULL;
  178. }
  179. return PySlice_New(start, stop, step);
  180. }
  181. PyDoc_STRVAR(slice_doc,
  182. "slice([start,] stop[, step])\n\
  183. \n\
  184. Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).");
  185. static void
  186. slice_dealloc(PySliceObject *r)
  187. {
  188. Py_DECREF(r->step);
  189. Py_DECREF(r->start);
  190. Py_DECREF(r->stop);
  191. PyObject_Del(r);
  192. }
  193. static PyObject *
  194. slice_repr(PySliceObject *r)
  195. {
  196. PyObject *s, *comma;
  197. s = PyString_FromString("slice(");
  198. comma = PyString_FromString(", ");
  199. PyString_ConcatAndDel(&s, PyObject_Repr(r->start));
  200. PyString_Concat(&s, comma);
  201. PyString_ConcatAndDel(&s, PyObject_Repr(r->stop));
  202. PyString_Concat(&s, comma);
  203. PyString_ConcatAndDel(&s, PyObject_Repr(r->step));
  204. PyString_ConcatAndDel(&s, PyString_FromString(")"));
  205. Py_DECREF(comma);
  206. return s;
  207. }
  208. static PyMemberDef slice_members[] = {
  209. {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY},
  210. {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY},
  211. {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY},
  212. {0}
  213. };
  214. static PyObject*
  215. slice_indices(PySliceObject* self, PyObject* len)
  216. {
  217. Py_ssize_t ilen, start, stop, step, slicelength;
  218. ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError);
  219. if (ilen == -1 && PyErr_Occurred()) {
  220. return NULL;
  221. }
  222. if (PySlice_GetIndicesEx(self, ilen, &start, &stop,
  223. &step, &slicelength) < 0) {
  224. return NULL;
  225. }
  226. return Py_BuildValue("(nnn)", start, stop, step);
  227. }
  228. PyDoc_STRVAR(slice_indices_doc,
  229. "S.indices(len) -> (start, stop, stride)\n\
  230. \n\
  231. Assuming a sequence of length len, calculate the start and stop\n\
  232. indices, and the stride length of the extended slice described by\n\
  233. S. Out of bounds indices are clipped in a manner consistent with the\n\
  234. handling of normal slices.");
  235. static PyObject *
  236. slice_reduce(PySliceObject* self)
  237. {
  238. return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step);
  239. }
  240. PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
  241. static PyMethodDef slice_methods[] = {
  242. {"indices", (PyCFunction)slice_indices,
  243. METH_O, slice_indices_doc},
  244. {"__reduce__", (PyCFunction)slice_reduce,
  245. METH_NOARGS, reduce_doc},
  246. {NULL, NULL}
  247. };
  248. static int
  249. slice_compare(PySliceObject *v, PySliceObject *w)
  250. {
  251. int result = 0;
  252. if (v == w)
  253. return 0;
  254. if (PyObject_Cmp(v->start, w->start, &result) < 0)
  255. return -2;
  256. if (result != 0)
  257. return result;
  258. if (PyObject_Cmp(v->stop, w->stop, &result) < 0)
  259. return -2;
  260. if (result != 0)
  261. return result;
  262. if (PyObject_Cmp(v->step, w->step, &result) < 0)
  263. return -2;
  264. return result;
  265. }
  266. static long
  267. slice_hash(PySliceObject *v)
  268. {
  269. PyErr_SetString(PyExc_TypeError, "unhashable type");
  270. return -1L;
  271. }
  272. PyTypeObject PySlice_Type = {
  273. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  274. "slice", /* Name of this type */
  275. sizeof(PySliceObject), /* Basic object size */
  276. 0, /* Item size for varobject */
  277. (destructor)slice_dealloc, /* tp_dealloc */
  278. 0, /* tp_print */
  279. 0, /* tp_getattr */
  280. 0, /* tp_setattr */
  281. (cmpfunc)slice_compare, /* tp_compare */
  282. (reprfunc)slice_repr, /* tp_repr */
  283. 0, /* tp_as_number */
  284. 0, /* tp_as_sequence */
  285. 0, /* tp_as_mapping */
  286. (hashfunc)slice_hash, /* tp_hash */
  287. 0, /* tp_call */
  288. 0, /* tp_str */
  289. PyObject_GenericGetAttr, /* tp_getattro */
  290. 0, /* tp_setattro */
  291. 0, /* tp_as_buffer */
  292. Py_TPFLAGS_DEFAULT, /* tp_flags */
  293. slice_doc, /* tp_doc */
  294. 0, /* tp_traverse */
  295. 0, /* tp_clear */
  296. 0, /* tp_richcompare */
  297. 0, /* tp_weaklistoffset */
  298. 0, /* tp_iter */
  299. 0, /* tp_iternext */
  300. slice_methods, /* tp_methods */
  301. slice_members, /* tp_members */
  302. 0, /* tp_getset */
  303. 0, /* tp_base */
  304. 0, /* tp_dict */
  305. 0, /* tp_descr_get */
  306. 0, /* tp_descr_set */
  307. 0, /* tp_dictoffset */
  308. 0, /* tp_init */
  309. 0, /* tp_alloc */
  310. slice_new, /* tp_new */
  311. };