]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-122334: Fix crash when importing ssl after re-initialization (#122481)
authorneonene <53406459+neonene@users.noreply.github.com>
Fri, 2 Aug 2024 13:36:20 +0000 (22:36 +0900)
committerGitHub <noreply@github.com>
Fri, 2 Aug 2024 13:36:20 +0000 (19:06 +0530)
* Fix crash when importing ssl after re-initialization

Lib/test/test_embed.py
Misc/NEWS.d/next/Library/2024-07-30-21-29-30.gh-issue-122334.LeoE1x.rst [new file with mode: 0644]
Python/getargs.c

index 9602f1a92c37c88e22e33865dcdc430b803f2fda..ab112d6be85b46bf40801db419cf8146164958cd 100644 (file)
@@ -461,6 +461,25 @@ class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
             self.assertEqual(result, {})
         self.assertEqual(out, '')
 
+    def test_getargs_reset_static_parser(self):
+        # Test _PyArg_Parser initializations via _PyArg_UnpackKeywords()
+        # https://github.com/python/cpython/issues/122334
+        code = textwrap.dedent("""
+            import _ssl
+            _ssl.txt2obj(txt='1.3')
+            print('1')
+
+            import _queue
+            _queue.SimpleQueue().put_nowait(item=None)
+            print('2')
+
+            import _zoneinfo
+            _zoneinfo.ZoneInfo.clear_cache(only_keys=['Foo/Bar'])
+            print('3')
+        """)
+        out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
+        self.assertEqual(out, '1\n2\n3\n' * INIT_LOOPS)
+
 
 @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
 class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Library/2024-07-30-21-29-30.gh-issue-122334.LeoE1x.rst b/Misc/NEWS.d/next/Library/2024-07-30-21-29-30.gh-issue-122334.LeoE1x.rst
new file mode 100644 (file)
index 0000000..cef801c
--- /dev/null
@@ -0,0 +1 @@
+Fix crash when importing :mod:`ssl` after the main interpreter restarts.
index b96ce3a22dae7c457f059a365c449dbfcd6a9aa1..ec2eeb15c832c320ad9083eeee2e2c7a22700afc 100644 (file)
@@ -2030,6 +2030,19 @@ parser_clear(struct _PyArg_Parser *parser)
     if (parser->is_kwtuple_owned) {
         Py_CLEAR(parser->kwtuple);
     }
+
+    if (parser->format) {
+        parser->fname = NULL;
+    }
+    else {
+        assert(parser->fname != NULL);
+    }
+    parser->custom_msg = NULL;
+    parser->pos = 0;
+    parser->min = 0;
+    parser->max = 0;
+    parser->is_kwtuple_owned = 0;
+    parser->once.v = 0;
 }
 
 static PyObject*