同步操作将从 OpenHarmony-SIG/python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
"""Weak reference support for Python.This module is an implementation of PEP 205:http://www.python.org/dev/peps/pep-0205/"""# Naming convention: Variables named "wr" are weak reference objects;# they are called this instead of "ref" to avoid name collisions with# the module-global ref() function imported from _weakref.from _weakref import (getweakrefcount,getweakrefs,ref,proxy,CallableProxyType,ProxyType,ReferenceType,_remove_dead_weakref)from _weakrefset import WeakSet, _IterationGuardimport _collections_abc # Import after _weakref to avoid circular import.import sysimport itertoolsProxyTypes = (ProxyType, CallableProxyType)__all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs","WeakKeyDictionary", "ReferenceType", "ProxyType","CallableProxyType", "ProxyTypes", "WeakValueDictionary","WeakSet", "WeakMethod", "finalize"]class WeakMethod(ref):"""A custom `weakref.ref` subclass which simulates a weak reference toa bound method, working around the lifetime problem of bound methods."""__slots__ = "_func_ref", "_meth_type", "_alive", "__weakref__"def __new__(cls, meth, callback=None):try:obj = meth.__self__func = meth.__func__except AttributeError:raise TypeError("argument should be a bound method, not {}".format(type(meth))) from Nonedef _cb(arg):# The self-weakref trick is needed to avoid creating a reference# cycle.self = self_wr()if self._alive:self._alive = Falseif callback is not None:callback(self)self = ref.__new__(cls, obj, _cb)self._func_ref = ref(func, _cb)self._meth_type = type(meth)self._alive = Trueself_wr = ref(self)return selfdef __call__(self):obj = super().__call__()func = self._func_ref()if obj is None or func is None:return Nonereturn self._meth_type(func, obj)def __eq__(self, other):if isinstance(other, WeakMethod):if not self._alive or not other._alive:return self is otherreturn ref.__eq__(self, other) and self._func_ref == other._func_refreturn Falsedef __ne__(self, other):if isinstance(other, WeakMethod):if not self._alive or not other._alive:return self is not otherreturn ref.__ne__(self, other) or self._func_ref != other._func_refreturn True__hash__ = ref.__hash__class WeakValueDictionary(_collections_abc.MutableMapping):"""Mapping class that references values weakly.Entries in the dictionary will be discarded when no strongreference to the value exists anymore"""# We inherit the constructor without worrying about the input# dictionary; since it uses our .update() method, we get the right# checks (if the other dictionary is a WeakValueDictionary,# objects are unwrapped on the way out, and we always wrap on the# way in).def __init__(self, other=(), /, **kw):def remove(wr, selfref=ref(self), _atomic_removal=_remove_dead_weakref):self = selfref()if self is not None:if self._iterating:self._pending_removals.append(wr.key)else:# Atomic removal is necessary since this function# can be called asynchronously by the GC_atomic_removal(self.data, wr.key)self._remove = remove# A list of keys to be removedself._pending_removals = []self._iterating = set()self.data = {}self.update(other, **kw)def _commit_removals(self):l = self._pending_removalsd = self.data# We shouldn't encounter any KeyError, because this method should# always be called *before* mutating the dict.while l:key = l.pop()_remove_dead_weakref(d, key)def __getitem__(self, key):if self._pending_removals:self._commit_removals()o = self.data[key]()if o is None:raise KeyError(key)else:return odef __delitem__(self, key):if self._pending_removals:self._commit_removals()del self.data[key]def __len__(self):if self._pending_removals:self._commit_removals()return len(self.data)def __contains__(self, key):if self._pending_removals:self._commit_removals()try:o = self.data[key]()except KeyError:return Falsereturn o is not Nonedef __repr__(self):return "<%s at %#x>" % (self.__class__.__name__, id(self))def __setitem__(self, key, value):if self._pending_removals:self._commit_removals()self.data[key] = KeyedRef(value, self._remove, key)def copy(self):if self._pending_removals:self._commit_removals()new = WeakValueDictionary()with _IterationGuard(self):for key, wr in self.data.items():o = wr()if o is not None:new[key] = oreturn new__copy__ = copydef __deepcopy__(self, memo):from copy import deepcopyif self._pending_removals:self._commit_removals()new = self.__class__()with _IterationGuard(self):for key, wr in self.data.items():o = wr()if o is not None:new[deepcopy(key, memo)] = oreturn newdef get(self, key, default=None):if self._pending_removals:self._commit_removals()try:wr = self.data[key]except KeyError:return defaultelse:o = wr()if o is None:# This should only happenreturn defaultelse:return odef items(self):if self._pending_removals:self._commit_removals()with _IterationGuard(self):for k, wr in self.data.items():v = wr()if v is not None:yield k, vdef keys(self):if self._pending_removals:self._commit_removals()with _IterationGuard(self):for k, wr in self.data.items():if wr() is not None:yield k__iter__ = keysdef itervaluerefs(self):"""Return an iterator that yields the weak references to the values.The references are not guaranteed to be 'live' at the timethey are used, so the result of calling the references needsto be checked before being used. This can be used to avoidcreating references that will cause the garbage collector tokeep the values around longer than needed."""if self._pending_removals:self._commit_removals()with _IterationGuard(self):yield from self.data.values()def values(self):if self._pending_removals:self._commit_removals()with _IterationGuard(self):for wr in self.data.values():obj = wr()if obj is not None:yield objdef popitem(self):if self._pending_removals:self._commit_removals()while True:key, wr = self.data.popitem()o = wr()if o is not None:return key, odef pop(self, key, *args):if self._pending_removals:self._commit_removals()try:o = self.data.pop(key)()except KeyError:o = Noneif o is None:if args:return args[0]else:raise KeyError(key)else:return odef setdefault(self, key, default=None):try:o = self.data[key]()except KeyError:o = Noneif o is None:if self._pending_removals:self._commit_removals()self.data[key] = KeyedRef(default, self._remove, key)return defaultelse:return odef update(self, other=None, /, **kwargs):if self._pending_removals:self._commit_removals()d = self.dataif other is not None:if not hasattr(other, "items"):other = dict(other)for key, o in other.items():d[key] = KeyedRef(o, self._remove, key)for key, o in kwargs.items():d[key] = KeyedRef(o, self._remove, key)def valuerefs(self):"""Return a list of weak references to the values.The references are not guaranteed to be 'live' at the timethey are used, so the result of calling the references needsto be checked before being used. This can be used to avoidcreating references that will cause the garbage collector tokeep the values around longer than needed."""if self._pending_removals:self._commit_removals()return list(self.data.values())class KeyedRef(ref):"""Specialized reference that includes a key corresponding to the value.This is used in the WeakValueDictionary to avoid having to createa function object for each key stored in the mapping. A sharedcallback object can use the 'key' attribute of a KeyedRef insteadof getting a reference to the key from an enclosing scope."""__slots__ = "key",def __new__(type, ob, callback, key):self = ref.__new__(type, ob, callback)self.key = keyreturn selfdef __init__(self, ob, callback, key):super().__init__(ob, callback)class WeakKeyDictionary(_collections_abc.MutableMapping):""" Mapping class that references keys weakly.Entries in the dictionary will be discarded when there is nolonger a strong reference to the key. This can be used toassociate additional data with an object owned by other parts ofan application without adding attributes to those objects. Thiscan be especially useful with objects that override attributeaccesses."""def __init__(self, dict=None):self.data = {}def remove(k, selfref=ref(self)):self = selfref()if self is not None:if self._iterating:self._pending_removals.append(k)else:del self.data[k]self._remove = remove# A list of dead weakrefs (keys to be removed)self._pending_removals = []self._iterating = set()self._dirty_len = Falseif dict is not None:self.update(dict)def _commit_removals(self):# NOTE: We don't need to call this method before mutating the dict,# because a dead weakref never compares equal to a live weakref,# even if they happened to refer to equal objects.# However, it means keys may already have been removed.l = self._pending_removalsd = self.datawhile l:try:del d[l.pop()]except KeyError:passdef _scrub_removals(self):d = self.dataself._pending_removals = [k for k in self._pending_removals if k in d]self._dirty_len = Falsedef __delitem__(self, key):self._dirty_len = Truedel self.data[ref(key)]def __getitem__(self, key):return self.data[ref(key)]def __len__(self):if self._dirty_len and self._pending_removals:# self._pending_removals may still contain keys which were# explicitly removed, we have to scrub them (see issue #21173).self._scrub_removals()return len(self.data) - len(self._pending_removals)def __repr__(self):return "<%s at %#x>" % (self.__class__.__name__, id(self))def __setitem__(self, key, value):self.data[ref(key, self._remove)] = valuedef copy(self):new = WeakKeyDictionary()with _IterationGuard(self):for key, value in self.data.items():o = key()if o is not None:new[o] = valuereturn new__copy__ = copydef __deepcopy__(self, memo):from copy import deepcopynew = self.__class__()with _IterationGuard(self):for key, value in self.data.items():o = key()if o is not None:new[o] = deepcopy(value, memo)return newdef get(self, key, default=None):return self.data.get(ref(key),default)def __contains__(self, key):try:wr = ref(key)except TypeError:return Falsereturn wr in self.datadef items(self):with _IterationGuard(self):for wr, value in self.data.items():key = wr()if key is not None:yield key, valuedef keys(self):with _IterationGuard(self):for wr in self.data:obj = wr()if obj is not None:yield obj__iter__ = keysdef values(self):with _IterationGuard(self):for wr, value in self.data.items():if wr() is not None:yield valuedef keyrefs(self):"""Return a list of weak references to the keys.The references are not guaranteed to be 'live' at the timethey are used, so the result of calling the references needsto be checked before being used. This can be used to avoidcreating references that will cause the garbage collector tokeep the keys around longer than needed."""return list(self.data)def popitem(self):self._dirty_len = Truewhile True:key, value = self.data.popitem()o = key()if o is not None:return o, valuedef pop(self, key, *args):self._dirty_len = Truereturn self.data.pop(ref(key), *args)def setdefault(self, key, default=None):return self.data.setdefault(ref(key, self._remove),default)def update(self, dict=None, /, **kwargs):d = self.dataif dict is not None:if not hasattr(dict, "items"):dict = type({})(dict)for key, value in dict.items():d[ref(key, self._remove)] = valueif len(kwargs):self.update(kwargs)class finalize:"""Class for finalization of weakrefable objectsfinalize(obj, func, *args, **kwargs) returns a callable finalizerobject which will be called when obj is garbage collected. Thefirst time the finalizer is called it evaluates func(*arg, **kwargs)and returns the result. After this the finalizer is dead, andcalling it just returns None.When the program exits any remaining finalizers for which theatexit attribute is true will be run in reverse order of creation.By default atexit is true."""# Finalizer objects don't have any state of their own. They are# just used as keys to lookup _Info objects in the registry. This# ensures that they cannot be part of a ref-cycle.__slots__ = ()_registry = {}_shutdown = False_index_iter = itertools.count()_dirty = False_registered_with_atexit = Falseclass _Info:__slots__ = ("weakref", "func", "args", "kwargs", "atexit", "index")def __init__(*args, **kwargs):if len(args) >= 3:self, obj, func, *args = argselif not args:raise TypeError("descriptor '__init__' of 'finalize' object ""needs an argument")else:if 'func' not in kwargs:raise TypeError('finalize expected at least 2 positional ''arguments, got %d' % (len(args)-1))func = kwargs.pop('func')if len(args) >= 2:self, obj, *args = argsimport warningswarnings.warn("Passing 'func' as keyword argument is deprecated",DeprecationWarning, stacklevel=2)else:if 'obj' not in kwargs:raise TypeError('finalize expected at least 2 positional ''arguments, got %d' % (len(args)-1))obj = kwargs.pop('obj')self, *args = argsimport warningswarnings.warn("Passing 'obj' as keyword argument is deprecated",DeprecationWarning, stacklevel=2)args = tuple(args)if not self._registered_with_atexit:# We may register the exit function more than once because# of a thread race, but that is harmlessimport atexitatexit.register(self._exitfunc)finalize._registered_with_atexit = Trueinfo = self._Info()info.weakref = ref(obj, self)info.func = funcinfo.args = argsinfo.kwargs = kwargs or Noneinfo.atexit = Trueinfo.index = next(self._index_iter)self._registry[self] = infofinalize._dirty = True__init__.__text_signature__ = '($self, obj, func, /, *args, **kwargs)'def __call__(self, _=None):"""If alive then mark as dead and return func(*args, **kwargs);otherwise return None"""info = self._registry.pop(self, None)if info and not self._shutdown:return info.func(*info.args, **(info.kwargs or {}))def detach(self):"""If alive then mark as dead and return (obj, func, args, kwargs);otherwise return None"""info = self._registry.get(self)obj = info and info.weakref()if obj is not None and self._registry.pop(self, None):return (obj, info.func, info.args, info.kwargs or {})def peek(self):"""If alive then return (obj, func, args, kwargs);otherwise return None"""info = self._registry.get(self)obj = info and info.weakref()if obj is not None:return (obj, info.func, info.args, info.kwargs or {})@propertydef alive(self):"""Whether finalizer is alive"""return self in self._registry@propertydef atexit(self):"""Whether finalizer should be called at exit"""info = self._registry.get(self)return bool(info) and info.atexit@atexit.setterdef atexit(self, value):info = self._registry.get(self)if info:info.atexit = bool(value)def __repr__(self):info = self._registry.get(self)obj = info and info.weakref()if obj is None:return '<%s object at %#x; dead>' % (type(self).__name__, id(self))else:return '<%s object at %#x; for %r at %#x>' % \(type(self).__name__, id(self), type(obj).__name__, id(obj))@classmethoddef _select_for_exit(cls):# Return live finalizers marked for exit, oldest firstL = [(f,i) for (f,i) in cls._registry.items() if i.atexit]L.sort(key=lambda item:item[1].index)return [f for (f,i) in L]@classmethoddef _exitfunc(cls):# At shutdown invoke finalizers for which atexit is true.# This is called once all other non-daemonic threads have been# joined.reenable_gc = Falsetry:if cls._registry:import gcif gc.isenabled():reenable_gc = Truegc.disable()pending = Nonewhile True:if pending is None or finalize._dirty:pending = cls._select_for_exit()finalize._dirty = Falseif not pending:breakf = pending.pop()try:# gc is disabled, so (assuming no daemonic# threads) the following is the only line in# this function which might trigger creation# of a new finalizerf()except Exception:sys.excepthook(*sys.exc_info())assert f not in cls._registryfinally:# prevent any more finalizers from executing during shutdownfinalize._shutdown = Trueif reenable_gc:gc.enable()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。