Skip to content

gh-116738: Make bisect module thread-safe #137021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Lib/test/test_free_threading/test_bisect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unittest

from itertools import cycle

from test.support import import_helper, threading_helper, subTests
from test.support.threading_helper import run_concurrently

bisect = import_helper.import_module("bisect")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this just be import bisect?

If I understand correctly, import_helper.import_module() handles missing optional modules but that doesn't seem to be relevant here.



NTHREADS = 10


@threading_helper.requires_working_threading()
class TestBisect(unittest.TestCase):
@subTests("insort_func", [bisect.insort_left, bisect.insort_right])
def test_racing_insort(self, insort_func):
lst = list(range(10))
insort_items_iter = cycle(list(range(11)))

def worker(lst, insort_items_iter):
for _ in range(10):
insort_item = next(insort_items_iter)
insort_func(lst, insort_item)

run_concurrently(
worker_func=worker,
nthreads=NTHREADS,
args=(lst, insort_items_iter),
)

self.assertTrue(self.is_sorted_ascending(lst))

@staticmethod
def is_sorted_ascending(lst):
"""
Check if the list is sorted in ascending order (non-decreasing).
"""
return all(lst[i - 1] <= lst[i] for i in range(1, len(lst)))


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make functions in :mod:`bisect` thread-safe on the :term:`free threaded
<free threading>` build.
6 changes: 4 additions & 2 deletions Modules/_bisectmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ _bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x,
}

/*[clinic input]
@critical_section a
_bisect.insort_right

a: object
Expand All @@ -201,7 +202,7 @@ A custom key function can be supplied to customize the sort order.
static PyObject *
_bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x,
Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
/*[clinic end generated code: output=ac3bf26d07aedda2 input=f60777d2b6ddb239]*/
/*[clinic end generated code: output=ac3bf26d07aedda2 input=93bd855be0c1add7]*/
{
PyObject *result, *key_x;
Py_ssize_t index;
Expand Down Expand Up @@ -365,6 +366,7 @@ _bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x,


/*[clinic input]
@critical_section a
_bisect.insort_left

a: object
Expand All @@ -387,7 +389,7 @@ A custom key function can be supplied to customize the sort order.
static PyObject *
_bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x,
Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
/*[clinic end generated code: output=b1d33e5e7ffff11e input=0a700a82edbd472c]*/
/*[clinic end generated code: output=b1d33e5e7ffff11e input=93e06f5a323e09ef]*/
{
PyObject *result, *key_x;
Py_ssize_t index;
Expand Down
7 changes: 6 additions & 1 deletion Modules/clinic/_bisectmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading