gh-128013: fix data race in PyUnicode_AsUTF8AndSize on free-threading (#128021)

This commit is contained in:
Kumar Aditya 2024-12-19 17:08:32 +05:30 committed by GitHub
parent 46dc1ba9c6
commit 3c168f7f79
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 51 additions and 18 deletions

View file

@ -1,7 +1,7 @@
import unittest
import sys
from test import support
from test.support import import_helper
from test.support import threading_helper
try:
import _testcapi
@ -1005,6 +1005,24 @@ class CAPITest(unittest.TestCase):
self.assertRaises(TypeError, unicode_asutf8, [], 0)
# CRASHES unicode_asutf8(NULL, 0)
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
@threading_helper.requires_working_threading()
def test_asutf8_race(self):
"""Test that there's no race condition in PyUnicode_AsUTF8()"""
unicode_asutf8 = _testcapi.unicode_asutf8
from threading import Thread
data = "😊"
def worker():
for _ in range(1000):
self.assertEqual(unicode_asutf8(data, 5), b'\xf0\x9f\x98\x8a\0')
threads = [Thread(target=worker) for _ in range(10)]
with threading_helper.start_threads(threads):
pass
@support.cpython_only
@unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module')
def test_asutf8andsize(self):