Repository navigation
Avoid suppressing all exceptions in PyObject_HasAttr() #75753
Description
Activity
Initially hasattr() suppressed all raised exceptions. In bpo-2196 hasattr() was changed to suppress only Exception exceptions and propagate exceptions like SystemExit and KeyboardInterrupt. In bpo-9666 hasattr() was changed to suppress only AttributeError.
But C API functions, PyObject_HasAttr() and like, were not changed. PyObject_HasAttr() is documented as an equivalent of hasattr(), but there is undocumented difference. The C code that uses PyObject_HasAttr() starves from the same problem as the Python code that used old hasattr().
The only solution of this problem is getting rid of PyObject_HasAttr() if favor of PyObject_GetAttr(). In this issue I'm going to propose a set of PRs that replace PyObject_HasAttr() invocations in different components with more correct code.
- addedextension-modulesC modules in the Modules dirC modules in the Modules dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Sep 24, 2017 The only solution of this problem is getting rid of PyObject_HasAttr() if favor of PyObject_GetAttr().
I don't understand why. Why not fix PyObject_HasAttr() like hasattr() was?
hasattr() can return True, False, or raise an exception. But PyObject_HasAttr() just returns an integer: 0 for False, not 0 for True. There is no way to return an error, and existing code doesn't expect that PyObject_HasAttr() returns an error. Leaking an exception from PyObject_HasAttr() will break existing code.
It seems like this issue caused a regression in _warnings: please see bpo-33509.
I suppose that the next step is to design a migration path to avoid legacy API which suppress exceptions :-) It was discussed at: capi-workgroup/problems#54
Maybe we can start by soft deprecating old APIs.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: