1993-10-26 14:58:25 -03:00
|
|
|
/* Range object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
2020-06-22 12:27:35 -03:00
|
|
|
#include "pycore_abstract.h" // _PyIndex_Check()
|
2020-10-26 22:24:34 -03:00
|
|
|
#include "pycore_long.h" // _PyLong_GetZero()
|
2023-07-03 06:39:11 -03:00
|
|
|
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
|
|
|
|
#include "pycore_range.h"
|
2020-06-22 12:27:35 -03:00
|
|
|
#include "pycore_tuple.h" // _PyTuple_ITEMS()
|
2023-07-25 10:28:30 -03:00
|
|
|
|
2001-07-09 09:30:54 -03:00
|
|
|
|
2007-05-07 19:24:25 -03:00
|
|
|
/* Support objects whose length is > PY_SSIZE_T_MAX.
|
|
|
|
|
2016-04-17 02:32:47 -03:00
|
|
|
This could be sped up for small PyLongs if they fit in a Py_ssize_t.
|
2016-09-06 14:46:49 -03:00
|
|
|
This only matters on Win64. Though we could use long long which
|
2007-05-07 19:24:25 -03:00
|
|
|
would presumably help perf.
|
|
|
|
*/
|
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
typedef struct {
|
2007-05-07 19:24:25 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *start;
|
|
|
|
PyObject *stop;
|
|
|
|
PyObject *step;
|
2010-12-03 10:26:13 -04:00
|
|
|
PyObject *length;
|
1993-10-26 14:58:25 -03:00
|
|
|
} rangeobject;
|
|
|
|
|
2007-05-07 19:24:25 -03:00
|
|
|
/* Helper function for validating step. Always returns a new reference or
|
2010-05-09 12:52:27 -03:00
|
|
|
NULL on error.
|
2007-05-07 19:24:25 -03:00
|
|
|
*/
|
|
|
|
static PyObject *
|
2010-11-20 19:05:39 -04:00
|
|
|
validate_step(PyObject *step)
|
|
|
|
{
|
2007-05-07 19:24:25 -03:00
|
|
|
/* No step specified, use a step of 1. */
|
|
|
|
if (!step)
|
2007-12-02 10:31:20 -04:00
|
|
|
return PyLong_FromLong(1);
|
2007-05-07 19:24:25 -03:00
|
|
|
|
|
|
|
step = PyNumber_Index(step);
|
2023-03-22 11:49:51 -03:00
|
|
|
if (step && _PyLong_IsZero((PyLongObject *)step)) {
|
2016-06-18 03:51:55 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"range() arg 3 must not be zero");
|
|
|
|
Py_CLEAR(step);
|
2007-05-07 19:24:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return step;
|
2002-06-05 20:12:45 -03:00
|
|
|
}
|
|
|
|
|
2010-12-03 10:26:13 -04:00
|
|
|
static PyObject *
|
|
|
|
compute_range_length(PyObject *start, PyObject *stop, PyObject *step);
|
|
|
|
|
|
|
|
static rangeobject *
|
|
|
|
make_range_object(PyTypeObject *type, PyObject *start,
|
|
|
|
PyObject *stop, PyObject *step)
|
|
|
|
{
|
|
|
|
rangeobject *obj = NULL;
|
|
|
|
PyObject *length;
|
|
|
|
length = compute_range_length(start, stop, step);
|
|
|
|
if (length == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
obj = PyObject_New(rangeobject, type);
|
|
|
|
if (obj == NULL) {
|
|
|
|
Py_DECREF(length);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
obj->start = start;
|
|
|
|
obj->stop = stop;
|
|
|
|
obj->step = step;
|
|
|
|
obj->length = length;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2007-05-07 19:24:25 -03:00
|
|
|
/* XXX(nnorwitz): should we error check if the user passes any empty ranges?
|
|
|
|
range(-10)
|
|
|
|
range(0, -5)
|
|
|
|
range(0, 5, -1)
|
|
|
|
*/
|
2002-06-05 20:12:45 -03:00
|
|
|
static PyObject *
|
2020-02-18 11:13:17 -04:00
|
|
|
range_from_array(PyTypeObject *type, PyObject *const *args, Py_ssize_t num_args)
|
2010-11-20 19:05:39 -04:00
|
|
|
{
|
2010-12-03 10:26:13 -04:00
|
|
|
rangeobject *obj;
|
2007-05-07 19:24:25 -03:00
|
|
|
PyObject *start = NULL, *stop = NULL, *step = NULL;
|
|
|
|
|
2020-01-05 13:30:53 -04:00
|
|
|
switch (num_args) {
|
|
|
|
case 3:
|
2020-02-18 11:13:17 -04:00
|
|
|
step = args[2];
|
2020-01-05 13:30:53 -04:00
|
|
|
/* fallthrough */
|
|
|
|
case 2:
|
2020-02-18 11:13:17 -04:00
|
|
|
/* Convert borrowed refs to owned refs */
|
|
|
|
start = PyNumber_Index(args[0]);
|
2020-01-05 13:30:53 -04:00
|
|
|
if (!start) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-02-18 11:13:17 -04:00
|
|
|
stop = PyNumber_Index(args[1]);
|
2020-01-05 13:30:53 -04:00
|
|
|
if (!stop) {
|
|
|
|
Py_DECREF(start);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-02-18 11:13:17 -04:00
|
|
|
step = validate_step(step); /* Caution, this can clear exceptions */
|
2020-01-05 13:30:53 -04:00
|
|
|
if (!step) {
|
|
|
|
Py_DECREF(start);
|
|
|
|
Py_DECREF(stop);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
2020-02-18 11:13:17 -04:00
|
|
|
stop = PyNumber_Index(args[0]);
|
2020-01-05 13:30:53 -04:00
|
|
|
if (!stop) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-08-04 20:24:50 -03:00
|
|
|
start = _PyLong_GetZero();
|
|
|
|
step = _PyLong_GetOne();
|
2020-01-05 13:30:53 -04:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"range expected at least 1 argument, got 0");
|
2009-06-12 15:40:16 -03:00
|
|
|
return NULL;
|
2020-01-05 13:30:53 -04:00
|
|
|
default:
|
2020-02-06 22:04:21 -04:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
2020-01-05 13:30:53 -04:00
|
|
|
"range expected at most 3 arguments, got %zd",
|
|
|
|
num_args);
|
2009-06-12 15:40:16 -03:00
|
|
|
return NULL;
|
2007-05-07 19:24:25 -03:00
|
|
|
}
|
2010-12-03 10:26:13 -04:00
|
|
|
obj = make_range_object(type, start, stop, step);
|
2020-02-18 11:13:17 -04:00
|
|
|
if (obj != NULL) {
|
2010-12-03 10:26:13 -04:00
|
|
|
return (PyObject *) obj;
|
2020-02-18 11:13:17 -04:00
|
|
|
}
|
2007-05-07 19:24:25 -03:00
|
|
|
|
2010-12-03 10:26:13 -04:00
|
|
|
/* Failed to create object, release attributes */
|
2016-06-18 03:44:03 -03:00
|
|
|
Py_DECREF(start);
|
|
|
|
Py_DECREF(stop);
|
|
|
|
Py_DECREF(step);
|
2007-05-07 19:24:25 -03:00
|
|
|
return NULL;
|
2002-06-05 20:12:45 -03:00
|
|
|
}
|
|
|
|
|
2020-02-18 11:13:17 -04:00
|
|
|
static PyObject *
|
|
|
|
range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
|
|
|
{
|
|
|
|
if (!_PyArg_NoKeywords("range", kw))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return range_from_array(type, _PyTuple_ITEMS(args), PyTuple_GET_SIZE(args));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
range_vectorcall(PyTypeObject *type, PyObject *const *args,
|
|
|
|
size_t nargsf, PyObject *kwnames)
|
|
|
|
{
|
|
|
|
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
|
2020-03-16 11:06:20 -03:00
|
|
|
if (!_PyArg_NoKwnames("range", kwnames)) {
|
2020-02-18 11:13:17 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return range_from_array(type, args, nargs);
|
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(range_doc,
|
2012-10-07 18:48:36 -03:00
|
|
|
"range(stop) -> range object\n\
|
|
|
|
range(start, stop[, step]) -> range object\n\
|
2002-06-05 20:12:45 -03:00
|
|
|
\n\
|
2015-04-22 10:16:07 -03:00
|
|
|
Return an object that produces a sequence of integers from start (inclusive)\n\
|
|
|
|
to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.\n\
|
|
|
|
start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.\n\
|
|
|
|
These are exactly the valid indices for a list of 4 elements.\n\
|
|
|
|
When step is given, it specifies the increment (or decrement).");
|
2002-06-05 20:12:45 -03:00
|
|
|
|
2007-05-07 19:24:25 -03:00
|
|
|
static void
|
2010-11-20 19:05:39 -04:00
|
|
|
range_dealloc(rangeobject *r)
|
|
|
|
{
|
2007-05-07 19:24:25 -03:00
|
|
|
Py_DECREF(r->start);
|
|
|
|
Py_DECREF(r->stop);
|
|
|
|
Py_DECREF(r->step);
|
2010-12-03 10:26:13 -04:00
|
|
|
Py_DECREF(r->length);
|
2020-12-01 05:37:39 -04:00
|
|
|
PyObject_Free(r);
|
2007-05-07 19:24:25 -03:00
|
|
|
}
|
|
|
|
|
2023-01-21 15:33:08 -04:00
|
|
|
static unsigned long
|
|
|
|
get_len_of_range(long lo, long hi, long step);
|
|
|
|
|
|
|
|
/* Return the length as a long, -2 for an overflow and -1 for any other type of error
|
|
|
|
*
|
|
|
|
* In case of an overflow no error is set
|
|
|
|
*/
|
|
|
|
static long compute_range_length_long(PyObject *start,
|
|
|
|
PyObject *stop, PyObject *step) {
|
|
|
|
int overflow = 0;
|
|
|
|
|
|
|
|
long long_start = PyLong_AsLongAndOverflow(start, &overflow);
|
|
|
|
if (overflow) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if (long_start == -1 && PyErr_Occurred()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
long long_stop = PyLong_AsLongAndOverflow(stop, &overflow);
|
|
|
|
if (overflow) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if (long_stop == -1 && PyErr_Occurred()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
long long_step = PyLong_AsLongAndOverflow(step, &overflow);
|
|
|
|
if (overflow) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if (long_step == -1 && PyErr_Occurred()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long ulen = get_len_of_range(long_start, long_stop, long_step);
|
|
|
|
if (ulen > (unsigned long)LONG_MAX) {
|
|
|
|
/* length too large for a long */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (long)ulen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-28 19:08:40 -03:00
|
|
|
/* Return number of items in range (lo, hi, step) as a PyLong object,
|
|
|
|
* when arguments are PyLong objects. Arguments MUST return 1 with
|
|
|
|
* PyLong_Check(). Return NULL when there is an error.
|
2007-05-07 19:24:25 -03:00
|
|
|
*/
|
|
|
|
static PyObject*
|
2010-12-03 10:26:13 -04:00
|
|
|
compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
|
2010-11-20 19:05:39 -04:00
|
|
|
{
|
2007-05-07 19:24:25 -03:00
|
|
|
/* -------------------------------------------------------------
|
|
|
|
Algorithm is equal to that of get_len_of_range(), but it operates
|
2009-06-24 18:14:38 -03:00
|
|
|
on PyObjects (which are assumed to be PyLong objects).
|
2007-05-07 19:24:25 -03:00
|
|
|
---------------------------------------------------------------*/
|
2009-02-01 06:28:51 -04:00
|
|
|
int cmp_result;
|
2007-05-07 19:24:25 -03:00
|
|
|
PyObject *lo, *hi;
|
|
|
|
PyObject *diff = NULL;
|
|
|
|
PyObject *tmp1 = NULL, *tmp2 = NULL, *result;
|
|
|
|
/* holds sub-expression evaluations */
|
|
|
|
|
2020-10-26 22:24:34 -03:00
|
|
|
PyObject *zero = _PyLong_GetZero(); // borrowed reference
|
|
|
|
PyObject *one = _PyLong_GetOne(); // borrowed reference
|
|
|
|
|
2023-01-21 15:33:08 -04:00
|
|
|
assert(PyLong_Check(start));
|
|
|
|
assert(PyLong_Check(stop));
|
|
|
|
assert(PyLong_Check(step));
|
|
|
|
|
|
|
|
/* fast path when all arguments fit into a long integer */
|
|
|
|
long len = compute_range_length_long(start, stop, step);
|
|
|
|
if (len >= 0) {
|
|
|
|
return PyLong_FromLong(len);
|
|
|
|
}
|
|
|
|
else if (len == -1) {
|
|
|
|
/* unexpected error from compute_range_length_long, we propagate to the caller */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
assert(len == -2);
|
|
|
|
|
2020-10-26 22:24:34 -03:00
|
|
|
cmp_result = PyObject_RichCompareBool(step, zero, Py_GT);
|
2009-02-01 06:28:51 -04:00
|
|
|
if (cmp_result == -1)
|
2007-05-07 19:24:25 -03:00
|
|
|
return NULL;
|
|
|
|
|
2009-02-01 06:28:51 -04:00
|
|
|
if (cmp_result == 1) {
|
2010-12-03 10:26:13 -04:00
|
|
|
lo = start;
|
|
|
|
hi = stop;
|
2007-05-07 19:24:25 -03:00
|
|
|
Py_INCREF(step);
|
|
|
|
} else {
|
2010-12-03 10:26:13 -04:00
|
|
|
lo = stop;
|
|
|
|
hi = start;
|
|
|
|
step = PyNumber_Negative(step);
|
2007-05-07 19:24:25 -03:00
|
|
|
if (!step)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if (lo >= hi), return length of 0. */
|
2015-05-30 11:45:22 -03:00
|
|
|
cmp_result = PyObject_RichCompareBool(lo, hi, Py_GE);
|
|
|
|
if (cmp_result != 0) {
|
2016-06-18 03:44:03 -03:00
|
|
|
Py_DECREF(step);
|
2015-05-30 11:45:22 -03:00
|
|
|
if (cmp_result < 0)
|
|
|
|
return NULL;
|
2020-10-26 22:24:34 -03:00
|
|
|
result = zero;
|
2022-11-10 18:40:31 -04:00
|
|
|
return Py_NewRef(result);
|
2007-05-07 19:24:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
|
|
|
|
goto Fail;
|
|
|
|
|
2020-10-26 22:24:34 -03:00
|
|
|
if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
|
2007-05-07 19:24:25 -03:00
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
|
|
|
|
goto Fail;
|
|
|
|
|
2020-10-26 22:24:34 -03:00
|
|
|
if ((result = PyNumber_Add(tmp2, one)) == NULL)
|
2007-05-07 19:24:25 -03:00
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
Py_DECREF(tmp2);
|
|
|
|
Py_DECREF(diff);
|
|
|
|
Py_DECREF(step);
|
|
|
|
Py_DECREF(tmp1);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
Fail:
|
2016-06-18 03:44:03 -03:00
|
|
|
Py_DECREF(step);
|
2007-05-07 19:24:25 -03:00
|
|
|
Py_XDECREF(tmp2);
|
|
|
|
Py_XDECREF(diff);
|
|
|
|
Py_XDECREF(tmp1);
|
|
|
|
return NULL;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2010-11-20 19:05:39 -04:00
|
|
|
range_length(rangeobject *r)
|
|
|
|
{
|
2010-12-03 10:26:13 -04:00
|
|
|
return PyLong_AsSsize_t(r->length);
|
2007-05-07 19:24:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2011-01-11 23:15:52 -04:00
|
|
|
compute_item(rangeobject *r, PyObject *i)
|
2010-11-20 19:05:39 -04:00
|
|
|
{
|
2011-01-11 23:15:52 -04:00
|
|
|
PyObject *incr, *result;
|
|
|
|
/* PyLong equivalent to:
|
|
|
|
* return r->start + (i * r->step)
|
|
|
|
*/
|
2020-10-26 22:24:34 -03:00
|
|
|
if (r->step == _PyLong_GetOne()) {
|
2020-10-20 22:29:14 -03:00
|
|
|
result = PyNumber_Add(r->start, i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
incr = PyNumber_Multiply(i, r->step);
|
|
|
|
if (!incr) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
result = PyNumber_Add(r->start, incr);
|
|
|
|
Py_DECREF(incr);
|
|
|
|
}
|
2007-05-07 19:24:25 -03:00
|
|
|
return result;
|
1993-12-21 18:50:31 -04:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2011-01-11 23:15:52 -04:00
|
|
|
compute_range_item(rangeobject *r, PyObject *arg)
|
2010-11-20 19:05:39 -04:00
|
|
|
{
|
2020-10-26 22:24:34 -03:00
|
|
|
PyObject *zero = _PyLong_GetZero(); // borrowed reference
|
2011-01-11 23:15:52 -04:00
|
|
|
int cmp_result;
|
|
|
|
PyObject *i, *result;
|
2007-05-07 19:24:25 -03:00
|
|
|
|
2011-01-11 23:15:52 -04:00
|
|
|
/* PyLong equivalent to:
|
|
|
|
* if (arg < 0) {
|
|
|
|
* i = r->length + arg
|
|
|
|
* } else {
|
|
|
|
* i = arg
|
|
|
|
* }
|
|
|
|
*/
|
2020-10-26 22:24:34 -03:00
|
|
|
cmp_result = PyObject_RichCompareBool(arg, zero, Py_LT);
|
2011-01-11 23:15:52 -04:00
|
|
|
if (cmp_result == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (cmp_result == 1) {
|
2017-03-30 03:09:41 -03:00
|
|
|
i = PyNumber_Add(r->length, arg);
|
|
|
|
if (!i) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-11 23:15:52 -04:00
|
|
|
} else {
|
2022-11-10 18:40:31 -04:00
|
|
|
i = Py_NewRef(arg);
|
2011-01-11 23:15:52 -04:00
|
|
|
}
|
2001-07-09 09:30:54 -03:00
|
|
|
|
2011-01-11 23:15:52 -04:00
|
|
|
/* PyLong equivalent to:
|
|
|
|
* if (i < 0 || i >= r->length) {
|
|
|
|
* <report index out of bounds>
|
|
|
|
* }
|
|
|
|
*/
|
2020-10-26 22:24:34 -03:00
|
|
|
cmp_result = PyObject_RichCompareBool(i, zero, Py_LT);
|
2011-01-11 23:15:52 -04:00
|
|
|
if (cmp_result == 0) {
|
|
|
|
cmp_result = PyObject_RichCompareBool(i, r->length, Py_GE);
|
|
|
|
}
|
|
|
|
if (cmp_result == -1) {
|
|
|
|
Py_DECREF(i);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (cmp_result == 1) {
|
|
|
|
Py_DECREF(i);
|
|
|
|
PyErr_SetString(PyExc_IndexError,
|
|
|
|
"range object index out of range");
|
2010-12-03 10:26:13 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-11 23:15:52 -04:00
|
|
|
|
|
|
|
result = compute_item(r, i);
|
|
|
|
Py_DECREF(i);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
range_item(rangeobject *r, Py_ssize_t i)
|
|
|
|
{
|
2012-05-16 09:37:54 -03:00
|
|
|
PyObject *res, *arg = PyLong_FromSsize_t(i);
|
2011-01-11 23:15:52 -04:00
|
|
|
if (!arg) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-13 00:22:54 -04:00
|
|
|
res = compute_range_item(r, arg);
|
|
|
|
Py_DECREF(arg);
|
|
|
|
return res;
|
2011-01-11 23:15:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
compute_slice(rangeobject *r, PyObject *_slice)
|
|
|
|
{
|
|
|
|
PySliceObject *slice = (PySliceObject *) _slice;
|
|
|
|
rangeobject *result;
|
|
|
|
PyObject *start = NULL, *stop = NULL, *step = NULL;
|
|
|
|
PyObject *substart = NULL, *substop = NULL, *substep = NULL;
|
2012-11-17 15:18:10 -04:00
|
|
|
int error;
|
2011-01-11 23:15:52 -04:00
|
|
|
|
2012-11-17 15:18:10 -04:00
|
|
|
error = _PySlice_GetLongIndices(slice, r->length, &start, &stop, &step);
|
|
|
|
if (error == -1)
|
|
|
|
return NULL;
|
2011-01-11 23:15:52 -04:00
|
|
|
|
|
|
|
substep = PyNumber_Multiply(r->step, step);
|
|
|
|
if (substep == NULL) goto fail;
|
|
|
|
Py_CLEAR(step);
|
|
|
|
|
|
|
|
substart = compute_item(r, start);
|
|
|
|
if (substart == NULL) goto fail;
|
|
|
|
Py_CLEAR(start);
|
|
|
|
|
2012-11-17 15:18:10 -04:00
|
|
|
substop = compute_item(r, stop);
|
|
|
|
if (substop == NULL) goto fail;
|
2011-01-11 23:15:52 -04:00
|
|
|
Py_CLEAR(stop);
|
|
|
|
|
|
|
|
result = make_range_object(Py_TYPE(r), substart, substop, substep);
|
|
|
|
if (result != NULL) {
|
|
|
|
return (PyObject *) result;
|
|
|
|
}
|
|
|
|
fail:
|
|
|
|
Py_XDECREF(start);
|
|
|
|
Py_XDECREF(stop);
|
|
|
|
Py_XDECREF(step);
|
|
|
|
Py_XDECREF(substart);
|
|
|
|
Py_XDECREF(substop);
|
|
|
|
Py_XDECREF(substep);
|
|
|
|
return NULL;
|
2008-06-10 01:03:04 -03:00
|
|
|
}
|
|
|
|
|
2010-09-13 18:16:29 -03:00
|
|
|
/* Assumes (PyLong_CheckExact(ob) || PyBool_Check(ob)) */
|
|
|
|
static int
|
2010-11-20 19:05:39 -04:00
|
|
|
range_contains_long(rangeobject *r, PyObject *ob)
|
|
|
|
{
|
2020-10-26 22:24:34 -03:00
|
|
|
PyObject *zero = _PyLong_GetZero(); // borrowed reference
|
2010-09-13 18:16:29 -03:00
|
|
|
int cmp1, cmp2, cmp3;
|
|
|
|
PyObject *tmp1 = NULL;
|
|
|
|
PyObject *tmp2 = NULL;
|
|
|
|
int result = -1;
|
|
|
|
|
|
|
|
/* Check if the value can possibly be in the range. */
|
|
|
|
|
2020-10-26 22:24:34 -03:00
|
|
|
cmp1 = PyObject_RichCompareBool(r->step, zero, Py_GT);
|
2010-09-13 18:16:29 -03:00
|
|
|
if (cmp1 == -1)
|
|
|
|
goto end;
|
|
|
|
if (cmp1 == 1) { /* positive steps: start <= ob < stop */
|
|
|
|
cmp2 = PyObject_RichCompareBool(r->start, ob, Py_LE);
|
|
|
|
cmp3 = PyObject_RichCompareBool(ob, r->stop, Py_LT);
|
|
|
|
}
|
|
|
|
else { /* negative steps: stop < ob <= start */
|
|
|
|
cmp2 = PyObject_RichCompareBool(ob, r->start, Py_LE);
|
|
|
|
cmp3 = PyObject_RichCompareBool(r->stop, ob, Py_LT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp2 == -1 || cmp3 == -1) /* TypeError */
|
|
|
|
goto end;
|
|
|
|
if (cmp2 == 0 || cmp3 == 0) { /* ob outside of range */
|
|
|
|
result = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the stride does not invalidate ob's membership. */
|
|
|
|
tmp1 = PyNumber_Subtract(ob, r->start);
|
|
|
|
if (tmp1 == NULL)
|
|
|
|
goto end;
|
|
|
|
tmp2 = PyNumber_Remainder(tmp1, r->step);
|
|
|
|
if (tmp2 == NULL)
|
|
|
|
goto end;
|
2016-09-12 01:47:04 -03:00
|
|
|
/* result = ((int(ob) - start) % step) == 0 */
|
2020-10-26 22:24:34 -03:00
|
|
|
result = PyObject_RichCompareBool(tmp2, zero, Py_EQ);
|
2010-09-13 18:16:29 -03:00
|
|
|
end:
|
|
|
|
Py_XDECREF(tmp1);
|
|
|
|
Py_XDECREF(tmp2);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-09-22 18:47:24 -03:00
|
|
|
static int
|
2010-11-20 19:05:39 -04:00
|
|
|
range_contains(rangeobject *r, PyObject *ob)
|
|
|
|
{
|
2010-09-13 18:16:29 -03:00
|
|
|
if (PyLong_CheckExact(ob) || PyBool_Check(ob))
|
|
|
|
return range_contains_long(r, ob);
|
|
|
|
|
|
|
|
return (int)_PySequence_IterSearch((PyObject*)r, ob,
|
|
|
|
PY_ITERSEARCH_CONTAINS);
|
|
|
|
}
|
|
|
|
|
2011-10-23 15:53:01 -03:00
|
|
|
/* Compare two range objects. Return 1 for equal, 0 for not equal
|
|
|
|
and -1 on error. The algorithm is roughly the C equivalent of
|
|
|
|
|
|
|
|
if r0 is r1:
|
|
|
|
return True
|
|
|
|
if len(r0) != len(r1):
|
|
|
|
return False
|
|
|
|
if not len(r0):
|
|
|
|
return True
|
|
|
|
if r0.start != r1.start:
|
|
|
|
return False
|
|
|
|
if len(r0) == 1:
|
|
|
|
return True
|
|
|
|
return r0.step == r1.step
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
range_equals(rangeobject *r0, rangeobject *r1)
|
|
|
|
{
|
|
|
|
int cmp_result;
|
|
|
|
|
|
|
|
if (r0 == r1)
|
|
|
|
return 1;
|
|
|
|
cmp_result = PyObject_RichCompareBool(r0->length, r1->length, Py_EQ);
|
|
|
|
/* Return False or error to the caller. */
|
|
|
|
if (cmp_result != 1)
|
|
|
|
return cmp_result;
|
|
|
|
cmp_result = PyObject_Not(r0->length);
|
|
|
|
/* Return True or error to the caller. */
|
|
|
|
if (cmp_result != 0)
|
|
|
|
return cmp_result;
|
|
|
|
cmp_result = PyObject_RichCompareBool(r0->start, r1->start, Py_EQ);
|
|
|
|
/* Return False or error to the caller. */
|
|
|
|
if (cmp_result != 1)
|
|
|
|
return cmp_result;
|
2020-10-26 22:24:34 -03:00
|
|
|
cmp_result = PyObject_RichCompareBool(r0->length, _PyLong_GetOne(), Py_EQ);
|
2011-10-23 15:53:01 -03:00
|
|
|
/* Return True or error to the caller. */
|
|
|
|
if (cmp_result != 0)
|
|
|
|
return cmp_result;
|
|
|
|
return PyObject_RichCompareBool(r0->step, r1->step, Py_EQ);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
range_richcompare(PyObject *self, PyObject *other, int op)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (!PyRange_Check(other))
|
|
|
|
Py_RETURN_NOTIMPLEMENTED;
|
|
|
|
switch (op) {
|
|
|
|
case Py_NE:
|
|
|
|
case Py_EQ:
|
|
|
|
result = range_equals((rangeobject*)self, (rangeobject*)other);
|
|
|
|
if (result == -1)
|
|
|
|
return NULL;
|
|
|
|
if (op == Py_NE)
|
|
|
|
result = !result;
|
|
|
|
if (result)
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
else
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
case Py_LE:
|
|
|
|
case Py_GE:
|
|
|
|
case Py_LT:
|
|
|
|
case Py_GT:
|
|
|
|
Py_RETURN_NOTIMPLEMENTED;
|
|
|
|
default:
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hash function for range objects. Rough C equivalent of
|
|
|
|
|
|
|
|
if not len(r):
|
|
|
|
return hash((len(r), None, None))
|
|
|
|
if len(r) == 1:
|
|
|
|
return hash((len(r), r.start, None))
|
|
|
|
return hash((len(r), r.start, r.step))
|
|
|
|
*/
|
|
|
|
static Py_hash_t
|
|
|
|
range_hash(rangeobject *r)
|
|
|
|
{
|
|
|
|
PyObject *t;
|
|
|
|
Py_hash_t result = -1;
|
|
|
|
int cmp_result;
|
|
|
|
|
|
|
|
t = PyTuple_New(3);
|
|
|
|
if (!t)
|
|
|
|
return -1;
|
2022-11-10 18:40:31 -04:00
|
|
|
PyTuple_SET_ITEM(t, 0, Py_NewRef(r->length));
|
2011-10-23 15:53:01 -03:00
|
|
|
cmp_result = PyObject_Not(r->length);
|
|
|
|
if (cmp_result == -1)
|
|
|
|
goto end;
|
|
|
|
if (cmp_result == 1) {
|
2022-11-10 18:40:31 -04:00
|
|
|
PyTuple_SET_ITEM(t, 1, Py_NewRef(Py_None));
|
|
|
|
PyTuple_SET_ITEM(t, 2, Py_NewRef(Py_None));
|
2011-10-23 15:53:01 -03:00
|
|
|
}
|
|
|
|
else {
|
2022-11-10 18:40:31 -04:00
|
|
|
PyTuple_SET_ITEM(t, 1, Py_NewRef(r->start));
|
2020-10-26 22:24:34 -03:00
|
|
|
cmp_result = PyObject_RichCompareBool(r->length, _PyLong_GetOne(), Py_EQ);
|
2011-10-23 15:53:01 -03:00
|
|
|
if (cmp_result == -1)
|
|
|
|
goto end;
|
|
|
|
if (cmp_result == 1) {
|
2022-11-10 18:40:31 -04:00
|
|
|
PyTuple_SET_ITEM(t, 2, Py_NewRef(Py_None));
|
2011-10-23 15:53:01 -03:00
|
|
|
}
|
|
|
|
else {
|
2022-11-10 18:40:31 -04:00
|
|
|
PyTuple_SET_ITEM(t, 2, Py_NewRef(r->step));
|
2011-10-23 15:53:01 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result = PyObject_Hash(t);
|
|
|
|
end:
|
|
|
|
Py_DECREF(t);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-09-13 18:16:29 -03:00
|
|
|
static PyObject *
|
2010-11-20 19:05:39 -04:00
|
|
|
range_count(rangeobject *r, PyObject *ob)
|
|
|
|
{
|
2009-09-24 17:04:23 -03:00
|
|
|
if (PyLong_CheckExact(ob) || PyBool_Check(ob)) {
|
2010-11-20 18:40:10 -04:00
|
|
|
int result = range_contains_long(r, ob);
|
|
|
|
if (result == -1)
|
|
|
|
return NULL;
|
2017-03-30 03:09:41 -03:00
|
|
|
return PyLong_FromLong(result);
|
2010-09-13 18:16:29 -03:00
|
|
|
} else {
|
|
|
|
Py_ssize_t count;
|
|
|
|
count = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_COUNT);
|
|
|
|
if (count == -1)
|
|
|
|
return NULL;
|
|
|
|
return PyLong_FromSsize_t(count);
|
|
|
|
}
|
|
|
|
}
|
2009-09-22 18:47:24 -03:00
|
|
|
|
2010-09-13 18:16:29 -03:00
|
|
|
static PyObject *
|
2010-11-20 19:05:39 -04:00
|
|
|
range_index(rangeobject *r, PyObject *ob)
|
|
|
|
{
|
2010-09-13 18:16:29 -03:00
|
|
|
int contains;
|
|
|
|
|
|
|
|
if (!PyLong_CheckExact(ob) && !PyBool_Check(ob)) {
|
|
|
|
Py_ssize_t index;
|
|
|
|
index = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_INDEX);
|
|
|
|
if (index == -1)
|
|
|
|
return NULL;
|
|
|
|
return PyLong_FromSsize_t(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
contains = range_contains_long(r, ob);
|
|
|
|
if (contains == -1)
|
|
|
|
return NULL;
|
|
|
|
|
2010-11-20 18:44:32 -04:00
|
|
|
if (contains) {
|
2020-10-20 23:29:56 -03:00
|
|
|
PyObject *idx = PyNumber_Subtract(ob, r->start);
|
|
|
|
if (idx == NULL) {
|
2010-09-13 18:16:29 -03:00
|
|
|
return NULL;
|
2020-10-20 23:29:56 -03:00
|
|
|
}
|
|
|
|
|
2020-10-26 22:24:34 -03:00
|
|
|
if (r->step == _PyLong_GetOne()) {
|
2020-10-20 23:29:56 -03:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2010-11-20 18:44:32 -04:00
|
|
|
/* idx = (ob - r.start) // r.step */
|
2020-10-20 23:29:56 -03:00
|
|
|
PyObject *sidx = PyNumber_FloorDivide(idx, r->step);
|
|
|
|
Py_DECREF(idx);
|
|
|
|
return sidx;
|
2009-09-22 18:47:24 -03:00
|
|
|
}
|
2010-11-20 18:44:32 -04:00
|
|
|
|
|
|
|
/* object is not in the range */
|
|
|
|
PyErr_Format(PyExc_ValueError, "%R is not in range", ob);
|
2010-09-13 18:16:29 -03:00
|
|
|
return NULL;
|
2009-09-22 18:47:24 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PySequenceMethods range_as_sequence = {
|
2010-05-09 12:52:27 -03:00
|
|
|
(lenfunc)range_length, /* sq_length */
|
2010-12-03 10:26:13 -04:00
|
|
|
0, /* sq_concat */
|
|
|
|
0, /* sq_repeat */
|
|
|
|
(ssizeargfunc)range_item, /* sq_item */
|
|
|
|
0, /* sq_slice */
|
|
|
|
0, /* sq_ass_item */
|
|
|
|
0, /* sq_ass_slice */
|
2009-09-22 18:47:24 -03:00
|
|
|
(objobjproc)range_contains, /* sq_contains */
|
1993-10-26 14:58:25 -03:00
|
|
|
};
|
|
|
|
|
2010-12-03 10:26:13 -04:00
|
|
|
static PyObject *
|
|
|
|
range_repr(rangeobject *r)
|
|
|
|
{
|
|
|
|
Py_ssize_t istep;
|
|
|
|
|
|
|
|
/* Check for special case values for printing. We don't always
|
2018-08-24 01:39:45 -03:00
|
|
|
need the step value. We don't care about overflow. */
|
2010-12-03 10:26:13 -04:00
|
|
|
istep = PyNumber_AsSsize_t(r->step, NULL);
|
2018-08-24 01:39:45 -03:00
|
|
|
if (istep == -1 && PyErr_Occurred()) {
|
|
|
|
assert(!PyErr_ExceptionMatches(PyExc_OverflowError));
|
|
|
|
return NULL;
|
2010-12-03 10:26:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (istep == 1)
|
|
|
|
return PyUnicode_FromFormat("range(%R, %R)", r->start, r->stop);
|
|
|
|
else
|
|
|
|
return PyUnicode_FromFormat("range(%R, %R, %R)",
|
|
|
|
r->start, r->stop, r->step);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pickling support */
|
|
|
|
static PyObject *
|
|
|
|
range_reduce(rangeobject *r, PyObject *args)
|
|
|
|
{
|
|
|
|
return Py_BuildValue("(O(OOO))", Py_TYPE(r),
|
|
|
|
r->start, r->stop, r->step);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
range_subscript(rangeobject* self, PyObject* item)
|
|
|
|
{
|
2020-04-07 21:01:56 -03:00
|
|
|
if (_PyIndex_Check(item)) {
|
2011-01-11 23:15:52 -04:00
|
|
|
PyObject *i, *result;
|
|
|
|
i = PyNumber_Index(item);
|
|
|
|
if (!i)
|
2010-12-03 10:26:13 -04:00
|
|
|
return NULL;
|
2011-01-11 23:15:52 -04:00
|
|
|
result = compute_range_item(self, i);
|
|
|
|
Py_DECREF(i);
|
|
|
|
return result;
|
2010-12-03 10:26:13 -04:00
|
|
|
}
|
|
|
|
if (PySlice_Check(item)) {
|
2011-01-11 23:15:52 -04:00
|
|
|
return compute_slice(self, item);
|
2010-12-03 10:26:13 -04:00
|
|
|
}
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"range indices must be integers or slices, not %.200s",
|
2020-02-06 22:04:21 -04:00
|
|
|
Py_TYPE(item)->tp_name);
|
2010-12-03 10:26:13 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyMappingMethods range_as_mapping = {
|
|
|
|
(lenfunc)range_length, /* mp_length */
|
|
|
|
(binaryfunc)range_subscript, /* mp_subscript */
|
|
|
|
(objobjargproc)0, /* mp_ass_subscript */
|
|
|
|
};
|
|
|
|
|
2017-03-20 03:44:46 -03:00
|
|
|
static int
|
|
|
|
range_bool(rangeobject* self)
|
|
|
|
{
|
|
|
|
return PyObject_IsTrue(self->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyNumberMethods range_as_number = {
|
|
|
|
.nb_bool = (inquiry)range_bool,
|
|
|
|
};
|
|
|
|
|
2002-07-17 13:30:39 -03:00
|
|
|
static PyObject * range_iter(PyObject *seq);
|
2018-04-29 15:59:33 -03:00
|
|
|
static PyObject * range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored));
|
2003-11-06 10:06:48 -04:00
|
|
|
|
|
|
|
PyDoc_STRVAR(reverse_doc,
|
2013-10-05 18:36:45 -03:00
|
|
|
"Return a reverse iterator.");
|
2003-11-06 10:06:48 -04:00
|
|
|
|
2010-09-13 18:16:29 -03:00
|
|
|
PyDoc_STRVAR(count_doc,
|
|
|
|
"rangeobject.count(value) -> integer -- return number of occurrences of value");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(index_doc,
|
2019-05-03 09:22:12 -03:00
|
|
|
"rangeobject.index(value) -> integer -- return index of value.\n"
|
2013-10-05 18:36:45 -03:00
|
|
|
"Raise ValueError if the value is not present.");
|
2010-09-13 18:16:29 -03:00
|
|
|
|
2003-11-06 10:06:48 -04:00
|
|
|
static PyMethodDef range_methods[] = {
|
2018-04-29 15:59:33 -03:00
|
|
|
{"__reversed__", range_reverse, METH_NOARGS, reverse_doc},
|
2010-09-13 18:16:29 -03:00
|
|
|
{"__reduce__", (PyCFunction)range_reduce, METH_VARARGS},
|
|
|
|
{"count", (PyCFunction)range_count, METH_O, count_doc},
|
|
|
|
{"index", (PyCFunction)range_index, METH_O, index_doc},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
2003-11-06 10:06:48 -04:00
|
|
|
};
|
2002-06-05 17:08:48 -03:00
|
|
|
|
2011-11-05 16:17:52 -03:00
|
|
|
static PyMemberDef range_members[] = {
|
2023-07-25 10:28:30 -03:00
|
|
|
{"start", Py_T_OBJECT_EX, offsetof(rangeobject, start), Py_READONLY},
|
|
|
|
{"stop", Py_T_OBJECT_EX, offsetof(rangeobject, stop), Py_READONLY},
|
|
|
|
{"step", Py_T_OBJECT_EX, offsetof(rangeobject, step), Py_READONLY},
|
2011-11-05 16:17:52 -03:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyRange_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"range", /* Name of this type */
|
|
|
|
sizeof(rangeobject), /* Basic object size */
|
|
|
|
0, /* Item size for varobject */
|
|
|
|
(destructor)range_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
(reprfunc)range_repr, /* tp_repr */
|
2017-03-20 03:44:46 -03:00
|
|
|
&range_as_number, /* tp_as_number */
|
2010-05-09 12:52:27 -03:00
|
|
|
&range_as_sequence, /* tp_as_sequence */
|
2010-12-03 10:26:13 -04:00
|
|
|
&range_as_mapping, /* tp_as_mapping */
|
2011-10-23 15:53:01 -03:00
|
|
|
(hashfunc)range_hash, /* tp_hash */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2021-04-30 05:50:28 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_SEQUENCE, /* tp_flags */
|
2010-05-09 12:52:27 -03:00
|
|
|
range_doc, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
2011-10-23 15:53:01 -03:00
|
|
|
range_richcompare, /* tp_richcompare */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
range_iter, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
range_methods, /* tp_methods */
|
2011-11-05 16:17:52 -03:00
|
|
|
range_members, /* tp_members */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
range_new, /* tp_new */
|
2020-02-18 11:13:17 -04:00
|
|
|
.tp_vectorcall = (vectorcallfunc)range_vectorcall
|
2002-06-05 17:08:48 -03:00
|
|
|
};
|
|
|
|
|
2007-05-21 15:01:17 -03:00
|
|
|
/*********************** range Iterator **************************/
|
2002-06-05 17:08:48 -03:00
|
|
|
|
2007-05-07 19:24:25 -03:00
|
|
|
/* There are 2 types of iterators, one for C longs, the other for
|
2013-08-27 13:40:23 -03:00
|
|
|
Python ints (ie, PyObjects). This should make iteration fast
|
2007-05-07 19:24:25 -03:00
|
|
|
in the normal case, but possible for any numeric value.
|
|
|
|
*/
|
|
|
|
|
2002-06-05 17:08:48 -03:00
|
|
|
static PyObject *
|
2022-06-21 07:19:26 -03:00
|
|
|
rangeiter_next(_PyRangeIterObject *r)
|
2010-11-20 19:05:39 -04:00
|
|
|
{
|
2022-11-30 17:04:30 -04:00
|
|
|
if (r->len > 0) {
|
|
|
|
long result = r->start;
|
|
|
|
r->start = result + r->step;
|
|
|
|
r->len--;
|
|
|
|
return PyLong_FromLong(result);
|
|
|
|
}
|
2007-05-07 19:24:25 -03:00
|
|
|
return NULL;
|
2002-06-05 17:08:48 -03:00
|
|
|
}
|
|
|
|
|
2005-09-24 18:23:05 -03:00
|
|
|
static PyObject *
|
2022-06-21 07:19:26 -03:00
|
|
|
rangeiter_len(_PyRangeIterObject *r, PyObject *Py_UNUSED(ignored))
|
2010-11-20 19:05:39 -04:00
|
|
|
{
|
2022-11-30 17:04:30 -04:00
|
|
|
return PyLong_FromLong(r->len);
|
2004-03-10 06:10:42 -04:00
|
|
|
}
|
|
|
|
|
2007-05-07 19:24:25 -03:00
|
|
|
PyDoc_STRVAR(length_hint_doc,
|
|
|
|
"Private method returning an estimate of len(list(it)).");
|
2004-03-10 06:10:42 -04:00
|
|
|
|
2012-04-03 07:49:41 -03:00
|
|
|
static PyObject *
|
2022-06-21 07:19:26 -03:00
|
|
|
rangeiter_reduce(_PyRangeIterObject *r, PyObject *Py_UNUSED(ignored))
|
2012-04-03 07:49:41 -03:00
|
|
|
{
|
|
|
|
PyObject *start=NULL, *stop=NULL, *step=NULL;
|
|
|
|
PyObject *range;
|
2012-10-07 18:56:27 -03:00
|
|
|
|
2012-04-03 07:49:41 -03:00
|
|
|
/* create a range object for pickling */
|
|
|
|
start = PyLong_FromLong(r->start);
|
|
|
|
if (start == NULL)
|
|
|
|
goto err;
|
|
|
|
stop = PyLong_FromLong(r->start + r->len * r->step);
|
|
|
|
if (stop == NULL)
|
|
|
|
goto err;
|
|
|
|
step = PyLong_FromLong(r->step);
|
|
|
|
if (step == NULL)
|
|
|
|
goto err;
|
|
|
|
range = (PyObject*)make_range_object(&PyRange_Type,
|
|
|
|
start, stop, step);
|
|
|
|
if (range == NULL)
|
|
|
|
goto err;
|
|
|
|
/* return the result */
|
2022-11-30 17:04:30 -04:00
|
|
|
return Py_BuildValue("N(N)O", _PyEval_GetBuiltin(&_Py_ID(iter)),
|
|
|
|
range, Py_None);
|
2012-04-03 07:49:41 -03:00
|
|
|
err:
|
|
|
|
Py_XDECREF(start);
|
|
|
|
Py_XDECREF(stop);
|
|
|
|
Py_XDECREF(step);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2022-06-21 07:19:26 -03:00
|
|
|
rangeiter_setstate(_PyRangeIterObject *r, PyObject *state)
|
2012-04-03 07:49:41 -03:00
|
|
|
{
|
|
|
|
long index = PyLong_AsLong(state);
|
|
|
|
if (index == -1 && PyErr_Occurred())
|
|
|
|
return NULL;
|
2014-03-05 09:47:57 -04:00
|
|
|
/* silently clip the index value */
|
|
|
|
if (index < 0)
|
|
|
|
index = 0;
|
|
|
|
else if (index > r->len)
|
|
|
|
index = r->len; /* exhausted iterator */
|
2022-11-30 17:04:30 -04:00
|
|
|
r->start += index * r->step;
|
|
|
|
r->len -= index;
|
2012-04-03 07:49:41 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
|
|
|
|
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
|
|
|
|
|
2005-09-24 18:23:05 -03:00
|
|
|
static PyMethodDef rangeiter_methods[] = {
|
2007-05-07 19:24:25 -03:00
|
|
|
{"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS,
|
2010-05-09 12:52:27 -03:00
|
|
|
length_hint_doc},
|
2012-04-03 07:49:41 -03:00
|
|
|
{"__reduce__", (PyCFunction)rangeiter_reduce, METH_NOARGS,
|
|
|
|
reduce_doc},
|
|
|
|
{"__setstate__", (PyCFunction)rangeiter_setstate, METH_O,
|
|
|
|
setstate_doc},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
2005-09-24 18:23:05 -03:00
|
|
|
};
|
2004-03-10 06:10:42 -04:00
|
|
|
|
2007-11-29 18:35:39 -04:00
|
|
|
PyTypeObject PyRangeIter_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2022-06-21 07:19:26 -03:00
|
|
|
"range_iterator", /* tp_name */
|
|
|
|
sizeof(_PyRangeIterObject), /* tp_basicsize */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
|
|
|
(destructor)PyObject_Del, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
|
|
|
(iternextfunc)rangeiter_next, /* tp_iternext */
|
|
|
|
rangeiter_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
2007-05-07 19:24:25 -03:00
|
|
|
};
|
|
|
|
|
2009-11-15 05:57:26 -04:00
|
|
|
/* Return number of items in range (lo, hi, step). step != 0
|
|
|
|
* required. The result always fits in an unsigned long.
|
2007-05-07 19:24:25 -03:00
|
|
|
*/
|
2009-11-15 05:57:26 -04:00
|
|
|
static unsigned long
|
2010-11-20 19:05:39 -04:00
|
|
|
get_len_of_range(long lo, long hi, long step)
|
|
|
|
{
|
2007-05-07 19:24:25 -03:00
|
|
|
/* -------------------------------------------------------------
|
2009-11-15 05:57:26 -04:00
|
|
|
If step > 0 and lo >= hi, or step < 0 and lo <= hi, the range is empty.
|
|
|
|
Else for step > 0, if n values are in the range, the last one is
|
2007-05-07 19:24:25 -03:00
|
|
|
lo + (n-1)*step, which must be <= hi-1. Rearranging,
|
|
|
|
n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
|
|
|
|
the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
|
|
|
|
the RHS is non-negative and so truncation is the same as the
|
|
|
|
floor. Letting M be the largest positive long, the worst case
|
|
|
|
for the RHS numerator is hi=M, lo=-M-1, and then
|
|
|
|
hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
|
2009-11-15 05:57:26 -04:00
|
|
|
precision to compute the RHS exactly. The analysis for step < 0
|
|
|
|
is similar.
|
2007-05-07 19:24:25 -03:00
|
|
|
---------------------------------------------------------------*/
|
2009-11-15 05:57:26 -04:00
|
|
|
assert(step != 0);
|
|
|
|
if (step > 0 && lo < hi)
|
|
|
|
return 1UL + (hi - 1UL - lo) / step;
|
|
|
|
else if (step < 0 && lo > hi)
|
|
|
|
return 1UL + (lo - 1UL - hi) / (0UL - step);
|
|
|
|
else
|
|
|
|
return 0UL;
|
2007-05-07 19:24:25 -03:00
|
|
|
}
|
|
|
|
|
2009-11-15 05:57:26 -04:00
|
|
|
/* Initialize a rangeiter object. If the length of the rangeiter object
|
|
|
|
is not representable as a C long, OverflowError is raised. */
|
|
|
|
|
2007-05-07 19:24:25 -03:00
|
|
|
static PyObject *
|
2021-09-04 15:02:21 -03:00
|
|
|
fast_range_iter(long start, long stop, long step, long len)
|
2010-11-20 19:05:39 -04:00
|
|
|
{
|
2022-06-21 07:19:26 -03:00
|
|
|
_PyRangeIterObject *it = PyObject_New(_PyRangeIterObject, &PyRangeIter_Type);
|
2007-05-07 19:24:25 -03:00
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
|
|
|
it->start = start;
|
|
|
|
it->step = step;
|
2021-09-04 15:02:21 -03:00
|
|
|
it->len = len;
|
2007-05-07 19:24:25 -03:00
|
|
|
return (PyObject *)it;
|
|
|
|
}
|
|
|
|
|
2010-12-03 10:26:13 -04:00
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *start;
|
|
|
|
PyObject *step;
|
|
|
|
PyObject *len;
|
|
|
|
} longrangeiterobject;
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
longrangeiter_len(longrangeiterobject *r, PyObject *no_args)
|
|
|
|
{
|
2022-11-30 17:04:30 -04:00
|
|
|
Py_INCREF(r->len);
|
|
|
|
return r->len;
|
2007-05-07 19:24:25 -03:00
|
|
|
}
|
|
|
|
|
2012-04-03 07:49:41 -03:00
|
|
|
static PyObject *
|
2018-04-29 15:59:33 -03:00
|
|
|
longrangeiter_reduce(longrangeiterobject *r, PyObject *Py_UNUSED(ignored))
|
2012-04-03 07:49:41 -03:00
|
|
|
{
|
|
|
|
PyObject *product, *stop=NULL;
|
|
|
|
PyObject *range;
|
|
|
|
|
|
|
|
/* create a range object for pickling. Must calculate the "stop" value */
|
|
|
|
product = PyNumber_Multiply(r->len, r->step);
|
|
|
|
if (product == NULL)
|
|
|
|
return NULL;
|
|
|
|
stop = PyNumber_Add(r->start, product);
|
|
|
|
Py_DECREF(product);
|
|
|
|
if (stop == NULL)
|
|
|
|
return NULL;
|
|
|
|
range = (PyObject*)make_range_object(&PyRange_Type,
|
2022-11-16 13:34:24 -04:00
|
|
|
Py_NewRef(r->start), stop, Py_NewRef(r->step));
|
2012-04-03 07:49:41 -03:00
|
|
|
if (range == NULL) {
|
|
|
|
Py_DECREF(r->start);
|
|
|
|
Py_DECREF(stop);
|
|
|
|
Py_DECREF(r->step);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the result */
|
2022-11-30 17:04:30 -04:00
|
|
|
return Py_BuildValue("N(N)O", _PyEval_GetBuiltin(&_Py_ID(iter)),
|
|
|
|
range, Py_None);
|
2012-04-03 07:49:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
|
|
|
|
{
|
2020-10-26 22:24:34 -03:00
|
|
|
PyObject *zero = _PyLong_GetZero(); // borrowed reference
|
2014-03-05 09:47:57 -04:00
|
|
|
int cmp;
|
2015-03-18 16:53:15 -03:00
|
|
|
|
2014-03-05 09:47:57 -04:00
|
|
|
/* clip the value */
|
2020-10-26 22:24:34 -03:00
|
|
|
cmp = PyObject_RichCompareBool(state, zero, Py_LT);
|
2017-03-30 03:09:41 -03:00
|
|
|
if (cmp < 0)
|
2014-03-05 09:47:57 -04:00
|
|
|
return NULL;
|
|
|
|
if (cmp > 0) {
|
2020-10-26 22:24:34 -03:00
|
|
|
state = zero;
|
2017-03-30 03:09:41 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
cmp = PyObject_RichCompareBool(r->len, state, Py_LT);
|
|
|
|
if (cmp < 0)
|
|
|
|
return NULL;
|
|
|
|
if (cmp > 0)
|
|
|
|
state = r->len;
|
2014-03-05 09:47:57 -04:00
|
|
|
}
|
2022-11-30 17:04:30 -04:00
|
|
|
PyObject *product = PyNumber_Multiply(state, r->step);
|
|
|
|
if (product == NULL)
|
|
|
|
return NULL;
|
|
|
|
PyObject *new_start = PyNumber_Add(r->start, product);
|
|
|
|
Py_DECREF(product);
|
|
|
|
if (new_start == NULL)
|
|
|
|
return NULL;
|
|
|
|
PyObject *new_len = PyNumber_Subtract(r->len, state);
|
|
|
|
if (new_len == NULL) {
|
|
|
|
Py_DECREF(new_start);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyObject *tmp = r->start;
|
|
|
|
r->start = new_start;
|
|
|
|
Py_SETREF(r->len, new_len);
|
|
|
|
Py_DECREF(tmp);
|
2012-04-03 07:49:41 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2007-05-07 19:24:25 -03:00
|
|
|
static PyMethodDef longrangeiter_methods[] = {
|
|
|
|
{"__length_hint__", (PyCFunction)longrangeiter_len, METH_NOARGS,
|
2010-05-09 12:52:27 -03:00
|
|
|
length_hint_doc},
|
2012-04-03 07:49:41 -03:00
|
|
|
{"__reduce__", (PyCFunction)longrangeiter_reduce, METH_NOARGS,
|
|
|
|
reduce_doc},
|
|
|
|
{"__setstate__", (PyCFunction)longrangeiter_setstate, METH_O,
|
|
|
|
setstate_doc},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
2007-05-07 19:24:25 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2010-11-20 19:05:39 -04:00
|
|
|
longrangeiter_dealloc(longrangeiterobject *r)
|
|
|
|
{
|
2007-05-08 12:18:31 -03:00
|
|
|
Py_XDECREF(r->start);
|
|
|
|
Py_XDECREF(r->step);
|
|
|
|
Py_XDECREF(r->len);
|
2020-12-01 05:37:39 -04:00
|
|
|
PyObject_Free(r);
|
2007-05-07 19:24:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2010-11-20 19:05:39 -04:00
|
|
|
longrangeiter_next(longrangeiterobject *r)
|
|
|
|
{
|
2022-11-30 17:04:30 -04:00
|
|
|
if (PyObject_RichCompareBool(r->len, _PyLong_GetZero(), Py_GT) != 1)
|
2007-05-07 19:24:25 -03:00
|
|
|
return NULL;
|
|
|
|
|
2022-11-30 17:04:30 -04:00
|
|
|
PyObject *new_start = PyNumber_Add(r->start, r->step);
|
|
|
|
if (new_start == NULL) {
|
2009-11-15 20:34:25 -04:00
|
|
|
return NULL;
|
2007-05-07 19:24:25 -03:00
|
|
|
}
|
2022-11-30 17:04:30 -04:00
|
|
|
PyObject *new_len = PyNumber_Subtract(r->len, _PyLong_GetOne());
|
|
|
|
if (new_len == NULL) {
|
|
|
|
Py_DECREF(new_start);
|
|
|
|
return NULL;
|
2009-11-15 20:34:25 -04:00
|
|
|
}
|
2022-11-30 17:04:30 -04:00
|
|
|
PyObject *result = r->start;
|
|
|
|
r->start = new_start;
|
|
|
|
Py_SETREF(r->len, new_len);
|
2007-05-07 19:24:25 -03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-11-29 18:35:39 -04:00
|
|
|
PyTypeObject PyLongRangeIter_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"longrange_iterator", /* tp_name */
|
|
|
|
sizeof(longrangeiterobject), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
|
|
|
(destructor)longrangeiter_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
|
|
|
(iternextfunc)longrangeiter_next, /* tp_iternext */
|
|
|
|
longrangeiter_methods, /* tp_methods */
|
|
|
|
0,
|
2002-06-05 17:08:48 -03:00
|
|
|
};
|
2006-04-21 07:40:58 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2010-11-20 19:05:39 -04:00
|
|
|
range_iter(PyObject *seq)
|
|
|
|
{
|
2007-05-07 19:24:25 -03:00
|
|
|
rangeobject *r = (rangeobject *)seq;
|
|
|
|
longrangeiterobject *it;
|
2007-12-20 18:57:23 -04:00
|
|
|
long lstart, lstop, lstep;
|
2021-09-04 15:02:21 -03:00
|
|
|
unsigned long ulen;
|
2007-05-07 19:24:25 -03:00
|
|
|
|
|
|
|
assert(PyRange_Check(seq));
|
2007-12-20 18:57:23 -04:00
|
|
|
|
2009-11-15 05:57:26 -04:00
|
|
|
/* If all three fields and the length convert to long, use the int
|
|
|
|
* version */
|
2007-12-20 18:57:23 -04:00
|
|
|
lstart = PyLong_AsLong(r->start);
|
2009-11-15 05:57:26 -04:00
|
|
|
if (lstart == -1 && PyErr_Occurred()) {
|
|
|
|
PyErr_Clear();
|
|
|
|
goto long_range;
|
|
|
|
}
|
|
|
|
lstop = PyLong_AsLong(r->stop);
|
|
|
|
if (lstop == -1 && PyErr_Occurred()) {
|
|
|
|
PyErr_Clear();
|
|
|
|
goto long_range;
|
|
|
|
}
|
|
|
|
lstep = PyLong_AsLong(r->step);
|
|
|
|
if (lstep == -1 && PyErr_Occurred()) {
|
|
|
|
PyErr_Clear();
|
|
|
|
goto long_range;
|
2007-12-20 18:57:23 -04:00
|
|
|
}
|
2021-09-04 15:02:21 -03:00
|
|
|
ulen = get_len_of_range(lstart, lstop, lstep);
|
|
|
|
if (ulen > (unsigned long)LONG_MAX) {
|
2009-11-15 05:57:26 -04:00
|
|
|
goto long_range;
|
|
|
|
}
|
2021-09-04 15:02:21 -03:00
|
|
|
/* check for potential overflow of lstart + ulen * lstep */
|
|
|
|
if (ulen) {
|
|
|
|
if (lstep > 0) {
|
|
|
|
if (lstop > LONG_MAX - (lstep - 1))
|
|
|
|
goto long_range;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (lstop < LONG_MIN + (-1 - lstep))
|
|
|
|
goto long_range;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fast_range_iter(lstart, lstop, lstep, (long)ulen);
|
2007-05-07 19:24:25 -03:00
|
|
|
|
2009-11-15 05:57:26 -04:00
|
|
|
long_range:
|
2007-11-29 18:35:39 -04:00
|
|
|
it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
|
2007-05-07 19:24:25 -03:00
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
2007-05-08 12:18:31 -03:00
|
|
|
|
2022-11-10 18:40:31 -04:00
|
|
|
it->start = Py_NewRef(r->start);
|
|
|
|
it->step = Py_NewRef(r->step);
|
|
|
|
it->len = Py_NewRef(r->length);
|
2007-05-07 19:24:25 -03:00
|
|
|
return (PyObject *)it;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2018-04-29 15:59:33 -03:00
|
|
|
range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored))
|
2010-11-20 19:05:39 -04:00
|
|
|
{
|
2007-05-07 19:24:25 -03:00
|
|
|
rangeobject *range = (rangeobject*) seq;
|
|
|
|
longrangeiterobject *it;
|
2017-03-30 03:09:41 -03:00
|
|
|
PyObject *sum, *diff, *product;
|
2009-11-15 05:57:26 -04:00
|
|
|
long lstart, lstop, lstep, new_start, new_stop;
|
|
|
|
unsigned long ulen;
|
2007-05-07 19:24:25 -03:00
|
|
|
|
|
|
|
assert(PyRange_Check(seq));
|
2007-12-20 18:57:23 -04:00
|
|
|
|
2009-11-15 05:57:26 -04:00
|
|
|
/* reversed(range(start, stop, step)) can be expressed as
|
|
|
|
range(start+(n-1)*step, start-step, -step), where n is the number of
|
|
|
|
integers in the range.
|
|
|
|
|
|
|
|
If each of start, stop, step, -step, start-step, and the length
|
|
|
|
of the iterator is representable as a C long, use the int
|
|
|
|
version. This excludes some cases where the reversed range is
|
|
|
|
representable as a range_iterator, but it's good enough for
|
|
|
|
common cases and it makes the checks simple. */
|
|
|
|
|
2007-12-20 18:57:23 -04:00
|
|
|
lstart = PyLong_AsLong(range->start);
|
2009-11-15 05:57:26 -04:00
|
|
|
if (lstart == -1 && PyErr_Occurred()) {
|
|
|
|
PyErr_Clear();
|
|
|
|
goto long_range;
|
2007-05-07 19:24:25 -03:00
|
|
|
}
|
2009-11-15 05:57:26 -04:00
|
|
|
lstop = PyLong_AsLong(range->stop);
|
|
|
|
if (lstop == -1 && PyErr_Occurred()) {
|
|
|
|
PyErr_Clear();
|
|
|
|
goto long_range;
|
|
|
|
}
|
|
|
|
lstep = PyLong_AsLong(range->step);
|
|
|
|
if (lstep == -1 && PyErr_Occurred()) {
|
|
|
|
PyErr_Clear();
|
|
|
|
goto long_range;
|
|
|
|
}
|
|
|
|
/* check for possible overflow of -lstep */
|
|
|
|
if (lstep == LONG_MIN)
|
|
|
|
goto long_range;
|
|
|
|
|
|
|
|
/* check for overflow of lstart - lstep:
|
|
|
|
|
|
|
|
for lstep > 0, need only check whether lstart - lstep < LONG_MIN.
|
|
|
|
for lstep < 0, need only check whether lstart - lstep > LONG_MAX
|
2007-05-07 19:24:25 -03:00
|
|
|
|
2009-11-15 05:57:26 -04:00
|
|
|
Rearrange these inequalities as:
|
|
|
|
|
|
|
|
lstart - LONG_MIN < lstep (lstep > 0)
|
|
|
|
LONG_MAX - lstart < -lstep (lstep < 0)
|
|
|
|
|
|
|
|
and compute both sides as unsigned longs, to avoid the
|
|
|
|
possibility of undefined behaviour due to signed overflow. */
|
|
|
|
|
|
|
|
if (lstep > 0) {
|
|
|
|
if ((unsigned long)lstart - LONG_MIN < (unsigned long)lstep)
|
|
|
|
goto long_range;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (LONG_MAX - (unsigned long)lstart < 0UL - lstep)
|
|
|
|
goto long_range;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulen = get_len_of_range(lstart, lstop, lstep);
|
|
|
|
if (ulen > (unsigned long)LONG_MAX)
|
|
|
|
goto long_range;
|
|
|
|
|
|
|
|
new_stop = lstart - lstep;
|
|
|
|
new_start = (long)(new_stop + ulen * lstep);
|
2021-09-04 15:02:21 -03:00
|
|
|
return fast_range_iter(new_start, new_stop, -lstep, (long)ulen);
|
2009-11-15 05:57:26 -04:00
|
|
|
|
|
|
|
long_range:
|
2007-11-29 18:35:39 -04:00
|
|
|
it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
|
2007-05-07 19:24:25 -03:00
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
2022-11-30 17:04:30 -04:00
|
|
|
it->start = it->step = NULL;
|
2007-05-07 19:24:25 -03:00
|
|
|
|
|
|
|
/* start + (len - 1) * step */
|
2022-11-10 18:40:31 -04:00
|
|
|
it->len = Py_NewRef(range->length);
|
2009-11-15 20:34:25 -04:00
|
|
|
|
2020-10-26 22:24:34 -03:00
|
|
|
diff = PyNumber_Subtract(it->len, _PyLong_GetOne());
|
2007-05-07 19:24:25 -03:00
|
|
|
if (!diff)
|
|
|
|
goto create_failure;
|
|
|
|
|
2009-11-15 05:57:26 -04:00
|
|
|
product = PyNumber_Multiply(diff, range->step);
|
|
|
|
Py_DECREF(diff);
|
2007-05-07 19:24:25 -03:00
|
|
|
if (!product)
|
|
|
|
goto create_failure;
|
|
|
|
|
|
|
|
sum = PyNumber_Add(range->start, product);
|
|
|
|
Py_DECREF(product);
|
|
|
|
it->start = sum;
|
|
|
|
if (!it->start)
|
|
|
|
goto create_failure;
|
2009-11-15 05:57:26 -04:00
|
|
|
|
2007-05-07 19:24:25 -03:00
|
|
|
it->step = PyNumber_Negative(range->step);
|
2009-11-15 20:34:25 -04:00
|
|
|
if (!it->step)
|
2009-11-15 05:57:26 -04:00
|
|
|
goto create_failure;
|
2007-05-07 19:24:25 -03:00
|
|
|
|
|
|
|
return (PyObject *)it;
|
|
|
|
|
|
|
|
create_failure:
|
2009-11-15 20:34:25 -04:00
|
|
|
Py_DECREF(it);
|
2007-05-07 19:24:25 -03:00
|
|
|
return NULL;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|