Skip to content

Commit b20893b

Browse files
[3.13] gh-122334: Fix crash when importing ssl after re-initialization (GH-122481) (#122614)
gh-122334: Fix crash when importing ssl after re-initialization (GH-122481) * Fix crash when importing ssl after re-initialization (cherry picked from commit 9fc1c99) Co-authored-by: neonene <[email protected]>
1 parent c1efeb3 commit b20893b

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
@@ -460,6 +460,25 @@ def add(cls, slot, own):
460460
self.assertEqual(result, {})
461461
self.assertEqual(out, '')
462462

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

464483
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
465484
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
@@ -2004,6 +2004,19 @@ parser_clear(struct _PyArg_Parser *parser)
20042004
if (parser->is_kwtuple_owned) {
20052005
Py_CLEAR(parser->kwtuple);
20062006
}
2007+
2008+
if (parser->format) {
2009+
parser->fname = NULL;
2010+
}
2011+
else {
2012+
assert(parser->fname != NULL);
2013+
}
2014+
parser->custom_msg = NULL;
2015+
parser->pos = 0;
2016+
parser->min = 0;
2017+
parser->max = 0;
2018+
parser->is_kwtuple_owned = 0;
2019+
parser->once.v = 0;
20072020
}
20082021

20092022
static PyObject*

0 commit comments

Comments
 (0)