Skip to content

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

Merged
JukkaL merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-hasattr-error
Nov 27, 2026
Merged

JukkaL merged 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.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@JukkaL
JukkaL merged commit 8824606 into python:master Nov 27, 2026
18 checks passed
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.

2 participants