Functions must not suppress exceptions (original) (raw)
The following functions suppress arbitrary exceptions raised inside. In some cases they can also clear exceptions raised before the call.
PyDict_GetItem
andPyDict_GetItemString
PyMapping_HasKey
andPyMapping_HasKeyString
PyObject_HasAttr
andPyObject_HasAttrString
PySys_GetObject
They can call Python code (via __hash__
, __eq__
, __getattr__
or __getattribute__
) which can raise arbitrary exception which will be silenced anyway. PyDict_GetItem
has a counterpart which keeps exception (PyDict_GetItemWithError
), but other functions do not have any.
These function should not be used in modern code, except in cases in which any error is silenced anyway (and it is bad that we still have such places). They should be deprecated in future, both at compile time and at run time. But first we should introduce replacements. Should we just introduce new functions with WithError
suffix? In CPython code all occurrences of PyObject_HasAttr
were replaced with _PyObject_LookupAttr
.
Historical reference: hasattr()
never raised exceptions in Python 2.