Commits

Armin Rigo committed f1c048b

(lac, arigo)

Fix the __complex__() method call: it must return really a complex
number object (and not e.g. a float). This is following CPython
(see test_cmath.py).

  • Participants
  • Parent commits f5166d5
  • Branches fast-forward

Comments (0)

Files changed (2)

pypy/objspace/std/complextype.py

                 raise
         else:
             w_real = space.call_function(w_method)
-            # __complex__() could return a string, which space.float()
-            # could accept below...  Let's catch this case.
-            if (space.is_true(space.isinstance(w_imag, space.w_str)) or
-                space.is_true(space.isinstance(w_imag, space.w_unicode))):
+            # __complex__() must return a complex object
+            if not space.is_true(space.isinstance(w_real, space.w_complex)):
                 raise OperationError(space.w_TypeError,
-                                     space.wrap("__complex__() cannot return"
-                                                " a string"))
+                                     space.wrap("__complex__() must return"
+                                                " a complex number"))
 
         # at this point w_real can be an instance of 'complex',
         # either because it is the result of __complex__() or because

pypy/objspace/std/test/test_complexobject.py

         h.assertEqual(complex(NS(1+10j)), 1+10j)
         h.raises(TypeError, complex, OS(None))
         h.raises(TypeError, complex, NS(None))
+        h.raises(TypeError, complex, OS(2.0))   # __complex__ must really
+        h.raises(TypeError, complex, NS(2.0))   # return a complex, not a float
 
         h.assertAlmostEqual(complex("1+10j"), 1+10j)
         h.assertAlmostEqual(complex(10), 10+0j)