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