summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2024-05-22 22:26:58 (GMT)
committerGitHub <noreply@github.com>2024-05-22 22:26:58 (GMT)
commit0d5fe2c7b417856ec62e46339aa769bce8dd5f12 (patch)
treec9048346faada0a200811126ddf892f1687a7033 /Lib/test
parent7eb59cd95b09ede8c7524e8f71fbea5623715f7f (diff)
downloadcpython-0d5fe2c7b417856ec62e46339aa769bce8dd5f12.zip
cpython-0d5fe2c7b417856ec62e46339aa769bce8dd5f12.tar.gz
cpython-0d5fe2c7b417856ec62e46339aa769bce8dd5f12.tar.bz2
[3.12] gh-119213: Be More Careful About _PyArg_Parser.kwtuple Across Interpreters (gh-119331) (gh-119425)
_PyArg_Parser holds static global data generated for modules by Argument Clinic. The _PyArg_Parser.kwtuple field is a tuple object, even though it's stored within a static global. In some cases the tuple is statically allocated and thus it's okay that it gets shared by multiple interpreters. However, in other cases the tuple is set lazily, allocated from the heap using the active interprepreter at the point the tuple is needed. This is a problem once that interpreter is destroyed since _PyArg_Parser.kwtuple becomes at dangling pointer, leading to crashes. It isn't a problem if the tuple is allocated under the main interpreter, since its lifetime is bound to the lifetime of the runtime. The solution here is to temporarily switch to the main interpreter. The alternative would be to always statically allocate the tuple. This change also fixes a bug where only the most recent parser was added to the global linked list. (cherry picked from commit 81865002aee8eaaeb3c7e402f86183afa6de77bf)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_capi/test_getargs.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_capi/test_getargs.py b/Lib/test/test_capi/test_getargs.py
index 3d8768b..69bd0f4 100644
--- a/Lib/test/test_capi/test_getargs.py
+++ b/Lib/test/test_capi/test_getargs.py
@@ -4,11 +4,17 @@ import string
import sys
from test import support
from test.support import import_helper
+from test.support import script_helper
from test.support import warnings_helper
# Skip this test if the _testcapi module isn't available.
_testcapi = import_helper.import_module('_testcapi')
from _testcapi import getargs_keywords, getargs_keyword_only
+try:
+ import _testinternalcapi
+except ImportError:
+ _testinternalcapi = NULL
+
# > How about the following counterproposal. This also changes some of
# > the other format codes to be a little more regular.
# >
@@ -1369,6 +1375,33 @@ class Test_testcapi(unittest.TestCase):
"argument 1 must be sequence of length 1, not 0"):
parse(((),), {}, '(' + f + ')', ['a'])
+ @unittest.skipIf(_testinternalcapi is None, 'needs _testinternalcapi')
+ def test_gh_119213(self):
+ rc, out, err = script_helper.assert_python_ok("-c", """if True:
+ from test import support
+ script = '''if True:
+ import _testinternalcapi
+ _testinternalcapi.gh_119213_getargs(spam='eggs')
+ '''
+ config = dict(
+ allow_fork=False,
+ allow_exec=False,
+ allow_threads=True,
+ allow_daemon_threads=False,
+ use_main_obmalloc=False,
+ gil=2,
+ check_multi_interp_extensions=True,
+ )
+ rc = support.run_in_subinterp_with_config(script, **config)
+ assert rc == 0
+
+ # The crash is different if the interpreter was not destroyed first.
+ #interpid = _testinternalcapi.create_interpreter()
+ #rc = _testinternalcapi.exec_interpreter(interpid, script)
+ #assert rc == 0
+ """)
+ self.assertEqual(rc, 0)
+
if __name__ == "__main__":
unittest.main()