Skip to content

Commit 833f59f

Browse files
gh-76595: Use PyObject_GetOptionalAttrString() in PyCapsule_Import()
Only AttributeError is silenced before falling back to importing a submodule; other errors from the attribute lookup are now propagated. As a side effect, a failed import now propagates ModuleNotFoundError instead of replacing it with the generic AttributeError. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 121bcec commit 833f59f

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

Objects/capsule.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,17 @@ PyCapsule_Import(const char *name, int Py_UNUSED(no_block))
246246
*dot = '\0';
247247
}
248248
if (object) {
249-
Py_SETREF(object, PyObject_GetAttrString(object, trace));
249+
PyObject *attr;
250+
if (PyObject_GetOptionalAttrString(object, trace, &attr) < 0) {
251+
Py_CLEAR(object);
252+
break;
253+
}
254+
Py_SETREF(object, attr);
250255
}
251256
if (!dot) {
252257
break;
253258
}
254259
if (!object) {
255-
if (PyErr_Occurred()) {
256-
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
257-
break;
258-
}
259-
PyErr_Clear();
260-
}
261260
object = PyImport_ImportModule(name_dup);
262261
if (!object) {
263262
break;
@@ -272,7 +271,7 @@ PyCapsule_Import(const char *name, int Py_UNUSED(no_block))
272271
PyCapsule *capsule = (PyCapsule *)object;
273272
return_value = capsule->pointer;
274273
}
275-
else if (object || trace == name_dup) {
274+
else if (!PyErr_Occurred()) {
276275
PyErr_Format(PyExc_AttributeError,
277276
"PyCapsule_Import \"%s\" is not valid",
278277
name);

0 commit comments

Comments
 (0)