This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: complex() doesn't use __complex__
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: gvanrossum, rhettinger
Priority: normal Keywords:

Created on 2002-06-03 05:17 by gvanrossum, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
complex.diff rhettinger, 2002-06-06 07:14 Patch to complexobject.c
Messages (4)
msg11035 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-06-03 05:17
complex() with a new-style instance of a class that
implements __complex__ doesn't work, because it only
looks for __complex__ when the argument is a classic
instance.  It should always look for __complex__ (but
probably after checking for a complex instance).

See
http://mail.python.org/pipermail/python-dev/2002-June/024898.html
and my reply.
msg11036 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-06-06 07:14
Logged In: YES 
user_id=80475

This patch passes regression and fixes the bug:

class OS:
   def __complex__(self): return 1+2j
class NS(object):
   def __complex__(self): return 3+4j
print map(complex, [5, 6.6, 7+8j, 9L, OS(), NS(), "10+11j"])

Please confirm that I'm using the best way to detect a new-
style class instances with Py_TPFLAGS_HEAPTYPE.

Also, incidental to this bug, which error messages do you 
want for int, float, complex, and long:
-- object can't be converted to float
-- float() needs a string argument
-- float() needs a string or number argument

Right now, the messages are not consistent.  I prefer the 
third style to be used through-out.
msg11037 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-06-06 14:27
Logged In: YES 
user_id=6380

- It's better not to test for any kind of instances at all
-- just take the __complex__ attribute if it exists. That
will allow extension types to support complex conversion.

- Please add a unit test.

- I like the 3rd form best too.
msg11038 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-06-06 15:47
Logged In: YES 
user_id=80475

Done.

Committed as:
 complexobject.c 2.60
 abstract.c 2.102
 test_b1.py 1.47

Closing bug.
History
Date User Action Args
2022-04-10 16:05:23adminsetgithub: 36690
2002-06-03 05:17:30gvanrossumcreate