Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop suppressing unrelated exceptions in PyAny::hasattr #3271

Merged
merged 1 commit into from
Jun 28, 2023

Conversation

davidhewitt
Copy link
Member

I realised today that our hasattr implementation suppresses all exceptions, which is inconsistent with hasattr, which only suppresses AttributeError.

>>> class Foo:
...     def __getattribute__(self, *args):
...         raise ValueError("oh no")
...
>>> hasattr(Foo(), "bar")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __getattribute__
ValueError: oh no

See also capi-workgroup/problems#51

TODO: Before merging, I should push a test for this.

src/types/any.rs Outdated
)
};
match result {
Ok(_) => Ok(true),
Copy link
Member

Choose a reason for hiding this comment

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

This does imply an unnecessary increment-then-decrement of the reference count, doesn't it? Also why GetItem instead of GetAttr as suggested by the docs?

If we do eat the reference count modifications, couldn't this just call getattr(name) and match on the error?

Copy link
Member Author

Choose a reason for hiding this comment

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

This does imply an unnecessary increment-then-decrement of the reference count, doesn't it?

Sadly yes, though this was always the case, just abstracted away in PyObject_HasAttr.

Also why GetItem instead of GetAttr as suggested by the docs?

A bug, should be GetAttr, this is exactly why I need to push a test 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed the implementation to go via getattr as suggested (and pushed tests).

Copy link
Member

Choose a reason for hiding this comment

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

Just to make sure I understand it correctly: Calling _getattr instead of getattr avoids the into_ref(py) which involves the pool? So when we drop the pool and getattr returns PyAny<'py> this distinction can be removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes that's exactly right. Avoiding the pool means that we don't end up holding a reference to whatever we checked for in hasattr in the pool - so it's just a micro-optimization for now that should reduce away soon.

@davidhewitt davidhewitt marked this pull request as ready for review June 27, 2023 22:20
@davidhewitt davidhewitt added this pull request to the merge queue Jun 28, 2023
Merged via the queue into PyO3:main with commit b329439 Jun 28, 2023
@davidhewitt davidhewitt deleted the hasattr branch June 28, 2023 09:07
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