-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
GH-116738: document thread-safety of bisect #136555
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
Merged
nascheme
merged 7 commits into
python:main
from
nascheme:gh-116738-bisect-threadsafe-doc
Jul 30, 2025
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5e4d174
GH-116738: document thread-safety of bisect
nascheme 4b7f113
Add free-threaded test for bisect.
nascheme e405cf8
Adjust indent of "note" block.
nascheme def0f47
Merge 'origin/main' into gh-116738-bisect-threadsafe-doc
nascheme 87a90f3
Use `threading_helper.run_concurrently`.
nascheme b74a790
Remove unused imports from test.
nascheme 229a531
Merge 'origin/main' into gh-116738-bisect-threadsafe-doc
nascheme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import unittest | ||
from test.support import import_helper, threading_helper | ||
from threading import Thread, Barrier | ||
import random | ||
|
||
py_bisect = import_helper.import_fresh_module('bisect', blocked=['_bisect']) | ||
c_bisect = import_helper.import_fresh_module('bisect', fresh=['_bisect']) | ||
|
||
|
||
NTHREADS = 4 | ||
OBJECT_COUNT = 500 | ||
|
||
|
||
class TestBase: | ||
def do_racing_insort(self, insert_method): | ||
def insert(data): | ||
for _ in range(OBJECT_COUNT): | ||
x = random.randint(-OBJECT_COUNT, OBJECT_COUNT) | ||
insert_method(data, x) | ||
|
||
data = list(range(OBJECT_COUNT)) | ||
self.run_concurrently( | ||
worker_func=insert, args=(data,), nthreads=NTHREADS | ||
) | ||
if False: | ||
# These functions are not thread-safe and so the list can become | ||
# unsorted. However, we don't want Python to crash if these | ||
# functions are used concurrently on the same sequence. This | ||
# should also not produce any TSAN warnings. | ||
self.assertTrue(self.is_sorted_ascending(data)) | ||
|
||
def test_racing_insert_right(self): | ||
self.do_racing_insort(self.mod.insort_right) | ||
|
||
def test_racing_insert_left(self): | ||
self.do_racing_insort(self.mod.insort_left) | ||
|
||
@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))) | ||
|
||
def run_concurrently(self, worker_func, args, nthreads): | ||
nascheme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Run the worker function concurrently in multiple threads. | ||
""" | ||
barrier = Barrier(nthreads) | ||
|
||
def wrapper_func(*args): | ||
# Wait for all threads to reach this point before proceeding. | ||
barrier.wait() | ||
worker_func(*args) | ||
|
||
with threading_helper.catch_threading_exception() as cm: | ||
workers = ( | ||
Thread(target=wrapper_func, args=args) | ||
for _ in range(nthreads) | ||
) | ||
with threading_helper.start_threads(workers): | ||
pass | ||
|
||
# Worker threads should not raise any exceptions | ||
self.assertIsNone(cm.exc_value) | ||
|
||
|
||
@threading_helper.requires_working_threading() | ||
class TestPyBisect(unittest.TestCase, TestBase): | ||
mod = py_bisect | ||
|
||
|
||
@threading_helper.requires_working_threading() | ||
class TestCBisect(unittest.TestCase, TestBase): | ||
mod = c_bisect | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would fix lint CI