gh-106307: C API: Add PyMapping_GetOptionalItem() function (GH-106308)
Also add PyMapping_GetOptionalItemString() function.
This commit is contained in:
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user