Skip to content

Commit 9fc1c99

Browse files
authored
gh-122334: Fix crash when importing ssl after re-initialization (#122481)
* Fix crash when importing ssl after re-initialization
1 parent b5e6fb3 commit 9fc1c99

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Lib/test/test_embed.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,25 @@ def add(cls, slot, own):
461461
self.assertEqual(result, {})
462462
self.assertEqual(out, '')
463463

464+
def test_getargs_reset_static_parser(self):
465+
# Test _PyArg_Parser initializations via _PyArg_UnpackKeywords()
466+
# https://github.com/python/cpython/issues/122334
467+
code = textwrap.dedent("""
468+
import _ssl
469+
_ssl.txt2obj(txt='1.3')
470+
print('1')
471+
472+
import _queue
473+
_queue.SimpleQueue().put_nowait(item=None)
474+
print('2')
475+
476+
import _zoneinfo
477+
_zoneinfo.ZoneInfo.clear_cache(only_keys=['Foo/Bar'])
478+
print('3')
479+
""")
480+
out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
481+
self.assertEqual(out, '1\n2\n3\n' * INIT_LOOPS)
482+
464483

465484
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
466485
class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix crash when importing :mod:`ssl` after the main interpreter restarts.

Python/getargs.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,19 @@ parser_clear(struct _PyArg_Parser *parser)
20302030
if (parser->is_kwtuple_owned) {
20312031
Py_CLEAR(parser->kwtuple);
20322032
}
2033+
2034+
if (parser->format) {
2035+
parser->fname = NULL;
2036+
}
2037+
else {
2038+
assert(parser->fname != NULL);
2039+
}
2040+
parser->custom_msg = NULL;
2041+
parser->pos = 0;
2042+
parser->min = 0;
2043+
parser->max = 0;
2044+
parser->is_kwtuple_owned = 0;
2045+
parser->once.v = 0;
20332046
}
20342047

20352048
static PyObject*

0 commit comments

Comments
 (0)