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: Make PyComplex_AsCComplex use __complex__
Type: Stage:
Components: Interpreter Core Versions: Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, mark.dickinson
Priority: normal Keywords: patch

Created on 2007-03-07 03:03 by mark.dickinson, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
complex_patch mark.dickinson, 2007-03-07 03:03 Patch to PyComplex_AsCComplex in complexobject.py to allow conversion to complex use __complex__
complex_patch.patch mark.dickinson, 2007-03-17 15:53 Updated patch, including documentation and test cases
Messages (5)
msg52070 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-03-07 03:03
The functions in the math module have the (pleasantly) surprising and 
apparently undocumented property that they'll accept not just floats, but any Python object having a float method:

>>> class test1(object):
...     def __float__(self):
...             return 3. 
... 
>>> from math import sqrt
>>> sqrt(test1())
1.7320508075688772

Based on this, one might expect the functions in the complex math module cmath to have the same property with respect to __complex__.
But this isn't so:

>>> class test2(object):
...     def __complex__(self):
...             return -3 + 0j
... 
>>> from cmath import sqrt as csqrt
>>> csqrt(test2())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required

The real surprise is that the cmath functions *will* call the __float__ method, if it's available:

>>> csqrt(test1())
(1.7320508075688772+0j)

This patch expands the PyComplex_AsCComplex method so that it looks for a __complex__ method before looking for the __float__ method.  This `fixes' the above behaviour.

Should it be a documented feature that the math functions will make use of __float__?  If so, and if the patched behaviour seems desirable, I'll add suitable tests to test_math and test_cmath.
msg52071 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-12 13:38
I think it is the right thing to do.

Test cases would be welcome.
msg52072 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-12 13:47
Please include both old-style and new-style classes in the test case, and for each one test if the check for a complex return from __complex__ works.
msg52073 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-03-17 15:53
File Added: complex_patch.patch
msg52074 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-17 16:09
OK, thank you! Committed the patch, new tests and docs in rev. 54421.
History
Date User Action Args
2022-04-11 14:56:23adminsetgithub: 44670
2007-03-07 03:03:07mark.dickinsoncreate