Skip to content

Functions must not suppress exceptions #51

@serhiy-storchaka

Description

@serhiy-storchaka

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions