Skip to content

Commit 41d145a

Browse files
committed
gh-116738: Address the review comments
1 parent 68f3a26 commit 41d145a

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

Lib/test/test_free_threading/test_heapq.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def create_heap(size: int, heap_kind: HeapKind) -> list[int]:
241241
@staticmethod
242242
def create_random_list(a: int, b: int, size: int) -> list[int]:
243243
"""
244-
Create a random list where elements are in the range a <= elem <= b
244+
Create a list of random numbers between a and b (inclusive).
245245
"""
246246
return [randint(-a, b) for _ in range(size)]
247247

Modules/_heapqmodule.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)