[Python-checkins] r78902 - in python/branches/py3k: Doc/library/stdtypes.rst Lib/test/test_complex.py Misc/NEWS Objects/complexobject.c

mark.dickinson python-checkins at python.org
Sat Mar 13 10:48:40 CET 2010


Author: mark.dickinson
Date: Sat Mar 13 10:48:39 2010
New Revision: 78902

Log:
Issue #7845:  Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.


Modified:
   python/branches/py3k/Doc/library/stdtypes.rst
   python/branches/py3k/Lib/test/test_complex.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Objects/complexobject.c

Modified: python/branches/py3k/Doc/library/stdtypes.rst
==============================================================================
--- python/branches/py3k/Doc/library/stdtypes.rst	(original)
+++ python/branches/py3k/Doc/library/stdtypes.rst	Sat Mar 13 10:48:39 2010
@@ -168,8 +168,9 @@
 Furthermore, some types (for example, function objects) support only a degenerate
 notion of comparison where any two objects of that type are unequal.  The ``<``,
 ``<=``, ``>`` and ``>=`` operators will raise a :exc:`TypeError` exception when
-any operand is a complex number, the objects are of different types that cannot
-be compared, or other cases where there is no defined ordering.
+comparing a complex number with another built-in numeric type, when the objects
+are of different types that cannot be compared, or in other cases where there is
+no defined ordering.
 
 .. index::
    single: __eq__() (instance method)

Modified: python/branches/py3k/Lib/test/test_complex.py
==============================================================================
--- python/branches/py3k/Lib/test/test_complex.py	(original)
+++ python/branches/py3k/Lib/test/test_complex.py	Sat Mar 13 10:48:39 2010
@@ -3,6 +3,7 @@
 
 from random import random
 from math import atan2, isnan, copysign
+import operator
 
 INF = float("inf")
 NAN = float("nan")
@@ -110,15 +111,23 @@
 
     def test_richcompare(self):
         self.assertRaises(OverflowError, complex.__eq__, 1+1j, 1<<10000)
-        self.assertEqual(complex.__lt__(1+1j, None), NotImplemented)
+        self.assertIs(complex.__lt__(1+1j, None), NotImplemented)
         self.assertIs(complex.__eq__(1+1j, 1+1j), True)
         self.assertIs(complex.__eq__(1+1j, 2+2j), False)
         self.assertIs(complex.__ne__(1+1j, 1+1j), False)
         self.assertIs(complex.__ne__(1+1j, 2+2j), True)
-        self.assertRaises(TypeError, complex.__lt__, 1+1j, 2+2j)
-        self.assertRaises(TypeError, complex.__le__, 1+1j, 2+2j)
-        self.assertRaises(TypeError, complex.__gt__, 1+1j, 2+2j)
-        self.assertRaises(TypeError, complex.__ge__, 1+1j, 2+2j)
+        self.assertIs(complex.__lt__(1+1j, 2+2j), NotImplemented)
+        self.assertIs(complex.__le__(1+1j, 2+2j), NotImplemented)
+        self.assertIs(complex.__gt__(1+1j, 2+2j), NotImplemented)
+        self.assertIs(complex.__ge__(1+1j, 2+2j), NotImplemented)
+        self.assertRaises(TypeError, operator.lt, 1+1j, 2+2j)
+        self.assertRaises(TypeError, operator.le, 1+1j, 2+2j)
+        self.assertRaises(TypeError, operator.gt, 1+1j, 2+2j)
+        self.assertRaises(TypeError, operator.ge, 1+1j, 2+2j)
+        self.assertIs(operator.eq(1+1j, 1+1j), True)
+        self.assertIs(operator.eq(1+1j, 2+2j), False)
+        self.assertIs(operator.ne(1+1j, 1+1j), False)
+        self.assertIs(operator.ne(1+1j, 2+2j), True)
 
     def test_mod(self):
         # % is no longer supported on complex numbers

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sat Mar 13 10:48:39 2010
@@ -12,6 +12,11 @@
 Core and Builtins
 -----------------
 
+- Issue #7845: Rich comparison methods on the complex type now return
+  NotImplemented rather than raising a TypeError when comparing with an
+  incompatible type; this allows user-defined classes to implement their own
+  comparisons with complex.
+
 - Issue #3137: Don't ignore errors at startup, especially a keyboard interrupt
   (SIGINT). If an error occurs while importing the site module, the error is
   printed and Python exits. Initialize the GIL before importing the site

Modified: python/branches/py3k/Objects/complexobject.c
==============================================================================
--- python/branches/py3k/Objects/complexobject.c	(original)
+++ python/branches/py3k/Objects/complexobject.c	Sat Mar 13 10:48:39 2010
@@ -625,10 +625,8 @@
 	TO_COMPLEX(w, j);
 
 	if (op != Py_EQ && op != Py_NE) {
-		/* XXX Should eventually return NotImplemented */
-		PyErr_SetString(PyExc_TypeError,
-			"no ordering relation is defined for complex numbers");
-		return NULL;
+		Py_INCREF(Py_NotImplemented);
+		return Py_NotImplemented;
 	}
 
 	if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))


More information about the Python-checkins mailing list