[New-bugs-announce] [issue16290] PyComplex_AsCComplex should allow __complex__ to return float.

Mark Dickinson report at bugs.python.org
Sat Oct 20 11:39:39 CEST 2012


New submission from Mark Dickinson:

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.

----------
assignee: mark.dickinson
components: Interpreter Core
messages: 173379
nosy: mark.dickinson
priority: normal
severity: normal
stage: needs patch
status: open
title: PyComplex_AsCComplex should allow __complex__ to return float.
type: enhancement
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16290>
_______________________________________


More information about the New-bugs-announce mailing list