Crash report
What happened?
_testcapi.sequence_fast_get_item() returns the value from
PySequence_Fast_GET_ITEM() directly.
PySequence_Fast_GET_ITEM() returns a borrowed reference, but the value
returned by the _testcapi function is treated as an owned reference by
the Python caller. As a result, the reference count is not incremented.
I reproduced this on a debug build of CPython 3.15.0rc2.
import sys
import _testcapi
obj = object()
lst = [obj]
before = sys.getrefcount(obj)
result = _testcapi.sequence_fast_get_item(lst, 0)
after = sys.getrefcount(obj)
print("before:", before)
print("after :", after)
print("delta :", after - before)
print("same :", result is obj)
Affected behaviour
before: 3
after : 3
delta : 0
same : True
The returned object does not acquire a new reference.
When the returned object is subsequently released, the debug build can abort with:
_Py_NegativeRefcount: Assertion failed: object has negative ref count
Fatal Python error: _PyObject_AssertFailed
Aborted (core dumped)
The expected behaviour is for the returned object to hold a new reference.
Changing the return statement to:
return Py_NewRef(PySequence_Fast_GET_ITEM(obj, index));
makes the reference count increase by one:
before: 3
after : 4
delta : 1
same : True
I also added a regression test in Lib/test/test_capi/test_abstract.py and verified:
./python -m test test_capi.test_abstract -v
Ran 53 tests
OK
CPython versions tested on:
3.15
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
3.15.0rc2 (tags/v3.15.0rc2:435c9e5a798, Sep 2 2026, 13:09:27) [GCC 13.3.0]
Linked PRs
Crash report
What happened?
_testcapi.sequence_fast_get_item()returns the value fromPySequence_Fast_GET_ITEM()directly.PySequence_Fast_GET_ITEM()returns a borrowed reference, but the valuereturned by the
_testcapifunction is treated as an owned reference bythe Python caller. As a result, the reference count is not incremented.
I reproduced this on a debug build of CPython 3.15.0rc2.
Affected behaviour
The returned object does not acquire a new reference.
When the returned object is subsequently released, the debug build can abort with:
The expected behaviour is for the returned object to hold a new reference.
Changing the return statement to:
makes the reference count increase by one:
I also added a regression test in Lib/test/test_capi/test_abstract.py and verified:
CPython versions tested on:
3.15
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
3.15.0rc2 (tags/v3.15.0rc2:435c9e5a798, Sep 2 2026, 13:09:27) [GCC 13.3.0]
Linked PRs