The following functions suppress arbitrary exceptions raised inside. In some cases they can also clear exceptions raised before the call.
PyDict_GetItem and PyDict_GetItemString
PyMapping_HasKey and PyMapping_HasKeyString
PyObject_HasAttr and PyObject_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.
The following functions suppress arbitrary exceptions raised inside. In some cases they can also clear exceptions raised before the call.
PyDict_GetItemandPyDict_GetItemStringPyMapping_HasKeyandPyMapping_HasKeyStringPyObject_HasAttrandPyObject_HasAttrStringPySys_GetObjectThey can call Python code (via
__hash__,__eq__,__getattr__or__getattribute__) which can raise arbitrary exception which will be silenced anyway.PyDict_GetItemhas 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
WithErrorsuffix? In CPython code all occurrences ofPyObject_HasAttrwere replaced with_PyObject_LookupAttr.Historical reference:
hasattr()never raised exceptions in Python 2.