Skip to content

Commit 4c8455b

Browse files
committed
Specialize lazy import exceptions
1 parent 40f7fbf commit 4c8455b

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Objects/lazyimportobject.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ lazy_import_dealloc(PyObject *op)
8181
Py_TYPE(op)->tp_free(op);
8282
}
8383

84+
static PyObject *
85+
lazy_import_getattro(PyObject *op, PyObject *name) {
86+
/* Suppress to override the error message. */
87+
PyObject *value = _PyObject_GenericGetAttrWithDict(op, name, NULL, /* suppress */1);
88+
if (value == NULL) {
89+
if (PyErr_Occurred()) {
90+
return NULL;
91+
}
92+
PyObject *lz_name = _PyLazyImport_GetName(op);
93+
if (lz_name == NULL) {
94+
return NULL;
95+
}
96+
PyErr_Format(PyExc_AttributeError,
97+
"lazy import '%U' has no attribute '%U'",
98+
lz_name, name);
99+
Py_DECREF(lz_name);
100+
return NULL;
101+
}
102+
return value;
103+
}
104+
84105
static PyObject *
85106
lazy_import_name(PyLazyImportObject *m)
86107
{
@@ -149,6 +170,7 @@ PyTypeObject PyLazyImport_Type = {
149170
.tp_repr = lazy_import_repr,
150171
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
151172
.tp_doc = lazy_import_doc,
173+
.tp_getattro = lazy_import_getattro,
152174
.tp_traverse = lazy_import_traverse,
153175
.tp_clear = lazy_import_clear,
154176
.tp_methods = lazy_import_methods,

0 commit comments

Comments
 (0)