https://github.com/python/cpython/commit/22f0730d40c9534672f745f35cf876ad63d450bb
commit: 22f0730d40c9534672f745f35cf876ad63d450bb
branch: main
author: Neil Schemenauer <nas-git...@arctrix.com>
committer: nascheme <nas-git...@arctrix.com>
date: 2025-04-28T12:54:55-07:00
summary:

gh-122320: Limit dict key versions used by test_opcache. (gh-132961)

The `test_load_global_module()` test consumes a lot of dict key versions.
Skip the test if we have consumed half of the available versions that can be
used for the "load global" cache.

files:
M Lib/test/test_opcache.py
M Modules/_testinternalcapi.c
M Modules/clinic/_testinternalcapi.c.h

diff --git a/Lib/test/test_opcache.py b/Lib/test/test_opcache.py
index ac132465b227ad..21d7e62833c061 100644
--- a/Lib/test/test_opcache.py
+++ b/Lib/test/test_opcache.py
@@ -16,6 +16,16 @@
 _testinternalcapi = import_module("_testinternalcapi")
 
 
+def have_dict_key_versions():
+    # max version value that can be stored in the load global cache. This is
+    # determined by the type of module_keys_version and builtin_keys_version
+    # in _PyLoadGlobalCache, uint16_t.
+    max_version = 1<<16
+    # use a wide safety margin (use only half of what's available)
+    limit = max_version // 2
+    return _testinternalcapi.get_next_dict_keys_version() < limit
+
+
 class TestBase(unittest.TestCase):
     def assert_specialized(self, f, opname):
         instructions = dis.get_instructions(f, adaptive=True)
@@ -1029,6 +1039,8 @@ def write(items):
 
     @requires_specialization_ft
     def test_load_global_module(self):
+        if not have_dict_key_versions():
+            raise unittest.SkipTest("Low on dict key versions")
         def get_items():
             items = []
             for _ in range(self.ITEMS):
diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c
index 99dca9f77df6be..353cb630513abc 100644
--- a/Modules/_testinternalcapi.c
+++ b/Modules/_testinternalcapi.c
@@ -1985,6 +1985,18 @@ gh_119213_getargs_impl(PyObject *module, PyObject *spam)
     return Py_NewRef(spam);
 }
 
+/*[clinic input]
+get_next_dict_keys_version
+[clinic start generated code]*/
+
+static PyObject *
+get_next_dict_keys_version_impl(PyObject *module)
+/*[clinic end generated code: output=e5405a509cf9d423 input=bd1cee7c6b9d3a3c]*/
+{
+    PyInterpreterState *interp = _PyInterpreterState_GET();
+    uint32_t keys_version = interp->dict_state.next_keys_version;
+    return PyLong_FromLong(keys_version);
+}
 
 static PyObject *
 get_static_builtin_types(PyObject *self, PyObject *Py_UNUSED(ignored))
@@ -2124,6 +2136,7 @@ static PyMethodDef module_functions[] = {
     {"get_tracked_heap_size", get_tracked_heap_size, METH_NOARGS},
     {"is_static_immortal", is_static_immortal, METH_O},
     {"incref_decref_delayed", incref_decref_delayed, METH_O},
+    GET_NEXT_DICT_KEYS_VERSION_METHODDEF
     {NULL, NULL} /* sentinel */
 };
 
diff --git a/Modules/clinic/_testinternalcapi.c.h 
b/Modules/clinic/_testinternalcapi.c.h
index 7dfc16965cc0d8..21f4ee3201e5bf 100644
--- a/Modules/clinic/_testinternalcapi.c.h
+++ b/Modules/clinic/_testinternalcapi.c.h
@@ -375,4 +375,21 @@ gh_119213_getargs(PyObject *module, PyObject *const *args, 
Py_ssize_t nargs, PyO
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=da34166d2c147e7a input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(get_next_dict_keys_version__doc__,
+"get_next_dict_keys_version($module, /)\n"
+"--\n"
+"\n");
+
+#define GET_NEXT_DICT_KEYS_VERSION_METHODDEF    \
+    {"get_next_dict_keys_version", (PyCFunction)get_next_dict_keys_version, 
METH_NOARGS, get_next_dict_keys_version__doc__},
+
+static PyObject *
+get_next_dict_keys_version_impl(PyObject *module);
+
+static PyObject *
+get_next_dict_keys_version(PyObject *module, PyObject *Py_UNUSED(ignored))
+{
+    return get_next_dict_keys_version_impl(module);
+}
+/*[clinic end generated code: output=fbd8b7e0cae8bac7 input=a9049054013a1b77]*/

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to