]> granicus.if.org Git - python/commitdiff
Allow list sort's comparison function to explicitly be None. See SF patch
authorSkip Montanaro <skip@pobox.com>
Thu, 2 Jan 2003 20:51:08 +0000 (20:51 +0000)
committerSkip Montanaro <skip@pobox.com>
Thu, 2 Jan 2003 20:51:08 +0000 (20:51 +0000)
661092.

Doc/lib/libstdtypes.tex
Lib/test/test_sort.py
Misc/NEWS
Objects/listobject.c

index d5c7c5d1fc923d42f4dd0248a22a1542c8d63a01..a3774269b8cc314fa47c26279609f80272ee4694 100644 (file)
@@ -925,7 +925,7 @@ The following operations are defined on mutable sequence types (where
        {same as \code{del \var{s}[\var{s}.index(\var{x})]}}{(3)}
   \lineiii{\var{s}.reverse()}
        {reverses the items of \var{s} in place}{(6)}
-  \lineiii{\var{s}.sort(\optional{\var{cmpfunc}})}
+  \lineiii{\var{s}.sort(\optional{\var{cmpfunc=None}})}
        {sort the items of \var{s} in place}{(6), (7), (8), (9)}
 \end{tableiii}
 \indexiv{operations on}{mutable}{sequence}{types}
@@ -970,10 +970,11 @@ Notes:
   the first argument is considered smaller than, equal to, or larger
   than the second argument.  Note that this slows the sorting process
   down considerably; e.g. to sort a list in reverse order it is much
-  faster to call method \method{sort()} followed by
-  \method{reverse()} than to use method
-  \method{sort()} with a comparison function that reverses the
-  ordering of the elements.
+  faster to call method \method{sort()} followed by \method{reverse()}
+  than to use method \method{sort()} with a comparison function that
+  reverses the ordering of the elements.  Passing \constant{None} as the
+  comparison function is semantically equivalent to calling
+  \method{sort()} with no comparison function.
 
 \item[(8)] Whether the \method{sort()} method is stable is not defined by
   the language (a sort is stable if it guarantees not to change the
index 5c7ae88c4a8dcbb9814c87369e269f77d5815f32..6c35f42f5f2c3d9bf4ccdcaff7fff189d35ff51f 100644 (file)
@@ -145,6 +145,26 @@ def bug453523():
 
 bug453523()
 
+def cmpNone():
+    global nerrors
+
+    if verbose:
+        print "Testing None as a comparison function."
+
+    L = range(50)
+    random.shuffle(L)
+    try:
+        L.sort(None)
+    except TypeError:
+        print "    Passing None as cmpfunc failed."
+        nerrors += 1
+    else:
+        if L != range(50):
+            print "    Passing None as cmpfunc failed."
+            nerrors += 1
+
+cmpNone()
+
 if nerrors:
     print "Test failed", nerrors
 elif verbose:
index e786af509d05e3206e3cb2b7e62090be6d2a8e44..b9c076d958cb3091f89531c28ce5e6be7b65cfaa 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 2.3 alpha 2?
 Core and builtins
 -----------------
 
+- List objects' sort() method now accepts None as the comparison function.
+  Passing None is semantically identical to calling sort() with no
+  arguments.
+
 Extension modules
 -----------------
 
index 461350c79c5ceb40e9a58b3a1ec98775753a525b..79403ccd55679ee125746a2f34dbf9a9a54d47c6 100644 (file)
@@ -1657,6 +1657,9 @@ listsort(PyListObject *self, PyObject *args)
                if (!PyArg_UnpackTuple(args, "sort", 0, 1, &compare))
                        return NULL;
        }
+       if (compare == Py_None)
+               compare = NULL;
+
        merge_init(&ms, compare);
 
        /* The list is temporarily made empty, so that mutations performed
@@ -2069,7 +2072,7 @@ PyDoc_STRVAR(count_doc,
 PyDoc_STRVAR(reverse_doc,
 "L.reverse() -- reverse *IN PLACE*");
 PyDoc_STRVAR(sort_doc,
-"L.sort([cmpfunc]) -- stable sort *IN PLACE*; cmpfunc(x, y) -> -1, 0, 1");
+"L.sort(cmpfunc=None) -- stable sort *IN PLACE*; cmpfunc(x, y) -> -1, 0, 1");
 
 static PyMethodDef list_methods[] = {
        {"append",      (PyCFunction)listappend,  METH_O, append_doc},