diff -r 6b37e6aff9ef Objects/listobject.c --- a/Objects/listobject.c Mon Jan 27 19:03:07 2014 -0700 +++ b/Objects/listobject.c Tue Jan 28 17:07:45 2014 +0800 @@ -9,6 +9,18 @@ #include /* For size_t */ #endif +/*[clinic input] +class list "PyListObject *" "PyList_Type" +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + +/*[python input] +class slice_index_converter(CConverter): + type = "Py_ssize_t" + converter = "_PyEval_SliceIndex" +[python start generated code]*/ +/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + /* Ensure ob_item has room for at least newsize elements, and set * ob_size to newsize. If newsize > ob_size on entry, the content * of the new slots at exit is undefined heap trash; it's the caller's @@ -547,7 +559,7 @@ } static int -list_clear(PyListObject *a) +listclear(PyListObject *a) { Py_ssize_t i; PyObject **item = a->ob_item; @@ -629,7 +641,7 @@ d = n - norig; if (Py_SIZE(a) + d == 0) { Py_XDECREF(v_as_SF); - return list_clear(a); + return listclear(a); } item = a->ob_item; /* recycle the items that we are about to remove */ @@ -702,7 +714,7 @@ } if (n < 1) { - (void)list_clear(self); + (void)listclear(self); Py_INCREF(self); return (PyObject *)self; } @@ -745,41 +757,155 @@ return 0; } +/*[clinic input] +list.insert + + index: Py_ssize_t + object: object + / + +Insert object before index. +[clinic start generated code]*/ + +PyDoc_STRVAR(list_insert__doc__, +"insert(self, index, object)\n" +"Insert object before index."); + +#define LIST_INSERT_METHODDEF \ + {"insert", (PyCFunction)list_insert, METH_VARARGS, list_insert__doc__}, + static PyObject * -listinsert(PyListObject *self, PyObject *args) +list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object); + +static PyObject * +list_insert(PyListObject *self, PyObject *args) { - Py_ssize_t i; - PyObject *v; - if (!PyArg_ParseTuple(args, "nO:insert", &i, &v)) - return NULL; - if (ins1(self, i, v) == 0) + PyObject *return_value = NULL; + Py_ssize_t index; + PyObject *object; + + if (!PyArg_ParseTuple(args, + "nO:insert", + &index, &object)) + goto exit; + return_value = list_insert_impl(self, index, object); + +exit: + return return_value; +} + +static PyObject * +list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object) +/*[clinic end generated code: checksum=16ec8655ab7a7cd3e20fa74de61b71d20fde2455]*/ +{ + if (ins1(self, index, object) == 0) Py_RETURN_NONE; return NULL; } +/*[clinic input] +list.clear + +Remove all items from list. +[clinic start generated code]*/ + +PyDoc_STRVAR(list_clear__doc__, +"clear(self)\n" +"Remove all items from list."); + +#define LIST_CLEAR_METHODDEF \ + {"clear", (PyCFunction)list_clear, METH_NOARGS, list_clear__doc__}, + static PyObject * -listclear(PyListObject *self) +list_clear_impl(PyListObject *self); + +static PyObject * +list_clear(PyListObject *self, PyObject *Py_UNUSED(ignored)) { - list_clear(self); + return list_clear_impl(self); +} + +static PyObject * +list_clear_impl(PyListObject *self) +/*[clinic end generated code: checksum=9b991f0501f6bcb7fbe2f4ddbf8b4f9a4adaf261]*/ +{ + listclear(self); Py_RETURN_NONE; } +/*[clinic input] +list.copy + +Return a shallow copy of list. +[clinic start generated code]*/ + +PyDoc_STRVAR(list_copy__doc__, +"copy(self)\n" +"Return a shallow copy of list."); + +#define LIST_COPY_METHODDEF \ + {"copy", (PyCFunction)list_copy, METH_NOARGS, list_copy__doc__}, + static PyObject * -listcopy(PyListObject *self) +list_copy_impl(PyListObject *self); + +static PyObject * +list_copy(PyListObject *self, PyObject *Py_UNUSED(ignored)) +{ + return list_copy_impl(self); +} + +static PyObject * +list_copy_impl(PyListObject *self) +/*[clinic end generated code: checksum=d77eea2c1cfd21100eefaeae59d27f65fd0873b7]*/ { return list_slice(self, 0, Py_SIZE(self)); } +/*[clinic input] +list.append + + object: object + / + +Append object to the end of list. +[clinic start generated code]*/ + +PyDoc_STRVAR(list_append__doc__, +"append(self, object)\n" +"Append object to the end of list."); + +#define LIST_APPEND_METHODDEF \ + {"append", (PyCFunction)list_append, METH_O, list_append__doc__}, + static PyObject * -listappend(PyListObject *self, PyObject *v) +list_append(PyListObject *self, PyObject *object) +/*[clinic end generated code: checksum=6f445703429be3999e9b099f1ef8dbc4ab35aad2]*/ { - if (app1(self, v) == 0) + if (app1(self, object) == 0) Py_RETURN_NONE; return NULL; } +/*[clinic input] +list.extend + + iterable: object + / + +Extend list by appending elements from the iterable. +[clinic start generated code]*/ + +PyDoc_STRVAR(list_extend__doc__, +"extend(self, iterable)\n" +"Extend list by appending elements from the iterable."); + +#define LIST_EXTEND_METHODDEF \ + {"extend", (PyCFunction)list_extend, METH_O, list_extend__doc__}, + static PyObject * -listextend(PyListObject *self, PyObject *b) +list_extend(PyListObject *self, PyObject *iterable) +/*[clinic end generated code: checksum=ce4fe5252526676735712eacfd93293aa48dfe1e]*/ { PyObject *it; /* iter(v) */ Py_ssize_t m; /* size of self */ @@ -792,46 +918,47 @@ 1) lists and tuples which can use PySequence_Fast ops 2) extending self to self requires making a copy first */ - if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) { + if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) || + (PyObject *)self == iterable) { PyObject **src, **dest; - b = PySequence_Fast(b, "argument must be iterable"); - if (!b) + iterable = PySequence_Fast(iterable, "argument must be iterable"); + if (!iterable) return NULL; - n = PySequence_Fast_GET_SIZE(b); + n = PySequence_Fast_GET_SIZE(iterable); if (n == 0) { /* short circuit when b is empty */ - Py_DECREF(b); + Py_DECREF(iterable); Py_RETURN_NONE; } m = Py_SIZE(self); if (list_resize(self, m + n) == -1) { - Py_DECREF(b); + Py_DECREF(iterable); return NULL; } - /* note that we may still have self == b here for the + /* note that we may still have self == iterable here for the * situation a.extend(a), but the following code works * in that case too. Just make sure to resize self * before calling PySequence_Fast_ITEMS. */ /* populate the end of self with b's items */ - src = PySequence_Fast_ITEMS(b); + src = PySequence_Fast_ITEMS(iterable); dest = self->ob_item + m; for (i = 0; i < n; i++) { PyObject *o = src[i]; Py_INCREF(o); dest[i] = o; } - Py_DECREF(b); + Py_DECREF(iterable); Py_RETURN_NONE; } - it = PyObject_GetIter(b); + it = PyObject_GetIter(iterable); if (it == NULL) return NULL; iternext = *it->ob_type->tp_iternext; /* Guess a result list size. */ - n = PyObject_LengthHint(b, 8); + n = PyObject_LengthHint(iterable, 8); if (n == -1) { Py_DECREF(it); return NULL; @@ -890,9 +1017,9 @@ } PyObject * -_PyList_Extend(PyListObject *self, PyObject *b) +_PyList_Extend(PyListObject *self, PyObject *iterable) { - return listextend(self, b); + return list_extend(self, iterable); } static PyObject * @@ -900,7 +1027,7 @@ { PyObject *result; - result = listextend(self, other); + result = list_extend(self, other); if (result == NULL) return result; Py_DECREF(result); @@ -908,29 +1035,65 @@ return (PyObject *)self; } +/*[clinic input] +list.pop + + index: Py_ssize_t = -1 + / + +Remove and return item at index (default last). + +Raises IndexError if list is empty or index is out of range. +[clinic start generated code]*/ + +PyDoc_STRVAR(list_pop__doc__, +"pop(self, index=-1)\n" +"Remove and return item at index (default last).\n" +"\n" +"Raises IndexError if list is empty or index is out of range."); + +#define LIST_POP_METHODDEF \ + {"pop", (PyCFunction)list_pop, METH_VARARGS, list_pop__doc__}, + static PyObject * -listpop(PyListObject *self, PyObject *args) +list_pop_impl(PyListObject *self, Py_ssize_t index); + +static PyObject * +list_pop(PyListObject *self, PyObject *args) { - Py_ssize_t i = -1; + PyObject *return_value = NULL; + Py_ssize_t index = -1; + + if (!PyArg_ParseTuple(args, + "|n:pop", + &index)) + goto exit; + return_value = list_pop_impl(self, index); + +exit: + return return_value; +} + +static PyObject * +list_pop_impl(PyListObject *self, Py_ssize_t index) +/*[clinic end generated code: checksum=1302fb8ecf24fedefbabc8a7e69af1bbe95c64b8]*/ +{ PyObject *v; int status; - if (!PyArg_ParseTuple(args, "|n:pop", &i)) - return NULL; - if (Py_SIZE(self) == 0) { /* Special-case most common failure cause */ PyErr_SetString(PyExc_IndexError, "pop from empty list"); return NULL; } - if (i < 0) - i += Py_SIZE(self); - if (i < 0 || i >= Py_SIZE(self)) { + if (index < 0) + index += Py_SIZE(self); + if (index < 0 || index >= Py_SIZE(self)) { PyErr_SetString(PyExc_IndexError, "pop index out of range"); return NULL; } - v = self->ob_item[i]; - if (i == Py_SIZE(self) - 1) { + v = self->ob_item[index]; + if (index == Py_SIZE(self) - 1) { status = list_resize(self, Py_SIZE(self) - 1); if (status >= 0) return v; /* and v now owns the reference the list had */ @@ -938,7 +1101,7 @@ return NULL; } Py_INCREF(v); - status = list_ass_slice(self, i, i+1, (PyObject *)NULL); + status = list_ass_slice(self, index, index+1, (PyObject *)NULL); if (status < 0) { Py_DECREF(v); return NULL; @@ -1905,8 +2068,47 @@ * list will be some permutation of its input state (nothing is lost or * duplicated). */ +/*[clinic input] +list.sort + + * + key: object = None + reverse: int(c_default="0") = False + +Stable sort *IN PLACE*. +[clinic start generated code]*/ + +PyDoc_STRVAR(list_sort__doc__, +"sort(self, *, key=None, reverse=False)\n" +"Stable sort *IN PLACE*."); + +#define LIST_SORT_METHODDEF \ + {"sort", (PyCFunction)list_sort, METH_VARARGS|METH_KEYWORDS, list_sort__doc__}, + static PyObject * -listsort(PyListObject *self, PyObject *args, PyObject *kwds) +list_sort_impl(PyListObject *self, PyObject *key, int reverse); + +static PyObject * +list_sort(PyListObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"key", "reverse", NULL}; + PyObject *key = Py_None; + int reverse = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|$Oi:sort", _keywords, + &key, &reverse)) + goto exit; + return_value = list_sort_impl(self, key, reverse); + +exit: + return return_value; +} + +static PyObject * +list_sort_impl(PyListObject *self, PyObject *key, int reverse) +/*[clinic end generated code: checksum=3edb199bd0a7a15a79165a828fde7adbdfed1fa9]*/ { MergeState ms; Py_ssize_t nremaining; @@ -1916,26 +2118,13 @@ PyObject **saved_ob_item; PyObject **final_ob_item; PyObject *result = NULL; /* guilty until proved innocent */ - int reverse = 0; - PyObject *keyfunc = NULL; Py_ssize_t i; - static char *kwlist[] = {"key", "reverse", 0}; PyObject **keys; assert(self != NULL); - assert (PyList_Check(self)); - if (args != NULL) { - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort", - kwlist, &keyfunc, &reverse)) - return NULL; - if (Py_SIZE(args) > 0) { - PyErr_SetString(PyExc_TypeError, - "must use keyword argument for key function"); - return NULL; - } - } - if (keyfunc == Py_None) - keyfunc = NULL; + assert(PyList_Check(self)); + if (key == Py_None) + key = NULL; /* The list is temporarily made empty, so that mutations performed * by comparison functions can't affect the slice of memory we're @@ -1949,7 +2138,7 @@ self->ob_item = NULL; self->allocated = -1; /* any operation will reset it to >= 0 */ - if (keyfunc == NULL) { + if (key == NULL) { keys = NULL; lo.keys = saved_ob_item; lo.values = NULL; @@ -1965,7 +2154,7 @@ } for (i = 0; i < saved_ob_size ; i++) { - keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i], + keys[i] = PyObject_CallFunctionObjArgs(key, saved_ob_item[i], NULL); if (keys[i] == NULL) { for (i=i-1 ; i>=0 ; i--) @@ -2067,7 +2256,7 @@ self->ob_item = saved_ob_item; self->allocated = saved_allocated; if (final_ob_item != NULL) { - /* we cannot use list_clear() for this because it does not + /* we cannot use listclear() for this because it does not guarantee that the list is really empty when it returns */ while (--i >= 0) { Py_XDECREF(final_ob_item[i]); @@ -2087,15 +2276,38 @@ PyErr_BadInternalCall(); return -1; } - v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL); + v = list_sort_impl((PyListObject *)v, (PyObject *)NULL, 0); if (v == NULL) return -1; Py_DECREF(v); return 0; } +/*[clinic input] +list.reverse + +Reverse *IN PLACE*. +[clinic start generated code]*/ + +PyDoc_STRVAR(list_reverse__doc__, +"reverse(self)\n" +"Reverse *IN PLACE*."); + +#define LIST_REVERSE_METHODDEF \ + {"reverse", (PyCFunction)list_reverse, METH_NOARGS, list_reverse__doc__}, + static PyObject * -listreverse(PyListObject *self) +list_reverse_impl(PyListObject *self); + +static PyObject * +list_reverse(PyListObject *self, PyObject *Py_UNUSED(ignored)) +{ + return list_reverse_impl(self); +} + +static PyObject * +list_reverse_impl(PyListObject *self) +/*[clinic end generated code: checksum=54822896060d54c5ebd8521e30c0b9e96d8225bf]*/ { if (Py_SIZE(self) > 1) reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self)); @@ -2141,16 +2353,55 @@ return w; } +/*[clinic input] +list.index + + value: object + start: slice_index = 0 + stop: slice_index(c_default="Py_SIZE(self)") = sys.maxsize + / + +Return first index of value. + +Raises ValueError if the value is not present. +[clinic start generated code]*/ + +PyDoc_STRVAR(list_index__doc__, +"index(self, value, start=0, stop=sys.maxsize)\n" +"Return first index of value.\n" +"\n" +"Raises ValueError if the value is not present."); + +#define LIST_INDEX_METHODDEF \ + {"index", (PyCFunction)list_index, METH_VARARGS, list_index__doc__}, + static PyObject * -listindex(PyListObject *self, PyObject *args) +list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start, Py_ssize_t stop); + +static PyObject * +list_index(PyListObject *self, PyObject *args) { - Py_ssize_t i, start=0, stop=Py_SIZE(self); - PyObject *v; - - if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, - _PyEval_SliceIndex, &start, - _PyEval_SliceIndex, &stop)) - return NULL; + PyObject *return_value = NULL; + PyObject *value; + Py_ssize_t start = 0; + Py_ssize_t stop = Py_SIZE(self); + + if (!PyArg_ParseTuple(args, + "O|O&O&:index", + &value, _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &stop)) + goto exit; + return_value = list_index_impl(self, value, start, stop); + +exit: + return return_value; +} + +static PyObject * +list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start, Py_ssize_t stop) +/*[clinic end generated code: checksum=6b52fb85becf85a28433d20846a2c0ceedc9ac10]*/ +{ + Py_ssize_t i; + if (start < 0) { start += Py_SIZE(self); if (start < 0) @@ -2162,24 +2413,41 @@ stop = 0; } for (i = start; i < stop && i < Py_SIZE(self); i++) { - int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); + int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ); if (cmp > 0) return PyLong_FromSsize_t(i); else if (cmp < 0) return NULL; } - PyErr_Format(PyExc_ValueError, "%R is not in list", v); + PyErr_Format(PyExc_ValueError, "%R is not in list", value); return NULL; } +/*[clinic input] +list.count + + value: object + / + +Return number of occurrences of value. +[clinic start generated code]*/ + +PyDoc_STRVAR(list_count__doc__, +"count(self, value)\n" +"Return number of occurrences of value."); + +#define LIST_COUNT_METHODDEF \ + {"count", (PyCFunction)list_count, METH_O, list_count__doc__}, + static PyObject * -listcount(PyListObject *self, PyObject *v) +list_count(PyListObject *self, PyObject *value) +/*[clinic end generated code: checksum=102d30f0572d1ce138f4d7a12712433c89ba8119]*/ { Py_ssize_t count = 0; Py_ssize_t i; for (i = 0; i < Py_SIZE(self); i++) { - int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); + int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ); if (cmp > 0) count++; else if (cmp < 0) @@ -2188,13 +2456,34 @@ return PyLong_FromSsize_t(count); } +/*[clinic input] +list.remove + + value: object + / + +Remove first occurrence of value. + +Raises ValueError if the value is not present. +[clinic start generated code]*/ + +PyDoc_STRVAR(list_remove__doc__, +"remove(self, value)\n" +"Remove first occurrence of value.\n" +"\n" +"Raises ValueError if the value is not present."); + +#define LIST_REMOVE_METHODDEF \ + {"remove", (PyCFunction)list_remove, METH_O, list_remove__doc__}, + static PyObject * -listremove(PyListObject *self, PyObject *v) +list_remove(PyListObject *self, PyObject *value) +/*[clinic end generated code: checksum=d96fad5ec0b0ccbfb817dbb89b78fa05af8ec56b]*/ { Py_ssize_t i; for (i = 0; i < Py_SIZE(self); i++) { - int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); + int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ); if (cmp > 0) { if (list_ass_slice(self, i, i+1, (PyObject *)NULL) == 0) @@ -2288,15 +2577,48 @@ return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op); } +/*[clinic input] +list.__init__ + + sequence: object(c_default="NULL") = () + +Initialize list. + +Initialize new empty list if sequence is not specified, or from sequence's +items otherwise. +[clinic start generated code]*/ + +PyDoc_STRVAR(list___init____doc__, +"list(sequence=())\n" +"Initialize list.\n" +"\n" +"Initialize new empty list if sequence is not specified, or from sequence\'s\n" +"items otherwise."); + static int -list_init(PyListObject *self, PyObject *args, PyObject *kw) +list___init___impl(PyListObject *self, PyObject *sequence); + +static int +list___init__(PyObject *self, PyObject *args, PyObject *kwargs) { - PyObject *arg = NULL; - static char *kwlist[] = {"sequence", 0}; - - if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg)) - return -1; - + int return_value = -1; + static char *_keywords[] = {"sequence", NULL}; + PyObject *sequence = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:list", _keywords, + &sequence)) + goto exit; + return_value = list___init___impl((PyListObject *)self, sequence); + +exit: + return return_value; +} + +static int +list___init___impl(PyListObject *self, PyObject *sequence) +/*[clinic end generated code: checksum=3139d9d6d0669923b6271de763211d9dcbd70d5b]*/ +{ /* Verify list invariants established by PyType_GenericAlloc() */ assert(0 <= Py_SIZE(self)); assert(Py_SIZE(self) <= self->allocated || self->allocated == -1); @@ -2305,10 +2627,10 @@ /* Empty previous contents */ if (self->ob_item != NULL) { - (void)list_clear(self); + (void)listclear(self); } - if (arg != NULL) { - PyObject *rv = listextend(self, arg); + if (sequence != NULL) { + PyObject *rv = list_extend(self, sequence); if (rv == NULL) return -1; Py_DECREF(rv); @@ -2316,12 +2638,33 @@ return 0; } +/*[clinic input] +list.__sizeof__ + +Return the size of list in memory, in bytes +[clinic start generated code]*/ + +PyDoc_STRVAR(list___sizeof____doc__, +"__sizeof__(self)\n" +"Return the size of list in memory, in bytes"); + +#define LIST___SIZEOF___METHODDEF \ + {"__sizeof__", (PyCFunction)list___sizeof__, METH_NOARGS, list___sizeof____doc__}, + static PyObject * -list_sizeof(PyListObject *self) +list___sizeof___impl(PyListObject *self); + +static PyObject * +list___sizeof__(PyListObject *self, PyObject *Py_UNUSED(ignored)) { - Py_ssize_t res; - - res = sizeof(PyListObject) + self->allocated * sizeof(void*); + return list___sizeof___impl(self); +} + +static PyObject * +list___sizeof___impl(PyListObject *self) +/*[clinic end generated code: checksum=69ac857466d9064fc18ce391c93475d80f86dba6]*/ +{ + Py_ssize_t res = sizeof(PyListObject) + self->allocated * sizeof(void*); return PyLong_FromSsize_t(res); } @@ -2330,56 +2673,9 @@ PyDoc_STRVAR(getitem_doc, "x.__getitem__(y) <==> x[y]"); -PyDoc_STRVAR(reversed_doc, -"L.__reversed__() -- return a reverse iterator over the list"); -PyDoc_STRVAR(sizeof_doc, -"L.__sizeof__() -- size of L in memory, in bytes"); -PyDoc_STRVAR(clear_doc, -"L.clear() -> None -- remove all items from L"); -PyDoc_STRVAR(copy_doc, -"L.copy() -> list -- a shallow copy of L"); -PyDoc_STRVAR(append_doc, -"L.append(object) -> None -- append object to end"); -PyDoc_STRVAR(extend_doc, -"L.extend(iterable) -> None -- extend list by appending elements from the iterable"); -PyDoc_STRVAR(insert_doc, -"L.insert(index, object) -- insert object before index"); -PyDoc_STRVAR(pop_doc, -"L.pop([index]) -> item -- remove and return item at index (default last).\n" -"Raises IndexError if list is empty or index is out of range."); -PyDoc_STRVAR(remove_doc, -"L.remove(value) -> None -- remove first occurrence of value.\n" -"Raises ValueError if the value is not present."); -PyDoc_STRVAR(index_doc, -"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n" -"Raises ValueError if the value is not present."); -PyDoc_STRVAR(count_doc, -"L.count(value) -> integer -- return number of occurrences of value"); -PyDoc_STRVAR(reverse_doc, -"L.reverse() -- reverse *IN PLACE*"); -PyDoc_STRVAR(sort_doc, -"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*"); static PyObject *list_subscript(PyListObject*, PyObject*); -static PyMethodDef list_methods[] = { - {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc}, - {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc}, - {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc}, - {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc}, - {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc}, - {"append", (PyCFunction)listappend, METH_O, append_doc}, - {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc}, - {"extend", (PyCFunction)listextend, METH_O, extend_doc}, - {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc}, - {"remove", (PyCFunction)listremove, METH_O, remove_doc}, - {"index", (PyCFunction)listindex, METH_VARARGS, index_doc}, - {"count", (PyCFunction)listcount, METH_O, count_doc}, - {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc}, - {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc}, - {NULL, NULL} /* sentinel */ -}; - static PySequenceMethods list_as_sequence = { (lenfunc)list_length, /* sq_length */ (binaryfunc)list_concat, /* sq_concat */ @@ -2393,10 +2689,6 @@ (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */ }; -PyDoc_STRVAR(list_doc, -"list() -> new empty list\n" -"list(iterable) -> new list initialized from iterable's items"); - static PyObject * list_subscript(PyListObject* self, PyObject* item) { @@ -2620,50 +2912,6 @@ (objobjargproc)list_ass_subscript }; -PyTypeObject PyList_Type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) - "list", - sizeof(PyListObject), - 0, - (destructor)list_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_reserved */ - (reprfunc)list_repr, /* tp_repr */ - 0, /* tp_as_number */ - &list_as_sequence, /* tp_as_sequence */ - &list_as_mapping, /* tp_as_mapping */ - PyObject_HashNotImplemented, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - PyObject_GenericGetAttr, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | - Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */ - list_doc, /* tp_doc */ - (traverseproc)list_traverse, /* tp_traverse */ - (inquiry)list_clear, /* tp_clear */ - list_richcompare, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - list_iter, /* tp_iter */ - 0, /* tp_iternext */ - list_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 */ - (initproc)list_init, /* tp_init */ - PyType_GenericAlloc, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ - PyObject_GC_Del, /* tp_free */ -}; - - /*********************** List Iterator **************************/ typedef struct { @@ -2872,18 +3120,41 @@ 0, }; +/*[clinic input] +list.__reversed__ + +Return a reverse iterator over the list. +[clinic start generated code]*/ + +PyDoc_STRVAR(list___reversed____doc__, +"__reversed__(self)\n" +"Return a reverse iterator over the list."); + +#define LIST___REVERSED___METHODDEF \ + {"__reversed__", (PyCFunction)list___reversed__, METH_NOARGS, list___reversed____doc__}, + static PyObject * -list_reversed(PyListObject *seq, PyObject *unused) +list___reversed___impl(PyListObject *self); + +static PyObject * +list___reversed__(PyListObject *self, PyObject *Py_UNUSED(ignored)) +{ + return list___reversed___impl(self); +} + +static PyObject * +list___reversed___impl(PyListObject *self) +/*[clinic end generated code: checksum=73de51bd49b5aa8009b8ccc7d7e126642a022c06]*/ { listreviterobject *it; it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type); if (it == NULL) return NULL; - assert(PyList_Check(seq)); - it->it_index = PyList_GET_SIZE(seq) - 1; - Py_INCREF(seq); - it->it_seq = seq; + assert(PyList_Check(self)); + it->it_index = PyList_GET_SIZE(self) - 1; + Py_INCREF(self); + it->it_seq = self; PyObject_GC_Track(it); return (PyObject *)it; } @@ -2980,3 +3251,64 @@ return NULL; return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list); } + +static PyMethodDef list_methods[] = { + {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc}, + LIST___REVERSED___METHODDEF + LIST___SIZEOF___METHODDEF + LIST_CLEAR_METHODDEF + LIST_COPY_METHODDEF + LIST_APPEND_METHODDEF + LIST_INSERT_METHODDEF + LIST_EXTEND_METHODDEF + LIST_POP_METHODDEF + LIST_REMOVE_METHODDEF + LIST_INDEX_METHODDEF + LIST_COUNT_METHODDEF + LIST_REVERSE_METHODDEF + LIST_SORT_METHODDEF + {NULL, NULL} /* sentinel */ +}; + +PyTypeObject PyList_Type = { + PyVarObject_HEAD_INIT(&PyType_Type, 0) + "list", + sizeof(PyListObject), + 0, + (destructor)list_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + (reprfunc)list_repr, /* tp_repr */ + 0, /* tp_as_number */ + &list_as_sequence, /* tp_as_sequence */ + &list_as_mapping, /* tp_as_mapping */ + PyObject_HashNotImplemented, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + PyObject_GenericGetAttr, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */ + list___init____doc__, /* tp_doc */ + (traverseproc)list_traverse, /* tp_traverse */ + (inquiry)listclear, /* tp_clear */ + list_richcompare, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + list_iter, /* tp_iter */ + 0, /* tp_iternext */ + list_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 */ + (initproc)list___init__, /* tp_init */ + PyType_GenericAlloc, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ + PyObject_GC_Del, /* tp_free */ +};