本文整理汇总了Python中weakref.WeakMethod方法的典型用法代码示例。如果您正苦于以下问题:Python weakref.WeakMethod方法的具体用法?Python weakref.WeakMethod怎么用?Python weakref.WeakMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weakref
的用法示例。
在下文中一共展示了weakref.WeakMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def connect(self, s, func):
"""Register *func* to be called when signal *s* is generated.
"""
self._func_cid_map.setdefault(s, {})
try:
proxy = WeakMethod(func, self._remove_proxy)
except TypeError:
proxy = _StrongRef(func)
if proxy in self._func_cid_map[s]:
return self._func_cid_map[s][proxy]
cid = next(self._cid_gen)
self._func_cid_map[s][proxy] = cid
self.callbacks.setdefault(s, {})
self.callbacks[s][cid] = proxy
return cid
示例2: _dispatch
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def _dispatch(self, event, sender, **kwargs):
"""Issue a dispatch for `event` coming from `sender.
Parameters
----------
event: str
event name from EVENTS
sender: DispatchClient
object requesting the dispatch
**kwargs
"""
for client, callback_ref in self.get_clients_dict(event).items():
logging.debug("Central dispatch calling {} about {}.".format(type(client).__name__, event))
callback_ref(event, sender=sender, **kwargs)
# When using WeakMethod references, this should rather be:
# callback_ref()(event, sender=sender, **kwargs)
示例3: test_hashing
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def test_hashing(self):
# Alive WeakMethods are hashable if the underlying object is
# hashable.
x = Object(1)
y = Object(1)
a = weakref.WeakMethod(x.some_method)
b = weakref.WeakMethod(y.some_method)
c = weakref.WeakMethod(y.other_method)
# Since WeakMethod objects are equal, the hashes should be equal.
self.assertEqual(hash(a), hash(b))
ha = hash(a)
# Dead WeakMethods retain their old hash value
del x, y
gc.collect()
self.assertEqual(hash(a), ha)
self.assertEqual(hash(b), ha)
# If it wasn't hashed when alive, a dead WeakMethod cannot be hashed.
self.assertRaises(TypeError, hash, c)
示例4: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def __init__(self):
super().__init__()
self._state = MediaState.Null
self._elements = []
self._old_pipe = ''
self._loop_count = 0
self._gst_pipe = Gst.Pipeline()
self._gst_state = Gst.State.NULL
self._time_query = Gst.Query.new_position(Gst.Format.TIME)
bus = self._gst_pipe.get_bus()
bus.add_signal_watch()
# Use a weakref instead of the method or the object will not be
# garbage-collected
on_message = weakref.WeakMethod(self.__on_message)
handler = bus.connect('message', lambda *args: on_message()(*args))
weakref.finalize(self, self.__finalizer, self._gst_pipe, handler,
self._elements)
self.changed('loop').connect(self.__prepare_loops)
self.changed('pipe').connect(self.__prepare_pipe)
示例5: connect
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def connect(self, slot_callable, mode=Connection.Direct):
"""Connect the given slot, if not already connected.
:param slot_callable: The slot (a python callable) to be connected
:param mode: Connection mode
:type mode: Connection
:raise ValueError: if mode not in Connection enum
"""
if mode not in Connection:
raise ValueError('invalid mode value: {0}'.format(mode))
with self.__lock:
# Create a new Slot object, use a weakref for the callback
# to avoid cyclic references.
callback = weak_call_proxy(weakref.WeakMethod(self.__remove_slot))
self.__slots[slot_id(slot_callable)] = mode.new_slot(slot_callable,
callback)
示例6: _remove_handler
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def _remove_handler(self, name, handler):
"""Used internally to remove all handler instances for the given event name.
This is normally called from a dead ``WeakMethod`` to remove itself from the
event stack.
"""
# Iterate over a copy as we might mutate the list
for frame in list(self._event_stack):
if name in frame:
try:
if frame[name] == handler:
del frame[name]
if not frame:
self._event_stack.remove(frame)
except TypeError:
# weakref is already dead
pass
示例7: connect
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def connect(self, slot):
"""Connect a slot to the signal.
If the input slot is already connected, nothing is done.
Args:
slot: Instance method or function to connect.
"""
# Create a weak reference to the method or function.
if inspect.ismethod(slot):
wr_slot = weakref.WeakMethod(slot)
else:
wr_slot = weakref.ref(slot)
# Only insert the slot if it doesn't already exist. If the index
# method throws, the slot doesn't exist yet. Insert it at the
# beginning so that when we call the slots in reverse order, they
# will be called in the order inserted.
if wr_slot not in self.slots:
self.slots.insert(0, wr_slot)
#-----------------------------------------------------------------------
示例8: disconnect
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def disconnect(self, slot):
"""Disconnect a slot from the signal.
If the input slot is not connected, nothing is done.
Args:
slot: Instance method or function to disconnect.
"""
# Create a weak reference to the method or function so that we can
# use the comparison operator on the weakref to find the slot.
if inspect.ismethod(slot):
wr_slot = weakref.WeakMethod(slot)
else:
wr_slot = weakref.ref(slot)
try:
self.slots.remove(wr_slot)
except ValueError:
pass
#-----------------------------------------------------------------------
示例9: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def __init__(self, func, asynchronous=False, priority=PRIORITIES.NONE, times=None, identifier=None, **kw):
self.func = kwargs_resilient(func)
self._func = get_original_func(func) # save the original funtion so we can unregister based on the function
# identifier only applicable to methods
if inspect.ismethod(self._func) or isinstance(self._func, weakref.WeakMethod):
self.identifier = identifier
else:
self.identifier = None
# Backwards compatibility
self.asynchronous = kw.pop("async", asynchronous)
assert not kw, "SignalHandler's kwargs should contain only 'async' argument for backwards compatibility"
self.priority = priority
self.times = times
self.idx = next(self._idx_gen)
if isinstance(func, weakref.WeakMethod):
func = func() # to allow accessing it's __code__ and __name__
self.filename = func.__code__.co_filename
self.lineno = func.__code__.co_firstlineno
self.name = self.__name__ = func.__name__
示例10: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def __init__(self, slot, weak=False):
self._weak = weak or isinstance(slot, weakref.ref)
if weak and not isinstance(slot, weakref.ref):
if isinstance(slot, types.MethodType):
slot = WeakMethod(slot)
else:
slot = weakref.ref(slot)
self._slot = slot
示例11: __set__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def __set__(self, obj, value):
if value is None:
self.__delete__(obj)
else:
try:
weakMethod = weakref.WeakMethod(value)
except TypeError:
setattr(obj, self.weakrefCallbackName, lambda: value)
else:
setattr(obj, self.weakrefCallbackName, weakMethod)
示例12: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def __init__(self):
self.clients_dict = {event: weakref.WeakKeyDictionary() for event in EVENTS} # central dispatch information
# For each event, store a dict that maps the clients registered for that event to their callback routines
# The objects are keys in the inner dict, implemented as a WeakKeyDictionary to allow deletion/garbage collection
# when object should expire. Callback methods are stored as weakref.WeakMethod for the same reason.
示例13: register
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def register(self, event, who, callback=None):
"""
Register object `who` for event `event`. (This modifies `clients_dict`.)
Parameters
----------
event: str
event name from EVENTS
who: DispatchClient
object to be registered
callback: method, optional
custom callback method other than `.receive()`
"""
logging.debug("Registering {} for {}. welcome.".format(type(who).__name__, event))
if callback is None:
callback_ref = getattr(who, 'receive')
# For purposes of garbage collection, this should preferably be:
# callback_ref = weakref.WeakMethod(getattr(who, 'receive'))
# However, as of 06/12/20, pathos balks on this on Windows (while Linux is passing).
# Note that reference to callback methods is likely to prevent proper garbage collection,
# so may have to revisit this issue if necessary.
else:
callback_ref = callback
# For purposes of garbage collection, this should preferably be:
# callback_ref = weakref.WeakMethod(callback)
# However, as of 06/12/20, pathos balks on this on Windows (while Linux is passing).
# Note that the reference to callback methods is likely to prevent proper garbage collection,
# so may have to revisit this issue if necessary.
self.get_clients_dict(event)[who] = callback_ref
示例14: test_alive
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def test_alive(self):
o = Object(1)
r = weakref.WeakMethod(o.some_method)
self.assertIsInstance(r, weakref.ReferenceType)
self.assertIsInstance(r(), type(o.some_method))
self.assertIs(r().__self__, o)
self.assertIs(r().__func__, o.some_method.__func__)
self.assertEqual(r()(), 4)
示例15: test_object_dead
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakMethod [as 别名]
def test_object_dead(self):
o = Object(1)
r = weakref.WeakMethod(o.some_method)
del o
gc.collect()
self.assertIs(r(), None)