Python weakref.WeakSet() Examples

The following are 30 code examples of weakref.WeakSet(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module weakref , or try the search function .
Example #1
Source File: test_multi_ordered_dict.py    From buzzard with Apache License 2.0 7 votes vote down vote up
def test_multi_ordered_dict():
    ref = _MultiOrderedDict_NSquared()
    test = MultiOrderedDict()
    ref_wset = weakref.WeakSet()
    test_wset = weakref.WeakSet()
    rng = np.random.RandomState()

    tests = [
        _test_a,
        _test_b,
        _test_c,
        _test_d,
        _test_e,
        _test_f,
        _test_g,
        _test_h,
    ]

    for _ in range(5000):
        i = rng.randint(0, len(tests))
        tests[i](ref, test, ref_wset, test_wset, rng, False) 
Example #2
Source File: client.py    From ibis with Apache License 2.0 6 votes vote down vote up
def __init__(self, con, hdfs_client=None, **params):
        import hdfs

        self.con = con

        if isinstance(hdfs_client, hdfs.Client):
            hdfs_client = WebHDFS(hdfs_client)
        elif hdfs_client is not None and not isinstance(hdfs_client, HDFS):
            raise TypeError(hdfs_client)

        self._hdfs = hdfs_client
        self._kudu = None

        self._temp_objects = weakref.WeakSet()

        self._ensure_temp_db_exists() 
Example #3
Source File: test_tracing.py    From sentry-python with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_memory_usage(sentry_init, capture_events, args, expected_refcount):
    sentry_init(**args)

    references = weakref.WeakSet()

    with start_transaction(name="hi"):
        for i in range(100):
            with start_span(op="helloworld", description="hi {}".format(i)) as span:

                def foo():
                    pass

                references.add(foo)
                span.set_tag("foo", foo)
                pass

        del foo
        del span

        # required only for pypy (cpython frees immediately)
        gc.collect()

        assert len(references) == expected_refcount 
Example #4
Source File: uta.py    From hgvs with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 url,
                 pooling=hgvs.global_config.uta.pooling,
                 application_name=None,
                 mode=None,
                 cache=None):
        if url.schema is None:
            raise Exception("No schema name provided in {url}".format(url=url))
        self.application_name = application_name
        self.pooling = pooling
        self._conn = None
        # If we're using connection pooling, track the set of DB
        # connections we've seen; on first use we set the schema
        # search path. Use weak references to avoid keeping connection
        # objects alive unnecessarily.
        self._conns_seen = weakref.WeakSet()
        super(UTA_postgresql, self).__init__(url, mode, cache) 
Example #5
Source File: service.py    From pyquarkchain with MIT License 6 votes vote down vote up
def __init__(
        self, token: CancelToken = None, loop: asyncio.AbstractEventLoop = None
    ) -> None:
        self.events = ServiceEvents()
        self._run_lock = asyncio.Lock()
        self._child_services = WeakSet()
        self._tasks = WeakSet()
        self._finished_callbacks = []

        self._loop = loop

        base_token = CancelToken(type(self).__name__, loop=loop)

        if token is None:
            self.cancel_token = base_token
        else:
            self.cancel_token = base_token.chain(token) 
Example #6
Source File: leak_test.py    From BiblioPixel with MIT License 6 votes vote down vote up
def test_leaks(self):
        def _get_items():
            items, stops = [], []
            for creator in _CREATORS:
                item, stop = creator()
                items.append(item)
                stops.append(stop)
            [stop() for stop in stops]
            return weakref.WeakSet(items)

        items = _get_items()
        _pause()
        # If this next line were uncommented, it would work without all the
        # weakrefs in the code.
        # gc.collect()
        self.assertEqual(list(items), []) 
Example #7
Source File: pipeline.py    From core with MIT License 6 votes vote down vote up
def on(event, callback):
    """Call `callback` on `event`

    Register `callback` to be run when `event` occurs.

    Example:
        >>> def on_init():
        ...    print("Init happened")
        ...
        >>> on("init", on_init)
        >>> del on_init

    Arguments:
        event (str): Name of event
        callback (callable): Any callable

    """

    if event not in _registered_event_handlers:
        _registered_event_handlers[event] = weakref.WeakSet()

    events = _registered_event_handlers[event]
    events.add(callback) 
Example #8
Source File: test_weakset.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_union(self):
        u = self.s.union(self.items2)
        for c in self.letters:
            self.assertEqual(c in u, c in self.d or c in self.items2)
        self.assertEqual(self.s, WeakSet(self.items))
        self.assertEqual(type(u), WeakSet)
        self.assertRaises(TypeError, self.s.union, [[]])
        for C in set, frozenset, dict.fromkeys, list, tuple:
            x = WeakSet(self.items + self.items2)
            c = C(self.items2)
            self.assertEqual(self.s.union(c), x)
            del c
        self.assertEqual(len(u), len(self.items) + len(self.items2))
        self.items2.pop()
        gc.collect()
        self.assertEqual(len(u), len(self.items) + len(self.items2)) 
Example #9
Source File: test_weakset.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_len_cycles(self):
        N = 20
        items = [RefCycle() for i in range(N)]
        s = WeakSet(items)
        del items
        it = iter(s)
        try:
            next(it)
        except StopIteration:
            pass
        gc.collect()
        n1 = len(s)
        del it
        gc.collect()
        n2 = len(s)
        # one item may be kept alive inside the iterator
        self.assertIn(n1, (0, 1))
        self.assertEqual(n2, 0) 
Example #10
Source File: test_weakset.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_len_race(self):
        # Extended sanity checks for len() in the face of cyclic collection
        self.addCleanup(gc.set_threshold, *gc.get_threshold())
        for th in range(1, 100):
            N = 20
            gc.collect(0)
            gc.set_threshold(th, th, th)
            items = [RefCycle() for i in range(N)]
            s = WeakSet(items)
            del items
            # All items will be collected at next garbage collection pass
            it = iter(s)
            try:
                next(it)
            except StopIteration:
                pass
            n1 = len(s)
            del it
            n2 = len(s)
            self.assertGreaterEqual(n1, 0)
            self.assertLessEqual(n1, N)
            self.assertGreaterEqual(n2, 0)
            self.assertLessEqual(n2, n1) 
Example #11
Source File: signals.py    From backend with GNU General Public License v2.0 6 votes vote down vote up
def connect(self, slot, sender=None):
    if sender:
      if inspect.ismethod(slot):
        if sender not in self._methods_subs:
          self._methods_subs[sender] = weakref.WeakKeyDictionary()

        if slot.__self__ not in self._methods_subs[sender]:
          self._methods_subs[sender][slot.__self__] = set()

        self._methods_subs[sender][slot.__self__].add(slot.__func__)
      else:
        if sender not in self._functions_subs:
          self._functions_subs[sender] = weakref.WeakSet()
        self._functions_subs[sender].add(slot)
    else:
      if inspect.ismethod(slot):
        if slot.__self__ not in self._methods:
          self._methods[slot.__self__] = set()
        self._methods[slot.__self__].add(slot.__func__)
      else:
        self._functions.add(slot) 
Example #12
Source File: test_weakset.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_union(self):
        u = self.s.union(self.items2)
        for c in self.letters:
            self.assertEqual(c in u, c in self.d or c in self.items2)
        self.assertEqual(self.s, WeakSet(self.items))
        self.assertEqual(type(u), WeakSet)
        self.assertRaises(TypeError, self.s.union, [[]])
        for C in set, frozenset, dict.fromkeys, list, tuple:
            x = WeakSet(self.items + self.items2)
            c = C(self.items2)
            self.assertEqual(self.s.union(c), x)
            del c
        self.assertEqual(len(u), len(self.items) + len(self.items2))
        self.items2.pop()
        gc.collect()
        self.assertEqual(len(u), len(self.items) + len(self.items2)) 
Example #13
Source File: test_weakset.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_len_cycles(self):
        N = 20
        items = [RefCycle() for i in range(N)]
        s = WeakSet(items)
        del items
        it = iter(s)
        try:
            next(it)
        except StopIteration:
            pass
        gc.collect()
        n1 = len(s)
        del it
        gc.collect()
        n2 = len(s)
        # one item may be kept alive inside the iterator
        self.assertIn(n1, (0, 1))
        self.assertEqual(n2, 0) 
Example #14
Source File: test_weakset.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_len_race(self):
        # Extended sanity checks for len() in the face of cyclic collection
        self.addCleanup(gc.set_threshold, *gc.get_threshold())
        for th in range(1, 100):
            N = 20
            gc.collect(0)
            gc.set_threshold(th, th, th)
            items = [RefCycle() for i in range(N)]
            s = WeakSet(items)
            del items
            # All items will be collected at next garbage collection pass
            it = iter(s)
            try:
                next(it)
            except StopIteration:
                pass
            n1 = len(s)
            del it
            n2 = len(s)
            self.assertGreaterEqual(n1, 0)
            self.assertLessEqual(n1, N)
            self.assertGreaterEqual(n2, 0)
            self.assertLessEqual(n2, n1) 
Example #15
Source File: test_weakset.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_union(self):
        u = self.s.union(self.items2)
        for c in self.letters:
            self.assertEqual(c in u, c in self.d or c in self.items2)
        self.assertEqual(self.s, WeakSet(self.items))
        self.assertEqual(type(u), WeakSet)
        self.assertRaises(TypeError, self.s.union, [[]])
        for C in set, frozenset, dict.fromkeys, list, tuple:
            x = WeakSet(self.items + self.items2)
            c = C(self.items2)
            self.assertEqual(self.s.union(c), x)
            del c
        self.assertEqual(len(u), len(self.items) + len(self.items2))
        self.items2.pop()
        gc.collect()
        self.assertEqual(len(u), len(self.items) + len(self.items2)) 
Example #16
Source File: test_weakset.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_len_cycles(self):
        N = 20
        items = [RefCycle() for i in range(N)]
        s = WeakSet(items)
        del items
        it = iter(s)
        try:
            next(it)
        except StopIteration:
            pass
        gc.collect()
        n1 = len(s)
        del it
        gc.collect()
        n2 = len(s)
        # one item may be kept alive inside the iterator
        self.assertIn(n1, (0, 1))
        self.assertEqual(n2, 0) 
Example #17
Source File: test_weakset.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_len_race(self):
        # Extended sanity checks for len() in the face of cyclic collection
        self.addCleanup(gc.set_threshold, *gc.get_threshold())
        for th in range(1, 100):
            N = 20
            gc.collect(0)
            gc.set_threshold(th, th, th)
            items = [RefCycle() for i in range(N)]
            s = WeakSet(items)
            del items
            # All items will be collected at next garbage collection pass
            it = iter(s)
            try:
                next(it)
            except StopIteration:
                pass
            n1 = len(s)
            del it
            n2 = len(s)
            self.assertGreaterEqual(n1, 0)
            self.assertLessEqual(n1, N)
            self.assertGreaterEqual(n2, 0)
            self.assertLessEqual(n2, n1) 
Example #18
Source File: port.py    From edgedb with Apache License 2.0 6 votes vote down vote up
def __init__(self, nethost: str, netport: int, auto_shutdown: bool,
                 max_protocol: Tuple[int, int], **kwargs):
        super().__init__(**kwargs)

        self._nethost = nethost
        self._netport = netport

        self._edgecon_id = 0
        self._num_connections = 0

        self._servers = []
        self._backends = weakref.WeakSet()

        self._auto_shutdown = auto_shutdown
        self._accepting = False
        self._max_protocol = max_protocol 
Example #19
Source File: util.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, cls, has_weakref=True):
        self._refs = weakref.WeakSet()

        # Create a weak referenceable subclass if necessary
        if not has_weakref:
            class __weakref_cls__(cls):
                __slots__ = ('__weakref__',)
        else:
            __weakref_cls__ = cls

        def override_new(original_cls, *args, **kwargs):
            """Function replacing new allowing to track instances.

            """
            new = __weakref_cls__.__old_new__(__weakref_cls__, *args, **kwargs)
            self._refs.add(new)
            return new

        __weakref_cls__.__old_new__ = cls.__new__
        cls.__new__ = override_new
        __weakref_cls__.original_cls = cls

        self.cls = __weakref_cls__ 
Example #20
Source File: test_weakset.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_constructor_identity(self):
        s = WeakSet(self.items)
        t = WeakSet(s)
        self.assertNotEqual(id(s), id(t)) 
Example #21
Source File: test_weakset.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_init(self):
        s = WeakSet()
        s.__init__(self.items)
        self.assertEqual(s, self.s)
        s.__init__(self.items2)
        self.assertEqual(s, WeakSet(self.items2))
        self.assertRaises(TypeError, s.__init__, s, 2);
        self.assertRaises(TypeError, s.__init__, 1); 
Example #22
Source File: test_weakset.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_gc(self):
        # Create a nest of cycles to exercise overall ref count check
        s = WeakSet(Foo() for i in range(1000))
        for elem in s:
            elem.cycle = s
            elem.sub = elem
            elem.set = WeakSet([elem]) 
Example #23
Source File: test_weakset.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_weak_destroy_and_mutate_while_iterating(self):
        # Issue #7105: iterators shouldn't crash when a key is implicitly removed
        items = [SomeClass(c) for c in string.ascii_letters]
        s = WeakSet(items)
        @contextlib.contextmanager
        def testcontext():
            try:
                it = iter(s)
                next(it)
                # Schedule an item for removal and recreate it
                u = SomeClass(str(items.pop()))
                gc.collect()      # just in case
                yield u
            finally:
                it = None           # should commit all removals

        with testcontext() as u:
            self.assertNotIn(u, s)
        with testcontext() as u:
            self.assertRaises(KeyError, s.remove, u)
        self.assertNotIn(u, s)
        with testcontext() as u:
            s.add(u)
        self.assertIn(u, s)
        t = s.copy()
        with testcontext() as u:
            s.update(t)
        self.assertEqual(len(s), len(t))
        with testcontext() as u:
            s.clear()
        self.assertEqual(len(s), 0) 
Example #24
Source File: utils.py    From mars with Apache License 2.0 5 votes vote down vote up
def build_tileable_graph(tileables, executed_tileable_keys, graph=None):
    from .operands import Fetch
    from .tiles import TileableGraphBuilder

    with build_mode():
        node_to_copy = weakref.WeakKeyDictionary()
        node_to_fetch = weakref.WeakKeyDictionary()
        copied = weakref.WeakSet()

        def replace_with_fetch_or_copy(n):
            n = n.data if hasattr(n, 'data') else n
            if n in copied:
                return n
            if n.key in executed_tileable_keys:
                if n not in node_to_fetch:
                    c = node_to_copy[n] = node_to_fetch[n] = build_fetch(n).data
                    copied.add(c)
                return node_to_fetch[n]
            if n not in node_to_copy:
                copy_op = n.op.copy()
                params = []
                for o in n.op.outputs:
                    p = o.params.copy()
                    p.update(o.extra_params)
                    p['_key'] = o.key
                    if isinstance(o.op, Fetch):
                        # chunks may be generated in the remote functions,
                        # thus bring chunks and nsplits for serialization
                        p['chunks'] = o.chunks
                        p['nsplits'] = o.nsplits
                    params.append(p)
                copies = copy_op.new_tileables([replace_with_fetch_or_copy(inp) for inp in n.inputs],
                                               kws=params, output_limit=len(params))
                for o, copy in zip(n.op.outputs, copies):
                    node_to_copy[o] = copy.data
                    copied.add(copy.data)
            return node_to_copy[n]

        tileable_graph_builder = TileableGraphBuilder(
            graph=graph, node_processor=replace_with_fetch_or_copy)
        return tileable_graph_builder.build(tileables) 
Example #25
Source File: test_weakset.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_symmetric_difference(self):
        i = self.s.symmetric_difference(self.items2)
        for c in self.letters:
            self.assertEqual(c in i, (c in self.d) ^ (c in self.items2))
        self.assertEqual(self.s, WeakSet(self.items))
        self.assertEqual(type(i), WeakSet)
        self.assertRaises(TypeError, self.s.symmetric_difference, [[]])
        self.assertEqual(len(i), len(self.items) + len(self.items2))
        self.items2.pop()
        gc.collect()
        self.assertEqual(len(i), len(self.items) + len(self.items2)) 
Example #26
Source File: test_weakset.py    From oss-ftp with MIT License 5 votes vote down vote up
def setUp(self):
        # need to keep references to them
        self.items = [SomeClass(c) for c in ('a', 'b', 'c')]
        self.items2 = [SomeClass(c) for c in ('x', 'y', 'z')]
        self.letters = [SomeClass(c) for c in string.ascii_letters]
        self.ab_items = [SomeClass(c) for c in 'ab']
        self.abcde_items = [SomeClass(c) for c in 'abcde']
        self.def_items = [SomeClass(c) for c in 'def']
        self.ab_weakset = WeakSet(self.ab_items)
        self.abcde_weakset = WeakSet(self.abcde_items)
        self.def_weakset = WeakSet(self.def_items)
        self.s = WeakSet(self.items)
        self.d = dict.fromkeys(self.items)
        self.obj = SomeClass('F')
        self.fs = WeakSet([self.obj]) 
Example #27
Source File: client.py    From ibis with Apache License 2.0 5 votes vote down vote up
def __init__(self, pool_size=8, database='default', **params):
        self.params = params
        self.database = database

        self.lock = threading.Lock()

        self.options = {}

        self.max_pool_size = pool_size
        self._connections = weakref.WeakSet()

        self.connection_pool = deque(maxlen=pool_size)
        self.connection_pool_size = 0 
Example #28
Source File: test_weakset.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_methods(self):
        weaksetmethods = dir(WeakSet)
        for method in dir(set):
            if method == 'test_c_api' or method.startswith('_'):
                continue
            self.assertIn(method, weaksetmethods,
                         "WeakSet missing method " + method) 
Example #29
Source File: test_weakset.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_new_or_init(self):
        self.assertRaises(TypeError, WeakSet, [], 2) 
Example #30
Source File: test_weakset.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_eq(self):
        # issue 5964
        self.assertTrue(self.s == self.s)
        self.assertTrue(self.s == WeakSet(self.items))
        self.assertFalse(self.s == set(self.items))
        self.assertFalse(self.s == list(self.items))
        self.assertFalse(self.s == tuple(self.items))
        self.assertFalse(self.s == 1)