Skip to content

gh-122320: Add _testinternalcapi.is_dict_version_overflowed #122324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Include/internal/pycore_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ extern PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);

/* Gets a version number unique to the current state of the keys of dict, if possible.
* Returns the version number, or zero if it was not possible to get a version number. */
extern uint32_t _PyDictKeys_GetVersionForCurrentState(
// Export for '_testinternalcapi' extension
PyAPI_FUNC(uint32_t) _PyDictKeys_GetVersionForCurrentState(
PyInterpreterState *interp, PyDictKeysObject *dictkeys);

/* Gets a version number unique to the current state of the keys of dict, if possible.
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ def assert_races_do_not_crash(
# Reset:
if check_items:
for item in items:
# Checking for overflow
if _testinternalcapi.is_dict_version_overflowed(item.__globals__):
return unittest.skip("Version number is overflowed; no rerun is possible")
item.__code__ = item.__code__.replace()
else:
read.__code__ = read.__code__.replace()
Expand Down
17 changes: 17 additions & 0 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,22 @@ gh_119213_getargs_impl(PyObject *module, PyObject *spam)
return Py_NewRef(spam);
}

/*[clinic input]
is_dict_version_overflowed
dict: object(type="PyDictObject *", subclass_of="&PyDict_Type")
[clinic start generated code]*/

static PyObject *
is_dict_version_overflowed_impl(PyObject *module, PyDictObject *dict)
/*[clinic end generated code: output=85daaa03aef739fd input=78fbf25d89b33b9a]*/
{
PyInterpreterState *interp = _PyInterpreterState_GET();
uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(interp, dict->ma_keys);
if (keys_version != (uint16_t)keys_version){
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}

static PyObject *
get_static_builtin_types(PyObject *self, PyObject *Py_UNUSED(ignored))
Expand Down Expand Up @@ -2165,6 +2181,7 @@ static PyMethodDef module_functions[] = {
{"has_deferred_refcount", has_deferred_refcount, METH_O},
{"get_tracked_heap_size", get_tracked_heap_size, METH_NOARGS},
{"is_static_immortal", is_static_immortal, METH_O},
IS_DICT_VERSION_OVERFLOWED_METHODDEF
{NULL, NULL} /* sentinel */
};

Expand Down
61 changes: 60 additions & 1 deletion Modules/clinic/_testinternalcapi.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading