summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2003-10-29 06:54:43 (GMT)
committerRaymond Hettinger <python@rcn.com>2003-10-29 06:54:43 (GMT)
commit0a9b9da0c39eb74075cc93f7d7bd3b5dbcc25320 (patch)
tree9f383ec9b1ddc0e5bc09d25cd502aaac2b2432f8 /Objects
parentc43a7e7c370215518ea110f4d98493935bd1c2b8 (diff)
downloadcpython-0a9b9da0c39eb74075cc93f7d7bd3b5dbcc25320.zip
cpython-0a9b9da0c39eb74075cc93f7d7bd3b5dbcc25320.tar.gz
cpython-0a9b9da0c39eb74075cc93f7d7bd3b5dbcc25320.tar.bz2
Add list.sorted() classmethod.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/listobject.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c
index b53b948..23d7d9a 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2001,6 +2001,38 @@ PyList_Sort(PyObject *v)
}
static PyObject *
+listsorted(PyObject *cls, PyObject *args, PyObject *kwds)
+{
+ PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
+ static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
+ long reverse;
+
+ if (args != NULL) {
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
+ kwlist, &seq, &compare, &keyfunc, &reverse))
+ return NULL;
+ }
+
+ newlist = PyObject_CallFunctionObjArgs(cls, seq, NULL);
+ if (newlist == NULL)
+ return NULL;
+
+ newargs = PyTuple_GetSlice(args, 1, 4);
+ if (newargs == NULL) {
+ Py_DECREF(newlist);
+ return NULL;
+ }
+ v = listsort((PyListObject *)newlist, newargs, kwds);
+ Py_DECREF(newargs);
+ if (v == NULL) {
+ Py_DECREF(newlist);
+ return NULL;
+ }
+ Py_DECREF(v);
+ return newlist;
+}
+
+static PyObject *
listreverse(PyListObject *self)
{
if (self->ob_size > 1)
@@ -2335,6 +2367,9 @@ PyDoc_STRVAR(reverse_doc,
PyDoc_STRVAR(sort_doc,
"L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n\
cmp(x, y) -> -1, 0, 1");
+PyDoc_STRVAR(sorted_doc,
+"list.sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list;\n\
+cmp(x, y) -> -1, 0, 1");
static PyMethodDef list_methods[] = {
{"append", (PyCFunction)listappend, METH_O, append_doc},
@@ -2346,6 +2381,8 @@ static PyMethodDef list_methods[] = {
{"count", (PyCFunction)listcount, METH_O, count_doc},
{"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
{"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
+ {"sorted", (PyCFunction)listsorted,
+ METH_VARARGS | METH_KEYWORDS | METH_CLASS, sorted_doc},
{NULL, NULL} /* sentinel */
};