python: define _BaseObject._ptr property
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 12 Jun 2023 20:32:42 +0000 (16:32 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Sat, 19 Oct 2024 11:12:09 +0000 (07:12 -0400)
Define the property `_ptr` on _BaseObject, which returns the value of
the currently-named `_ptr` field.  Rename the `_ptr` field to
`_ptr_internal`, for the lack of a better name.

The goal is for mixin classes to be able to declare that they require
such a property to exist, using `abc.abstractmethod`, and for
_BaseObject to fill in the implementation (and for static type checkers
to be happy).  For instance:

    class MyMixin(abc.ABC):
        @property
@abc.abstractmethod
def _ptr(self):
    raise NotImplementedError

def my_method(self):
    ... do something with self._ptr ...

   class MyObject(object._SharedObject, MyMixin):
      ...

In this case, object._SharedObject provides the `_ptr` implementation
that `MyMixin` requires.

Change-Id: I696c04ed690ede7cd50bcb71890cc0b7b25f44e4
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10326
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/bindings/python/bt2/bt2/object.py

index dcff667f49aaf52d1a889d29e06e8cfdcb187156..9976802bc3bd3ddcef90ff98ac1c97597faeda0f 100644 (file)
@@ -7,20 +7,24 @@ import abc
 
 
 class _BaseObject:
-    # Ensure that the object always has _ptr set, even if it throws during
+    # Ensure that the object always has _ptr_internal set, even if it throws during
     # construction.
 
     def __new__(cls, *args, **kwargs):
         obj = super().__new__(cls)
-        obj._ptr = None
+        obj._ptr_internal = None
         return obj
 
     def __init__(self, ptr):
-        self._ptr = ptr
+        self._ptr_internal = ptr
+
+    @property
+    def _ptr(self):
+        return self._ptr_internal
 
     @property
     def addr(self):
-        return int(self._ptr)
+        return int(self._ptr_internal)
 
     def __repr__(self):
         return "<{}.{} object @ {}>".format(
@@ -57,7 +61,7 @@ class _UniqueObject(_BaseObject):
     def _create_from_ptr_and_get_ref(cls, ptr, owner_ptr, owner_get_ref, owner_put_ref):
         obj = cls.__new__(cls)
 
-        obj._ptr = ptr
+        obj._ptr_internal = ptr
         obj._owner_ptr = owner_ptr
         obj._owner_get_ref = owner_get_ref
         obj._owner_put_ref = owner_put_ref
@@ -100,7 +104,7 @@ class _SharedObject(_BaseObject, abc.ABC):
     @classmethod
     def _create_from_ptr(cls, ptr_owned):
         obj = cls.__new__(cls)
-        obj._ptr = ptr_owned
+        obj._ptr_internal = ptr_owned
         return obj
 
     # Like _create_from_ptr, but acquire a new reference rather than
@@ -109,8 +113,8 @@ class _SharedObject(_BaseObject, abc.ABC):
     @classmethod
     def _create_from_ptr_and_get_ref(cls, ptr):
         obj = cls._create_from_ptr(ptr)
-        cls._get_ref(obj._ptr)
+        cls._get_ref(obj._ptr_internal)
         return obj
 
     def __del__(self):
-        self._put_ref(self._ptr)
+        self._put_ref(self._ptr_internal)
This page took 0.028948 seconds and 4 git commands to generate.