Skip to content
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

Make PyComplex_AsCComplex use __complex__ #44670

Closed
mdickinson opened this issue Mar 7, 2007 · 5 comments
Closed

Make PyComplex_AsCComplex use __complex__ #44670

mdickinson opened this issue Mar 7, 2007 · 5 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@mdickinson
Copy link
Member

BPO 1675423
Nosy @birkenfeld, @mdickinson
Files
  • complex_patch: Patch to PyComplex_AsCComplex in complexobject.py to allow conversion to complex use complex
  • complex_patch.patch: Updated patch, including documentation and test cases
  • 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:

    assignee = 'https://github.com/birkenfeld'
    closed_at = <Date 2007-03-17.16:09:34.000>
    created_at = <Date 2007-03-07.03:03:07.000>
    labels = ['interpreter-core']
    title = 'Make PyComplex_AsCComplex use __complex__'
    updated_at = <Date 2007-03-17.16:09:34.000>
    user = 'https://github.com/mdickinson'

    bugs.python.org fields:

    activity = <Date 2007-03-17.16:09:34.000>
    actor = 'georg.brandl'
    assignee = 'georg.brandl'
    closed = True
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2007-03-07.03:03:07.000>
    creator = 'mark.dickinson'
    dependencies = []
    files = ['7829', '7830']
    hgrepos = []
    issue_num = 1675423
    keywords = ['patch']
    message_count = 5.0
    messages = ['52070', '52071', '52072', '52073', '52074']
    nosy_count = 2.0
    nosy_names = ['georg.brandl', 'mark.dickinson']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1675423'
    versions = ['Python 2.6']

    @mdickinson
    Copy link
    Member Author

    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.

    @mdickinson mdickinson added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Mar 7, 2007
    @mdickinson mdickinson added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Mar 7, 2007
    @birkenfeld
    Copy link
    Member

    I think it is the right thing to do.

    Test cases would be welcome.

    @birkenfeld
    Copy link
    Member

    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.

    @mdickinson
    Copy link
    Member Author

    File Added: complex_patch.patch

    @birkenfeld
    Copy link
    Member

    OK, thank you! Committed the patch, new tests and docs in rev. 54421.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants