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