1993-10-27 01:58:25 +08:00
|
|
|
/* Range object implementation */
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
#include "Python.h"
|
2001-07-09 20:30:54 +08:00
|
|
|
|
2007-05-08 06:24:25 +08:00
|
|
|
/* Support objects whose length is > PY_SSIZE_T_MAX.
|
|
|
|
|
|
|
|
This could be sped up for small PyLongs if they fit in an Py_ssize_t.
|
|
|
|
This only matters on Win64. Though we could use PY_LONG_LONG which
|
|
|
|
would presumably help perf.
|
|
|
|
*/
|
|
|
|
|
1993-10-27 01:58:25 +08:00
|
|
|
typedef struct {
|
2007-05-08 06:24:25 +08:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *start;
|
|
|
|
PyObject *stop;
|
|
|
|
PyObject *step;
|
1993-10-27 01:58:25 +08:00
|
|
|
} rangeobject;
|
|
|
|
|
2007-05-08 06:24:25 +08:00
|
|
|
/* Helper function for validating step. Always returns a new reference or
|
|
|
|
NULL on error.
|
|
|
|
*/
|
|
|
|
static PyObject *
|
|
|
|
validate_step(PyObject *step)
|
2002-06-06 07:12:45 +08:00
|
|
|
{
|
2007-05-08 06:24:25 +08:00
|
|
|
/* No step specified, use a step of 1. */
|
|
|
|
if (!step)
|
2007-12-02 22:31:20 +08:00
|
|
|
return PyLong_FromLong(1);
|
2007-05-08 06:24:25 +08:00
|
|
|
|
|
|
|
step = PyNumber_Index(step);
|
|
|
|
if (step) {
|
|
|
|
Py_ssize_t istep = PyNumber_AsSsize_t(step, NULL);
|
|
|
|
if (istep == -1 && PyErr_Occurred()) {
|
|
|
|
/* Ignore OverflowError, we know the value isn't 0. */
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
else if (istep == 0) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"range() arg 3 must not be zero");
|
|
|
|
Py_CLEAR(step);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return step;
|
2002-06-06 07:12:45 +08:00
|
|
|
}
|
|
|
|
|
2007-05-08 06:24:25 +08: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-06 07:12:45 +08:00
|
|
|
static PyObject *
|
|
|
|
range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
|
|
|
{
|
2007-05-08 06:24:25 +08:00
|
|
|
rangeobject *obj = NULL;
|
|
|
|
PyObject *start = NULL, *stop = NULL, *step = NULL;
|
|
|
|
|
|
|
|
if (!_PyArg_NoKeywords("range()", kw))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (PyTuple_Size(args) <= 1) {
|
|
|
|
if (!PyArg_UnpackTuple(args, "range", 1, 1, &stop))
|
2009-06-13 02:40:16 +08:00
|
|
|
return NULL;
|
2007-05-08 06:24:25 +08:00
|
|
|
stop = PyNumber_Index(stop);
|
|
|
|
if (!stop)
|
2009-06-13 02:40:16 +08:00
|
|
|
return NULL;
|
2007-12-02 22:31:20 +08:00
|
|
|
start = PyLong_FromLong(0);
|
2009-06-13 02:40:16 +08:00
|
|
|
if (!start) {
|
|
|
|
Py_DECREF(stop);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-02 22:31:20 +08:00
|
|
|
step = PyLong_FromLong(1);
|
2009-06-13 02:40:16 +08:00
|
|
|
if (!step) {
|
|
|
|
Py_DECREF(stop);
|
|
|
|
Py_DECREF(start);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-05-08 06:24:25 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!PyArg_UnpackTuple(args, "range", 2, 3,
|
|
|
|
&start, &stop, &step))
|
2009-06-13 02:40:16 +08:00
|
|
|
return NULL;
|
2007-05-08 06:24:25 +08:00
|
|
|
|
|
|
|
/* Convert borrowed refs to owned refs */
|
|
|
|
start = PyNumber_Index(start);
|
2009-06-13 02:40:16 +08:00
|
|
|
if (!start)
|
|
|
|
return NULL;
|
2007-05-08 06:24:25 +08:00
|
|
|
stop = PyNumber_Index(stop);
|
2009-06-13 02:40:16 +08:00
|
|
|
if (!stop) {
|
|
|
|
Py_DECREF(start);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
step = validate_step(step); /* Caution, this can clear exceptions */
|
|
|
|
if (!step) {
|
|
|
|
Py_DECREF(start);
|
|
|
|
Py_DECREF(stop);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-05-08 06:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
obj = PyObject_New(rangeobject, &PyRange_Type);
|
|
|
|
if (obj == NULL)
|
|
|
|
goto Fail;
|
|
|
|
obj->start = start;
|
|
|
|
obj->stop = stop;
|
|
|
|
obj->step = step;
|
|
|
|
return (PyObject *) obj;
|
|
|
|
|
|
|
|
Fail:
|
|
|
|
Py_XDECREF(start);
|
|
|
|
Py_XDECREF(stop);
|
|
|
|
Py_XDECREF(step);
|
|
|
|
return NULL;
|
2002-06-06 07:12:45 +08:00
|
|
|
}
|
|
|
|
|
2002-06-14 04:33:02 +08:00
|
|
|
PyDoc_STRVAR(range_doc,
|
2007-05-08 06:24:25 +08:00
|
|
|
"range([start,] stop[, step]) -> range object\n\
|
2002-06-06 07:12:45 +08:00
|
|
|
\n\
|
2007-05-08 06:24:25 +08:00
|
|
|
Returns an iterator that generates the numbers in the range on demand.");
|
2002-06-06 07:12:45 +08:00
|
|
|
|
2007-05-08 06:24:25 +08:00
|
|
|
static void
|
|
|
|
range_dealloc(rangeobject *r)
|
1993-10-27 01:58:25 +08:00
|
|
|
{
|
2007-05-08 06:24:25 +08:00
|
|
|
Py_DECREF(r->start);
|
|
|
|
Py_DECREF(r->stop);
|
|
|
|
Py_DECREF(r->step);
|
2007-11-16 04:52:21 +08:00
|
|
|
PyObject_Del(r);
|
2007-05-08 06:24:25 +08:00
|
|
|
}
|
|
|
|
|
2009-06-25 05:14:38 +08:00
|
|
|
/* Return number of items in range (lo, hi, step), when arguments are PyLong
|
2009-06-25 06:16:41 +08:00
|
|
|
* objects. Return a value < 0 if & only if the true value is too large to
|
|
|
|
* fit in a signed long. Arguments MUST return 1 with PyLong_Check(). Return
|
|
|
|
* -1 when there is an error.
|
2007-05-08 06:24:25 +08:00
|
|
|
*/
|
|
|
|
static PyObject*
|
|
|
|
range_length_obj(rangeobject *r)
|
|
|
|
{
|
|
|
|
/* -------------------------------------------------------------
|
|
|
|
Algorithm is equal to that of get_len_of_range(), but it operates
|
2009-06-25 05:14:38 +08:00
|
|
|
on PyObjects (which are assumed to be PyLong objects).
|
2007-05-08 06:24:25 +08:00
|
|
|
---------------------------------------------------------------*/
|
2009-02-01 18:28:51 +08:00
|
|
|
int cmp_result;
|
2007-05-08 06:24:25 +08:00
|
|
|
PyObject *lo, *hi;
|
|
|
|
PyObject *step = NULL;
|
|
|
|
PyObject *diff = NULL;
|
|
|
|
PyObject *one = NULL;
|
|
|
|
PyObject *tmp1 = NULL, *tmp2 = NULL, *result;
|
|
|
|
/* holds sub-expression evaluations */
|
|
|
|
|
|
|
|
PyObject *zero = PyLong_FromLong(0);
|
|
|
|
if (zero == NULL)
|
|
|
|
return NULL;
|
2009-02-01 18:28:51 +08:00
|
|
|
cmp_result = PyObject_RichCompareBool(r->step, zero, Py_GT);
|
2007-05-08 06:24:25 +08:00
|
|
|
Py_DECREF(zero);
|
2009-02-01 18:28:51 +08:00
|
|
|
if (cmp_result == -1)
|
2007-05-08 06:24:25 +08:00
|
|
|
return NULL;
|
|
|
|
|
2009-02-01 18:28:51 +08:00
|
|
|
if (cmp_result == 1) {
|
2007-05-08 06:24:25 +08:00
|
|
|
lo = r->start;
|
|
|
|
hi = r->stop;
|
|
|
|
step = r->step;
|
|
|
|
Py_INCREF(step);
|
|
|
|
} else {
|
|
|
|
lo = r->stop;
|
|
|
|
hi = r->start;
|
|
|
|
step = PyNumber_Negative(r->step);
|
|
|
|
if (!step)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if (lo >= hi), return length of 0. */
|
2009-02-01 18:28:51 +08:00
|
|
|
if (PyObject_RichCompareBool(lo, hi, Py_GE) == 1) {
|
2007-05-08 06:24:25 +08:00
|
|
|
Py_XDECREF(step);
|
|
|
|
return PyLong_FromLong(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((one = PyLong_FromLong(1L)) == NULL)
|
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
|
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
|
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
|
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
if ((result = PyNumber_Add(tmp2, one)) == NULL)
|
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
Py_DECREF(tmp2);
|
|
|
|
Py_DECREF(diff);
|
|
|
|
Py_DECREF(step);
|
|
|
|
Py_DECREF(tmp1);
|
|
|
|
Py_DECREF(one);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
Fail:
|
|
|
|
Py_XDECREF(tmp2);
|
|
|
|
Py_XDECREF(diff);
|
|
|
|
Py_XDECREF(step);
|
|
|
|
Py_XDECREF(tmp1);
|
|
|
|
Py_XDECREF(one);
|
|
|
|
return NULL;
|
1993-10-27 01:58:25 +08:00
|
|
|
}
|
|
|
|
|
2006-02-16 01:27:45 +08:00
|
|
|
static Py_ssize_t
|
2000-07-09 14:21:27 +08:00
|
|
|
range_length(rangeobject *r)
|
1993-10-27 01:58:25 +08:00
|
|
|
{
|
2007-05-08 06:24:25 +08:00
|
|
|
PyObject *len = range_length_obj(r);
|
|
|
|
Py_ssize_t result = -1;
|
|
|
|
if (len) {
|
|
|
|
result = PyLong_AsSsize_t(len);
|
|
|
|
Py_DECREF(len);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* range(...)[x] is necessary for: seq[:] = range(...) */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
range_item(rangeobject *r, Py_ssize_t i)
|
|
|
|
{
|
|
|
|
Py_ssize_t len = range_length(r);
|
|
|
|
PyObject *rem, *incr, *result;
|
|
|
|
|
|
|
|
/* XXX(nnorwitz): should negative indices be supported? */
|
|
|
|
/* XXX(nnorwitz): should support range[x] where x > PY_SSIZE_T_MAX? */
|
|
|
|
if (i < 0 || i >= len) {
|
|
|
|
if (!PyErr_Occurred())
|
|
|
|
PyErr_SetString(PyExc_IndexError,
|
2007-05-22 02:01:17 +08:00
|
|
|
"range object index out of range");
|
2008-04-26 05:15:37 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-05-08 06:24:25 +08:00
|
|
|
|
|
|
|
/* XXX(nnorwitz): optimize for short ints. */
|
2008-02-09 12:13:49 +08:00
|
|
|
rem = PyLong_FromSsize_t(i);
|
2007-05-08 06:24:25 +08:00
|
|
|
if (!rem)
|
|
|
|
return NULL;
|
|
|
|
incr = PyNumber_Multiply(rem, r->step);
|
|
|
|
Py_DECREF(rem);
|
|
|
|
if (!incr)
|
|
|
|
return NULL;
|
|
|
|
result = PyNumber_Add(r->start, incr);
|
|
|
|
Py_DECREF(incr);
|
|
|
|
return result;
|
1993-12-22 06:50:31 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
range_repr(rangeobject *r)
|
1993-10-27 01:58:25 +08:00
|
|
|
{
|
2007-05-21 18:43:34 +08:00
|
|
|
Py_ssize_t istep;
|
2007-05-08 06:24:25 +08:00
|
|
|
|
|
|
|
/* Check for special case values for printing. We don't always
|
2007-05-21 18:43:34 +08:00
|
|
|
need the step value. We don't care about errors
|
2007-05-08 06:24:25 +08:00
|
|
|
(it means overflow), so clear the errors. */
|
|
|
|
istep = PyNumber_AsSsize_t(r->step, NULL);
|
|
|
|
if (istep != 1 || (istep == -1 && PyErr_Occurred())) {
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
|
2007-05-21 18:43:34 +08:00
|
|
|
if (istep == 1)
|
2007-05-20 16:19:54 +08:00
|
|
|
return PyUnicode_FromFormat("range(%R, %R)", r->start, r->stop);
|
|
|
|
else
|
|
|
|
return PyUnicode_FromFormat("range(%R, %R, %R)",
|
|
|
|
r->start, r->stop, r->step);
|
2001-07-09 20:30:54 +08:00
|
|
|
}
|
|
|
|
|
2008-06-10 12:03:04 +08:00
|
|
|
/* Pickling support */
|
|
|
|
static PyObject *
|
|
|
|
range_reduce(rangeobject *r, PyObject *args)
|
|
|
|
{
|
|
|
|
return Py_BuildValue("(O(OOO))", Py_TYPE(r),
|
|
|
|
r->start, r->stop, r->step);
|
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PySequenceMethods range_as_sequence = {
|
2007-05-08 06:24:25 +08:00
|
|
|
(lenfunc)range_length, /* sq_length */
|
|
|
|
0, /* sq_concat */
|
|
|
|
0, /* sq_repeat */
|
|
|
|
(ssizeargfunc)range_item, /* sq_item */
|
|
|
|
0, /* sq_slice */
|
1993-10-27 01:58:25 +08:00
|
|
|
};
|
|
|
|
|
2002-07-18 00:30:39 +08:00
|
|
|
static PyObject * range_iter(PyObject *seq);
|
2003-11-06 22:06:48 +08:00
|
|
|
static PyObject * range_reverse(PyObject *seq);
|
|
|
|
|
|
|
|
PyDoc_STRVAR(reverse_doc,
|
|
|
|
"Returns a reverse iterator.");
|
|
|
|
|
|
|
|
static PyMethodDef range_methods[] = {
|
2007-05-08 06:24:25 +08:00
|
|
|
{"__reversed__", (PyCFunction)range_reverse, METH_NOARGS,
|
|
|
|
reverse_doc},
|
2008-06-10 12:03:04 +08:00
|
|
|
{"__reduce__", (PyCFunction)range_reduce, METH_VARARGS},
|
2007-05-08 06:24:25 +08:00
|
|
|
{NULL, NULL} /* sentinel */
|
2003-11-06 22:06:48 +08:00
|
|
|
};
|
2002-06-06 04:08:48 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyTypeObject PyRange_Type = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-22 01:22:18 +08:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2007-05-08 06:24:25 +08:00
|
|
|
"range", /* Name of this type */
|
2006-04-21 18:40:58 +08:00
|
|
|
sizeof(rangeobject), /* Basic object size */
|
|
|
|
0, /* Item size for varobject */
|
2007-05-08 06:24:25 +08:00
|
|
|
(destructor)range_dealloc, /* tp_dealloc */
|
2006-04-21 18:40:58 +08:00
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2009-02-03 04:36:42 +08:00
|
|
|
0, /* tp_reserved */
|
2006-04-21 18:40:58 +08:00
|
|
|
(reprfunc)range_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
&range_as_sequence, /* 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 */
|
|
|
|
range_doc, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
range_iter, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
range_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
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 */
|
2002-06-06 04:08:48 +08:00
|
|
|
};
|
|
|
|
|
2007-05-22 02:01:17 +08:00
|
|
|
/*********************** range Iterator **************************/
|
2002-06-06 04:08:48 +08:00
|
|
|
|
2007-05-08 06:24:25 +08:00
|
|
|
/* There are 2 types of iterators, one for C longs, the other for
|
|
|
|
Python longs (ie, PyObjects). This should make iteration fast
|
|
|
|
in the normal case, but possible for any numeric value.
|
|
|
|
*/
|
|
|
|
|
2002-06-06 04:08:48 +08:00
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
long index;
|
|
|
|
long start;
|
|
|
|
long step;
|
|
|
|
long len;
|
|
|
|
} rangeiterobject;
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
rangeiter_next(rangeiterobject *r)
|
|
|
|
{
|
2007-05-08 06:24:25 +08:00
|
|
|
if (r->index < r->len)
|
2007-12-02 22:31:20 +08:00
|
|
|
return PyLong_FromLong(r->start + (r->index++) * r->step);
|
2007-05-08 06:24:25 +08:00
|
|
|
return NULL;
|
2002-06-06 04:08:48 +08:00
|
|
|
}
|
|
|
|
|
2005-09-25 05:23:05 +08:00
|
|
|
static PyObject *
|
2004-03-10 18:10:42 +08:00
|
|
|
rangeiter_len(rangeiterobject *r)
|
|
|
|
{
|
2007-12-02 22:31:20 +08:00
|
|
|
return PyLong_FromLong(r->len - r->index);
|
2004-03-10 18:10:42 +08:00
|
|
|
}
|
|
|
|
|
2007-05-08 06:24:25 +08:00
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *index;
|
|
|
|
PyObject *start;
|
|
|
|
PyObject *step;
|
|
|
|
PyObject *len;
|
|
|
|
} longrangeiterobject;
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
longrangeiter_len(longrangeiterobject *r, PyObject *no_args)
|
|
|
|
{
|
|
|
|
return PyNumber_Subtract(r->len, r->index);
|
|
|
|
}
|
2007-05-08 23:18:31 +08:00
|
|
|
|
2007-05-08 06:24:25 +08:00
|
|
|
static PyObject *rangeiter_new(PyTypeObject *, PyObject *args, PyObject *kw);
|
|
|
|
|
|
|
|
PyDoc_STRVAR(length_hint_doc,
|
|
|
|
"Private method returning an estimate of len(list(it)).");
|
2004-03-10 18:10:42 +08:00
|
|
|
|
2005-09-25 05:23:05 +08:00
|
|
|
static PyMethodDef rangeiter_methods[] = {
|
2007-05-08 06:24:25 +08:00
|
|
|
{"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS,
|
|
|
|
length_hint_doc},
|
|
|
|
{NULL, NULL} /* sentinel */
|
2005-09-25 05:23:05 +08:00
|
|
|
};
|
2004-03-10 18:10:42 +08:00
|
|
|
|
2007-11-30 06:35:39 +08:00
|
|
|
PyTypeObject PyRangeIter_Type = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-22 01:22:18 +08:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2007-11-28 17:44:38 +08:00
|
|
|
"range_iterator", /* tp_name */
|
2002-06-06 22:58:21 +08:00
|
|
|
sizeof(rangeiterobject), /* tp_basicsize */
|
2002-06-06 04:08:48 +08:00
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
|
|
|
(destructor)PyObject_Del, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2009-02-03 04:36:42 +08:00
|
|
|
0, /* tp_reserved */
|
2002-06-06 04:08:48 +08:00
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
2005-09-25 05:23:05 +08:00
|
|
|
0, /* tp_as_sequence */
|
2002-06-06 04:08:48 +08:00
|
|
|
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 */
|
2003-03-18 03:46:11 +08:00
|
|
|
PyObject_SelfIter, /* tp_iter */
|
2002-06-06 04:08:48 +08:00
|
|
|
(iternextfunc)rangeiter_next, /* tp_iternext */
|
2005-09-25 05:23:05 +08:00
|
|
|
rangeiter_methods, /* tp_methods */
|
2007-05-08 06:24:25 +08:00
|
|
|
0, /* tp_members */
|
|
|
|
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 */
|
|
|
|
rangeiter_new, /* tp_new */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Return number of items in range/xrange (lo, hi, step). step > 0
|
|
|
|
* required. Return a value < 0 if & only if the true value is too
|
|
|
|
* large to fit in a signed long.
|
|
|
|
*/
|
|
|
|
static long
|
|
|
|
get_len_of_range(long lo, long hi, long step)
|
|
|
|
{
|
|
|
|
/* -------------------------------------------------------------
|
|
|
|
If lo >= hi, the range is empty.
|
|
|
|
Else if n values are in the range, the last one is
|
|
|
|
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
|
|
|
|
precision to compute the RHS exactly.
|
|
|
|
---------------------------------------------------------------*/
|
|
|
|
long n = 0;
|
|
|
|
if (lo < hi) {
|
|
|
|
unsigned long uhi = (unsigned long)hi;
|
|
|
|
unsigned long ulo = (unsigned long)lo;
|
|
|
|
unsigned long diff = uhi - ulo - 1;
|
|
|
|
n = (long)(diff / (unsigned long)step + 1);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
int_range_iter(long start, long stop, long step)
|
|
|
|
{
|
2007-11-30 06:35:39 +08:00
|
|
|
rangeiterobject *it = PyObject_New(rangeiterobject, &PyRangeIter_Type);
|
2007-05-08 06:24:25 +08:00
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
|
|
|
it->start = start;
|
|
|
|
it->step = step;
|
|
|
|
if (step > 0)
|
|
|
|
it->len = get_len_of_range(start, stop, step);
|
|
|
|
else
|
|
|
|
it->len = get_len_of_range(stop, start, -step);
|
|
|
|
it->index = 0;
|
|
|
|
return (PyObject *)it;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
rangeiter_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
|
|
|
{
|
|
|
|
long start, stop, step;
|
|
|
|
|
|
|
|
if (!_PyArg_NoKeywords("rangeiter()", kw))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "lll;rangeiter() requires 3 int arguments",
|
|
|
|
&start, &stop, &step))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return int_range_iter(start, stop, step);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef longrangeiter_methods[] = {
|
|
|
|
{"__length_hint__", (PyCFunction)longrangeiter_len, METH_NOARGS,
|
|
|
|
length_hint_doc},
|
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
longrangeiter_dealloc(longrangeiterobject *r)
|
|
|
|
{
|
|
|
|
Py_XDECREF(r->index);
|
2007-05-08 23:18:31 +08:00
|
|
|
Py_XDECREF(r->start);
|
|
|
|
Py_XDECREF(r->step);
|
|
|
|
Py_XDECREF(r->len);
|
2007-11-16 04:52:21 +08:00
|
|
|
PyObject_Del(r);
|
2007-05-08 06:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
longrangeiter_next(longrangeiterobject *r)
|
|
|
|
{
|
|
|
|
PyObject *one, *product, *new_index, *result;
|
|
|
|
if (PyObject_RichCompareBool(r->index, r->len, Py_LT) != 1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
one = PyLong_FromLong(1);
|
|
|
|
if (!one)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
product = PyNumber_Multiply(r->index, r->step);
|
|
|
|
if (!product) {
|
|
|
|
Py_DECREF(one);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_index = PyNumber_Add(r->index, one);
|
|
|
|
Py_DECREF(one);
|
|
|
|
if (!new_index) {
|
|
|
|
Py_DECREF(product);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = PyNumber_Add(r->start, product);
|
|
|
|
Py_DECREF(product);
|
|
|
|
if (result) {
|
|
|
|
Py_DECREF(r->index);
|
|
|
|
r->index = new_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-11-30 06:35:39 +08:00
|
|
|
PyTypeObject PyLongRangeIter_Type = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-22 01:22:18 +08:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2007-11-30 06:35:39 +08:00
|
|
|
"longrange_iterator", /* tp_name */
|
2007-05-08 06:24:25 +08:00
|
|
|
sizeof(longrangeiterobject), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
|
|
|
(destructor)longrangeiter_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2009-02-03 04:36:42 +08:00
|
|
|
0, /* tp_reserved */
|
2007-05-08 06:24:25 +08: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 */
|
2005-09-25 05:23:05 +08:00
|
|
|
0,
|
2002-06-06 04:08:48 +08:00
|
|
|
};
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
range_iter(PyObject *seq)
|
|
|
|
{
|
2007-05-08 06:24:25 +08:00
|
|
|
rangeobject *r = (rangeobject *)seq;
|
|
|
|
longrangeiterobject *it;
|
2007-12-21 06:57:23 +08:00
|
|
|
long lstart, lstop, lstep;
|
2007-05-08 06:24:25 +08:00
|
|
|
|
|
|
|
assert(PyRange_Check(seq));
|
2007-12-21 06:57:23 +08:00
|
|
|
|
|
|
|
/* If all three fields convert to long, use the int version */
|
|
|
|
lstart = PyLong_AsLong(r->start);
|
|
|
|
if (lstart != -1 || !PyErr_Occurred()) {
|
|
|
|
lstop = PyLong_AsLong(r->stop);
|
|
|
|
if (lstop != -1 || !PyErr_Occurred()) {
|
|
|
|
lstep = PyLong_AsLong(r->step);
|
|
|
|
if (lstep != -1 || !PyErr_Occurred())
|
|
|
|
return int_range_iter(lstart, lstop, lstep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Some conversion failed, so there is an error set. Clear it,
|
|
|
|
and try again with a long range. */
|
|
|
|
PyErr_Clear();
|
2007-05-08 06:24:25 +08:00
|
|
|
|
2007-11-30 06:35:39 +08:00
|
|
|
it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
|
2007-05-08 06:24:25 +08:00
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
2007-05-08 23:18:31 +08:00
|
|
|
|
|
|
|
/* Do all initialization here, so we can DECREF on failure. */
|
2007-05-08 06:24:25 +08:00
|
|
|
it->start = r->start;
|
2007-05-08 23:18:31 +08:00
|
|
|
it->step = r->step;
|
|
|
|
Py_INCREF(it->start);
|
|
|
|
Py_INCREF(it->step);
|
|
|
|
|
|
|
|
it->len = it->index = NULL;
|
|
|
|
|
2009-06-25 02:36:46 +08:00
|
|
|
it->len = range_length_obj(r);
|
|
|
|
if (!it->len)
|
2007-05-08 06:24:25 +08:00
|
|
|
goto create_failure;
|
|
|
|
it->index = PyLong_FromLong(0);
|
|
|
|
if (!it->index)
|
|
|
|
goto create_failure;
|
|
|
|
|
|
|
|
return (PyObject *)it;
|
|
|
|
|
|
|
|
create_failure:
|
2007-05-08 23:18:31 +08:00
|
|
|
Py_DECREF(it);
|
2007-05-08 06:24:25 +08:00
|
|
|
return NULL;
|
2006-04-21 18:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
range_reverse(PyObject *seq)
|
|
|
|
{
|
2007-05-08 06:24:25 +08:00
|
|
|
rangeobject *range = (rangeobject*) seq;
|
|
|
|
longrangeiterobject *it;
|
|
|
|
PyObject *one, *sum, *diff, *len = NULL, *product;
|
2007-12-21 06:57:23 +08:00
|
|
|
long lstart, lstop, lstep;
|
2007-05-08 06:24:25 +08:00
|
|
|
|
|
|
|
/* XXX(nnorwitz): do the calc for the new start/stop first,
|
|
|
|
then if they fit, call the proper iter()?
|
|
|
|
*/
|
|
|
|
assert(PyRange_Check(seq));
|
2007-12-21 06:57:23 +08:00
|
|
|
|
|
|
|
/* If all three fields convert to long, use the int version */
|
|
|
|
lstart = PyLong_AsLong(range->start);
|
|
|
|
if (lstart != -1 || !PyErr_Occurred()) {
|
|
|
|
lstop = PyLong_AsLong(range->stop);
|
|
|
|
if (lstop != -1 || !PyErr_Occurred()) {
|
|
|
|
lstep = PyLong_AsLong(range->step);
|
|
|
|
if (lstep != -1 || !PyErr_Occurred()) {
|
|
|
|
/* XXX(nnorwitz): need to check for overflow and simplify. */
|
|
|
|
long len = get_len_of_range(lstart, lstop, lstep);
|
|
|
|
long new_start = lstart + (len - 1) * lstep;
|
|
|
|
long new_stop = lstart;
|
|
|
|
if (lstep > 0)
|
|
|
|
new_stop -= 1;
|
|
|
|
else
|
|
|
|
new_stop += 1;
|
|
|
|
return int_range_iter(new_start, new_stop, -lstep);
|
|
|
|
}
|
|
|
|
}
|
2007-05-08 06:24:25 +08:00
|
|
|
}
|
2007-12-21 06:57:23 +08:00
|
|
|
PyErr_Clear();
|
2007-05-08 06:24:25 +08:00
|
|
|
|
2007-11-30 06:35:39 +08:00
|
|
|
it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
|
2007-05-08 06:24:25 +08:00
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* start + (len - 1) * step */
|
|
|
|
len = range_length_obj(range);
|
|
|
|
if (!len)
|
|
|
|
goto create_failure;
|
|
|
|
|
|
|
|
one = PyLong_FromLong(1);
|
|
|
|
if (!one)
|
|
|
|
goto create_failure;
|
|
|
|
|
|
|
|
diff = PyNumber_Subtract(len, one);
|
|
|
|
Py_DECREF(one);
|
|
|
|
if (!diff)
|
|
|
|
goto create_failure;
|
|
|
|
|
|
|
|
product = PyNumber_Multiply(len, range->step);
|
|
|
|
if (!product)
|
|
|
|
goto create_failure;
|
|
|
|
|
|
|
|
sum = PyNumber_Add(range->start, product);
|
|
|
|
Py_DECREF(product);
|
|
|
|
it->start = sum;
|
|
|
|
if (!it->start)
|
|
|
|
goto create_failure;
|
|
|
|
it->step = PyNumber_Negative(range->step);
|
|
|
|
if (!it->step) {
|
|
|
|
Py_DECREF(it->start);
|
|
|
|
PyObject_Del(it);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Steal reference to len. */
|
|
|
|
it->len = len;
|
|
|
|
|
|
|
|
it->index = PyLong_FromLong(0);
|
|
|
|
if (!it->index) {
|
|
|
|
Py_DECREF(it);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (PyObject *)it;
|
|
|
|
|
|
|
|
create_failure:
|
|
|
|
Py_XDECREF(len);
|
|
|
|
PyObject_Del(it);
|
|
|
|
return NULL;
|
2006-04-21 18:40:58 +08:00
|
|
|
}
|