Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: weakref.py
"""Weak reference support for Python.
[0] Fix | Delete
[1] Fix | Delete
This module is an implementation of PEP 205:
[2] Fix | Delete
[3] Fix | Delete
http://www.python.org/dev/peps/pep-0205/
[4] Fix | Delete
"""
[5] Fix | Delete
[6] Fix | Delete
# Naming convention: Variables named "wr" are weak reference objects;
[7] Fix | Delete
# they are called this instead of "ref" to avoid name collisions with
[8] Fix | Delete
# the module-global ref() function imported from _weakref.
[9] Fix | Delete
[10] Fix | Delete
import UserDict
[11] Fix | Delete
[12] Fix | Delete
from _weakref import (
[13] Fix | Delete
getweakrefcount,
[14] Fix | Delete
getweakrefs,
[15] Fix | Delete
ref,
[16] Fix | Delete
proxy,
[17] Fix | Delete
CallableProxyType,
[18] Fix | Delete
ProxyType,
[19] Fix | Delete
ReferenceType)
[20] Fix | Delete
[21] Fix | Delete
from _weakrefset import WeakSet, _IterationGuard
[22] Fix | Delete
[23] Fix | Delete
from exceptions import ReferenceError
[24] Fix | Delete
[25] Fix | Delete
[26] Fix | Delete
ProxyTypes = (ProxyType, CallableProxyType)
[27] Fix | Delete
[28] Fix | Delete
__all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs",
[29] Fix | Delete
"WeakKeyDictionary", "ReferenceError", "ReferenceType", "ProxyType",
[30] Fix | Delete
"CallableProxyType", "ProxyTypes", "WeakValueDictionary", 'WeakSet']
[31] Fix | Delete
[32] Fix | Delete
[33] Fix | Delete
class WeakValueDictionary(UserDict.UserDict):
[34] Fix | Delete
"""Mapping class that references values weakly.
[35] Fix | Delete
[36] Fix | Delete
Entries in the dictionary will be discarded when no strong
[37] Fix | Delete
reference to the value exists anymore
[38] Fix | Delete
"""
[39] Fix | Delete
# We inherit the constructor without worrying about the input
[40] Fix | Delete
# dictionary; since it uses our .update() method, we get the right
[41] Fix | Delete
# checks (if the other dictionary is a WeakValueDictionary,
[42] Fix | Delete
# objects are unwrapped on the way out, and we always wrap on the
[43] Fix | Delete
# way in).
[44] Fix | Delete
[45] Fix | Delete
def __init__(*args, **kw):
[46] Fix | Delete
if not args:
[47] Fix | Delete
raise TypeError("descriptor '__init__' of 'WeakValueDictionary' "
[48] Fix | Delete
"object needs an argument")
[49] Fix | Delete
self = args[0]
[50] Fix | Delete
args = args[1:]
[51] Fix | Delete
if len(args) > 1:
[52] Fix | Delete
raise TypeError('expected at most 1 arguments, got %d' % len(args))
[53] Fix | Delete
def remove(wr, selfref=ref(self)):
[54] Fix | Delete
self = selfref()
[55] Fix | Delete
if self is not None:
[56] Fix | Delete
if self._iterating:
[57] Fix | Delete
self._pending_removals.append(wr.key)
[58] Fix | Delete
else:
[59] Fix | Delete
del self.data[wr.key]
[60] Fix | Delete
self._remove = remove
[61] Fix | Delete
# A list of keys to be removed
[62] Fix | Delete
self._pending_removals = []
[63] Fix | Delete
self._iterating = set()
[64] Fix | Delete
UserDict.UserDict.__init__(self, *args, **kw)
[65] Fix | Delete
[66] Fix | Delete
def _commit_removals(self):
[67] Fix | Delete
l = self._pending_removals
[68] Fix | Delete
d = self.data
[69] Fix | Delete
# We shouldn't encounter any KeyError, because this method should
[70] Fix | Delete
# always be called *before* mutating the dict.
[71] Fix | Delete
while l:
[72] Fix | Delete
del d[l.pop()]
[73] Fix | Delete
[74] Fix | Delete
def __getitem__(self, key):
[75] Fix | Delete
o = self.data[key]()
[76] Fix | Delete
if o is None:
[77] Fix | Delete
raise KeyError, key
[78] Fix | Delete
else:
[79] Fix | Delete
return o
[80] Fix | Delete
[81] Fix | Delete
def __delitem__(self, key):
[82] Fix | Delete
if self._pending_removals:
[83] Fix | Delete
self._commit_removals()
[84] Fix | Delete
del self.data[key]
[85] Fix | Delete
[86] Fix | Delete
def __contains__(self, key):
[87] Fix | Delete
try:
[88] Fix | Delete
o = self.data[key]()
[89] Fix | Delete
except KeyError:
[90] Fix | Delete
return False
[91] Fix | Delete
return o is not None
[92] Fix | Delete
[93] Fix | Delete
def has_key(self, key):
[94] Fix | Delete
try:
[95] Fix | Delete
o = self.data[key]()
[96] Fix | Delete
except KeyError:
[97] Fix | Delete
return False
[98] Fix | Delete
return o is not None
[99] Fix | Delete
[100] Fix | Delete
def __repr__(self):
[101] Fix | Delete
return "<WeakValueDictionary at %s>" % id(self)
[102] Fix | Delete
[103] Fix | Delete
def __setitem__(self, key, value):
[104] Fix | Delete
if self._pending_removals:
[105] Fix | Delete
self._commit_removals()
[106] Fix | Delete
self.data[key] = KeyedRef(value, self._remove, key)
[107] Fix | Delete
[108] Fix | Delete
def clear(self):
[109] Fix | Delete
if self._pending_removals:
[110] Fix | Delete
self._commit_removals()
[111] Fix | Delete
self.data.clear()
[112] Fix | Delete
[113] Fix | Delete
def copy(self):
[114] Fix | Delete
new = WeakValueDictionary()
[115] Fix | Delete
for key, wr in self.data.items():
[116] Fix | Delete
o = wr()
[117] Fix | Delete
if o is not None:
[118] Fix | Delete
new[key] = o
[119] Fix | Delete
return new
[120] Fix | Delete
[121] Fix | Delete
__copy__ = copy
[122] Fix | Delete
[123] Fix | Delete
def __deepcopy__(self, memo):
[124] Fix | Delete
from copy import deepcopy
[125] Fix | Delete
new = self.__class__()
[126] Fix | Delete
for key, wr in self.data.items():
[127] Fix | Delete
o = wr()
[128] Fix | Delete
if o is not None:
[129] Fix | Delete
new[deepcopy(key, memo)] = o
[130] Fix | Delete
return new
[131] Fix | Delete
[132] Fix | Delete
def get(self, key, default=None):
[133] Fix | Delete
try:
[134] Fix | Delete
wr = self.data[key]
[135] Fix | Delete
except KeyError:
[136] Fix | Delete
return default
[137] Fix | Delete
else:
[138] Fix | Delete
o = wr()
[139] Fix | Delete
if o is None:
[140] Fix | Delete
# This should only happen
[141] Fix | Delete
return default
[142] Fix | Delete
else:
[143] Fix | Delete
return o
[144] Fix | Delete
[145] Fix | Delete
def items(self):
[146] Fix | Delete
L = []
[147] Fix | Delete
for key, wr in self.data.items():
[148] Fix | Delete
o = wr()
[149] Fix | Delete
if o is not None:
[150] Fix | Delete
L.append((key, o))
[151] Fix | Delete
return L
[152] Fix | Delete
[153] Fix | Delete
def iteritems(self):
[154] Fix | Delete
with _IterationGuard(self):
[155] Fix | Delete
for wr in self.data.itervalues():
[156] Fix | Delete
value = wr()
[157] Fix | Delete
if value is not None:
[158] Fix | Delete
yield wr.key, value
[159] Fix | Delete
[160] Fix | Delete
def iterkeys(self):
[161] Fix | Delete
with _IterationGuard(self):
[162] Fix | Delete
for k in self.data.iterkeys():
[163] Fix | Delete
yield k
[164] Fix | Delete
[165] Fix | Delete
__iter__ = iterkeys
[166] Fix | Delete
[167] Fix | Delete
def itervaluerefs(self):
[168] Fix | Delete
"""Return an iterator that yields the weak references to the values.
[169] Fix | Delete
[170] Fix | Delete
The references are not guaranteed to be 'live' at the time
[171] Fix | Delete
they are used, so the result of calling the references needs
[172] Fix | Delete
to be checked before being used. This can be used to avoid
[173] Fix | Delete
creating references that will cause the garbage collector to
[174] Fix | Delete
keep the values around longer than needed.
[175] Fix | Delete
[176] Fix | Delete
"""
[177] Fix | Delete
with _IterationGuard(self):
[178] Fix | Delete
for wr in self.data.itervalues():
[179] Fix | Delete
yield wr
[180] Fix | Delete
[181] Fix | Delete
def itervalues(self):
[182] Fix | Delete
with _IterationGuard(self):
[183] Fix | Delete
for wr in self.data.itervalues():
[184] Fix | Delete
obj = wr()
[185] Fix | Delete
if obj is not None:
[186] Fix | Delete
yield obj
[187] Fix | Delete
[188] Fix | Delete
def popitem(self):
[189] Fix | Delete
if self._pending_removals:
[190] Fix | Delete
self._commit_removals()
[191] Fix | Delete
while 1:
[192] Fix | Delete
key, wr = self.data.popitem()
[193] Fix | Delete
o = wr()
[194] Fix | Delete
if o is not None:
[195] Fix | Delete
return key, o
[196] Fix | Delete
[197] Fix | Delete
def pop(self, key, *args):
[198] Fix | Delete
if self._pending_removals:
[199] Fix | Delete
self._commit_removals()
[200] Fix | Delete
try:
[201] Fix | Delete
o = self.data.pop(key)()
[202] Fix | Delete
except KeyError:
[203] Fix | Delete
if args:
[204] Fix | Delete
return args[0]
[205] Fix | Delete
raise
[206] Fix | Delete
if o is None:
[207] Fix | Delete
raise KeyError, key
[208] Fix | Delete
else:
[209] Fix | Delete
return o
[210] Fix | Delete
[211] Fix | Delete
def setdefault(self, key, default=None):
[212] Fix | Delete
try:
[213] Fix | Delete
wr = self.data[key]
[214] Fix | Delete
except KeyError:
[215] Fix | Delete
if self._pending_removals:
[216] Fix | Delete
self._commit_removals()
[217] Fix | Delete
self.data[key] = KeyedRef(default, self._remove, key)
[218] Fix | Delete
return default
[219] Fix | Delete
else:
[220] Fix | Delete
return wr()
[221] Fix | Delete
[222] Fix | Delete
def update(*args, **kwargs):
[223] Fix | Delete
if not args:
[224] Fix | Delete
raise TypeError("descriptor 'update' of 'WeakValueDictionary' "
[225] Fix | Delete
"object needs an argument")
[226] Fix | Delete
self = args[0]
[227] Fix | Delete
args = args[1:]
[228] Fix | Delete
if len(args) > 1:
[229] Fix | Delete
raise TypeError('expected at most 1 arguments, got %d' % len(args))
[230] Fix | Delete
dict = args[0] if args else None
[231] Fix | Delete
if self._pending_removals:
[232] Fix | Delete
self._commit_removals()
[233] Fix | Delete
d = self.data
[234] Fix | Delete
if dict is not None:
[235] Fix | Delete
if not hasattr(dict, "items"):
[236] Fix | Delete
dict = type({})(dict)
[237] Fix | Delete
for key, o in dict.items():
[238] Fix | Delete
d[key] = KeyedRef(o, self._remove, key)
[239] Fix | Delete
if len(kwargs):
[240] Fix | Delete
self.update(kwargs)
[241] Fix | Delete
[242] Fix | Delete
def valuerefs(self):
[243] Fix | Delete
"""Return a list of weak references to the values.
[244] Fix | Delete
[245] Fix | Delete
The references are not guaranteed to be 'live' at the time
[246] Fix | Delete
they are used, so the result of calling the references needs
[247] Fix | Delete
to be checked before being used. This can be used to avoid
[248] Fix | Delete
creating references that will cause the garbage collector to
[249] Fix | Delete
keep the values around longer than needed.
[250] Fix | Delete
[251] Fix | Delete
"""
[252] Fix | Delete
return self.data.values()
[253] Fix | Delete
[254] Fix | Delete
def values(self):
[255] Fix | Delete
L = []
[256] Fix | Delete
for wr in self.data.values():
[257] Fix | Delete
o = wr()
[258] Fix | Delete
if o is not None:
[259] Fix | Delete
L.append(o)
[260] Fix | Delete
return L
[261] Fix | Delete
[262] Fix | Delete
[263] Fix | Delete
class KeyedRef(ref):
[264] Fix | Delete
"""Specialized reference that includes a key corresponding to the value.
[265] Fix | Delete
[266] Fix | Delete
This is used in the WeakValueDictionary to avoid having to create
[267] Fix | Delete
a function object for each key stored in the mapping. A shared
[268] Fix | Delete
callback object can use the 'key' attribute of a KeyedRef instead
[269] Fix | Delete
of getting a reference to the key from an enclosing scope.
[270] Fix | Delete
[271] Fix | Delete
"""
[272] Fix | Delete
[273] Fix | Delete
__slots__ = "key",
[274] Fix | Delete
[275] Fix | Delete
def __new__(type, ob, callback, key):
[276] Fix | Delete
self = ref.__new__(type, ob, callback)
[277] Fix | Delete
self.key = key
[278] Fix | Delete
return self
[279] Fix | Delete
[280] Fix | Delete
def __init__(self, ob, callback, key):
[281] Fix | Delete
super(KeyedRef, self).__init__(ob, callback)
[282] Fix | Delete
[283] Fix | Delete
[284] Fix | Delete
class WeakKeyDictionary(UserDict.UserDict):
[285] Fix | Delete
""" Mapping class that references keys weakly.
[286] Fix | Delete
[287] Fix | Delete
Entries in the dictionary will be discarded when there is no
[288] Fix | Delete
longer a strong reference to the key. This can be used to
[289] Fix | Delete
associate additional data with an object owned by other parts of
[290] Fix | Delete
an application without adding attributes to those objects. This
[291] Fix | Delete
can be especially useful with objects that override attribute
[292] Fix | Delete
accesses.
[293] Fix | Delete
"""
[294] Fix | Delete
[295] Fix | Delete
def __init__(self, dict=None):
[296] Fix | Delete
self.data = {}
[297] Fix | Delete
def remove(k, selfref=ref(self)):
[298] Fix | Delete
self = selfref()
[299] Fix | Delete
if self is not None:
[300] Fix | Delete
if self._iterating:
[301] Fix | Delete
self._pending_removals.append(k)
[302] Fix | Delete
else:
[303] Fix | Delete
del self.data[k]
[304] Fix | Delete
self._remove = remove
[305] Fix | Delete
# A list of dead weakrefs (keys to be removed)
[306] Fix | Delete
self._pending_removals = []
[307] Fix | Delete
self._iterating = set()
[308] Fix | Delete
if dict is not None:
[309] Fix | Delete
self.update(dict)
[310] Fix | Delete
[311] Fix | Delete
def _commit_removals(self):
[312] Fix | Delete
# NOTE: We don't need to call this method before mutating the dict,
[313] Fix | Delete
# because a dead weakref never compares equal to a live weakref,
[314] Fix | Delete
# even if they happened to refer to equal objects.
[315] Fix | Delete
# However, it means keys may already have been removed.
[316] Fix | Delete
l = self._pending_removals
[317] Fix | Delete
d = self.data
[318] Fix | Delete
while l:
[319] Fix | Delete
try:
[320] Fix | Delete
del d[l.pop()]
[321] Fix | Delete
except KeyError:
[322] Fix | Delete
pass
[323] Fix | Delete
[324] Fix | Delete
def __delitem__(self, key):
[325] Fix | Delete
del self.data[ref(key)]
[326] Fix | Delete
[327] Fix | Delete
def __getitem__(self, key):
[328] Fix | Delete
return self.data[ref(key)]
[329] Fix | Delete
[330] Fix | Delete
def __repr__(self):
[331] Fix | Delete
return "<WeakKeyDictionary at %s>" % id(self)
[332] Fix | Delete
[333] Fix | Delete
def __setitem__(self, key, value):
[334] Fix | Delete
self.data[ref(key, self._remove)] = value
[335] Fix | Delete
[336] Fix | Delete
def copy(self):
[337] Fix | Delete
new = WeakKeyDictionary()
[338] Fix | Delete
for key, value in self.data.items():
[339] Fix | Delete
o = key()
[340] Fix | Delete
if o is not None:
[341] Fix | Delete
new[o] = value
[342] Fix | Delete
return new
[343] Fix | Delete
[344] Fix | Delete
__copy__ = copy
[345] Fix | Delete
[346] Fix | Delete
def __deepcopy__(self, memo):
[347] Fix | Delete
from copy import deepcopy
[348] Fix | Delete
new = self.__class__()
[349] Fix | Delete
for key, value in self.data.items():
[350] Fix | Delete
o = key()
[351] Fix | Delete
if o is not None:
[352] Fix | Delete
new[o] = deepcopy(value, memo)
[353] Fix | Delete
return new
[354] Fix | Delete
[355] Fix | Delete
def get(self, key, default=None):
[356] Fix | Delete
return self.data.get(ref(key),default)
[357] Fix | Delete
[358] Fix | Delete
def has_key(self, key):
[359] Fix | Delete
try:
[360] Fix | Delete
wr = ref(key)
[361] Fix | Delete
except TypeError:
[362] Fix | Delete
return 0
[363] Fix | Delete
return wr in self.data
[364] Fix | Delete
[365] Fix | Delete
def __contains__(self, key):
[366] Fix | Delete
try:
[367] Fix | Delete
wr = ref(key)
[368] Fix | Delete
except TypeError:
[369] Fix | Delete
return 0
[370] Fix | Delete
return wr in self.data
[371] Fix | Delete
[372] Fix | Delete
def items(self):
[373] Fix | Delete
L = []
[374] Fix | Delete
for key, value in self.data.items():
[375] Fix | Delete
o = key()
[376] Fix | Delete
if o is not None:
[377] Fix | Delete
L.append((o, value))
[378] Fix | Delete
return L
[379] Fix | Delete
[380] Fix | Delete
def iteritems(self):
[381] Fix | Delete
with _IterationGuard(self):
[382] Fix | Delete
for wr, value in self.data.iteritems():
[383] Fix | Delete
key = wr()
[384] Fix | Delete
if key is not None:
[385] Fix | Delete
yield key, value
[386] Fix | Delete
[387] Fix | Delete
def iterkeyrefs(self):
[388] Fix | Delete
"""Return an iterator that yields the weak references to the keys.
[389] Fix | Delete
[390] Fix | Delete
The references are not guaranteed to be 'live' at the time
[391] Fix | Delete
they are used, so the result of calling the references needs
[392] Fix | Delete
to be checked before being used. This can be used to avoid
[393] Fix | Delete
creating references that will cause the garbage collector to
[394] Fix | Delete
keep the keys around longer than needed.
[395] Fix | Delete
[396] Fix | Delete
"""
[397] Fix | Delete
with _IterationGuard(self):
[398] Fix | Delete
for wr in self.data.iterkeys():
[399] Fix | Delete
yield wr
[400] Fix | Delete
[401] Fix | Delete
def iterkeys(self):
[402] Fix | Delete
with _IterationGuard(self):
[403] Fix | Delete
for wr in self.data.iterkeys():
[404] Fix | Delete
obj = wr()
[405] Fix | Delete
if obj is not None:
[406] Fix | Delete
yield obj
[407] Fix | Delete
[408] Fix | Delete
__iter__ = iterkeys
[409] Fix | Delete
[410] Fix | Delete
def itervalues(self):
[411] Fix | Delete
with _IterationGuard(self):
[412] Fix | Delete
for value in self.data.itervalues():
[413] Fix | Delete
yield value
[414] Fix | Delete
[415] Fix | Delete
def keyrefs(self):
[416] Fix | Delete
"""Return a list of weak references to the keys.
[417] Fix | Delete
[418] Fix | Delete
The references are not guaranteed to be 'live' at the time
[419] Fix | Delete
they are used, so the result of calling the references needs
[420] Fix | Delete
to be checked before being used. This can be used to avoid
[421] Fix | Delete
creating references that will cause the garbage collector to
[422] Fix | Delete
keep the keys around longer than needed.
[423] Fix | Delete
[424] Fix | Delete
"""
[425] Fix | Delete
return self.data.keys()
[426] Fix | Delete
[427] Fix | Delete
def keys(self):
[428] Fix | Delete
L = []
[429] Fix | Delete
for wr in self.data.keys():
[430] Fix | Delete
o = wr()
[431] Fix | Delete
if o is not None:
[432] Fix | Delete
L.append(o)
[433] Fix | Delete
return L
[434] Fix | Delete
[435] Fix | Delete
def popitem(self):
[436] Fix | Delete
while 1:
[437] Fix | Delete
key, value = self.data.popitem()
[438] Fix | Delete
o = key()
[439] Fix | Delete
if o is not None:
[440] Fix | Delete
return o, value
[441] Fix | Delete
[442] Fix | Delete
def pop(self, key, *args):
[443] Fix | Delete
return self.data.pop(ref(key), *args)
[444] Fix | Delete
[445] Fix | Delete
def setdefault(self, key, default=None):
[446] Fix | Delete
return self.data.setdefault(ref(key, self._remove),default)
[447] Fix | Delete
[448] Fix | Delete
def update(self, dict=None, **kwargs):
[449] Fix | Delete
d = self.data
[450] Fix | Delete
if dict is not None:
[451] Fix | Delete
if not hasattr(dict, "items"):
[452] Fix | Delete
dict = type({})(dict)
[453] Fix | Delete
for key, value in dict.items():
[454] Fix | Delete
d[ref(key, self._remove)] = value
[455] Fix | Delete
if len(kwargs):
[456] Fix | Delete
self.update(kwargs)
[457] Fix | Delete
[458] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function