gh-106307: C API: Add PyMapping_GetOptionalItem() function (GH-106308)

Also add PyMapping_GetOptionalItemString() function.
This commit is contained in:
Serhiy Storchaka 2023-07-11 23:04:12 +03:00 committed by GitHub
parent b444bfb0a3
commit 4bf43710d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 739 additions and 896 deletions

View file

@ -249,16 +249,7 @@ import_get_module(PyThreadState *tstate, PyObject *name)
PyObject *m;
Py_INCREF(modules);
if (PyDict_CheckExact(modules)) {
m = PyDict_GetItemWithError(modules, name); /* borrowed */
Py_XINCREF(m);
}
else {
m = PyObject_GetItem(modules, name);
if (m == NULL && _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
_PyErr_Clear(tstate);
}
}
(void)PyMapping_GetOptionalItem(modules, name, &m);
Py_DECREF(modules);
return m;
}
@ -322,18 +313,7 @@ import_add_module(PyThreadState *tstate, PyObject *name)
}
PyObject *m;
if (PyDict_CheckExact(modules)) {
m = Py_XNewRef(PyDict_GetItemWithError(modules, name));
}
else {
m = PyObject_GetItem(modules, name);
// For backward-compatibility we copy the behavior
// of PyDict_GetItemWithError().
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
_PyErr_Clear(tstate);
}
}
if (_PyErr_Occurred(tstate)) {
if (PyMapping_GetOptionalItem(modules, name, &m) < 0) {
return NULL;
}
if (m != NULL && PyModule_Check(m)) {