Skip to content

Commit

Permalink
Use __getattr__ instead of __getattribute__ in type stub
Browse files Browse the repository at this point in the history
Pyre apparently doesn't support __getattribute__, but it does support
__getattr__. Update the type stub to work around this limitation.

The actual implementation in the C extension uses tp_getattro, which is
essentially __getattribute__, but it tries the equivalent of
object.__getattribute__ first, so it's closer to __getattr__ in spirit.

Signed-off-by: Omar Sandoval <osandov@osandov.com>
  • Loading branch information
osandov committed Nov 6, 2023
1 parent 3fb557e commit 7565538
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions _drgn.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1067,8 +1067,8 @@ class Object:
* Relational operators: ``==``, ``!=``, ``<``, ``>``, ``<=``, ``>=``
* Subscripting: :meth:`[] <__getitem__>` (Python does not have a unary
``*`` operator, so pointers are dereferenced with ``ptr[0]``)
* Member access: :meth:`. <__getattribute__>` (Python does not have a
``->`` operator, so ``.`` is also used to access members of pointers to
* Member access: :meth:`. <__getattr__>` (Python does not have a ``->``
operator, so ``.`` is also used to access members of pointers to
structures)
* The address-of operator: :meth:`drgn.Object.address_of_()` (this is a
method because Python does not have a ``&`` operator)
Expand Down Expand Up @@ -1199,15 +1199,15 @@ class Object:
"""
Size in bits of this object if it is a bit field, ``None`` if it is not.
"""
def __getattribute__(self, name: str) -> Object:
def __getattr__(self, name: str) -> Object:
"""
Implement ``self.name``.
This corresponds to both the member access (``.``) and member access
through pointer (``->``) operators in C.
If *name* is an attribute of the :class:`Object` class, then this
returns that attribute. Otherwise, it is equivalent to
Note that if *name* is an attribute or method of the :class:`Object`
class, then that takes precedence. Otherwise, this is equivalent to
:meth:`member_()`.
>>> print(prog['init_task'].pid)
Expand Down Expand Up @@ -1295,9 +1295,9 @@ class Object:
those. If the object is a pointer, it is automatically dereferenced
first.
Normally the dot operator (:meth:`. <__getattribute__>`) can be used to
Normally the dot operator (:meth:`. <__getattr__>`) can be used to
accomplish the same thing, but this method can be used if there is a
name conflict with an ``Object`` member or method.
name conflict with an ``Object`` attribute or method.
:param name: Name of the member.
:raises TypeError: if this object is not a structure, union, class, or
Expand Down
2 changes: 1 addition & 1 deletion docs/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ substitutions are necessary:
* Instead of ``*ptr``, dereference a pointer with :meth:`ptr[0]
<drgn.Object.__getitem__>`.
* Instead of ``ptr->member``, access a member through a pointer with
:meth:`ptr.member <drgn.Object.__getattribute__>`.
:meth:`ptr.member <drgn.Object.__getattr__>`.
* Instead of ``&var``, get the address of a variable with
:meth:`var.address_of_() <drgn.Object.address_of_>`.

Expand Down

0 comments on commit 7565538

Please sign in to comment.