summaryrefslogtreecommitdiffstats
path: root/Python/import.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-07-11 20:04:12 (GMT)
committerGitHub <noreply@github.com>2023-07-11 20:04:12 (GMT)
commit4bf43710d1e1f19cc46b116b5d8524f6c75dabfa (patch)
treef005fe94ab083da1f9a21485f51373efa7cf8008 /Python/import.c
parentb444bfb0a325dea8c29f7b1828233b00fbf4a1cb (diff)
downloadcpython-4bf43710d1e1f19cc46b116b5d8524f6c75dabfa.zip
cpython-4bf43710d1e1f19cc46b116b5d8524f6c75dabfa.tar.gz
cpython-4bf43710d1e1f19cc46b116b5d8524f6c75dabfa.tar.bz2
gh-106307: C API: Add PyMapping_GetOptionalItem() function (GH-106308)
Also add PyMapping_GetOptionalItemString() function.
Diffstat (limited to 'Python/import.c')
-rw-r--r--Python/import.c24
1 files changed, 2 insertions, 22 deletions
diff --git a/Python/import.c b/Python/import.c
index fcc0346..7ce89cd 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -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)) {