summaryrefslogtreecommitdiff
path: root/silx/utils/weakref.py
blob: 06646e8d2def33d439f1b6a66140288f0f57ebce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2018 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""Weakref utils for compatibility between Python 2 and Python 3 or for
extended features.
"""
from __future__ import absolute_import

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "15/09/2016"


import weakref
import types
import inspect


def ref(object, callback=None):
    """Returns a weak reference to object. The original object can be retrieved
    by calling the reference object if the referent is still alive. If the
    referent is no longer alive, calling the reference object will cause None
    to be returned.

    The signature is the same as the standard `weakref` library, but it returns
    `WeakMethod` if the object is a bound method.

    :param object: An object
    :param func callback: If provided, and the returned weakref object is
        still alive, the callback will be called when the object is about to
        be finalized. The weak reference object will be passed as the only
        parameter to the callback. Then the referent will no longer be
        available.
    :return: A weak reference to the object
    """
    if inspect.ismethod(object):
        return WeakMethod(object, callback)
    else:
        return weakref.ref(object, callback)


def proxy(object, callback=None):
    """Return a proxy to object which uses a weak reference. This supports use
    of the proxy in most contexts instead of requiring the explicit
    dereferencing used with weak reference objects.

    The signature is the same as the standard `weakref` library, but it returns
    `WeakMethodProxy` if the object is a bound method.

    :param object: An object
    :param func callback: If provided, and the returned weakref object is
        still alive, the callback will be called when the object is about to
        be finalized. The weak reference object will be passed as the only
        parameter to the callback. Then the referent will no longer be
        available.
    :return: A proxy to a weak reference of the object
    """
    if inspect.ismethod(object):
        return WeakMethodProxy(object, callback)
    else:
        return weakref.proxy(object, callback)


class WeakMethod(object):
    """Wraps a callable object like a function or a bound method.
    Feature callback when the object is about to be finalized.
    Provids the same interface as a normal weak reference.
    """

    def __init__(self, function, callback=None):
        """
        Constructor
        :param function: Function/method to be called
        :param callback: If callback is provided and not None,
            and the returned weakref object is still alive, the
            callback will be called when the object is about to
            be finalized; the weak reference object will be passed
            as the only parameter to the callback; the referent will
            no longer be available
        """
        self.__callback = callback

        if inspect.ismethod(function):
            # it is a bound method
            self.__obj = weakref.ref(function.__self__, self.__call_callback)
            self.__method = weakref.ref(function.__func__, self.__call_callback)
        else:
            self.__obj = None
            self.__method = weakref.ref(function, self.__call_callback)

    def __call_callback(self, ref):
        """Called when the object is about to be finalized"""
        if not self.is_alive():
            return
        self.__obj = None
        self.__method = None
        if self.__callback is not None:
            self.__callback(self)

    def __call__(self):
        """Return a callable function or None if the WeakMethod is dead."""
        if self.__obj is not None:
            method = self.__method()
            obj = self.__obj()
            if method is None or obj is None:
                return None
            return types.MethodType(method, obj)
        elif self.__method is not None:
            return self.__method()
        else:
            return None

    def is_alive(self):
        """True if the WeakMethod is still alive"""
        return self.__method is not None

    def __eq__(self, other):
        """Check it another obect is equal to this.

        :param object other: Object to compare with
        """
        if isinstance(other, WeakMethod):
            if not self.is_alive():
                return False
            return self.__obj == other.__obj and self.__method == other.__method
        return False

    def __ne__(self, other):
        """Check it another obect is not equal to this.

        :param object other: Object to compare with
        """
        if isinstance(other, WeakMethod):
            if not self.is_alive():
                return False
            return self.__obj != other.__obj or self.__method != other.__method
        return True

    def __hash__(self):
        """Returns the hash for the object."""
        return self.__obj.__hash__() ^ self.__method.__hash__()


class WeakMethodProxy(WeakMethod):
    """Wraps a callable object like a function or a bound method
    with a weakref proxy.
    """
    def __call__(self, *args, **kwargs):
        """Dereference the method and call it if the method is still alive.
        Else raises an ReferenceError.

        :raises: ReferenceError, if the method is not alive
        """
        fn = super(WeakMethodProxy, self).__call__()
        if fn is None:
            raise ReferenceError("weakly-referenced object no longer exists")
        return fn(*args, **kwargs)


class WeakList(list):
    """Manage a list of weaked references.
    When an object is dead, the list is flaged as invalid.
    If expected the list is cleaned up to remove dead objects.
    """

    def __init__(self, enumerator=()):
        """Create a WeakList

        :param iterator enumerator: A list of object to initialize the
            list
        """
        list.__init__(self)
        self.__list = []
        self.__is_valid = True
        for obj in enumerator:
            self.append(obj)

    def __invalidate(self, ref):
        """Flag the list as invalidated. The list contains dead references."""
        self.__is_valid = False

    def __create_ref(self, obj):
        """Create a weakref from an object. It uses the `ref` module function.
        """
        return ref(obj, self.__invalidate)

    def __clean(self):
        """Clean the list from dead references"""
        if self.__is_valid:
            return
        self.__list = [ref for ref in self.__list if ref() is not None]
        self.__is_valid = True

    def __iter__(self):
        """Iterate over objects of the list"""
        for ref in self.__list:
            obj = ref()
            if obj is not None:
                yield obj

    def __len__(self):
        """Count item on the list"""
        self.__clean()
        return len(self.__list)

    def __getitem__(self, key):
        """Returns the object at the requested index

        :param key: Indexes to get
        :type key: int or slice
        """
        self.__clean()
        data = self.__list[key]
        if isinstance(data, list):
            result = [ref() for ref in data]
        else:
            result = data()
        return result

    def __setitem__(self, key, obj):
        """Set an item at an index

        :param key: Indexes to set
        :type key: int or slice
        """
        self.__clean()
        if isinstance(key, slice):
            objs = [self.__create_ref(o) for o in obj]
            self.__list[key] = objs
        else:
            obj_ref = self.__create_ref(obj)
            self.__list[key] = obj_ref

    def __delitem__(self, key):
        """Delete an Indexes item of this list

        :param key: Index to delete
        :type key: int or slice
         """
        self.__clean()
        del self.__list[key]

    def __delslice__(self, i, j):
        """Looks to be used in Python 2.7"""
        self.__delitem__(slice(i, j, None))

    def __setslice__(self, i, j, sequence):
        """Looks to be used in Python 2.7"""
        self.__setitem__(slice(i, j, None), sequence)

    def __getslice__(self, i, j):
        """Looks to be used in Python 2.7"""
        return self.__getitem__(slice(i, j, None))

    def __reversed__(self):
        """Returns a copy of the reverted list"""
        reversed_list = reversed(list(self))
        return WeakList(reversed_list)

    def __contains__(self, obj):
        """Returns true if the object is in the list"""
        ref = self.__create_ref(obj)
        return ref in self.__list

    def __add__(self, other):
        """Returns a WeakList containing this list an the other"""
        l = WeakList(self)
        l.extend(other)
        return l

    def __iadd__(self, other):
        """Add objects to this list inplace"""
        self.extend(other)
        return self

    def __mul__(self, n):
        """Returns a WeakList containing n-duplication object of this list"""
        return WeakList(list(self) * n)

    def __imul__(self, n):
        """N-duplication of the objects to this list inplace"""
        self.__list *= n
        return self

    def append(self, obj):
        """Add an object at the end of the list"""
        ref = self.__create_ref(obj)
        self.__list.append(ref)

    def count(self, obj):
        """Returns the number of occurencies of an object"""
        ref = self.__create_ref(obj)
        return self.__list.count(ref)

    def extend(self, other):
        """Append the list with all objects from another list"""
        for obj in other:
            self.append(obj)

    def index(self, obj):
        """Returns the index of an object"""
        ref = self.__create_ref(obj)
        return self.__list.index(ref)

    def insert(self, index, obj):
        """Insert an object at the requested index"""
        ref = self.__create_ref(obj)
        self.__list.insert(index, ref)

    def pop(self, index=-1):
        """Remove and return an object at the requested index"""
        self.__clean()
        obj = self.__list.pop(index)()
        return obj

    def remove(self, obj):
        """Remove an object from the list"""
        ref = self.__create_ref(obj)
        self.__list.remove(ref)

    def reverse(self):
        """Reverse the list inplace"""
        self.__list.reverse()

    def sort(self, key=None, reverse=False):
        """Sort the list inplace.
        Not very efficient.
        """
        sorted_list = list(self)
        sorted_list.sort(key=key, reverse=reverse)
        self.__list = []
        self.extend(sorted_list)

    def __str__(self):
        unref_list = list(self)
        return "WeakList(%s)" % str(unref_list)

    def __repr__(self):
        unref_list = list(self)
        return "WeakList(%s)" % repr(unref_list)