Skip to content

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

Open
miss-islington wants to merge 1 commit into
python:3.14from
miss-islington:backport-47c249a-3.14
Open

miss-islington wants to merge 1 commit into
python:3.14from
miss-islington:backport-47c249a-3.14

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 47c249adf3bc2ae3264a7373f7f1f5e7c4d84955)

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants