@@ -11,7 +11,7 @@ annotated by François Pinard, and converted to C by Raymond Hettinger.
1111#endif
1212
1313#include "Python.h"
14- #include "pycore_list.h" // _PyList_ITEMS()
14+ #include "pycore_list.h" // _PyList_ITEMS(), _PyList_AppendTakeRef()
1515
1616#include "clinic/_heapqmodule.c.h"
1717
@@ -131,7 +131,9 @@ static PyObject *
131131_heapq_heappush_impl (PyObject * module , PyObject * heap , PyObject * item )
132132/*[clinic end generated code: output=912c094f47663935 input=f7a4f03ef8d52e67]*/
133133{
134- if (PyList_Append (heap , item ))
134+ // In a free-threaded build, the heap is locked at this point.
135+ // Therefore, calling _PyList_AppendTakeRef() is safe and no overhead.
136+ if (_PyList_AppendTakeRef ((PyListObject * )heap , Py_NewRef (item )))
135137 return NULL ;
136138
137139 if (siftdown ((PyListObject * )heap , 0 , PyList_GET_SIZE (heap )- 1 ))
@@ -500,7 +502,9 @@ static PyObject *
500502_heapq_heappush_max_impl (PyObject * module , PyObject * heap , PyObject * item )
501503/*[clinic end generated code: output=c869d5f9deb08277 input=c437e3d1ff8dcb70]*/
502504{
503- if (PyList_Append (heap , item )) {
505+ // In a free-threaded build, the heap is locked at this point.
506+ // Therefore, calling _PyList_AppendTakeRef() is safe and no overhead.
507+ if (_PyList_AppendTakeRef ((PyListObject * )heap , Py_NewRef (item ))) {
504508 return NULL ;
505509 }
506510
0 commit comments