mirror of
https://github.com/python/cpython.git
synced 2026-06-28 11:50:50 +00:00
gh-151218: Replace sys.flags in PyConfig_Set() (GH-151402)
PyConfig_Set() and sys.set_int_max_str_digits() now replace
sys.flags (create a new object), instead of modifying sys.flags in-place.
Modifying sys.flags in-place can lead to data races when multiple
threads are reading or writing sys.flags in parallel.
Use _Py_atomic functions to get and set max_str_digits members.
(cherry picked from commit b16d23fc9f)
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
28 lines
913 B
Python
28 lines
913 B
Python
import sys
|
|
import unittest
|
|
from test.support import threading_helper
|
|
|
|
|
|
class SysModuleTest(unittest.TestCase):
|
|
def test_int_max_str_digits_thread(self):
|
|
# gh-151218: Check that it's safe to call get_int_max_str_digits() and
|
|
# set_int_max_str_digits() in parallel. Previously, this test triggered
|
|
# warnings in TSan on a free threaded build.
|
|
|
|
old_limit = sys.get_int_max_str_digits()
|
|
self.addCleanup(sys.set_int_max_str_digits, old_limit)
|
|
|
|
def worker(worker_id):
|
|
if not worker_id:
|
|
for i in range (20_000):
|
|
sys.get_int_max_str_digits()
|
|
else:
|
|
for i in range (20_000):
|
|
sys.set_int_max_str_digits(4300 + (i & 7))
|
|
|
|
workers = [lambda: worker(i) for i in range(5)]
|
|
threading_helper.run_concurrently(workers)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|