-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
The function `operator.methodcaller` was not thread-safe since the additional of the vectorcall method in gh-89013. In the free threading build the issue is easy to trigger, for the normal build harder. This makes the `methodcaller` safe by: * Replacing the lazy initialization with initialization in the constructor. * Using a stack allocated space for the vectorcall arguments and falling back to `tp_call` for calls with more than 8 arguments.
- Loading branch information
1 parent
5a23994
commit b0f278f
Showing
4 changed files
with
131 additions
and
96 deletions.
There are no files selected for viewing
This file contains 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,33 @@ | ||
import unittest | ||
from threading import Thread | ||
from test.support import threading_helper | ||
from operator import methodcaller | ||
|
||
|
||
class TestMethodcaller(unittest.TestCase): | ||
def test_methodcaller_threading(self): | ||
number_of_threads = 10 | ||
size = 4_000 | ||
|
||
mc = methodcaller("append", 2) | ||
|
||
def work(mc, l, ii): | ||
for _ in range(ii): | ||
mc(l) | ||
|
||
worker_threads = [] | ||
lists = [] | ||
for ii in range(number_of_threads): | ||
l = [] | ||
lists.append(l) | ||
worker_threads.append(Thread(target=work, args=[mc, l, size])) | ||
for t in worker_threads: | ||
t.start() | ||
for t in worker_threads: | ||
t.join() | ||
for l in lists: | ||
assert len(l) == size | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains 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
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2024-12-01-22-28-41.gh-issue-127065.tFpRer.rst
This file contains 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 @@ | ||
Make :func:`operator.methodcaller` thread-safe and re-entrant safe. |
This file contains 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