How to use the blinker._saferef.safe_ref function in blinker

To help you get started, we’ve selected a few blinker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jek / blinker / tests / test_saferef.py View on Github external
def test_ShortCircuit(self):
        """Test that creation short-circuits to reuse existing references"""
        sd = {}
        for s in self.ss:
            sd[s] = 1
        for t in self.ts:
            if hasattr(t, 'x'):
                assert safe_ref(t.x) in sd
            else:
                assert safe_ref(t) in sd
github jek / blinker / tests / test_saferef.py View on Github external
def test_ShortCircuit(self):
        """Test that creation short-circuits to reuse existing references"""
        sd = {}
        for s in self.ss:
            sd[s] = 1
        for t in self.ts:
            if hasattr(t, 'x'):
                assert safe_ref(t.x) in sd
            else:
                assert safe_ref(t) in sd
github jek / blinker / tests / test_saferef.py View on Github external
def test_In(self):
        """Test the `in` operator for safe references (cmp)"""
        for t in self.ts[:50]:
            assert safe_ref(t.x) in self.ss
github jek / blinker / tests / test_saferef.py View on Github external
def setUp(self):
        ts = []
        ss = []
        for x in range(100):
            t = _Sample1()
            ts.append(t)
            s = safe_ref(t.x, self._closure)
            ss.append(s)
        ts.append(_sample2)
        ss.append(safe_ref(_sample2, self._closure))
        for x in range(30):
            t = _Sample3()
            ts.append(t)
            s = safe_ref(t, self._closure)
            ss.append(s)
        self.ts = ts
        self.ss = ss
        self.closure_count = 0
github jek / blinker / tests / test_saferef.py View on Github external
def setUp(self):
        ts = []
        ss = []
        for x in range(100):
            t = _Sample1()
            ts.append(t)
            s = safe_ref(t.x, self._closure)
            ss.append(s)
        ts.append(_sample2)
        ss.append(safe_ref(_sample2, self._closure))
        for x in range(30):
            t = _Sample3()
            ts.append(t)
            s = safe_ref(t, self._closure)
            ss.append(s)
        self.ts = ts
        self.ss = ss
        self.closure_count = 0
github jek / blinker / tests / test_saferef.py View on Github external
def setUp(self):
        ts = []
        ss = []
        for x in range(100):
            t = _Sample1()
            ts.append(t)
            s = safe_ref(t.x, self._closure)
            ss.append(s)
        ts.append(_sample2)
        ss.append(safe_ref(_sample2, self._closure))
        for x in range(30):
            t = _Sample3()
            ts.append(t)
            s = safe_ref(t, self._closure)
            ss.append(s)
        self.ts = ts
        self.ss = ss
        self.closure_count = 0
github mitmproxy / mitmproxy / mitmproxy / optmanager.py View on Github external
def subscribe(self, func, opts):
        """
            Subscribe a callable to the .changed signal, but only for a
            specified list of options. The callable should accept arguments
            (options, updated), and may raise an OptionsError.

            The event will automatically be unsubscribed if the callable goes out of scope.
        """
        for i in opts:
            if i not in self._options:
                raise exceptions.OptionsError("No such option: %s" % i)

        # We reuse blinker's safe reference functionality to cope with weakrefs
        # to bound methods.
        func = blinker._saferef.safe_ref(func)

        @functools.wraps(func)
        def _call(options, updated):
            if updated.intersection(set(opts)):
                f = func()
                if f:
                    f(options, updated)
                else:
                    self.changed.disconnect(_call)

        # Our wrapper function goes out of scope immediately, so we have to set
        # weakrefs to false. This means we need to keep our own weakref, and
        # clean up the hook when it's gone.
        self.changed.connect(_call, weak=False)