From 05387861ea96dc7603519f00d95dcd7ad61b8919 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 19 Mar 2008 17:45:19 +0000 Subject: [PATCH] Issue 2354: Fix-up compare warning. Patch contributed by Jeff Balogh. --- Lib/test/test_py3kwarn.py | 14 ++++++++++++++ Objects/listobject.c | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_py3kwarn.py b/Lib/test/test_py3kwarn.py index cb450ff14b..f7a6949b13 100644 --- a/Lib/test/test_py3kwarn.py +++ b/Lib/test/test_py3kwarn.py @@ -91,6 +91,20 @@ class TestPy3KWarnings(unittest.TestCase): def assertWarning(self, _, warning, expected_message): self.assertEqual(str(warning.message), expected_message) + def test_sort_cmp_arg(self): + expected = "In 3.x, the cmp argument is no longer supported." + lst = range(5) + cmp = lambda x,y: -1 + + with catch_warning() as w: + self.assertWarning(lst.sort(cmp=cmp), w, expected) + with catch_warning() as w: + self.assertWarning(sorted(lst, cmp=cmp), w, expected) + with catch_warning() as w: + self.assertWarning(lst.sort(cmp), w, expected) + with catch_warning() as w: + self.assertWarning(sorted(lst, cmp), w, expected) + def test_main(): run_unittest(TestPy3KWarnings) diff --git a/Objects/listobject.c b/Objects/listobject.c index 9e86592101..d4faf0a15e 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2037,7 +2037,7 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds) } if (compare == Py_None) compare = NULL; - if (compare == NULL && + if (compare != NULL && Py_Py3kWarningFlag && PyErr_Warn(PyExc_DeprecationWarning, "In 3.x, the cmp argument is no longer supported.") < 0) -- 2.40.0