]> granicus.if.org Git - python/commitdiff
Issue #5211: Complete removal of implicit coercions for the complex
authorMark Dickinson <dickinsm@gmail.com>
Sun, 30 May 2010 12:12:25 +0000 (12:12 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Sun, 30 May 2010 12:12:25 +0000 (12:12 +0000)
type.  Coercion for arithmetic operations was already removed in
r78280, but that commit didn't remove coercion for rich comparisons.

Lib/test/test_complex.py
Misc/NEWS
Objects/complexobject.c

index fc47b23682176bc91e3a7029c9fd889b22ae9e8b..f27593e1537bba86bf5b93681b99c2fb711a3f5c 100644 (file)
@@ -115,6 +115,19 @@ class ComplexTest(unittest.TestCase):
     def test_coerce(self):
         self.assertRaises(OverflowError, complex.__coerce__, 1+1j, 1L<<10000)
 
+    def test_no_implicit_coerce(self):
+        # Python 2.7 removed implicit coercion from the complex type
+        class A(object):
+            def __coerce__(self, other):
+                raise RuntimeError
+            __hash__ = None
+            def __cmp__(self, other):
+                return -1
+
+        a = A()
+        self.assertRaises(TypeError, lambda: a + 2.0j)
+        self.assertTrue(a < 2.0j)
+
     def test_richcompare(self):
         self.assertRaises(OverflowError, complex.__eq__, 1+1j, 1L<<10000)
         self.assertEqual(complex.__lt__(1+1j, None), NotImplemented)
index 17f71641b73b38aacc97d5f470415568a243c8a1..e63d905d3439aaf325fbbd81acdeba47bdec7910 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 2.7 Release Candidate 1?
 Core and Builtins
 -----------------
 
+- Issue #5211: Implicit coercion for the complex type is now completely
+  removed.  (Coercion for arithmetic operations was already removed in 2.7
+  alpha 4, but coercion for rich comparisons was accidentally left in.)
+
 - Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding
   and error handler, instead of writing to the C stderr file in utf-8
 
index 5f26a6abfc5794ddacb2d82d246791e23f7c327e..3577a29a39d6b05040ff30e9b00c747ef5dfdd9b 100644 (file)
@@ -787,25 +787,8 @@ complex_richcompare(PyObject *v, PyObject *w, int op)
     Py_complex i, j;
     PyObject *res;
 
-    c = PyNumber_CoerceEx(&v, &w);
-    if (c < 0)
-        return NULL;
-    if (c > 0) {
-        Py_INCREF(Py_NotImplemented);
-        return Py_NotImplemented;
-    }
-    /* Make sure both arguments are complex. */
-    if (!(PyComplex_Check(v) && PyComplex_Check(w))) {
-        Py_DECREF(v);
-        Py_DECREF(w);
-        Py_INCREF(Py_NotImplemented);
-        return Py_NotImplemented;
-    }
-
-    i = ((PyComplexObject *)v)->cval;
-    j = ((PyComplexObject *)w)->cval;
-    Py_DECREF(v);
-    Py_DECREF(w);
+    TO_COMPLEX(v, i);
+    TO_COMPLEX(w, j);
 
     if (op != Py_EQ && op != Py_NE) {
         PyErr_SetString(PyExc_TypeError,