예제 #1
0
    def register_agent(self, agent, form, is_form, success_cb, error_cb):
        if not app.account_is_connected(self._account):
            return

        weak_success_cb = weakref.WeakMethod(success_cb)
        weak_error_cb = weakref.WeakMethod(error_cb)

        iq = nbxmpp.Iq('set', nbxmpp.NS_REGISTER, to=agent)
        if is_form:
            query = iq.setQuery()
            form.setAttr('type', 'submit')
            query.addChild(node=form)
        else:
            for field in form.keys():
                iq.setTag('query').setTagData(field, form[field])

        self._con.connection.SendAndCallForResponse(
            iq, self._register_agent_response, {
                'agent': agent,
                'success_cb': weak_success_cb,
                'error_cb': weak_error_cb
            })

        self.agent_registrations[agent] = {
            'roster_push': False,
            'sub_received': False
        }
예제 #2
0
    def register(
        self, event: str, who: "DispatchClient", callback: Callable = None
    ) -> 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()`
        """
        LOGGER.debug(
            "Registering {} for {}. welcome.".format(type(who).__name__, event)
        )
        if callback is None:
            callback_ref = weakref.WeakMethod(getattr(who, "receive"))
            # For purposes of garbage collection, this should preferably be:
            # callback_ref = weakref.WeakMethod(getattr(who, 'receive'))
            # Intermittently, pathos has balked on pickling this. Workaround that
            # will likely prevent proper garbage collection:
            #
            # callback_ref = getattr(who, "receive")
        else:
            callback_ref = weakref.WeakMethod(callback)
            # See comment just above. Workaround if pathos fails to pickle:
            #
            # callback_ref = callback
        self.get_clients_dict(event)[who] = callback_ref
예제 #3
0
    def get_register_form(self, jid, success_cb, error_cb):
        if not app.account_is_connected(self._account):
            return

        weak_success_cb = weakref.WeakMethod(success_cb)
        weak_error_cb = weakref.WeakMethod(error_cb)

        iq = nbxmpp.Iq('get', nbxmpp.NS_REGISTER, to=jid)
        self._con.connection.SendAndCallForResponse(
            iq, self._register_info_response, {
                'success_cb': weak_success_cb,
                'error_cb': weak_error_cb
            })
예제 #4
0
 def test_hashing(self):
     x = Object(1)
     y = Object(1)
     a = weakref.WeakMethod(x.some_method)
     b = weakref.WeakMethod(y.some_method)
     c = weakref.WeakMethod(y.other_method)
     self.assertEqual(hash(a), hash(b))
     ha = hash(a)
     del x, y
     gc.collect()
     self.assertEqual(hash(a), ha)
     self.assertEqual(hash(b), ha)
     self.assertRaises(TypeError, hash, c)
예제 #5
0
파일: helpers.py 프로젝트: slokhorst/gajim
    def connect(self, signal_name, func):
        if inspect.ismethod(func):
            weak_func = weakref.WeakMethod(func)
        elif inspect.isfunction(func):
            weak_func = weakref.ref(func)

        self._callbacks[signal_name].append(weak_func)
예제 #6
0
        def _add_sub(self, port, topic, callback, host):
            def callback_wrapper(weak_callback, msg):
                [topic, obj_s] = msg # pylint: disable=unused-variable
                try:
                    if weak_callback and weak_callback():
                        weak_callback()(pickle.loads(obj_s))
                except Exception as ex:
                    print(ex, file=sys.stderr) # TODO: standardize this
                    raise

            # connect to stream socket
            context = zmq.Context()
            self.topic = topic.encode()
            self._socket = context.socket(zmq.SUB)

            utils.debug_log("Subscriber connecting...", (host, port), verbosity=1)
            self._socket.connect("tcp://%s:%d" % (host, port))
            utils.debug_log("Subscriber connected!", (host, port), verbosity=1)

            # setup socket filtering
            if topic != "":
                self._socket.setsockopt(zmq.SUBSCRIBE, self.topic)

            # if callback is specified then create a stream and set it 
            # for on_recv event - this would require running ioloop
            if callback is not None:
                self._stream = zmqstream.ZMQStream(self._socket)
                wr_cb = weakref.WeakMethod(callback)
                wrapper = functools.partial(callback_wrapper, wr_cb)
                self._stream.on_recv(wrapper)
예제 #7
0
    def __init__(self, obj):
        super().__setattr__('__hgxproxyref__', weakref.ref(obj))

        # Add some things to allow overrides
        overridable = {
            '__eq__', '__ge__', '__gt__', '__le__', '__lt__', '__ne__',
            '__subclasshook__'
        }

        objdir = set(dir(obj))
        forbidden = set(dir(self)) - overridable
        potential_overrides = objdir - forbidden

        for candidate_name in potential_overrides:
            candidate = getattr(obj, candidate_name)
            if inspect.ismethod(candidate):
                super().__setattr__(candidate_name,
                                    weakref.WeakMethod(candidate))
            elif inspect.isbuiltin(candidate):

                def weaker(*args, **kwargs):
                    proxy = super().__getattribute__('__hgxproxyref__')()
                    weakmethod = getattr(proxy, candidate_name)
                    return weakmethod(*args, **kwargs)

                super().__setattr__(candidate_name, weaker)
예제 #8
0
    def __init__(
        self,
        magic: object,
        client: HttpClient,
        paths: Iterable[str],
        update_timer: Optional[ManualResetTimer] = None,
        heartbeat_timer: Optional[ManualResetTimer] = None,
    ) -> None:
        assert (
            magic is self.__MAGIC
        ), "Do not construct an HttpTagSubscription directly. Use create() instead."
        super().__init__(paths, heartbeat_timer)
        self._api = client.at_uri("/nitag/v2/subscriptions")
        if update_timer is not None:
            self._update_timer = update_timer
        else:
            self._update_timer = ManualResetTimer(
                datetime.timedelta(
                    milliseconds=self._DEFAULT_POLLING_INTERVAL_MILLISECONDS))
            # _exit_stack is instantiated in the base class
            self._exit_stack.enter_context(self._update_timer)

        callback_ref = weakref.WeakMethod(
            self._update_timer_elapsed)  # type: ignore

        def callback() -> None:
            actual_callback = callback_ref()  # type: ignore
            if actual_callback:
                actual_callback()

        self._update_timer_handler = callback
        self._update_timer.elapsed += self._update_timer_handler
        self._token = None  # type: Optional[str]
예제 #9
0
 def addPygletListener(self, event_type, handler):
     """
     Registers an event handler.
     
     The specified callable handler will be called every time an event with the same ``event_type`` is encountered.
     
     All event arguments are passed as positional arguments.
     
     This method should be used to listen for pyglet events.
     For new code, it is recommended to use :py:meth:`addEventListener()` instead.
     
     See :py:meth:`handleEvent()` for information about tunneled pyglet events.
     
     For custom events, use :py:meth:`addEventListener()` instead.
     """
     if self.cfg["debug.events.register"]:
         print("Registered Event: %s Handler: %s" % (event_type, handler))
     if event_type not in self.pygletEventHandlers:
         self.pygletEventHandlers[event_type] = []
     # Only a weak reference is kept
     if inspect.ismethod(handler):
         handler = weakref.WeakMethod(handler)
     else:
         handler = weakref.ref(handler)
     self.pygletEventHandlers[event_type].append(handler)
예제 #10
0
파일: callbacks.py 프로젝트: cindolfi/mle
    def remove(self, callback):
        if inspect.ismethod(callback):
            callback = weakref.WeakMethod(callback)
        else:
            callback = weakref.ref(callback)

        self._callbacks.remove(callback)
예제 #11
0
 def __set__(self, obj, value):
     if isinstance(value, types.MethodType) \
             and hasattr(weakref, 'WeakMethod'):
         ref = weakref.WeakMethod(value, self.callback)
     else:
         ref = weakref.ref(value, self.callback)
     obj.__dict__[self.attrname] = ref
예제 #12
0
 def __init__(self, remote_fn, receiver, slot):
     self.key = "%r.%r->%r" % (remote_fn, receiver, slot)
     if self.key not in self.PROXY_FUNCTIONS:
         weak_fn = weakref.WeakMethod(remote_fn)
         weak_receiver = weakref.ref(receiver)
         self.PROXY_FUNCTIONS[self.key] = proxy_fn(weak_fn, weak_receiver, slot)
     self.proxy_function = self.PROXY_FUNCTIONS[self.key]
예제 #13
0
 def connect(self, listener):
     if hasattr(listener, '__self__'):
         listener_ref = weakref.WeakMethod(listener)
     else:
         listener_ref = weakref.ref(listener)
     self.listeners.append(listener_ref)
     return listener
예제 #14
0
    def test_subscribe_diff_callback_same_event(self, pubpen):
        first = pubpen.subscribe('test_event', function)
        foo = Foo()
        second = pubpen.subscribe('test_event', foo.method)

        # Test internals of subscribing worked
        assert pubpen._subscriptions[first] == 'test_event'
        assert pubpen._subscriptions[second] == 'test_event'
        assert len(pubpen._event_handlers['test_event']) == 2

        events = pubpen._event_handlers['test_event']
        assert events[first] != events[second]
        assert events[first] in (weakref.ref(function),
                                 weakref.WeakMethod(foo.method))
        assert events[second] in (weakref.ref(function),
                                  weakref.WeakMethod(foo.method))
예제 #15
0
    def add_callback(self, func):
        """
        Add a callback to receive responses.

        Parameters
        ----------
        func : callable
            Expected signature: ``func(response)``

        Returns
        -------
        token : int
            Integer token that can be passed to :meth:`remove_callback`.
        """
        def removed(_):
            self.remove_callback(cb_id)

        if inspect.ismethod(func):
            ref = weakref.WeakMethod(func, removed)
        else:
            # TODO: strong reference to non-instance methods?
            ref = weakref.ref(func, removed)

        with self._callback_lock:
            cb_id = self._callback_id
            self._callback_id += 1
            self.callbacks[cb_id] = ref
        return cb_id
예제 #16
0
def test_weakref_method():
    def handle():
        print("handle a")

    r = weakref.ref(handle)
    handle()
    r()()
    del handle
    if r() is not None:
        r()()
    # if handle:
    # handle()

    class C:
        def method(self):
            print("method called!", id(self))

    c = C()
    c.method()
    r = weakref.ref(c)
    r().method()
    r = weakref.WeakMethod(c.method)
    r()()
    del c
    # c.mehod()
    if r() is not None:
        r()()
예제 #17
0
    def __init__(self, actionType, game, activePlayer, callbackDataAlias,
                 doubtWelcomeTextTitle, continueActionHandler,
                 abortActionHandler):
        self.actionType = actionType
        self._game = weakref.ref(game)
        self.activePlayer = activePlayer
        self.doubtedPlayer = None
        self.callbackDataAlias = callbackDataAlias
        self.doubtWelcomeTextTitle = doubtWelcomeTextTitle
        self._continueActionHandler = weakref.WeakMethod(continueActionHandler)
        self._abortActionHandler = weakref.WeakMethod(abortActionHandler)

        self.timingMessageContext = None
        self.currentDoubtedPlayerPersonalMessageId = 0

        self.stateMachine = DoubtStateMachine()
예제 #18
0
def _saferef(listener):
    """
    Weak reference for a (possibly bound method) listener.

    Returns a weakref.WeakMethod reference for bound methods,
    and a regular weakref.ref otherwise.

    This means that for example ``_saferef(myobj.mymethod)``
    returns a reference whose lifetime is connected to the
    lifetime of the object ``myobj``, rather than the lifetime
    of the temporary method ``myobj.mymethod``.

    Parameters
    ----------
    listener : callable
        Listener to return a weak reference for. This can be
        either a plain function, a bound method, or some other
        form of callable.

    Returns
    -------
    weakref.ref
        A weak reference to the listener. This will be a ``weakref.WeakMethod``
        object if the listener is an instance of ``types.MethodType``, and a
        plain ``weakref.ref`` otherwise.

    """
    if isinstance(listener, types.MethodType):
        return weakref.WeakMethod(listener)
    else:
        return weakref.ref(listener)
예제 #19
0
    def add_callback(self, cb, way='direct', allow_run_cb_now=True):
        """
        Callback will be called with cb(job) upon stopping. May be called
        more than once if job is restarted.

        If job has run and is now stopped, this will be called immediately
        (in calling thread) so as to guarantee it runs at least once.

        `way` may be
        - 'direct': store direct reference to `cb`.
        - 'weak'  : store weak reference to `cb`
        - 'weakmethod' : store WeakMethod reference to `cb`.

         (Use 'weakmethod' for bound methods! See weakref documentation.
        """
        if way == 'direct':
            cbr = hardref(cb)
        elif way == 'weak':
            cbr = weakref.ref(cb)
        elif way == 'weakmethod':
            cbr = weakref.WeakMethod(cb)
        else:
            raise ValueError(way)
        with self._statelock:
            self.callbacks.append(cbr)
            if self.running or self.has_never_run:
                # We are waiting to run first time, or currently running.
                run_cb_now = False
            else:
                # We have run and we are now stopped.
                run_cb_now = True
        if run_cb_now and allow_run_cb_now:
            cb(self)
예제 #20
0
    def __init__(self,
                 receiver: callable,
                 expected_arguments: Union[List, None] = None) -> None:
        """
        AUTHORS:
        --------

        :author: Alix Leroy

        DESCRIPTION:
        ------------

        Initialize the connection with a weak reference to the method to connect

        PARAMETERS:
        -----------

        :param receiver(callable): The method to connect
        :param expected_arguments(Union[List, None]): The list of expected arguments (None means all the arguments)

        RETURN:
        -------

        :return: None

        """
        # Get the weak reference of the method to call
        self.receiver = weakref.WeakMethod(receiver)
        self.expected_arguments = expected_arguments
예제 #21
0
    def test_deleted_handler_silence_notifier(self):
        # If the handler is an instance method and the instance is garbage
        # collected, the notifier is silenced.

        # Create a dummy observer_handler otherwise the default mock object
        # keep references to call argument during the sanity check.
        def observer_handler(*args, **kwargs):
            pass

        instance = DummyClass()
        method_ref = weakref.WeakMethod(instance.dummy_method)
        target = mock.Mock()
        event_factory = mock.Mock()
        notifier = create_notifier(
            observer_handler=observer_handler,
            target=target,
            handler=instance.dummy_method,
            event_factory=event_factory,
        )

        # sanity check
        notifier(b=3)
        self.assertEqual(event_factory.call_count, 1)
        event_factory.reset_mock()

        # when
        del instance
        self.assertIsNone(method_ref())
        notifier(a=1, b=2)

        # then
        event_factory.assert_not_called()
예제 #22
0
def connect(sender, signal, receiver, slot):
    # print "Connect::", sender, signal, receiver, slot
    if sender is None:
        print("Connect Error::", sender, signal, receiver, slot)
        return False
    m = re.search(r"^(\w+)\.(\w+)(\(.*\))?", slot)
    if slot.endswith("()"): slot = slot[:-2]
    remote_fn = getattr(receiver, slot, None)
    if remote_fn:
        try:
            weak_fn = weakref.WeakMethod(remote_fn)
            weak_receiver = weakref.ref(receiver)
            QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), proxy_fn(weak_fn, weak_receiver, slot))
        except RuntimeError as e:
            print("ERROR Connecting:", sender, QtCore.SIGNAL(signal), remote_fn)
            print("ERROR %s : %s" % (e.__class__.__name__, str(e)))
            return False
    elif m:
        remote_obj = getattr(receiver, m.group(1), None)
        if remote_obj is None: raise AttributeError("Object %s not found on %s" % (remote_obj, str(receiver)))
        remote_fn = getattr(remote_obj, m.group(2), None)
        if remote_fn is None: raise AttributeError("Object %s not found on %s" % (remote_fn, remote_obj))
        try:
            QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), remote_fn)
        except RuntimeError as e:
            print("ERROR Connecting:", sender, QtCore.SIGNAL(signal), remote_fn)
            print("ERROR %s : %s" % (e.__class__.__name__, str(e)))
            return False

    else:
        if isinstance(receiver, QtCore.QObject):
            QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), receiver, QtCore.SLOT(slot))
        else:
            print("ERROR: Al realizar connect %r:%r -> %r:%r ; el slot no se reconoce y el receptor no es QObject." % (sender, signal, receiver, slot))
    return True
예제 #23
0
 def test_method_dead(self):
     C = self._subclass()
     o = C(1)
     r = weakref.WeakMethod(o.some_method)
     del C.some_method
     gc.collect()
     self.assertIs(r(), None)
예제 #24
0
 def add_callback(self, fnc, key=None):
     if key == None:
         key = id(fnc)
     if isinstance(fnc, types.MethodType):
         fnc = weakref.WeakMethod(fnc)
     self.observers[key] = fnc
     return key
예제 #25
0
 def WEAK(cls, f):
     if not hasattr(f, "__call__"):
         raise TypeError("must be callable, got {!r}".format(f))
     if isinstance(f, types.MethodType):
         ref = weakref.WeakMethod(f)
     else:
         ref = weakref.ref(f)
     return functools.partial(cls._weakref_wrapper, ref)
예제 #26
0
 def _subscribe(cls, *, func, event):
     if event not in cls._subscribers.keys():
         cls._subscribers[event] = []
     if is_bound(func):
         subscriber = weakref.WeakMethod(func)
     else:
         subscriber = weakref.ref(func)
     cls._subscribers[event].append(subscriber)
예제 #27
0
 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)
예제 #28
0
 def __bind(owner, widget, wkey, event, callback, _type):
     NBinder.__bindings.extend([[
         owner,
         weakref.ref(widget), wkey, event,
         weakref.WeakMethod(callback), _type
     ]])
     logging.debug("Global bindings after bind = {}".format(
         len(NBinder.__bindings)))
예제 #29
0
 def ref(self, item):
     if inspect.ismethod(item):
         try:
             item = weakref.WeakMethod(item, self.remove_all)
         finally:
             return item
     else:
         return super().ref(item)
예제 #30
0
 def _default_data_value(self, value):
     try:
         self._default_data_value_ref = weakref.WeakMethod(value)
     except TypeError:
         try:
             self._default_data_value_ref = weakref.ref(value)
         except TypeError:
             self._default_data_value_ref = value