1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
7 # Ensure that the object always has _ptr set, even if it throws during
10 def __new__(cls
, *args
, **kwargs
):
11 obj
= super().__new
__(cls
)
15 def __init__(self
, ptr
):
23 return '<{}.{} object @ {}>'.format(
24 self
.__class
__.__module
__, self
.__class
__.__name
__, hex(self
.addr
)
28 raise NotImplementedError
30 def __deepcopy__(self
, memo
):
31 raise NotImplementedError
34 # A Python object that is itself not refcounted, but is wholly owned by an
35 # object that is itself refcounted (a _SharedObject). A Babeltrace unique
36 # object gets destroyed once its owner gets destroyed (its refcount drops to
39 # In the Python bindings, to avoid having to deal with issues with the lifetime
40 # of unique objects, we make it so acquiring a reference on a unique object
41 # acquires a reference on its owner.
44 class _UniqueObject(_BaseObject
):
46 # Create a _UniqueObject.
48 # - ptr: SWIG Object, pointer to the unique object.
49 # - owner_ptr: SWIG Object, pointer to the owner of the unique
50 # object. A new reference is acquired.
51 # - owner_get_ref: Callback to get a reference on the owner
52 # - owner_put_ref: Callback to put a reference on the owner.
55 def _create_from_ptr_and_get_ref(cls
, ptr
, owner_ptr
, owner_get_ref
, owner_put_ref
):
56 obj
= cls
.__new
__(cls
)
59 obj
._owner
_ptr
= owner_ptr
60 obj
._owner
_get
_ref
= owner_get_ref
61 obj
._owner
_put
_ref
= owner_put_ref
63 obj
._owner
_get
_ref
(obj
._owner
_ptr
)
68 self
._owner
_put
_ref
(self
._owner
_ptr
)
71 # Python object that owns a reference to a Babeltrace object.
72 class _SharedObject(_BaseObject
):
74 # Get a new reference on ptr.
76 # This must be implemented by subclasses to work correctly with a pointer
77 # of the native type they wrap.
81 raise NotImplementedError
83 # Put a reference on ptr.
85 # This must be implemented by subclasses to work correctly with a pointer
86 # of the native type they wrap.
90 raise NotImplementedError
92 # Create a _SharedObject from an existing reference.
94 # This assumes that the caller owns a reference to the Babeltrace object
95 # and transfers this ownership to the newly created Python object.
98 def _create_from_ptr(cls
, ptr_owned
):
99 obj
= cls
.__new
__(cls
)
103 # Like _create_from_ptr, but acquire a new reference rather than
104 # stealing the caller's reference.
107 def _create_from_ptr_and_get_ref(cls
, ptr
):
108 obj
= cls
._create
_from
_ptr
(ptr
)
109 cls
._get
_ref
(obj
._ptr
)
113 self
._put
_ref
(self
._ptr
)
This page took 0.031409 seconds and 4 git commands to generate.