Py_NewRef(item) passed inline during deque_copy. If append to new deque fails (OOM at block boundary), the ref is orphaned → dealloc corruption.
import _testcapi
from collections import deque
for n in range(1, 80):
d = deque(range(200))
_testcapi.set_nomemory(n, 0)
try:
d2 = d.copy()
_testcapi.remove_mem_hooks()
break
except MemoryError:
_testcapi.remove_mem_hooks()
except:
_testcapi.remove_mem_hooks()
# Fatal: _Py_Dealloc: Deallocator of type 'collections.deque' cleared