rangeobject.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /* Range object implementation */
  2. #include "Python.h"
  3. #include "pycore_abstract.h" // _PyIndex_Check()
  4. #include "pycore_range.h"
  5. #include "pycore_long.h" // _PyLong_GetZero()
  6. #include "pycore_tuple.h" // _PyTuple_ITEMS()
  7. #include "structmember.h" // PyMemberDef
  8. /* Support objects whose length is > PY_SSIZE_T_MAX.
  9. This could be sped up for small PyLongs if they fit in a Py_ssize_t.
  10. This only matters on Win64. Though we could use long long which
  11. would presumably help perf.
  12. */
  13. typedef struct {
  14. PyObject_HEAD
  15. PyObject *start;
  16. PyObject *stop;
  17. PyObject *step;
  18. PyObject *length;
  19. } rangeobject;
  20. /* Helper function for validating step. Always returns a new reference or
  21. NULL on error.
  22. */
  23. static PyObject *
  24. validate_step(PyObject *step)
  25. {
  26. /* No step specified, use a step of 1. */
  27. if (!step)
  28. return PyLong_FromLong(1);
  29. step = PyNumber_Index(step);
  30. if (step && _PyLong_IsZero((PyLongObject *)step)) {
  31. PyErr_SetString(PyExc_ValueError,
  32. "range() arg 3 must not be zero");
  33. Py_CLEAR(step);
  34. }
  35. return step;
  36. }
  37. static PyObject *
  38. compute_range_length(PyObject *start, PyObject *stop, PyObject *step);
  39. static rangeobject *
  40. make_range_object(PyTypeObject *type, PyObject *start,
  41. PyObject *stop, PyObject *step)
  42. {
  43. rangeobject *obj = NULL;
  44. PyObject *length;
  45. length = compute_range_length(start, stop, step);
  46. if (length == NULL) {
  47. return NULL;
  48. }
  49. obj = PyObject_New(rangeobject, type);
  50. if (obj == NULL) {
  51. Py_DECREF(length);
  52. return NULL;
  53. }
  54. obj->start = start;
  55. obj->stop = stop;
  56. obj->step = step;
  57. obj->length = length;
  58. return obj;
  59. }
  60. /* XXX(nnorwitz): should we error check if the user passes any empty ranges?
  61. range(-10)
  62. range(0, -5)
  63. range(0, 5, -1)
  64. */
  65. static PyObject *
  66. range_from_array(PyTypeObject *type, PyObject *const *args, Py_ssize_t num_args)
  67. {
  68. rangeobject *obj;
  69. PyObject *start = NULL, *stop = NULL, *step = NULL;
  70. switch (num_args) {
  71. case 3:
  72. step = args[2];
  73. /* fallthrough */
  74. case 2:
  75. /* Convert borrowed refs to owned refs */
  76. start = PyNumber_Index(args[0]);
  77. if (!start) {
  78. return NULL;
  79. }
  80. stop = PyNumber_Index(args[1]);
  81. if (!stop) {
  82. Py_DECREF(start);
  83. return NULL;
  84. }
  85. step = validate_step(step); /* Caution, this can clear exceptions */
  86. if (!step) {
  87. Py_DECREF(start);
  88. Py_DECREF(stop);
  89. return NULL;
  90. }
  91. break;
  92. case 1:
  93. stop = PyNumber_Index(args[0]);
  94. if (!stop) {
  95. return NULL;
  96. }
  97. start = Py_NewRef(_PyLong_GetZero());
  98. step = Py_NewRef(_PyLong_GetOne());
  99. break;
  100. case 0:
  101. PyErr_SetString(PyExc_TypeError,
  102. "range expected at least 1 argument, got 0");
  103. return NULL;
  104. default:
  105. PyErr_Format(PyExc_TypeError,
  106. "range expected at most 3 arguments, got %zd",
  107. num_args);
  108. return NULL;
  109. }
  110. obj = make_range_object(type, start, stop, step);
  111. if (obj != NULL) {
  112. return (PyObject *) obj;
  113. }
  114. /* Failed to create object, release attributes */
  115. Py_DECREF(start);
  116. Py_DECREF(stop);
  117. Py_DECREF(step);
  118. return NULL;
  119. }
  120. static PyObject *
  121. range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
  122. {
  123. if (!_PyArg_NoKeywords("range", kw))
  124. return NULL;
  125. return range_from_array(type, _PyTuple_ITEMS(args), PyTuple_GET_SIZE(args));
  126. }
  127. static PyObject *
  128. range_vectorcall(PyTypeObject *type, PyObject *const *args,
  129. size_t nargsf, PyObject *kwnames)
  130. {
  131. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  132. if (!_PyArg_NoKwnames("range", kwnames)) {
  133. return NULL;
  134. }
  135. return range_from_array(type, args, nargs);
  136. }
  137. PyDoc_STRVAR(range_doc,
  138. "range(stop) -> range object\n\
  139. range(start, stop[, step]) -> range object\n\
  140. \n\
  141. Return an object that produces a sequence of integers from start (inclusive)\n\
  142. to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.\n\
  143. start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.\n\
  144. These are exactly the valid indices for a list of 4 elements.\n\
  145. When step is given, it specifies the increment (or decrement).");
  146. static void
  147. range_dealloc(rangeobject *r)
  148. {
  149. Py_DECREF(r->start);
  150. Py_DECREF(r->stop);
  151. Py_DECREF(r->step);
  152. Py_DECREF(r->length);
  153. PyObject_Free(r);
  154. }
  155. static unsigned long
  156. get_len_of_range(long lo, long hi, long step);
  157. /* Return the length as a long, -2 for an overflow and -1 for any other type of error
  158. *
  159. * In case of an overflow no error is set
  160. */
  161. static long compute_range_length_long(PyObject *start,
  162. PyObject *stop, PyObject *step) {
  163. int overflow = 0;
  164. long long_start = PyLong_AsLongAndOverflow(start, &overflow);
  165. if (overflow) {
  166. return -2;
  167. }
  168. if (long_start == -1 && PyErr_Occurred()) {
  169. return -1;
  170. }
  171. long long_stop = PyLong_AsLongAndOverflow(stop, &overflow);
  172. if (overflow) {
  173. return -2;
  174. }
  175. if (long_stop == -1 && PyErr_Occurred()) {
  176. return -1;
  177. }
  178. long long_step = PyLong_AsLongAndOverflow(step, &overflow);
  179. if (overflow) {
  180. return -2;
  181. }
  182. if (long_step == -1 && PyErr_Occurred()) {
  183. return -1;
  184. }
  185. unsigned long ulen = get_len_of_range(long_start, long_stop, long_step);
  186. if (ulen > (unsigned long)LONG_MAX) {
  187. /* length too large for a long */
  188. return -2;
  189. }
  190. else {
  191. return (long)ulen;
  192. }
  193. }
  194. /* Return number of items in range (lo, hi, step) as a PyLong object,
  195. * when arguments are PyLong objects. Arguments MUST return 1 with
  196. * PyLong_Check(). Return NULL when there is an error.
  197. */
  198. static PyObject*
  199. compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
  200. {
  201. /* -------------------------------------------------------------
  202. Algorithm is equal to that of get_len_of_range(), but it operates
  203. on PyObjects (which are assumed to be PyLong objects).
  204. ---------------------------------------------------------------*/
  205. int cmp_result;
  206. PyObject *lo, *hi;
  207. PyObject *diff = NULL;
  208. PyObject *tmp1 = NULL, *tmp2 = NULL, *result;
  209. /* holds sub-expression evaluations */
  210. PyObject *zero = _PyLong_GetZero(); // borrowed reference
  211. PyObject *one = _PyLong_GetOne(); // borrowed reference
  212. assert(PyLong_Check(start));
  213. assert(PyLong_Check(stop));
  214. assert(PyLong_Check(step));
  215. /* fast path when all arguments fit into a long integer */
  216. long len = compute_range_length_long(start, stop, step);
  217. if (len >= 0) {
  218. return PyLong_FromLong(len);
  219. }
  220. else if (len == -1) {
  221. /* unexpected error from compute_range_length_long, we propagate to the caller */
  222. return NULL;
  223. }
  224. assert(len == -2);
  225. cmp_result = PyObject_RichCompareBool(step, zero, Py_GT);
  226. if (cmp_result == -1)
  227. return NULL;
  228. if (cmp_result == 1) {
  229. lo = start;
  230. hi = stop;
  231. Py_INCREF(step);
  232. } else {
  233. lo = stop;
  234. hi = start;
  235. step = PyNumber_Negative(step);
  236. if (!step)
  237. return NULL;
  238. }
  239. /* if (lo >= hi), return length of 0. */
  240. cmp_result = PyObject_RichCompareBool(lo, hi, Py_GE);
  241. if (cmp_result != 0) {
  242. Py_DECREF(step);
  243. if (cmp_result < 0)
  244. return NULL;
  245. result = zero;
  246. return Py_NewRef(result);
  247. }
  248. if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
  249. goto Fail;
  250. if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
  251. goto Fail;
  252. if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
  253. goto Fail;
  254. if ((result = PyNumber_Add(tmp2, one)) == NULL)
  255. goto Fail;
  256. Py_DECREF(tmp2);
  257. Py_DECREF(diff);
  258. Py_DECREF(step);
  259. Py_DECREF(tmp1);
  260. return result;
  261. Fail:
  262. Py_DECREF(step);
  263. Py_XDECREF(tmp2);
  264. Py_XDECREF(diff);
  265. Py_XDECREF(tmp1);
  266. return NULL;
  267. }
  268. static Py_ssize_t
  269. range_length(rangeobject *r)
  270. {
  271. return PyLong_AsSsize_t(r->length);
  272. }
  273. static PyObject *
  274. compute_item(rangeobject *r, PyObject *i)
  275. {
  276. PyObject *incr, *result;
  277. /* PyLong equivalent to:
  278. * return r->start + (i * r->step)
  279. */
  280. if (r->step == _PyLong_GetOne()) {
  281. result = PyNumber_Add(r->start, i);
  282. }
  283. else {
  284. incr = PyNumber_Multiply(i, r->step);
  285. if (!incr) {
  286. return NULL;
  287. }
  288. result = PyNumber_Add(r->start, incr);
  289. Py_DECREF(incr);
  290. }
  291. return result;
  292. }
  293. static PyObject *
  294. compute_range_item(rangeobject *r, PyObject *arg)
  295. {
  296. PyObject *zero = _PyLong_GetZero(); // borrowed reference
  297. int cmp_result;
  298. PyObject *i, *result;
  299. /* PyLong equivalent to:
  300. * if (arg < 0) {
  301. * i = r->length + arg
  302. * } else {
  303. * i = arg
  304. * }
  305. */
  306. cmp_result = PyObject_RichCompareBool(arg, zero, Py_LT);
  307. if (cmp_result == -1) {
  308. return NULL;
  309. }
  310. if (cmp_result == 1) {
  311. i = PyNumber_Add(r->length, arg);
  312. if (!i) {
  313. return NULL;
  314. }
  315. } else {
  316. i = Py_NewRef(arg);
  317. }
  318. /* PyLong equivalent to:
  319. * if (i < 0 || i >= r->length) {
  320. * <report index out of bounds>
  321. * }
  322. */
  323. cmp_result = PyObject_RichCompareBool(i, zero, Py_LT);
  324. if (cmp_result == 0) {
  325. cmp_result = PyObject_RichCompareBool(i, r->length, Py_GE);
  326. }
  327. if (cmp_result == -1) {
  328. Py_DECREF(i);
  329. return NULL;
  330. }
  331. if (cmp_result == 1) {
  332. Py_DECREF(i);
  333. PyErr_SetString(PyExc_IndexError,
  334. "range object index out of range");
  335. return NULL;
  336. }
  337. result = compute_item(r, i);
  338. Py_DECREF(i);
  339. return result;
  340. }
  341. static PyObject *
  342. range_item(rangeobject *r, Py_ssize_t i)
  343. {
  344. PyObject *res, *arg = PyLong_FromSsize_t(i);
  345. if (!arg) {
  346. return NULL;
  347. }
  348. res = compute_range_item(r, arg);
  349. Py_DECREF(arg);
  350. return res;
  351. }
  352. static PyObject *
  353. compute_slice(rangeobject *r, PyObject *_slice)
  354. {
  355. PySliceObject *slice = (PySliceObject *) _slice;
  356. rangeobject *result;
  357. PyObject *start = NULL, *stop = NULL, *step = NULL;
  358. PyObject *substart = NULL, *substop = NULL, *substep = NULL;
  359. int error;
  360. error = _PySlice_GetLongIndices(slice, r->length, &start, &stop, &step);
  361. if (error == -1)
  362. return NULL;
  363. substep = PyNumber_Multiply(r->step, step);
  364. if (substep == NULL) goto fail;
  365. Py_CLEAR(step);
  366. substart = compute_item(r, start);
  367. if (substart == NULL) goto fail;
  368. Py_CLEAR(start);
  369. substop = compute_item(r, stop);
  370. if (substop == NULL) goto fail;
  371. Py_CLEAR(stop);
  372. result = make_range_object(Py_TYPE(r), substart, substop, substep);
  373. if (result != NULL) {
  374. return (PyObject *) result;
  375. }
  376. fail:
  377. Py_XDECREF(start);
  378. Py_XDECREF(stop);
  379. Py_XDECREF(step);
  380. Py_XDECREF(substart);
  381. Py_XDECREF(substop);
  382. Py_XDECREF(substep);
  383. return NULL;
  384. }
  385. /* Assumes (PyLong_CheckExact(ob) || PyBool_Check(ob)) */
  386. static int
  387. range_contains_long(rangeobject *r, PyObject *ob)
  388. {
  389. PyObject *zero = _PyLong_GetZero(); // borrowed reference
  390. int cmp1, cmp2, cmp3;
  391. PyObject *tmp1 = NULL;
  392. PyObject *tmp2 = NULL;
  393. int result = -1;
  394. /* Check if the value can possibly be in the range. */
  395. cmp1 = PyObject_RichCompareBool(r->step, zero, Py_GT);
  396. if (cmp1 == -1)
  397. goto end;
  398. if (cmp1 == 1) { /* positive steps: start <= ob < stop */
  399. cmp2 = PyObject_RichCompareBool(r->start, ob, Py_LE);
  400. cmp3 = PyObject_RichCompareBool(ob, r->stop, Py_LT);
  401. }
  402. else { /* negative steps: stop < ob <= start */
  403. cmp2 = PyObject_RichCompareBool(ob, r->start, Py_LE);
  404. cmp3 = PyObject_RichCompareBool(r->stop, ob, Py_LT);
  405. }
  406. if (cmp2 == -1 || cmp3 == -1) /* TypeError */
  407. goto end;
  408. if (cmp2 == 0 || cmp3 == 0) { /* ob outside of range */
  409. result = 0;
  410. goto end;
  411. }
  412. /* Check that the stride does not invalidate ob's membership. */
  413. tmp1 = PyNumber_Subtract(ob, r->start);
  414. if (tmp1 == NULL)
  415. goto end;
  416. tmp2 = PyNumber_Remainder(tmp1, r->step);
  417. if (tmp2 == NULL)
  418. goto end;
  419. /* result = ((int(ob) - start) % step) == 0 */
  420. result = PyObject_RichCompareBool(tmp2, zero, Py_EQ);
  421. end:
  422. Py_XDECREF(tmp1);
  423. Py_XDECREF(tmp2);
  424. return result;
  425. }
  426. static int
  427. range_contains(rangeobject *r, PyObject *ob)
  428. {
  429. if (PyLong_CheckExact(ob) || PyBool_Check(ob))
  430. return range_contains_long(r, ob);
  431. return (int)_PySequence_IterSearch((PyObject*)r, ob,
  432. PY_ITERSEARCH_CONTAINS);
  433. }
  434. /* Compare two range objects. Return 1 for equal, 0 for not equal
  435. and -1 on error. The algorithm is roughly the C equivalent of
  436. if r0 is r1:
  437. return True
  438. if len(r0) != len(r1):
  439. return False
  440. if not len(r0):
  441. return True
  442. if r0.start != r1.start:
  443. return False
  444. if len(r0) == 1:
  445. return True
  446. return r0.step == r1.step
  447. */
  448. static int
  449. range_equals(rangeobject *r0, rangeobject *r1)
  450. {
  451. int cmp_result;
  452. if (r0 == r1)
  453. return 1;
  454. cmp_result = PyObject_RichCompareBool(r0->length, r1->length, Py_EQ);
  455. /* Return False or error to the caller. */
  456. if (cmp_result != 1)
  457. return cmp_result;
  458. cmp_result = PyObject_Not(r0->length);
  459. /* Return True or error to the caller. */
  460. if (cmp_result != 0)
  461. return cmp_result;
  462. cmp_result = PyObject_RichCompareBool(r0->start, r1->start, Py_EQ);
  463. /* Return False or error to the caller. */
  464. if (cmp_result != 1)
  465. return cmp_result;
  466. cmp_result = PyObject_RichCompareBool(r0->length, _PyLong_GetOne(), Py_EQ);
  467. /* Return True or error to the caller. */
  468. if (cmp_result != 0)
  469. return cmp_result;
  470. return PyObject_RichCompareBool(r0->step, r1->step, Py_EQ);
  471. }
  472. static PyObject *
  473. range_richcompare(PyObject *self, PyObject *other, int op)
  474. {
  475. int result;
  476. if (!PyRange_Check(other))
  477. Py_RETURN_NOTIMPLEMENTED;
  478. switch (op) {
  479. case Py_NE:
  480. case Py_EQ:
  481. result = range_equals((rangeobject*)self, (rangeobject*)other);
  482. if (result == -1)
  483. return NULL;
  484. if (op == Py_NE)
  485. result = !result;
  486. if (result)
  487. Py_RETURN_TRUE;
  488. else
  489. Py_RETURN_FALSE;
  490. case Py_LE:
  491. case Py_GE:
  492. case Py_LT:
  493. case Py_GT:
  494. Py_RETURN_NOTIMPLEMENTED;
  495. default:
  496. PyErr_BadArgument();
  497. return NULL;
  498. }
  499. }
  500. /* Hash function for range objects. Rough C equivalent of
  501. if not len(r):
  502. return hash((len(r), None, None))
  503. if len(r) == 1:
  504. return hash((len(r), r.start, None))
  505. return hash((len(r), r.start, r.step))
  506. */
  507. static Py_hash_t
  508. range_hash(rangeobject *r)
  509. {
  510. PyObject *t;
  511. Py_hash_t result = -1;
  512. int cmp_result;
  513. t = PyTuple_New(3);
  514. if (!t)
  515. return -1;
  516. PyTuple_SET_ITEM(t, 0, Py_NewRef(r->length));
  517. cmp_result = PyObject_Not(r->length);
  518. if (cmp_result == -1)
  519. goto end;
  520. if (cmp_result == 1) {
  521. PyTuple_SET_ITEM(t, 1, Py_NewRef(Py_None));
  522. PyTuple_SET_ITEM(t, 2, Py_NewRef(Py_None));
  523. }
  524. else {
  525. PyTuple_SET_ITEM(t, 1, Py_NewRef(r->start));
  526. cmp_result = PyObject_RichCompareBool(r->length, _PyLong_GetOne(), Py_EQ);
  527. if (cmp_result == -1)
  528. goto end;
  529. if (cmp_result == 1) {
  530. PyTuple_SET_ITEM(t, 2, Py_NewRef(Py_None));
  531. }
  532. else {
  533. PyTuple_SET_ITEM(t, 2, Py_NewRef(r->step));
  534. }
  535. }
  536. result = PyObject_Hash(t);
  537. end:
  538. Py_DECREF(t);
  539. return result;
  540. }
  541. static PyObject *
  542. range_count(rangeobject *r, PyObject *ob)
  543. {
  544. if (PyLong_CheckExact(ob) || PyBool_Check(ob)) {
  545. int result = range_contains_long(r, ob);
  546. if (result == -1)
  547. return NULL;
  548. return PyLong_FromLong(result);
  549. } else {
  550. Py_ssize_t count;
  551. count = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_COUNT);
  552. if (count == -1)
  553. return NULL;
  554. return PyLong_FromSsize_t(count);
  555. }
  556. }
  557. static PyObject *
  558. range_index(rangeobject *r, PyObject *ob)
  559. {
  560. int contains;
  561. if (!PyLong_CheckExact(ob) && !PyBool_Check(ob)) {
  562. Py_ssize_t index;
  563. index = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_INDEX);
  564. if (index == -1)
  565. return NULL;
  566. return PyLong_FromSsize_t(index);
  567. }
  568. contains = range_contains_long(r, ob);
  569. if (contains == -1)
  570. return NULL;
  571. if (contains) {
  572. PyObject *idx = PyNumber_Subtract(ob, r->start);
  573. if (idx == NULL) {
  574. return NULL;
  575. }
  576. if (r->step == _PyLong_GetOne()) {
  577. return idx;
  578. }
  579. /* idx = (ob - r.start) // r.step */
  580. PyObject *sidx = PyNumber_FloorDivide(idx, r->step);
  581. Py_DECREF(idx);
  582. return sidx;
  583. }
  584. /* object is not in the range */
  585. PyErr_Format(PyExc_ValueError, "%R is not in range", ob);
  586. return NULL;
  587. }
  588. static PySequenceMethods range_as_sequence = {
  589. (lenfunc)range_length, /* sq_length */
  590. 0, /* sq_concat */
  591. 0, /* sq_repeat */
  592. (ssizeargfunc)range_item, /* sq_item */
  593. 0, /* sq_slice */
  594. 0, /* sq_ass_item */
  595. 0, /* sq_ass_slice */
  596. (objobjproc)range_contains, /* sq_contains */
  597. };
  598. static PyObject *
  599. range_repr(rangeobject *r)
  600. {
  601. Py_ssize_t istep;
  602. /* Check for special case values for printing. We don't always
  603. need the step value. We don't care about overflow. */
  604. istep = PyNumber_AsSsize_t(r->step, NULL);
  605. if (istep == -1 && PyErr_Occurred()) {
  606. assert(!PyErr_ExceptionMatches(PyExc_OverflowError));
  607. return NULL;
  608. }
  609. if (istep == 1)
  610. return PyUnicode_FromFormat("range(%R, %R)", r->start, r->stop);
  611. else
  612. return PyUnicode_FromFormat("range(%R, %R, %R)",
  613. r->start, r->stop, r->step);
  614. }
  615. /* Pickling support */
  616. static PyObject *
  617. range_reduce(rangeobject *r, PyObject *args)
  618. {
  619. return Py_BuildValue("(O(OOO))", Py_TYPE(r),
  620. r->start, r->stop, r->step);
  621. }
  622. static PyObject *
  623. range_subscript(rangeobject* self, PyObject* item)
  624. {
  625. if (_PyIndex_Check(item)) {
  626. PyObject *i, *result;
  627. i = PyNumber_Index(item);
  628. if (!i)
  629. return NULL;
  630. result = compute_range_item(self, i);
  631. Py_DECREF(i);
  632. return result;
  633. }
  634. if (PySlice_Check(item)) {
  635. return compute_slice(self, item);
  636. }
  637. PyErr_Format(PyExc_TypeError,
  638. "range indices must be integers or slices, not %.200s",
  639. Py_TYPE(item)->tp_name);
  640. return NULL;
  641. }
  642. static PyMappingMethods range_as_mapping = {
  643. (lenfunc)range_length, /* mp_length */
  644. (binaryfunc)range_subscript, /* mp_subscript */
  645. (objobjargproc)0, /* mp_ass_subscript */
  646. };
  647. static int
  648. range_bool(rangeobject* self)
  649. {
  650. return PyObject_IsTrue(self->length);
  651. }
  652. static PyNumberMethods range_as_number = {
  653. .nb_bool = (inquiry)range_bool,
  654. };
  655. static PyObject * range_iter(PyObject *seq);
  656. static PyObject * range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored));
  657. PyDoc_STRVAR(reverse_doc,
  658. "Return a reverse iterator.");
  659. PyDoc_STRVAR(count_doc,
  660. "rangeobject.count(value) -> integer -- return number of occurrences of value");
  661. PyDoc_STRVAR(index_doc,
  662. "rangeobject.index(value) -> integer -- return index of value.\n"
  663. "Raise ValueError if the value is not present.");
  664. static PyMethodDef range_methods[] = {
  665. {"__reversed__", range_reverse, METH_NOARGS, reverse_doc},
  666. {"__reduce__", (PyCFunction)range_reduce, METH_VARARGS},
  667. {"count", (PyCFunction)range_count, METH_O, count_doc},
  668. {"index", (PyCFunction)range_index, METH_O, index_doc},
  669. {NULL, NULL} /* sentinel */
  670. };
  671. static PyMemberDef range_members[] = {
  672. {"start", T_OBJECT_EX, offsetof(rangeobject, start), READONLY},
  673. {"stop", T_OBJECT_EX, offsetof(rangeobject, stop), READONLY},
  674. {"step", T_OBJECT_EX, offsetof(rangeobject, step), READONLY},
  675. {0}
  676. };
  677. PyTypeObject PyRange_Type = {
  678. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  679. "range", /* Name of this type */
  680. sizeof(rangeobject), /* Basic object size */
  681. 0, /* Item size for varobject */
  682. (destructor)range_dealloc, /* tp_dealloc */
  683. 0, /* tp_vectorcall_offset */
  684. 0, /* tp_getattr */
  685. 0, /* tp_setattr */
  686. 0, /* tp_as_async */
  687. (reprfunc)range_repr, /* tp_repr */
  688. &range_as_number, /* tp_as_number */
  689. &range_as_sequence, /* tp_as_sequence */
  690. &range_as_mapping, /* tp_as_mapping */
  691. (hashfunc)range_hash, /* tp_hash */
  692. 0, /* tp_call */
  693. 0, /* tp_str */
  694. PyObject_GenericGetAttr, /* tp_getattro */
  695. 0, /* tp_setattro */
  696. 0, /* tp_as_buffer */
  697. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_SEQUENCE, /* tp_flags */
  698. range_doc, /* tp_doc */
  699. 0, /* tp_traverse */
  700. 0, /* tp_clear */
  701. range_richcompare, /* tp_richcompare */
  702. 0, /* tp_weaklistoffset */
  703. range_iter, /* tp_iter */
  704. 0, /* tp_iternext */
  705. range_methods, /* tp_methods */
  706. range_members, /* tp_members */
  707. 0, /* tp_getset */
  708. 0, /* tp_base */
  709. 0, /* tp_dict */
  710. 0, /* tp_descr_get */
  711. 0, /* tp_descr_set */
  712. 0, /* tp_dictoffset */
  713. 0, /* tp_init */
  714. 0, /* tp_alloc */
  715. range_new, /* tp_new */
  716. .tp_vectorcall = (vectorcallfunc)range_vectorcall
  717. };
  718. /*********************** range Iterator **************************/
  719. /* There are 2 types of iterators, one for C longs, the other for
  720. Python ints (ie, PyObjects). This should make iteration fast
  721. in the normal case, but possible for any numeric value.
  722. */
  723. static PyObject *
  724. rangeiter_next(_PyRangeIterObject *r)
  725. {
  726. if (r->len > 0) {
  727. long result = r->start;
  728. r->start = result + r->step;
  729. r->len--;
  730. return PyLong_FromLong(result);
  731. }
  732. return NULL;
  733. }
  734. static PyObject *
  735. rangeiter_len(_PyRangeIterObject *r, PyObject *Py_UNUSED(ignored))
  736. {
  737. return PyLong_FromLong(r->len);
  738. }
  739. PyDoc_STRVAR(length_hint_doc,
  740. "Private method returning an estimate of len(list(it)).");
  741. static PyObject *
  742. rangeiter_reduce(_PyRangeIterObject *r, PyObject *Py_UNUSED(ignored))
  743. {
  744. PyObject *start=NULL, *stop=NULL, *step=NULL;
  745. PyObject *range;
  746. /* create a range object for pickling */
  747. start = PyLong_FromLong(r->start);
  748. if (start == NULL)
  749. goto err;
  750. stop = PyLong_FromLong(r->start + r->len * r->step);
  751. if (stop == NULL)
  752. goto err;
  753. step = PyLong_FromLong(r->step);
  754. if (step == NULL)
  755. goto err;
  756. range = (PyObject*)make_range_object(&PyRange_Type,
  757. start, stop, step);
  758. if (range == NULL)
  759. goto err;
  760. /* return the result */
  761. return Py_BuildValue("N(N)O", _PyEval_GetBuiltin(&_Py_ID(iter)),
  762. range, Py_None);
  763. err:
  764. Py_XDECREF(start);
  765. Py_XDECREF(stop);
  766. Py_XDECREF(step);
  767. return NULL;
  768. }
  769. static PyObject *
  770. rangeiter_setstate(_PyRangeIterObject *r, PyObject *state)
  771. {
  772. long index = PyLong_AsLong(state);
  773. if (index == -1 && PyErr_Occurred())
  774. return NULL;
  775. /* silently clip the index value */
  776. if (index < 0)
  777. index = 0;
  778. else if (index > r->len)
  779. index = r->len; /* exhausted iterator */
  780. r->start += index * r->step;
  781. r->len -= index;
  782. Py_RETURN_NONE;
  783. }
  784. PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
  785. PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
  786. static PyMethodDef rangeiter_methods[] = {
  787. {"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS,
  788. length_hint_doc},
  789. {"__reduce__", (PyCFunction)rangeiter_reduce, METH_NOARGS,
  790. reduce_doc},
  791. {"__setstate__", (PyCFunction)rangeiter_setstate, METH_O,
  792. setstate_doc},
  793. {NULL, NULL} /* sentinel */
  794. };
  795. PyTypeObject PyRangeIter_Type = {
  796. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  797. "range_iterator", /* tp_name */
  798. sizeof(_PyRangeIterObject), /* tp_basicsize */
  799. 0, /* tp_itemsize */
  800. /* methods */
  801. (destructor)PyObject_Del, /* tp_dealloc */
  802. 0, /* tp_vectorcall_offset */
  803. 0, /* tp_getattr */
  804. 0, /* tp_setattr */
  805. 0, /* tp_as_async */
  806. 0, /* tp_repr */
  807. 0, /* tp_as_number */
  808. 0, /* tp_as_sequence */
  809. 0, /* tp_as_mapping */
  810. 0, /* tp_hash */
  811. 0, /* tp_call */
  812. 0, /* tp_str */
  813. PyObject_GenericGetAttr, /* tp_getattro */
  814. 0, /* tp_setattro */
  815. 0, /* tp_as_buffer */
  816. Py_TPFLAGS_DEFAULT, /* tp_flags */
  817. 0, /* tp_doc */
  818. 0, /* tp_traverse */
  819. 0, /* tp_clear */
  820. 0, /* tp_richcompare */
  821. 0, /* tp_weaklistoffset */
  822. PyObject_SelfIter, /* tp_iter */
  823. (iternextfunc)rangeiter_next, /* tp_iternext */
  824. rangeiter_methods, /* tp_methods */
  825. 0, /* tp_members */
  826. };
  827. /* Return number of items in range (lo, hi, step). step != 0
  828. * required. The result always fits in an unsigned long.
  829. */
  830. static unsigned long
  831. get_len_of_range(long lo, long hi, long step)
  832. {
  833. /* -------------------------------------------------------------
  834. If step > 0 and lo >= hi, or step < 0 and lo <= hi, the range is empty.
  835. Else for step > 0, if n values are in the range, the last one is
  836. lo + (n-1)*step, which must be <= hi-1. Rearranging,
  837. n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
  838. the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
  839. the RHS is non-negative and so truncation is the same as the
  840. floor. Letting M be the largest positive long, the worst case
  841. for the RHS numerator is hi=M, lo=-M-1, and then
  842. hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
  843. precision to compute the RHS exactly. The analysis for step < 0
  844. is similar.
  845. ---------------------------------------------------------------*/
  846. assert(step != 0);
  847. if (step > 0 && lo < hi)
  848. return 1UL + (hi - 1UL - lo) / step;
  849. else if (step < 0 && lo > hi)
  850. return 1UL + (lo - 1UL - hi) / (0UL - step);
  851. else
  852. return 0UL;
  853. }
  854. /* Initialize a rangeiter object. If the length of the rangeiter object
  855. is not representable as a C long, OverflowError is raised. */
  856. static PyObject *
  857. fast_range_iter(long start, long stop, long step, long len)
  858. {
  859. _PyRangeIterObject *it = PyObject_New(_PyRangeIterObject, &PyRangeIter_Type);
  860. if (it == NULL)
  861. return NULL;
  862. it->start = start;
  863. it->step = step;
  864. it->len = len;
  865. return (PyObject *)it;
  866. }
  867. typedef struct {
  868. PyObject_HEAD
  869. PyObject *start;
  870. PyObject *step;
  871. PyObject *len;
  872. } longrangeiterobject;
  873. static PyObject *
  874. longrangeiter_len(longrangeiterobject *r, PyObject *no_args)
  875. {
  876. Py_INCREF(r->len);
  877. return r->len;
  878. }
  879. static PyObject *
  880. longrangeiter_reduce(longrangeiterobject *r, PyObject *Py_UNUSED(ignored))
  881. {
  882. PyObject *product, *stop=NULL;
  883. PyObject *range;
  884. /* create a range object for pickling. Must calculate the "stop" value */
  885. product = PyNumber_Multiply(r->len, r->step);
  886. if (product == NULL)
  887. return NULL;
  888. stop = PyNumber_Add(r->start, product);
  889. Py_DECREF(product);
  890. if (stop == NULL)
  891. return NULL;
  892. range = (PyObject*)make_range_object(&PyRange_Type,
  893. Py_NewRef(r->start), stop, Py_NewRef(r->step));
  894. if (range == NULL) {
  895. Py_DECREF(r->start);
  896. Py_DECREF(stop);
  897. Py_DECREF(r->step);
  898. return NULL;
  899. }
  900. /* return the result */
  901. return Py_BuildValue("N(N)O", _PyEval_GetBuiltin(&_Py_ID(iter)),
  902. range, Py_None);
  903. }
  904. static PyObject *
  905. longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
  906. {
  907. PyObject *zero = _PyLong_GetZero(); // borrowed reference
  908. int cmp;
  909. /* clip the value */
  910. cmp = PyObject_RichCompareBool(state, zero, Py_LT);
  911. if (cmp < 0)
  912. return NULL;
  913. if (cmp > 0) {
  914. state = zero;
  915. }
  916. else {
  917. cmp = PyObject_RichCompareBool(r->len, state, Py_LT);
  918. if (cmp < 0)
  919. return NULL;
  920. if (cmp > 0)
  921. state = r->len;
  922. }
  923. PyObject *product = PyNumber_Multiply(state, r->step);
  924. if (product == NULL)
  925. return NULL;
  926. PyObject *new_start = PyNumber_Add(r->start, product);
  927. Py_DECREF(product);
  928. if (new_start == NULL)
  929. return NULL;
  930. PyObject *new_len = PyNumber_Subtract(r->len, state);
  931. if (new_len == NULL) {
  932. Py_DECREF(new_start);
  933. return NULL;
  934. }
  935. PyObject *tmp = r->start;
  936. r->start = new_start;
  937. Py_SETREF(r->len, new_len);
  938. Py_DECREF(tmp);
  939. Py_RETURN_NONE;
  940. }
  941. static PyMethodDef longrangeiter_methods[] = {
  942. {"__length_hint__", (PyCFunction)longrangeiter_len, METH_NOARGS,
  943. length_hint_doc},
  944. {"__reduce__", (PyCFunction)longrangeiter_reduce, METH_NOARGS,
  945. reduce_doc},
  946. {"__setstate__", (PyCFunction)longrangeiter_setstate, METH_O,
  947. setstate_doc},
  948. {NULL, NULL} /* sentinel */
  949. };
  950. static void
  951. longrangeiter_dealloc(longrangeiterobject *r)
  952. {
  953. Py_XDECREF(r->start);
  954. Py_XDECREF(r->step);
  955. Py_XDECREF(r->len);
  956. PyObject_Free(r);
  957. }
  958. static PyObject *
  959. longrangeiter_next(longrangeiterobject *r)
  960. {
  961. if (PyObject_RichCompareBool(r->len, _PyLong_GetZero(), Py_GT) != 1)
  962. return NULL;
  963. PyObject *new_start = PyNumber_Add(r->start, r->step);
  964. if (new_start == NULL) {
  965. return NULL;
  966. }
  967. PyObject *new_len = PyNumber_Subtract(r->len, _PyLong_GetOne());
  968. if (new_len == NULL) {
  969. Py_DECREF(new_start);
  970. return NULL;
  971. }
  972. PyObject *result = r->start;
  973. r->start = new_start;
  974. Py_SETREF(r->len, new_len);
  975. return result;
  976. }
  977. PyTypeObject PyLongRangeIter_Type = {
  978. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  979. "longrange_iterator", /* tp_name */
  980. sizeof(longrangeiterobject), /* tp_basicsize */
  981. 0, /* tp_itemsize */
  982. /* methods */
  983. (destructor)longrangeiter_dealloc, /* tp_dealloc */
  984. 0, /* tp_vectorcall_offset */
  985. 0, /* tp_getattr */
  986. 0, /* tp_setattr */
  987. 0, /* tp_as_async */
  988. 0, /* tp_repr */
  989. 0, /* tp_as_number */
  990. 0, /* tp_as_sequence */
  991. 0, /* tp_as_mapping */
  992. 0, /* tp_hash */
  993. 0, /* tp_call */
  994. 0, /* tp_str */
  995. PyObject_GenericGetAttr, /* tp_getattro */
  996. 0, /* tp_setattro */
  997. 0, /* tp_as_buffer */
  998. Py_TPFLAGS_DEFAULT, /* tp_flags */
  999. 0, /* tp_doc */
  1000. 0, /* tp_traverse */
  1001. 0, /* tp_clear */
  1002. 0, /* tp_richcompare */
  1003. 0, /* tp_weaklistoffset */
  1004. PyObject_SelfIter, /* tp_iter */
  1005. (iternextfunc)longrangeiter_next, /* tp_iternext */
  1006. longrangeiter_methods, /* tp_methods */
  1007. 0,
  1008. };
  1009. static PyObject *
  1010. range_iter(PyObject *seq)
  1011. {
  1012. rangeobject *r = (rangeobject *)seq;
  1013. longrangeiterobject *it;
  1014. long lstart, lstop, lstep;
  1015. unsigned long ulen;
  1016. assert(PyRange_Check(seq));
  1017. /* If all three fields and the length convert to long, use the int
  1018. * version */
  1019. lstart = PyLong_AsLong(r->start);
  1020. if (lstart == -1 && PyErr_Occurred()) {
  1021. PyErr_Clear();
  1022. goto long_range;
  1023. }
  1024. lstop = PyLong_AsLong(r->stop);
  1025. if (lstop == -1 && PyErr_Occurred()) {
  1026. PyErr_Clear();
  1027. goto long_range;
  1028. }
  1029. lstep = PyLong_AsLong(r->step);
  1030. if (lstep == -1 && PyErr_Occurred()) {
  1031. PyErr_Clear();
  1032. goto long_range;
  1033. }
  1034. ulen = get_len_of_range(lstart, lstop, lstep);
  1035. if (ulen > (unsigned long)LONG_MAX) {
  1036. goto long_range;
  1037. }
  1038. /* check for potential overflow of lstart + ulen * lstep */
  1039. if (ulen) {
  1040. if (lstep > 0) {
  1041. if (lstop > LONG_MAX - (lstep - 1))
  1042. goto long_range;
  1043. }
  1044. else {
  1045. if (lstop < LONG_MIN + (-1 - lstep))
  1046. goto long_range;
  1047. }
  1048. }
  1049. return fast_range_iter(lstart, lstop, lstep, (long)ulen);
  1050. long_range:
  1051. it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
  1052. if (it == NULL)
  1053. return NULL;
  1054. it->start = Py_NewRef(r->start);
  1055. it->step = Py_NewRef(r->step);
  1056. it->len = Py_NewRef(r->length);
  1057. return (PyObject *)it;
  1058. }
  1059. static PyObject *
  1060. range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored))
  1061. {
  1062. rangeobject *range = (rangeobject*) seq;
  1063. longrangeiterobject *it;
  1064. PyObject *sum, *diff, *product;
  1065. long lstart, lstop, lstep, new_start, new_stop;
  1066. unsigned long ulen;
  1067. assert(PyRange_Check(seq));
  1068. /* reversed(range(start, stop, step)) can be expressed as
  1069. range(start+(n-1)*step, start-step, -step), where n is the number of
  1070. integers in the range.
  1071. If each of start, stop, step, -step, start-step, and the length
  1072. of the iterator is representable as a C long, use the int
  1073. version. This excludes some cases where the reversed range is
  1074. representable as a range_iterator, but it's good enough for
  1075. common cases and it makes the checks simple. */
  1076. lstart = PyLong_AsLong(range->start);
  1077. if (lstart == -1 && PyErr_Occurred()) {
  1078. PyErr_Clear();
  1079. goto long_range;
  1080. }
  1081. lstop = PyLong_AsLong(range->stop);
  1082. if (lstop == -1 && PyErr_Occurred()) {
  1083. PyErr_Clear();
  1084. goto long_range;
  1085. }
  1086. lstep = PyLong_AsLong(range->step);
  1087. if (lstep == -1 && PyErr_Occurred()) {
  1088. PyErr_Clear();
  1089. goto long_range;
  1090. }
  1091. /* check for possible overflow of -lstep */
  1092. if (lstep == LONG_MIN)
  1093. goto long_range;
  1094. /* check for overflow of lstart - lstep:
  1095. for lstep > 0, need only check whether lstart - lstep < LONG_MIN.
  1096. for lstep < 0, need only check whether lstart - lstep > LONG_MAX
  1097. Rearrange these inequalities as:
  1098. lstart - LONG_MIN < lstep (lstep > 0)
  1099. LONG_MAX - lstart < -lstep (lstep < 0)
  1100. and compute both sides as unsigned longs, to avoid the
  1101. possibility of undefined behaviour due to signed overflow. */
  1102. if (lstep > 0) {
  1103. if ((unsigned long)lstart - LONG_MIN < (unsigned long)lstep)
  1104. goto long_range;
  1105. }
  1106. else {
  1107. if (LONG_MAX - (unsigned long)lstart < 0UL - lstep)
  1108. goto long_range;
  1109. }
  1110. ulen = get_len_of_range(lstart, lstop, lstep);
  1111. if (ulen > (unsigned long)LONG_MAX)
  1112. goto long_range;
  1113. new_stop = lstart - lstep;
  1114. new_start = (long)(new_stop + ulen * lstep);
  1115. return fast_range_iter(new_start, new_stop, -lstep, (long)ulen);
  1116. long_range:
  1117. it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
  1118. if (it == NULL)
  1119. return NULL;
  1120. it->start = it->step = NULL;
  1121. /* start + (len - 1) * step */
  1122. it->len = Py_NewRef(range->length);
  1123. diff = PyNumber_Subtract(it->len, _PyLong_GetOne());
  1124. if (!diff)
  1125. goto create_failure;
  1126. product = PyNumber_Multiply(diff, range->step);
  1127. Py_DECREF(diff);
  1128. if (!product)
  1129. goto create_failure;
  1130. sum = PyNumber_Add(range->start, product);
  1131. Py_DECREF(product);
  1132. it->start = sum;
  1133. if (!it->start)
  1134. goto create_failure;
  1135. it->step = PyNumber_Negative(range->step);
  1136. if (!it->step)
  1137. goto create_failure;
  1138. return (PyObject *)it;
  1139. create_failure:
  1140. Py_DECREF(it);
  1141. return NULL;
  1142. }