mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
gh-106307: C API: Add PyMapping_GetOptionalItem() function (GH-106308)
Also add PyMapping_GetOptionalItemString() function.
This commit is contained in:
parent
b444bfb0a3
commit
4bf43710d1
15 changed files with 739 additions and 896 deletions
|
@ -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)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue