1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 # Ensure that the object always has _ptr set, even if it throws during
28 def __new__(cls
, *args
, **kwargs
):
29 obj
= super().__new
__(cls
)
33 def __init__(self
, ptr
):
41 return '<{}.{} object @ {}>'.format(
42 self
.__class
__.__module
__, self
.__class
__.__name
__, hex(self
.addr
)
46 raise NotImplementedError
48 def __deepcopy__(self
, memo
):
49 raise NotImplementedError
52 # A Python object that is itself not refcounted, but is wholly owned by an
53 # object that is itself refcounted (a _SharedObject). A Babeltrace unique
54 # object gets destroyed once its owner gets destroyed (its refcount drops to
57 # In the Python bindings, to avoid having to deal with issues with the lifetime
58 # of unique objects, we make it so acquiring a reference on a unique object
59 # acquires a reference on its owner.
62 class _UniqueObject(_BaseObject
):
64 # Create a _UniqueObject.
66 # - ptr: SWIG Object, pointer to the unique object.
67 # - owner_ptr: SWIG Object, pointer to the owner of the unique
68 # object. A new reference is acquired.
69 # - owner_get_ref: Callback to get a reference on the owner
70 # - owner_put_ref: Callback to put a reference on the owner.
73 def _create_from_ptr_and_get_ref(cls
, ptr
, owner_ptr
, owner_get_ref
, owner_put_ref
):
74 obj
= cls
.__new
__(cls
)
77 obj
._owner
_ptr
= owner_ptr
78 obj
._owner
_get
_ref
= owner_get_ref
79 obj
._owner
_put
_ref
= owner_put_ref
81 obj
._owner
_get
_ref
(obj
._owner
_ptr
)
86 self
._owner
_put
_ref
(self
._owner
_ptr
)
89 # Python object that owns a reference to a Babeltrace object.
90 class _SharedObject(_BaseObject
):
92 # Get a new reference on ptr.
94 # This must be implemented by subclasses to work correctly with a pointer
95 # of the native type they wrap.
99 raise NotImplementedError
101 # Put a reference on ptr.
103 # This must be implemented by subclasses to work correctly with a pointer
104 # of the native type they wrap.
108 raise NotImplementedError
110 # Create a _SharedObject from an existing reference.
112 # This assumes that the caller owns a reference to the Babeltrace object
113 # and transfers this ownership to the newly created Python object.
116 def _create_from_ptr(cls
, ptr_owned
):
117 obj
= cls
.__new
__(cls
)
121 # Like _create_from_ptr, but acquire a new reference rather than
122 # stealing the caller's reference.
125 def _create_from_ptr_and_get_ref(cls
, ptr
):
126 obj
= cls
._create
_from
_ptr
(ptr
)
127 cls
._get
_ref
(obj
._ptr
)
131 self
._put
_ref
(self
._ptr
)
This page took 0.030925 seconds and 4 git commands to generate.