Skip to content

[mypyc] Don't swallow exceptions other than AttributeError in hasattr() - #22562

Open
Jahongir-Qurbonov wants to merge 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-hasattr-error
Open

Jahongir-Qurbonov wants to merge 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-hasattr-error

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

hasattr() was compiled to PyObject_HasAttr, which suppresses all exceptions, not just AttributeError. So if __getattr__ (or a property, descriptor etc.) raised e.g. ValueError, compiled code returned False instead of propagating the exception, and on 3.13+ CPython also printed an "Exception ignored in PyObject_HasAttr()" warning. A non-str name also returned False instead of raising TypeError.

Use PyObject_HasAttrWithError instead (provided by pythoncapi_compat.h on older Python versions), which only suppresses AttributeError, matching the hasattr builtin. This also affects the __prepare__ check when creating non-extension classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant