Skip to content

[3.15] gh-150868: Fix sum() deallocating a float subclass as an exact float (GH-151063) - #161048

Merged
eendebakpt merged 1 commit into
python:3.15from
miss-islington:backport-47c249a-3.15
Nov 1, 2026
Merged

eendebakpt merged 1 commit into
python:3.15from
miss-islington:backport-47c249a-3.15

Conversation

@miss-islington

Copy link
Copy Markdown
Contributor

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

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


(cherry picked from commit 47c249a)

Co-authored-by: Pieter Eendebak pieter.eendebak@gmail.com
Co-authored-by: Claude Opus 4.8 (1M context) noreply@anthropic.com

…float (pythonGH-151063)

* pythongh-151060: Fix sum() deallocating a float subclass as an exact 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>

* pythongh-150868: Also use exact-type checks for ints in the fast sum() 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>

---------
(cherry picked from commit 47c249a)

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@eendebakpt
eendebakpt merged commit c391d1a into python:3.15 Nov 1, 2026
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants