weakref.py 10.4 KB
Newer Older
1 2 3 4
"""Weak reference support for Python.

This module is an implementation of PEP 205:

5
http://www.python.org/dev/peps/pep-0205/
6 7
"""

8 9 10 11
# Naming convention: Variables named "wr" are weak reference objects;
# they are called this instead of "ref" to avoid name collisions with
# the module-global ref() function imported from _weakref.

12 13
import UserDict

14 15 16 17 18 19 20 21
from _weakref import (
     getweakrefcount,
     getweakrefs,
     ref,
     proxy,
     CallableProxyType,
     ProxyType,
     ReferenceType)
22

23 24
from _weakrefset import WeakSet

25 26 27
from exceptions import ReferenceError


28 29
ProxyTypes = (ProxyType, CallableProxyType)

30
__all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs",
31
           "WeakKeyDictionary", "ReferenceError", "ReferenceType", "ProxyType",
32
           "CallableProxyType", "ProxyTypes", "WeakValueDictionary", 'WeakSet']
33

34

35
class WeakValueDictionary(UserDict.UserDict):
36
    """Mapping class that references values weakly.
37

38 39 40
    Entries in the dictionary will be discarded when no strong
    reference to the value exists anymore
    """
41 42
    # We inherit the constructor without worrying about the input
    # dictionary; since it uses our .update() method, we get the right
43 44 45
    # checks (if the other dictionary is a WeakValueDictionary,
    # objects are unwrapped on the way out, and we always wrap on the
    # way in).
46

47 48 49 50 51 52
    def __init__(self, *args, **kw):
        def remove(wr, selfref=ref(self)):
            self = selfref()
            if self is not None:
                del self.data[wr.key]
        self._remove = remove
53
        UserDict.UserDict.__init__(self, *args, **kw)
54

55
    def __getitem__(self, key):
56
        o = self.data[key]()
57 58 59 60 61
        if o is None:
            raise KeyError, key
        else:
            return o

62 63 64 65 66 67 68 69 70 71 72 73 74 75
    def __contains__(self, key):
        try:
            o = self.data[key]()
        except KeyError:
            return False
        return o is not None

    def has_key(self, key):
        try:
            o = self.data[key]()
        except KeyError:
            return False
        return o is not None

76
    def __repr__(self):
77
        return "<WeakValueDictionary at %s>" % id(self)
78 79

    def __setitem__(self, key, value):
80
        self.data[key] = KeyedRef(value, self._remove, key)
81 82

    def copy(self):
83
        new = WeakValueDictionary()
84 85
        for key, wr in self.data.items():
            o = wr()
86 87
            if o is not None:
                new[key] = o
88
        return new
89

90 91 92 93 94 95 96 97 98 99 100
    __copy__ = copy

    def __deepcopy__(self, memo):
        from copy import deepcopy
        new = self.__class__()
        for key, wr in self.data.items():
            o = wr()
            if o is not None:
                new[deepcopy(key, memo)] = o
        return new

101
    def get(self, key, default=None):
102
        try:
103
            wr = self.data[key]
104 105 106
        except KeyError:
            return default
        else:
107
            o = wr()
108 109 110 111 112 113 114
            if o is None:
                # This should only happen
                return default
            else:
                return o

    def items(self):
115
        L = []
116 117
        for key, wr in self.data.items():
            o = wr()
118
            if o is not None:
119
                L.append((key, o))
120 121
        return L

122
    def iteritems(self):
123 124 125 126
        for wr in self.data.itervalues():
            value = wr()
            if value is not None:
                yield wr.key, value
127 128 129

    def iterkeys(self):
        return self.data.iterkeys()
130 131 132

    def __iter__(self):
        return self.data.iterkeys()
133

134 135 136 137 138 139 140 141 142 143 144 145
    def itervaluerefs(self):
        """Return an iterator that yields the weak references to the values.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the values around longer than needed.

        """
        return self.data.itervalues()

146
    def itervalues(self):
147 148 149 150
        for wr in self.data.itervalues():
            obj = wr()
            if obj is not None:
                yield obj
151

152 153
    def popitem(self):
        while 1:
154 155
            key, wr = self.data.popitem()
            o = wr()
156 157 158
            if o is not None:
                return key, o

159 160 161 162 163 164 165 166 167 168 169 170
    def pop(self, key, *args):
        try:
            o = self.data.pop(key)()
        except KeyError:
            if args:
                return args[0]
            raise
        if o is None:
            raise KeyError, key
        else:
            return o

171
    def setdefault(self, key, default=None):
172
        try:
173
            wr = self.data[key]
174
        except KeyError:
175
            self.data[key] = KeyedRef(default, self._remove, key)
176 177
            return default
        else:
178
            return wr()
179

180
    def update(self, dict=None, **kwargs):
181
        d = self.data
182 183 184 185
        if dict is not None:
            if not hasattr(dict, "items"):
                dict = type({})(dict)
            for key, o in dict.items():
186
                d[key] = KeyedRef(o, self._remove, key)
187 188
        if len(kwargs):
            self.update(kwargs)
189

190 191 192 193 194 195 196 197 198 199 200 201
    def valuerefs(self):
        """Return a list of weak references to the values.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the values around longer than needed.

        """
        return self.data.values()

202 203
    def values(self):
        L = []
204 205
        for wr in self.data.values():
            o = wr()
206 207 208 209
            if o is not None:
                L.append(o)
        return L

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

class KeyedRef(ref):
    """Specialized reference that includes a key corresponding to the value.

    This is used in the WeakValueDictionary to avoid having to create
    a function object for each key stored in the mapping.  A shared
    callback object can use the 'key' attribute of a KeyedRef instead
    of getting a reference to the key from an enclosing scope.

    """

    __slots__ = "key",

    def __new__(type, ob, callback, key):
        self = ref.__new__(type, ob, callback)
        self.key = key
        return self

    def __init__(self, ob, callback, key):
        super(KeyedRef,  self).__init__(ob, callback)
230

231

232
class WeakKeyDictionary(UserDict.UserDict):
233 234 235 236 237 238 239 240 241
    """ Mapping class that references keys weakly.

    Entries in the dictionary will be discarded when there is no
    longer a strong reference to the key. This can be used to
    associate additional data with an object owned by other parts of
    an application without adding attributes to those objects. This
    can be especially useful with objects that override attribute
    accesses.
    """
242 243 244

    def __init__(self, dict=None):
        self.data = {}
245 246 247 248
        def remove(k, selfref=ref(self)):
            self = selfref()
            if self is not None:
                del self.data[k]
249
        self._remove = remove
250
        if dict is not None: self.update(dict)
251

252
    def __delitem__(self, key):
253
        del self.data[ref(key)]
254

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    def __getitem__(self, key):
        return self.data[ref(key)]

    def __repr__(self):
        return "<WeakKeyDictionary at %s>" % id(self)

    def __setitem__(self, key, value):
        self.data[ref(key, self._remove)] = value

    def copy(self):
        new = WeakKeyDictionary()
        for key, value in self.data.items():
            o = key()
            if o is not None:
                new[o] = value
270
        return new
271

272 273 274 275 276 277 278 279 280 281 282
    __copy__ = copy

    def __deepcopy__(self, memo):
        from copy import deepcopy
        new = self.__class__()
        for key, value in self.data.items():
            o = key()
            if o is not None:
                new[o] = deepcopy(value, memo)
        return new

283
    def get(self, key, default=None):
284 285
        return self.data.get(ref(key),default)

286
    def has_key(self, key):
287 288 289 290
        try:
            wr = ref(key)
        except TypeError:
            return 0
291
        return wr in self.data
292

293 294 295 296 297 298
    def __contains__(self, key):
        try:
            wr = ref(key)
        except TypeError:
            return 0
        return wr in self.data
Tim Peters's avatar
Tim Peters committed
299

300 301 302 303 304 305 306 307
    def items(self):
        L = []
        for key, value in self.data.items():
            o = key()
            if o is not None:
                L.append((o, value))
        return L

308
    def iteritems(self):
309 310 311 312
        for wr, value in self.data.iteritems():
            key = wr()
            if key is not None:
                yield key, value
313

314 315 316 317 318 319 320 321 322 323 324 325
    def iterkeyrefs(self):
        """Return an iterator that yields the weak references to the keys.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the keys around longer than needed.

        """
        return self.data.iterkeys()

326
    def iterkeys(self):
327 328 329 330 331 332 333
        for wr in self.data.iterkeys():
            obj = wr()
            if obj is not None:
                yield obj

    def __iter__(self):
        return self.iterkeys()
334 335 336 337

    def itervalues(self):
        return self.data.itervalues()

338 339 340 341 342 343 344 345 346 347 348 349
    def keyrefs(self):
        """Return a list of weak references to the keys.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the keys around longer than needed.

        """
        return self.data.keys()

350 351
    def keys(self):
        L = []
352 353
        for wr in self.data.keys():
            o = wr()
354 355 356 357
            if o is not None:
                L.append(o)
        return L

358 359 360 361 362 363 364
    def popitem(self):
        while 1:
            key, value = self.data.popitem()
            o = key()
            if o is not None:
                return o, value

365 366 367
    def pop(self, key, *args):
        return self.data.pop(ref(key), *args)

368
    def setdefault(self, key, default=None):
369 370
        return self.data.setdefault(ref(key, self._remove),default)

371
    def update(self, dict=None, **kwargs):
372
        d = self.data
373 374 375 376 377 378 379
        if dict is not None:
            if not hasattr(dict, "items"):
                dict = type({})(dict)
            for key, value in dict.items():
                d[ref(key, self._remove)] = value
        if len(kwargs):
            self.update(kwargs)