[Patches] [ python-Patches-1675423 ] Make PyComplex_AsCComplex use __complex__
SourceForge.net
noreply at sourceforge.net
Sat Mar 17 16:53:26 CET 2007
Patches item #1675423, was opened at 2007-03-07 03:03
Message generated for change (Comment added) made by marketdickinson
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1675423&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core (C code)
Group: Python 2.6
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Mark Dickinson (marketdickinson)
Assigned to: Georg Brandl (gbrandl)
Summary: Make PyComplex_AsCComplex use __complex__
Initial Comment:
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.
----------------------------------------------------------------------
>Comment By: Mark Dickinson (marketdickinson)
Date: 2007-03-17 15:53
Message:
Logged In: YES
user_id=703403
Originator: YES
File Added: complex_patch.patch
----------------------------------------------------------------------
Comment By: Georg Brandl (gbrandl)
Date: 2007-03-12 13:47
Message:
Logged In: YES
user_id=849994
Originator: NO
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.
----------------------------------------------------------------------
Comment By: Georg Brandl (gbrandl)
Date: 2007-03-12 13:38
Message:
Logged In: YES
user_id=849994
Originator: NO
I think it is the right thing to do.
Test cases would be welcome.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1675423&group_id=5470
More information about the Patches
mailing list