Index: Makefile.pre.in =================================================================== RCS file: /cvsroot/python/python/dist/src/Makefile.pre.in,v retrieving revision 1.86 diff -c -r1.86 Makefile.pre.in *** Makefile.pre.in 21 Jun 2002 14:48:36 -0000 1.86 --- Makefile.pre.in 29 Jun 2002 21:50:44 -0000 *************** *** 272,278 **** Objects/moduleobject.o \ Objects/object.o \ Objects/obmalloc.o \ - Objects/rangeobject.o \ Objects/sliceobject.o \ Objects/stringobject.o \ Objects/structseq.o \ --- 272,277 ---- *************** *** 467,473 **** Include/pyport.h \ Include/pystate.h \ Include/pythonrun.h \ - Include/rangeobject.h \ Include/sliceobject.h \ Include/stringobject.h \ Include/structseq.h \ --- 466,471 ---- Index: Include/sliceobject.h =================================================================== RCS file: /cvsroot/python/python/dist/src/Include/sliceobject.h,v retrieving revision 2.6 diff -c -r2.6 sliceobject.h *** Include/sliceobject.h 11 Jun 2002 10:55:09 -0000 2.6 --- Include/sliceobject.h 29 Jun 2002 21:50:59 -0000 *************** *** 36,41 **** --- 36,48 ---- int *start, int *stop, int *step, int *slicelength); + /* Old Xrange object interface */ + + #define PyRange_Type PySlice_Type + #define PyRange_Check PySlice_Check + extern DL_IMPORT(PyObject *) PyRange_New(long, long, long, int); + + #ifdef __cplusplus } #endif Index: Lib/test/test_repr.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_repr.py,v retrieving revision 1.13 diff -c -r1.13 test_repr.py *** Lib/test/test_repr.py 2 May 2002 18:40:31 -0000 1.13 --- Lib/test/test_repr.py 29 Jun 2002 21:51:08 -0000 *************** *** 107,115 **** def test_xrange(self): import warnings eq = self.assertEquals ! eq(repr(xrange(1)), 'xrange(1)') ! eq(repr(xrange(1, 2)), 'xrange(1, 2)') ! eq(repr(xrange(1, 2, 3)), 'xrange(1, 4, 3)') def test_nesting(self): eq = self.assertEquals --- 107,115 ---- def test_xrange(self): import warnings eq = self.assertEquals ! eq(repr(xrange(1)), repr(slice(1))) ! eq(repr(xrange(1, 2)), repr(slice(1, 2))) ! eq(repr(xrange(1, 2, 3)), repr(slice(1, 2, 3))) def test_nesting(self): eq = self.assertEquals Index: Objects/sliceobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/sliceobject.c,v retrieving revision 2.15 diff -c -r2.15 sliceobject.c *** Objects/sliceobject.c 14 Jun 2002 20:41:15 -0000 2.15 --- Objects/sliceobject.c 29 Jun 2002 21:51:33 -0000 *************** *** 79,84 **** --- 79,93 ---- return (PyObject *) obj; } + + PyObject * + PyRange_New(long start, long len, long step, int reps) + { + return PySlice_New(PyInt_FromLong(start), + PyInt_FromLong(len), + PyInt_FromLong(step)); + } + int PySlice_GetIndices(PySliceObject *r, int length, int *start, int *stop, int *step) *************** *** 164,169 **** --- 173,225 ---- return 0; } + static int + slice_getindices(PySliceObject *r, + int *start, int *stop, int *step, int *slicelength) + { + if (r->start == Py_None) { + *start = 0; + } else { + *start = PyInt_AsLong(r->start); + if (*start == -1 && PyErr_Occurred()) + return -1; + } + + if (r->stop == Py_None) { + PyErr_SetString(PyExc_ValueError, + "slice stop argument cannot be None"); + return -1; + } else { + *stop = PyInt_AsLong(r->stop); + if (*stop == -1 && PyErr_Occurred()) + return -1; + } + + if (r->step == Py_None) { + *step = 1; + } else { + *step = PyInt_AsLong(r->step); + if (*step == -1 && PyErr_Occurred()) + return -1; + else if (*step == 0) { + PyErr_SetString(PyExc_ValueError, + "slice step cannot be zero"); + return -1; + } + } + + if ((*stop - *start)*(*step) <= 0) { + *slicelength = 0; + } + else if (*step < 0) { + *slicelength = (*stop-*start+1)/(*step)+1; + } else { + *slicelength = (*stop-*start-1)/(*step)+1; + } + + return 0; + } + static PyObject * slice_new(PyTypeObject *type, PyObject *args, PyObject *kw) { *************** *** 242,247 **** --- 298,337 ---- return result; } + static int + slice_length(PySliceObject *s) + { + long start, stop, step, n; + if (slice_getindices(s, &start, &stop, &step, &n) == -1) + return 0; /* ??? What should be returned on error */ + return n; + } + + static PyObject * + slice_item(PySliceObject *s, int i) + { + long start, stop, step, n; + if (slice_getindices(s, &start, &stop, &step, &n) == -1) + return NULL; + + if (i < 0 || i >= n) { + PyErr_SetString(PyExc_IndexError, + "slice object index out of range"); + return NULL; + } + return PyInt_FromLong(start + (i % n) * step); + } + + static PySequenceMethods slice_as_sequence = { + (inquiry)slice_length, /* sq_length */ + 0, /* sq_concat */ + 0, /* sq_repeat */ + (intargfunc)slice_item, /* sq_item */ + 0, /* sq_slice */ + }; + + staticforward PyObject * slice_iter(PyObject *seq); + PyTypeObject PySlice_Type = { PyObject_HEAD_INIT(&PyType_Type) 0, /* Number of items for varobject */ *************** *** 255,261 **** (cmpfunc)slice_compare, /* tp_compare */ (reprfunc)slice_repr, /* tp_repr */ 0, /* tp_as_number */ ! 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ --- 345,351 ---- (cmpfunc)slice_compare, /* tp_compare */ (reprfunc)slice_repr, /* tp_repr */ 0, /* tp_as_number */ ! &slice_as_sequence, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ *************** *** 269,275 **** 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ ! 0, /* tp_iter */ 0, /* tp_iternext */ 0, /* tp_methods */ slice_members, /* tp_members */ --- 359,365 ---- 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ ! (getiterfunc)slice_iter, /* tp_iter */ 0, /* tp_iternext */ 0, /* tp_methods */ slice_members, /* tp_members */ *************** *** 283,285 **** --- 373,470 ---- 0, /* tp_alloc */ slice_new, /* tp_new */ }; + + + /*********************** Slice Iterator **************************/ + + typedef struct { + PyObject_HEAD + long index; + long start; + long step; + long len; + } sliceiterobject; + + staticforward PyTypeObject Pysliceiter_Type; + + static PyObject * + slice_iter(PyObject *s) + { + sliceiterobject *it; + long start, stop, step, n; + + if (!PySlice_Check(s)) { + PyErr_BadInternalCall(); + return NULL; + } + if (slice_getindices((PySliceObject *)s, &start, &stop, &step, &n) == -1) + return NULL; + it = PyObject_New(sliceiterobject, &Pysliceiter_Type); + if (it == NULL) + return NULL; + + it->index = 0; + it->start = start; + it->step = step; + it->len = n; + + return (PyObject *)it; + } + + static PyObject * + sliceiter_getiter(PyObject *it) + { + Py_INCREF(it); + return it; + } + + static PyObject * + sliceiter_next(sliceiterobject *r) + { + if (r->index < r->len) + return PyInt_FromLong(r->start + (r->index++) * r->step); + PyErr_SetObject(PyExc_StopIteration, Py_None); + return NULL; + } + + static PyMethodDef sliceiter_methods[] = { + {"next", (PyCFunction)sliceiter_next, METH_NOARGS, + "it.next() -- get the next value, or raise StopIteration"}, + {NULL, NULL} /* sentinel */ + }; + + staticforward PyObject * slice_iter(PyObject *seq); + + static PyTypeObject Pysliceiter_Type = { + PyObject_HEAD_INIT(&PyType_Type) + 0, /* ob_size */ + "sliceiterator", /* tp_name */ + sizeof(sliceiterobject), /* tp_basicsize */ + 0, /* tp_itemsize */ + /* methods */ + (destructor)PyObject_Del, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + 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 */ + (getiterfunc)sliceiter_getiter, /* tp_iter */ + (iternextfunc)sliceiter_next, /* tp_iternext */ + sliceiter_methods, /* tp_methods */ + }; + Index: Python/bltinmodule.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v retrieving revision 2.260 diff -c -r2.260 bltinmodule.c *** Python/bltinmodule.c 14 Jun 2002 20:41:16 -0000 2.260 --- Python/bltinmodule.c 29 Jun 2002 21:51:37 -0000 *************** *** 1820,1826 **** SETBUILTIN("super", &PySuper_Type); SETBUILTIN("tuple", &PyTuple_Type); SETBUILTIN("type", &PyType_Type); ! SETBUILTIN("xrange", &PyRange_Type); /* Note that open() is just an alias of file(). */ SETBUILTIN("open", &PyFile_Type); --- 1820,1826 ---- SETBUILTIN("super", &PySuper_Type); SETBUILTIN("tuple", &PyTuple_Type); SETBUILTIN("type", &PyType_Type); ! SETBUILTIN("xrange", &PySlice_Type); /* Note that open() is just an alias of file(). */ SETBUILTIN("open", &PyFile_Type);