[Python-checkins] bpo-47182: Fix crash by named unicode characters after interpreter reinitialization (GH-32212)

miss-islington webhook-mailer at python.org
Thu Mar 31 11:15:02 EDT 2022


https://github.com/python/cpython/commit/44e915028d75f7cef141aa1aada962465a5907d6
commit: 44e915028d75f7cef141aa1aada962465a5907d6
branch: main
author: Christian Heimes <christian at python.org>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-03-31T08:14:50-07:00
summary:

bpo-47182: Fix crash by named unicode characters after interpreter reinitialization (GH-32212)



Automerge-Triggered-By: GH:tiran

files:
A Misc/NEWS.d/next/Core and Builtins/2022-03-31-15-37-02.bpo-47182.e_4SsC.rst
M Lib/test/test_embed.py
M Objects/unicodeobject.c

diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py
index f0c88de68e89e..7e5e4c144851e 100644
--- a/Lib/test/test_embed.py
+++ b/Lib/test/test_embed.py
@@ -343,6 +343,11 @@ def test_finalize_structseq(self):
         out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
         self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS)
 
+    def test_ucnhash_capi_reset(self):
+        # bpo-47182: unicodeobject.c:ucnhash_capi was not reset on shutdown.
+        code = "print('\\N{digit nine}')"
+        out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
+        self.assertEqual(out, '9\n' * INIT_LOOPS)
 
 class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
     maxDiff = 4096
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-03-31-15-37-02.bpo-47182.e_4SsC.rst b/Misc/NEWS.d/next/Core and Builtins/2022-03-31-15-37-02.bpo-47182.e_4SsC.rst
new file mode 100644
index 0000000000000..08036bc680933
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-03-31-15-37-02.bpo-47182.e_4SsC.rst	
@@ -0,0 +1,2 @@
+Fix a crash when using a named unicode character like ``"\N{digit nine}"``
+after the main interpreter has been initialized a second time.
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 5a1d2c0630167..2d4096397784f 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -16085,6 +16085,9 @@ _PyUnicode_Fini(PyInterpreterState *interp)
     if (_Py_IsMainInterpreter(interp)) {
         // _PyUnicode_ClearInterned() must be called before _PyUnicode_Fini()
         assert(interned == NULL);
+        // bpo-47182: force a unicodedata CAPI capsule re-import on
+        // subsequent initialization of main interpreter.
+        ucnhash_capi = NULL;
     }
 
     _PyUnicode_FiniEncodings(&state->fs_codec);



More information about the Python-checkins mailing list