summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_capi
diff options
context:
space:
mode:
authorKumar Aditya <kumaraditya@python.org>2024-12-19 11:38:32 (GMT)
committerGitHub <noreply@github.com>2024-12-19 11:38:32 (GMT)
commit3c168f7f79d1da2323d35dcf88c2d3c8730e5df6 (patch)
treec6b670263fd219a38f1c343ec7684a820057bed4 /Lib/test/test_capi
parent46dc1ba9c6e8b95635fa27607d01d6108d8f677e (diff)
downloadcpython-3c168f7f79d1da2323d35dcf88c2d3c8730e5df6.zip
cpython-3c168f7f79d1da2323d35dcf88c2d3c8730e5df6.tar.gz
cpython-3c168f7f79d1da2323d35dcf88c2d3c8730e5df6.tar.bz2
gh-128013: fix data race in `PyUnicode_AsUTF8AndSize` on free-threading (#128021)
Diffstat (limited to 'Lib/test/test_capi')
-rw-r--r--Lib/test/test_capi/test_unicode.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/Lib/test/test_capi/test_unicode.py b/Lib/test/test_capi/test_unicode.py
index 65d8242..3408c10f 100644
--- a/Lib/test/test_capi/test_unicode.py
+++ b/Lib/test/test_capi/test_unicode.py
@@ -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):