annotate Lib/weakref.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 04cc075bddf5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 """Weak reference support for Python.
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 This module is an implementation of PEP 205:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4
46571
47233a17e970 Fixed URL of PEP 205 in weakref's module docstring.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 38079
diff changeset
5 http://www.python.org/dev/peps/pep-0205/
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 """
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
8 # Naming convention: Variables named "wr" are weak reference objects;
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
9 # they are called this instead of "ref" to avoid name collisions with
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
10 # the module-global ref() function imported from _weakref.
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
11
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 import UserDict
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13
33356
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
14 from _weakref import (
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
15 getweakrefcount,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
16 getweakrefs,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
17 ref,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
18 proxy,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
19 CallableProxyType,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
20 ProxyType,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
21 ReferenceType)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 54099
diff changeset
23 from _weakrefset import WeakSet
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 54099
diff changeset
24
20281
380d4b4ee602 ReferenceError is now built-in, so pick it up from the right place.
Fred Drake <fdrake@acm.org>
parents: 20155
diff changeset
25 from exceptions import ReferenceError
380d4b4ee602 ReferenceError is now built-in, so pick it up from the right place.
Fred Drake <fdrake@acm.org>
parents: 20155
diff changeset
26
380d4b4ee602 ReferenceError is now built-in, so pick it up from the right place.
Fred Drake <fdrake@acm.org>
parents: 20155
diff changeset
27
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28 ProxyTypes = (ProxyType, CallableProxyType)
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29
17386
bbcc9837312e mapping(): Remove this function since it does not add anything to the API.
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
30 __all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs",
48278
62c69d88e985 Update __all__ for cookielib, csv, os, and urllib2 for objects imported into
Brett Cannon <bcannon@gmail.com>
parents: 46571
diff changeset
31 "WeakKeyDictionary", "ReferenceError", "ReferenceType", "ProxyType",
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 54099
diff changeset
32 "CallableProxyType", "ProxyTypes", "WeakValueDictionary", 'WeakSet']
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
34
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
35 class WeakValueDictionary(UserDict.UserDict):
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
36 """Mapping class that references values weakly.
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
38 Entries in the dictionary will be discarded when no strong
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
39 reference to the value exists anymore
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
40 """
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 # We inherit the constructor without worrying about the input
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 # dictionary; since it uses our .update() method, we get the right
16884
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
43 # checks (if the other dictionary is a WeakValueDictionary,
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
44 # objects are unwrapped on the way out, and we always wrap on the
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
45 # way in).
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
47 def __init__(self, *args, **kw):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
48 def remove(wr, selfref=ref(self)):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
49 self = selfref()
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
50 if self is not None:
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
51 del self.data[wr.key]
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
52 self._remove = remove
35018
40f7e1da1bdf Backport bug #1196315: fix weakref.WeakValueDictionary constructor.
Georg Brandl <georg@python.org>
parents: 33356
diff changeset
53 UserDict.UserDict.__init__(self, *args, **kw)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
54
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 def __getitem__(self, key):
18952
e90a658a58d4 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents: 17650
diff changeset
56 o = self.data[key]()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 if o is None:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58 raise KeyError, key
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 else:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 return o
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61
33197
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
62 def __contains__(self, key):
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
63 try:
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
64 o = self.data[key]()
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
65 except KeyError:
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
66 return False
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
67 return o is not None
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
68
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
69 def has_key(self, key):
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
70 try:
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
71 o = self.data[key]()
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
72 except KeyError:
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
73 return False
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
74 return o is not None
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
75
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76 def __repr__(self):
16884
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
77 return "<WeakValueDictionary at %s>" % id(self)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
79 def __setitem__(self, key, value):
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
80 self.data[key] = KeyedRef(value, self._remove, key)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 def copy(self):
16884
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
83 new = WeakValueDictionary()
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
84 for key, wr in self.data.items():
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
85 o = wr()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86 if o is not None:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87 new[key] = o
16884
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
88 return new
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89
54099
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
90 __copy__ = copy
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
91
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
92 def __deepcopy__(self, memo):
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
93 from copy import deepcopy
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
94 new = self.__class__()
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
95 for key, wr in self.data.items():
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
96 o = wr()
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
97 if o is not None:
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
98 new[deepcopy(key, memo)] = o
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
99 return new
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
100
17546
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
101 def get(self, key, default=None):
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102 try:
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
103 wr = self.data[key]
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104 except KeyError:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 return default
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106 else:
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
107 o = wr()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108 if o is None:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 # This should only happen
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110 return default
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111 else:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112 return o
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
113
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
114 def items(self):
16417
901b94656928 WeakDictionary.items(): Do not allow (key,ref) pairs to leak out for
Fred Drake <fdrake@acm.org>
parents: 16385
diff changeset
115 L = []
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
116 for key, wr in self.data.items():
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
117 o = wr()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118 if o is not None:
16417
901b94656928 WeakDictionary.items(): Do not allow (key,ref) pairs to leak out for
Fred Drake <fdrake@acm.org>
parents: 16385
diff changeset
119 L.append((key, o))
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 return L
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
122 def iteritems(self):
33197
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
123 for wr in self.data.itervalues():
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
124 value = wr()
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
125 if value is not None:
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
126 yield wr.key, value
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
127
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
128 def iterkeys(self):
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
129 return self.data.iterkeys()
33197
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
130
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
131 def __iter__(self):
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
132 return self.data.iterkeys()
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
133
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
134 def itervaluerefs(self):
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
135 """Return an iterator that yields the weak references to the values.
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
136
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
137 The references are not guaranteed to be 'live' at the time
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
138 they are used, so the result of calling the references needs
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
139 to be checked before being used. This can be used to avoid
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
140 creating references that will cause the garbage collector to
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
141 keep the values around longer than needed.
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
142
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
143 """
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
144 return self.data.itervalues()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
145
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
146 def itervalues(self):
33197
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
147 for wr in self.data.itervalues():
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
148 obj = wr()
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
149 if obj is not None:
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
150 yield obj
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
151
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
152 def popitem(self):
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
153 while 1:
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
154 key, wr = self.data.popitem()
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
155 o = wr()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
156 if o is not None:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
157 return key, o
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
158
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
159 def pop(self, key, *args):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
160 try:
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
161 o = self.data.pop(key)()
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
162 except KeyError:
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
163 if args:
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
164 return args[0]
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
165 raise
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
166 if o is None:
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
167 raise KeyError, key
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
168 else:
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
169 return o
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
170
32155
efb84ed7377d The default argument in dict.setdefault() defaults to None.
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
171 def setdefault(self, key, default=None):
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
172 try:
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
173 wr = self.data[key]
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
174 except KeyError:
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
175 self.data[key] = KeyedRef(default, self._remove, key)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
176 return default
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
177 else:
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
178 return wr()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
179
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
180 def update(self, dict=None, **kwargs):
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
181 d = self.data
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
182 if dict is not None:
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
183 if not hasattr(dict, "items"):
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
184 dict = type({})(dict)
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
185 for key, o in dict.items():
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
186 d[key] = KeyedRef(o, self._remove, key)
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
187 if len(kwargs):
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
188 self.update(kwargs)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
189
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
190 def valuerefs(self):
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
191 """Return a list of weak references to the values.
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
192
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
193 The references are not guaranteed to be 'live' at the time
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
194 they are used, so the result of calling the references needs
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
195 to be checked before being used. This can be used to avoid
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
196 creating references that will cause the garbage collector to
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
197 keep the values around longer than needed.
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
198
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
199 """
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
200 return self.data.values()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
201
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
202 def values(self):
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
203 L = []
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
204 for wr in self.data.values():
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
205 o = wr()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
206 if o is not None:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
207 L.append(o)
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
208 return L
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
209
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
210
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
211 class KeyedRef(ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
212 """Specialized reference that includes a key corresponding to the value.
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
213
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
214 This is used in the WeakValueDictionary to avoid having to create
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
215 a function object for each key stored in the mapping. A shared
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
216 callback object can use the 'key' attribute of a KeyedRef instead
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
217 of getting a reference to the key from an enclosing scope.
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
218
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
219 """
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
220
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
221 __slots__ = "key",
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
222
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
223 def __new__(type, ob, callback, key):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
224 self = ref.__new__(type, ob, callback)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
225 self.key = key
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
226 return self
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
227
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
228 def __init__(self, ob, callback, key):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
229 super(KeyedRef, self).__init__(ob, callback)
20155
bbb353d31e25 Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents: 19763
diff changeset
230
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
231
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
232 class WeakKeyDictionary(UserDict.UserDict):
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
233 """ Mapping class that references keys weakly.
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
234
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
235 Entries in the dictionary will be discarded when there is no
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
236 longer a strong reference to the key. This can be used to
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
237 associate additional data with an object owned by other parts of
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
238 an application without adding attributes to those objects. This
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
239 can be especially useful with objects that override attribute
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
240 accesses.
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
241 """
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
242
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
243 def __init__(self, dict=None):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
244 self.data = {}
20155
bbb353d31e25 Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents: 19763
diff changeset
245 def remove(k, selfref=ref(self)):
bbb353d31e25 Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents: 19763
diff changeset
246 self = selfref()
bbb353d31e25 Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents: 19763
diff changeset
247 if self is not None:
bbb353d31e25 Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents: 19763
diff changeset
248 del self.data[k]
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
249 self._remove = remove
23764
d58fb5445118 SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
250 if dict is not None: self.update(dict)
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
251
19763
94d0f2eea047 Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
252 def __delitem__(self, key):
29257
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28240
diff changeset
253 del self.data[ref(key)]
19763
94d0f2eea047 Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
254
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
255 def __getitem__(self, key):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
256 return self.data[ref(key)]
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
257
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
258 def __repr__(self):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
259 return "<WeakKeyDictionary at %s>" % id(self)
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
260
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
261 def __setitem__(self, key, value):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
262 self.data[ref(key, self._remove)] = value
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
263
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
264 def copy(self):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
265 new = WeakKeyDictionary()
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
266 for key, value in self.data.items():
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
267 o = key()
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
268 if o is not None:
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
269 new[o] = value
16884
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
270 return new
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
271
54099
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
272 __copy__ = copy
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
273
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
274 def __deepcopy__(self, memo):
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
275 from copy import deepcopy
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
276 new = self.__class__()
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
277 for key, value in self.data.items():
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
278 o = key()
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
279 if o is not None:
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
280 new[o] = deepcopy(value, memo)
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
281 return new
c40f187af6dd Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48278
diff changeset
282
17546
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
283 def get(self, key, default=None):
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
284 return self.data.get(ref(key),default)
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
285
17546
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
286 def has_key(self, key):
20902
d3f3d21ac04b WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents: 20281
diff changeset
287 try:
d3f3d21ac04b WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents: 20281
diff changeset
288 wr = ref(key)
d3f3d21ac04b WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents: 20281
diff changeset
289 except TypeError:
d3f3d21ac04b WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents: 20281
diff changeset
290 return 0
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
291 return wr in self.data
17546
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
292
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
293 def __contains__(self, key):
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
294 try:
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
295 wr = ref(key)
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
296 except TypeError:
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
297 return 0
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
298 return wr in self.data
24263
e6033254df21 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23764
diff changeset
299
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
300 def items(self):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
301 L = []
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
302 for key, value in self.data.items():
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
303 o = key()
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
304 if o is not None:
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
305 L.append((o, value))
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
306 return L
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
307
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
308 def iteritems(self):
33197
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
309 for wr, value in self.data.iteritems():
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
310 key = wr()
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
311 if key is not None:
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
312 yield key, value
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
313
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
314 def iterkeyrefs(self):
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
315 """Return an iterator that yields the weak references to the keys.
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
316
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
317 The references are not guaranteed to be 'live' at the time
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
318 they are used, so the result of calling the references needs
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
319 to be checked before being used. This can be used to avoid
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
320 creating references that will cause the garbage collector to
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
321 keep the keys around longer than needed.
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
322
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
323 """
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
324 return self.data.iterkeys()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
325
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
326 def iterkeys(self):
33197
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
327 for wr in self.data.iterkeys():
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
328 obj = wr()
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
329 if obj is not None:
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
330 yield obj
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
331
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
332 def __iter__(self):
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
333 return self.iterkeys()
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
334
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
335 def itervalues(self):
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
336 return self.data.itervalues()
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
337
38079
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
338 def keyrefs(self):
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
339 """Return a list of weak references to the keys.
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
340
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
341 The references are not guaranteed to be 'live' at the time
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
342 they are used, so the result of calling the references needs
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
343 to be checked before being used. This can be used to avoid
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
344 creating references that will cause the garbage collector to
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
345 keep the keys around longer than needed.
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
346
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
347 """
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
348 return self.data.keys()
728c16f16267 SF #1479988: add methods to allow access to weakrefs for the
Fred Drake <fdrake@acm.org>
parents: 35018
diff changeset
349
17546
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
350 def keys(self):
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
351 L = []
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
352 for wr in self.data.keys():
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
353 o = wr()
17546
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
354 if o is not None:
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
355 L.append(o)
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
356 return L
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
357
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
358 def popitem(self):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
359 while 1:
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
360 key, value = self.data.popitem()
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
361 o = key()
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
362 if o is not None:
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
363 return o, value
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
364
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
365 def pop(self, key, *args):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
366 return self.data.pop(ref(key), *args)
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
367
32155
efb84ed7377d The default argument in dict.setdefault() defaults to None.
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
368 def setdefault(self, key, default=None):
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
369 return self.data.setdefault(ref(key, self._remove),default)
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
370
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
371 def update(self, dict=None, **kwargs):
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
372 d = self.data
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
373 if dict is not None:
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
374 if not hasattr(dict, "items"):
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
375 dict = type({})(dict)
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
376 for key, value in dict.items():
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
377 d[ref(key, self._remove)] = value
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
378 if len(kwargs):
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
379 self.update(kwargs)