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