Skip to content

gh-150868: Fix sum() deallocating a float subclass as an exact float - #151063

Open
eendebakpt wants to merge 2 commits into
python:mainfrom
eendebakpt:fix-sum-float-subclass-dealloc
Open

gh-150868: Fix sum() deallocating a float subclass as an exact float#151063
eendebakpt wants to merge 2 commits into
python:mainfrom
eendebakpt:fix-sum-float-subclass-dealloc

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

…float

The complex-total fast path in builtin_sum_impl() guarded with
PyFloat_Check() (which accepts subclasses) but freed the item with
_Py_DECREF_SPECIALIZED(item, _PyFloat_ExactDealloc), which assumes an
exact float. A float subclass instance reaching refcount 0 in that loop
aborts on an assertion in debug builds, and in release builds skips the
subclass tp_dealloc (type refcount leak, __del__ never runs, subclass
object pushed onto the float freelist).

Use PyFloat_CheckExact() instead, matching the float-total branch, so
subclass instances fall through to the generic PyNumber_Add() path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@StanFromIreland StanFromIreland changed the title gh-151060: Fix sum() deallocating a float subclass as an exact float gh-150868: Fix sum() deallocating a float subclass as an exact float Jun 7, 2026
@eendebakpt

Copy link
Copy Markdown
Contributor Author

There was already a PR opened earlier, lets give that a chance to be merged.

@skirpichev

Copy link
Copy Markdown
Member

I like this pr, it has a test.

@eendebakpt, could you extend this fix with this (and add tests too):
#150868 (comment)
?

…) loops

Per review: use PyLong_CheckExact with the specialized exact-int
deallocator in the float and complex fast-sum loops, so int subclasses
take the generic path.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@eendebakpt

Copy link
Copy Markdown
Contributor Author

@skirpichev I updated the branch with your suggestion. I think this will need backports.

@skirpichev skirpichev added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants