From 05a32d38d44145b04cd645d824eff230b822672d Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Mon, 12 Jun 2023 16:32:42 -0400 Subject: [PATCH] python: define _BaseObject._ptr property 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 Reviewed-on: https://review.lttng.org/c/babeltrace/+/10326 Reviewed-by: Philippe Proulx Tested-by: jenkins --- src/bindings/python/bt2/bt2/object.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/bindings/python/bt2/bt2/object.py b/src/bindings/python/bt2/bt2/object.py index dcff667f..9976802b 100644 --- a/src/bindings/python/bt2/bt2/object.py +++ b/src/bindings/python/bt2/bt2/object.py @@ -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) -- 2.34.1