Skip to content

Commit b3a3843

Browse files
authored
gh-116738: Make _suggestions module thread-safe (gh-140321)
1 parent 29b38b7 commit b3a3843

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
3+
from test.support import import_helper, threading_helper
4+
from test.support.threading_helper import run_concurrently
5+
6+
suggestions = import_helper.import_module("_suggestions")
7+
8+
NTHREADS = 10
9+
10+
11+
@threading_helper.requires_working_threading()
12+
class SuggestionsTests(unittest.TestCase):
13+
def test_generate_suggestions(self):
14+
candidates = [str(i) for i in range(100)]
15+
16+
def worker():
17+
_ = suggestions._generate_suggestions(candidates, "42")
18+
candidates.clear()
19+
20+
run_concurrently(worker_func=worker, nthreads=NTHREADS)
21+
22+
23+
if __name__ == "__main__":
24+
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make _suggestions module thread-safe on the :term:`free threaded <free
2+
threading>` build.

Modules/_suggestions.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module _suggestions
88
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e58d81fafad5637b]*/
99

1010
/*[clinic input]
11+
@critical_section candidates
1112
_suggestions._generate_suggestions
1213
candidates: object
1314
item: unicode
@@ -18,7 +19,7 @@ Returns the candidate in candidates that's closest to item
1819
static PyObject *
1920
_suggestions__generate_suggestions_impl(PyObject *module,
2021
PyObject *candidates, PyObject *item)
21-
/*[clinic end generated code: output=79be7b653ae5e7ca input=ba2a8dddc654e33a]*/
22+
/*[clinic end generated code: output=79be7b653ae5e7ca input=92861a6c9bd8f667]*/
2223
{
2324
// Check if dir is a list
2425
if (!PyList_CheckExact(candidates)) {
@@ -29,7 +30,7 @@ _suggestions__generate_suggestions_impl(PyObject *module,
2930
// Check if all elements in the list are Unicode
3031
Py_ssize_t size = PyList_Size(candidates);
3132
for (Py_ssize_t i = 0; i < size; ++i) {
32-
PyObject *elem = PyList_GetItem(candidates, i);
33+
PyObject *elem = PyList_GET_ITEM(candidates, i);
3334
if (!PyUnicode_Check(elem)) {
3435
PyErr_SetString(PyExc_TypeError, "all elements in 'candidates' must be strings");
3536
return NULL;

Modules/clinic/_suggestions.c.h

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)