-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PyComplex_AsCComplex should allow __complex__ to return float. #60494
Comments
See thread starting at http://mail.python.org/pipermail/python-dev/2012-October/122241.html The cmath module functions don't accept custom types that define __complex__, but whose __complex__ method returns a float rather than a complex number: Python 3.4.0a0 (default:53a7c2226a2b+, Oct 19 2012, 12:16:36)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
... def __complex__(self): return 42.0
...
>>> import cmath
>>> cmath.sqrt(A())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __complex__ should return a complex object In contrast, the complex constructor is happy to accept such objects. The root cause is that PyArg_ParseTuple's 'D' format calls PyComplex_AsCComplex (in Objects/complexobject.c), which refuses to accept a __complex__ result of type float. |
Here's a patch |
Patch LGTM. Is there a new feature, not a bugfix? If yes, then I expect some documentation changes. |
There's an ongoing argument about that on the python-dev thread. :-) But yes, I think this needs a doc update---even if it's considered a bugfix, the current docs could be clarified. http://docs.python.org/dev/reference/datamodel.html?highlight=__complex__#object.\_\_complex__ says "Should return a value of the appropriate type." My interpretation of that is that __int__ should return an int, __float__ a float, __complex__ a complex number; with this change, that interpretation no longer works. I'll update the patch (and add a Misc/NEWS entry, too). Thanks! |
It's a reasonable interpretation. Changing it can be confusing. On the other hand, int and float look like specialized subclasses of complex |
I'd rather not go that far, at least in this issue: we'd also end up changing __float__ to allow it to return an int, in that case. If we're considering that, the discussion should go back to python-dev / python-ideas. One difference is that int -> float is a lossy conversion that has to care about details like rounding and overflow. float -> complex in contrast is much simpler. |
I've been convinced by the python-dev discussion that Antonio was right: it's complex_new that's in error rather than the cmath functions. Even so, it would be unsafe to change the behaviour in the maintenance releases. Attaching a new patch that disallows 'float'-type returns from the __complex__ method. |
Mark, for the sake of keeping a reference in this bug entry, could you possibly post the URL of the archived python-dev thread? Thanks. |
Jesús: see the first message. |
New changeset 399e59ad0a70 by Mark Dickinson in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: