From: Kirill Podoprigora Date: Mon, 12 Feb 2024 16:05:30 +0000 (+0300) Subject: gh-115058: Add ``reset_rare_event_counters`` function in `_testinternalcapi` (GH... X-Git-Tag: v3.13.0a4~64 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=93ac78ac3ee124942bca7492149c3ff0003b6e30;p=thirdparty%2FPython%2Fcpython.git gh-115058: Add ``reset_rare_event_counters`` function in `_testinternalcapi` (GH-115128) --- diff --git a/Lib/test/test_optimizer.py b/Lib/test/test_optimizer.py index c8554c40df4b..dfea8be3c695 100644 --- a/Lib/test/test_optimizer.py +++ b/Lib/test/test_optimizer.py @@ -7,6 +7,9 @@ _testinternalcapi = import_helper.import_module("_testinternalcapi") class TestRareEventCounters(unittest.TestCase): + def setUp(self): + _testinternalcapi.reset_rare_event_counters() + def test_set_class(self): class A: pass diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 0bb739b5398b..3834f00009ce 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -1650,6 +1650,20 @@ get_rare_event_counters(PyObject *self, PyObject *type) ); } +static PyObject * +reset_rare_event_counters(PyObject *self, PyObject *Py_UNUSED(type)) +{ + PyInterpreterState *interp = PyInterpreterState_Get(); + + interp->rare_events.set_class = 0; + interp->rare_events.set_bases = 0; + interp->rare_events.set_eval_frame_func = 0; + interp->rare_events.builtin_dict = 0; + interp->rare_events.func_modification = 0; + + return Py_None; +} + #ifdef Py_GIL_DISABLED static PyObject * @@ -1727,6 +1741,7 @@ static PyMethodDef module_functions[] = { _TESTINTERNALCAPI_TEST_LONG_NUMBITS_METHODDEF {"get_type_module_name", get_type_module_name, METH_O}, {"get_rare_event_counters", get_rare_event_counters, METH_NOARGS}, + {"reset_rare_event_counters", reset_rare_event_counters, METH_NOARGS}, #ifdef Py_GIL_DISABLED {"py_thread_id", get_py_thread_id, METH_NOARGS}, #endif