c1a23fdf353c8657503221834884f0ec675075e0
1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
10 # Ensure that the object always has _ptr set, even if it throws during
13 def __new__(cls
, *args
, **kwargs
):
14 obj
= super().__new
__(cls
)
18 def __init__(self
, ptr
):
26 return "<{}.{} object @ {}>".format(
27 self
.__class
__.__module
__, self
.__class
__.__name
__, hex(self
.addr
)
31 raise NotImplementedError
33 def __deepcopy__(self
, memo
):
34 raise NotImplementedError
37 # A Python object that is itself not refcounted, but is wholly owned by an
38 # object that is itself refcounted (a _SharedObject). A Babeltrace unique
39 # object gets destroyed once its owner gets destroyed (its refcount drops to
42 # In the Python bindings, to avoid having to deal with issues with the lifetime
43 # of unique objects, we make it so acquiring a reference on a unique object
44 # acquires a reference on its owner.
47 class _UniqueObject(_BaseObject
):
49 # Create a _UniqueObject.
51 # - ptr: SWIG Object, pointer to the unique object.
52 # - owner_ptr: SWIG Object, pointer to the owner of the unique
53 # object. A new reference is acquired.
54 # - owner_get_ref: Callback to get a reference on the owner
55 # - owner_put_ref: Callback to put a reference on the owner.
58 def _create_from_ptr_and_get_ref(cls
, ptr
, owner_ptr
, owner_get_ref
, owner_put_ref
):
59 obj
= cls
.__new
__(cls
)
62 obj
._owner
_ptr
= owner_ptr
63 obj
._owner
_get
_ref
= owner_get_ref
64 obj
._owner
_put
_ref
= owner_put_ref
66 obj
._owner
_get
_ref
(obj
._owner
_ptr
)
71 self
._owner
_put
_ref
(self
._owner
_ptr
)
74 # Python object that owns a reference to a Babeltrace object.
75 class _SharedObject(_BaseObject
, abc
.ABC
):
76 # Get a new reference on ptr.
78 # This must be implemented by subclasses to work correctly with a pointer
79 # of the native type they wrap.
84 raise NotImplementedError
86 # Put a reference on ptr.
88 # This must be implemented by subclasses to work correctly with a pointer
89 # of the native type they wrap.
94 raise NotImplementedError
96 # Create a _SharedObject from an existing reference.
98 # This assumes that the caller owns a reference to the Babeltrace object
99 # and transfers this ownership to the newly created Python object.
102 def _create_from_ptr(cls
, ptr_owned
):
103 obj
= cls
.__new
__(cls
)
107 # Like _create_from_ptr, but acquire a new reference rather than
108 # stealing the caller's reference.
111 def _create_from_ptr_and_get_ref(cls
, ptr
):
112 obj
= cls
._create
_from
_ptr
(ptr
)
113 cls
._get
_ref
(obj
._ptr
)
117 self
._put
_ref
(self
._ptr
)
This page took 0.035145 seconds and 3 git commands to generate.