Skip to content

sum() mishandles a float subclass when accumulating into a complex total #151060

@eendebakpt

Description

@eendebakpt

Bug report

Bug description:

The complex-total fast path in builtin_sum_impl (Python/bltinmodule.c:3049) guards with PyFloat_Check(item) (accepts subclasses) but frees the item with _Py_DECREF_SPECIALIZED(item, _PyFloat_ExactDealloc)`, which assumes an exact float.

Reproducer:

class F(float):
    def __del__(self):
        print("__del__ ran")

sum((F(x) for x in (2.0, 3.0)), 1j)   # generator so refcount hits 0

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15pre-release feature fixes, bugs and security fixes3.16new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions