예제 #1
0
def test_weakref(create_weakref, create_weakref_with_callback):
    from weakref import getweakrefcount

    # Apparently, you cannot weakly reference an object()
    class WeaklyReferenced(object):
        pass

    def callback(wr):
        # No `nonlocal` in Python 2
        callback.called = True

    obj = WeaklyReferenced()
    assert getweakrefcount(obj) == 0
    wr = create_weakref(obj)
    assert getweakrefcount(obj) == 1

    obj = WeaklyReferenced()
    assert getweakrefcount(obj) == 0
    callback.called = False
    wr = create_weakref_with_callback(obj, callback)  # noqa: F841
    assert getweakrefcount(obj) == 1
    assert not callback.called
    del obj
    pytest.gc_collect()
    assert callback.called
예제 #2
0
    def test_getweakrefcount(self):
        o = C()
        
        def runTest():
            ref1 = weakref.ref(o)
            ref2 = weakref.ref(o, self.callback)
            self.assertTrue(weakref.getweakrefcount(o) == 2,
                         "got wrong number of weak reference objects")

            proxy1 = weakref.proxy(o)
            proxy2 = weakref.proxy(o, self.callback)
            self.assertTrue(weakref.getweakrefcount(o) == 4,
                         "got wrong number of weak reference objects")

            del ref1, ref2, proxy1, proxy2

        runTest()
        test_support.force_gc_collect()
        self.assertTrue(weakref.getweakrefcount(o) == 0,
                     "weak reference objects not unlinked from"
                     " referent when discarded.")

        # assumes ints do not support weakrefs
        self.assertTrue(weakref.getweakrefcount(1) == 0,
                     "got wrong number of weak reference objects for int")
예제 #3
0
    def test_getweakrefcount(self):
        o = C()
        ref1 = weakref.ref(o)
        ref2 = weakref.ref(o, self.callback)
        self.assert_(
            weakref.getweakrefcount(o) == 2,
            "got wrong number of weak reference objects")

        proxy1 = weakref.proxy(o)
        proxy2 = weakref.proxy(o, self.callback)
        self.assert_(
            weakref.getweakrefcount(o) == 4,
            "got wrong number of weak reference objects")

        del ref1, ref2, proxy1, proxy2
        gc.collect()
        self.assert_(
            weakref.getweakrefcount(o) == 0,
            "weak reference objects not unlinked from"
            " referent when discarded.")

        # assumes ints do not support weakrefs
        self.assert_(
            weakref.getweakrefcount(1) == 0,
            "got wrong number of weak reference objects for int")
예제 #4
0
    def test_getweakrefcount(self):
        o = C()
        ref1 = weakref.ref(o)
        ref2 = weakref.ref(o, self.callback)
        self.assert_(weakref.getweakrefcount(o) == 2,
                     "got wrong number of weak reference objects")

        proxy1 = weakref.proxy(o)
        proxy2 = weakref.proxy(o, self.callback)
        self.assert_(weakref.getweakrefcount(o) == 4,
                     "got wrong number of weak reference objects")
예제 #5
0
    def test_getweakrefcount(self):
        o = C()
        ref1 = weakref.ref(o)
        ref2 = weakref.ref(o, self.callback)
        self.assert_(weakref.getweakrefcount(o) == 2,
                     "got wrong number of weak reference objects")

        proxy1 = weakref.proxy(o)
        proxy2 = weakref.proxy(o, self.callback)
        self.assert_(weakref.getweakrefcount(o) == 4,
                     "got wrong number of weak reference objects")
예제 #6
0
        def runTest():
            ref1 = weakref.ref(o)
            ref2 = weakref.ref(o, self.callback)
            self.assertTrue(weakref.getweakrefcount(o) == 2,
                         "got wrong number of weak reference objects")

            proxy1 = weakref.proxy(o)
            proxy2 = weakref.proxy(o, self.callback)
            self.assertTrue(weakref.getweakrefcount(o) == 4,
                         "got wrong number of weak reference objects")

            del ref1, ref2, proxy1, proxy2
예제 #7
0
        def runTest():
            ref1 = weakref.ref(o)
            ref2 = weakref.ref(o, self.callback)
            self.assertTrue(weakref.getweakrefcount(o) == 2,
                         "got wrong number of weak reference objects")

            proxy1 = weakref.proxy(o)
            proxy2 = weakref.proxy(o, self.callback)
            self.assertTrue(weakref.getweakrefcount(o) == 4,
                         "got wrong number of weak reference objects")

            del ref1, ref2, proxy1, proxy2
예제 #8
0
    def test_ref_reuse(self):
        def runTest():
            o = C()
            ref1 = weakref.ref(o)
            # create a proxy to make sure that there's an intervening creation
            # between these two; it should make no difference
            proxy = weakref.proxy(o)
            ref2 = weakref.ref(o)
            self.assertTrue(ref1 is ref2,
                         "reference object w/out callback should be re-used")

            o = C()
            proxy = weakref.proxy(o)
            ref1 = weakref.ref(o)
            ref2 = weakref.ref(o)
            self.assertTrue(ref1 is ref2,
                         "reference object w/out callback should be re-used")
            self.assertTrue(weakref.getweakrefcount(o) == 2,
                         "wrong weak ref count for object")
            del proxy
            return o, ref1

        o, ref1 = runTest()
        test_support.force_gc_collect()
        self.assertTrue(weakref.getweakrefcount(o) == 1,
                     "wrong weak ref count for object after deleting proxy")
예제 #9
0
 def test_weak_keys(self):
     #
     #  This exercises d.copy(), d.items(), d[] = v, d[], del d[],
     #  len(d), k in d.
     #
     dict, objects = self.make_weak_keyed_dict()
     for o in objects:
         self.assertTrue(weakref.getweakrefcount(o) == 1,
                      "wrong number of weak references to %r!" % o)
         self.assertTrue(o.arg is dict[o],
                      "wrong object returned by weak dict!")
     items1 = dict.items()
     items2 = dict.copy().items()
     self.assertEqual(set(items1), set(items2),
                  "cloning of weak-keyed dictionary did not work!")
     del items1, items2
     self.assertEqual(len(dict), self.COUNT)
     del objects[0]
     self.assertTrue(len(dict) == (self.COUNT - 1),
                  "deleting object did not cause dictionary update")
     del objects, o
     self.assertTrue(len(dict) == 0,
                  "deleting the keys did not clear the dictionary")
     o = Object(42)
     dict[o] = "What is the meaning of the universe?"
     self.assertIn(o, dict)
     self.assertNotIn(34, dict)
예제 #10
0
 def test_weak_values(self):
     #
     #  This exercises d.copy(), d.items(), d[], del d[], len(d).
     #
     dict, objects = self.make_weak_valued_dict()
     for o in objects:
         if not support.is_jython:  # Such dictionaries now use MapMaker
             self.assertTrue(
                 weakref.getweakrefcount(o) == 1,
                 "wrong number of weak references to %r!" % o)
         self.assertTrue(o is dict[o.arg],
                         "wrong object returned by weak dict!")
     items1 = list(dict.items())
     items2 = list(dict.copy().items())
     items1.sort()
     items2.sort()
     self.assertTrue(items1 == items2,
                     "cloning of weak-valued dictionary did not work!")
     del items1, items2
     self.assertTrue(len(dict) == self.COUNT)
     del objects[0]
     gc.collect()
     # underlying Map.size is guaranteed only to be eventually consistent for MapMaker
     self.assertEqual(len(list(dict.keys())), self.COUNT - 1,
                      "deleting object did not cause dictionary update")
     del objects, o
     gc.collect()
     self.assertEqual(len(list(dict.keys())), 0,
                      "deleting the values did not clear the dictionary")
     # regression on SF bug #447152:
     dict = weakref.WeakValueDictionary()
     self.assertRaises(KeyError, dict.__getitem__, 1)
     dict[2] = C()
     gc.collect()
     self.assertRaises(KeyError, dict.__getitem__, 2)
예제 #11
0
    def get(self):
        # clean if need
        if self._clean_counter >= self._clean_interval:
           self._clean()

        self._clean_counter += 1

        # do grant
        if self.idle > 0:
            for conn in self.busy_array:
                if weakref.getweakrefcount(conn) > 0:
                    continue

                conn = weakref.proxy(conn)
                if not conn.ping():
                    conn.connect()
                elif not conn.reusable:
                    conn.make_reusable()

                plog("get: conn(%d) [idle/total/max: %d/%d/%d]" % (id(conn), self.idle, self.total, self._max))
                return conn

        elif self.total < self._max:
            conn = self._conn_cls(**self._db_config)
            self._pool.append(conn)

            conn = weakref.proxy(conn)
            conn.connect()

            # dig the pool

            plog("new: conn(%d) [idle/total/max: %d/%d/%d]" % (id(conn), self.idle, self.total, self._max))
            return conn

        return None
예제 #12
0
 def test_weak_keys(self):
     #
     #  This exercises d.copy(), d.items(), d[] = v, d[], del d[],
     #  len(d), d.has_key().
     #
     import gc
     dict, objects = self.make_weak_keyed_dict()
     for o in objects:
         self.assert_(weakref.getweakrefcount(o) == 1,
                      "wrong number of weak references to %r!" % o)
         self.assert_(o.arg is dict[o],
                      "wrong object returned by weak dict!")
     items1 = dict.items()
     items2 = dict.copy().items()
     self.assert_(set(items1) == set(items2),
                  "cloning of weak-keyed dictionary did not work!")
     del items1, items2
     test_support.gc_collect()
     self.assert_(len(dict) == self.COUNT)
     del objects[0]
     test_support.gc_collect()
     self.assert_(len(dict) == (self.COUNT - 1),
                  "deleting object did not cause dictionary update")
     del objects, o
     test_support.gc_collect()
     self.assert_(len(dict) == 0,
                  "deleting the keys did not clear the dictionary")
     o = Object(42)
     dict[o] = "What is the meaning of the universe?"
     self.assert_(dict.has_key(o))
     self.assert_(not dict.has_key(34))
예제 #13
0
 def test_weak_values(self):
     #
     #  This exercises d.copy(), d.items(), d[], del d[], len(d).
     #
     dict, objects = self.make_weak_valued_dict()
     for o in objects:
         self.assert_(
             weakref.getweakrefcount(o) == 1,
             "wrong number of weak references to %r!" % o)
         self.assert_(o is dict[o.arg],
                      "wrong object returned by weak dict!")
     items1 = dict.items()
     items2 = dict.copy().items()
     items1.sort()
     items2.sort()
     self.assert_(items1 == items2,
                  "cloning of weak-valued dictionary did not work!")
     del items1, items2
     self.assert_(len(dict) == self.COUNT)
     del objects[0]
     extra_collect()
     self.assert_(
         len(dict) == (self.COUNT - 1),
         "deleting object did not cause dictionary update")
     del objects, o
     extra_collect()
     self.assert_(
         len(dict) == 0, "deleting the values did not clear the dictionary")
     # regression on SF bug #447152:
     dict = weakref.WeakValueDictionary()
     self.assertRaises(KeyError, dict.__getitem__, 1)
     dict[2] = C()
     extra_collect()
     self.assertRaises(KeyError, dict.__getitem__, 2)
예제 #14
0
 def test_subclass_refs_dont_replace_standard_refs(self):
     class MyRef(weakref.ref):
         pass
     o = Object(42)
     r1 = MyRef(o)
     r2 = weakref.ref(o)
     self.assertTrue(r1 is not r2)
     self.assertEqual(weakref.getweakrefs(o), [r2, r1])
     self.assertEqual(weakref.getweakrefcount(o), 2)
     r3 = MyRef(o)
     self.assertEqual(weakref.getweakrefcount(o), 3)
     refs = weakref.getweakrefs(o)
     self.assertEqual(len(refs), 3)
     self.assertTrue(r2 is refs[0])
     self.assertIn(r1, refs[1:])
     self.assertIn(r3, refs[1:])
예제 #15
0
 def test_weak_values(self):
     #
     #  This exercises d.copy(), d.items(), d[], del d[], len(d).
     #
     dict, objects = self.make_weak_valued_dict()
     for o in objects:
         self.assertEqual(weakref.getweakrefcount(o), 1)
         self.assertTrue(o is dict[o.arg],
                      "wrong object returned by weak dict!")
     items1 = list(dict.items())
     items2 = list(dict.copy().items())
     items1.sort()
     items2.sort()
     self.assertEqual(items1, items2,
                  "cloning of weak-valued dictionary did not work!")
     del items1, items2
     self.assertEqual(len(dict), self.COUNT)
     del objects[0]
     self.assertEqual(len(dict), self.COUNT - 1,
                  "deleting object did not cause dictionary update")
     del objects, o
     self.assertEqual(len(dict), 0,
                  "deleting the values did not clear the dictionary")
     # regression on SF bug #447152:
     dict = weakref.WeakValueDictionary()
     self.assertRaises(KeyError, dict.__getitem__, 1)
     dict[2] = C()
     self.assertRaises(KeyError, dict.__getitem__, 2)
예제 #16
0
 def test_weak_values(self):
     #
     #  This exercises d.copy(), d.items(), d[], del d[], len(d).
     #
     import gc
     dict, objects = self.make_weak_valued_dict()
     for o in objects:
         self.assert_(weakref.getweakrefcount(o) == 1,
                      "wrong number of weak references to %r!" % o)
         self.assert_(o is dict[o.arg],
                      "wrong object returned by weak dict!")
     items1 = dict.items()
     items2 = dict.copy().items()
     items1.sort()
     items2.sort()
     self.assert_(items1 == items2,
                  "cloning of weak-valued dictionary did not work!")
     del items1, items2
     test_support.gc_collect()
     self.assert_(len(dict) == self.COUNT)
     del objects[0]
     test_support.gc_collect()
     self.assert_(len(dict) == (self.COUNT - 1),
                  "deleting object did not cause dictionary update")
     del objects, o
     test_support.gc_collect()
     self.assert_(len(dict) == 0,
                  "deleting the values did not clear the dictionary")
     # regression on SF bug #447152:
     dict = weakref.WeakValueDictionary()
     self.assertRaises(KeyError, dict.__getitem__, 1)
     dict[2] = C()
     test_support.gc_collect()
     self.assertRaises(KeyError, dict.__getitem__, 2)
예제 #17
0
 def test_subclass_refs_dont_replace_standard_refs(self):
     class MyRef(weakref.ref):
         pass
     o = Object(42)
     r1 = MyRef(o)
     r2 = weakref.ref(o)
     self.assert_(r1 is not r2)
     self.assertEqual(weakref.getweakrefs(o), [r2, r1])
     self.assertEqual(weakref.getweakrefcount(o), 2)
     r3 = MyRef(o)
     self.assertEqual(weakref.getweakrefcount(o), 3)
     refs = weakref.getweakrefs(o)
     self.assertEqual(len(refs), 3)
     self.assert_(r2 is refs[0])
     self.assert_(r1 in refs[1:])
     self.assert_(r3 in refs[1:])
예제 #18
0
    def test_weak_keys(self):
        dict = weakref.WeakKeyDictionary()
        objects = map(Object, range(self.COUNT))
        for o in objects:
            dict[o] = o.arg

        for o in objects:
            self.assert_(weakref.getweakrefcount(o) == 1,
                         "wrong number of weak references to %r!" % o)
            self.assert_(o.arg is dict[o],
                         "wrong object returned by weak dict!")
        items1 = dict.items()
        items2 = dict.copy().items()
        items1.sort()
        items2.sort()
        self.assert_(items1 == items2,
                     "cloning of weak-keyed dictionary did not work!")
        del items1, items2
        self.assert_(len(dict) == self.COUNT)
        del objects[0]
        self.assert_(len(dict) == (self.COUNT - 1),
                     "deleting object did not cause dictionary update")
        del objects, o
        self.assert_(len(dict) == 0,
                     "deleting the keys did not clear the dictionary")
예제 #19
0
    def test_ref_reuse(self):
        def runTest():
            o = C()
            ref1 = weakref.ref(o)
            # create a proxy to make sure that there's an intervening creation
            # between these two; it should make no difference
            proxy = weakref.proxy(o)
            ref2 = weakref.ref(o)
            self.assertTrue(
                ref1 is ref2,
                "reference object w/out callback should be re-used")

            o = C()
            proxy = weakref.proxy(o)
            ref1 = weakref.ref(o)
            ref2 = weakref.ref(o)
            self.assertTrue(
                ref1 is ref2,
                "reference object w/out callback should be re-used")
            self.assertTrue(
                weakref.getweakrefcount(o) == 2,
                "wrong weak ref count for object")
            del proxy
            return o, ref1

        o, ref1 = runTest()
        test_support.force_gc_collect()
        self.assertTrue(
            weakref.getweakrefcount(o) == 1,
            "wrong weak ref count for object after deleting proxy")
예제 #20
0
 def test_weak_values(self):
     #
     #  This exercises d.copy(), d.items(), d[], del d[], len(d).
     #
     dict, objects = self.make_weak_valued_dict()
     for o in objects:
         self.assertEqual(weakref.getweakrefcount(o), 1)
         self.assertTrue(o is dict[o.arg],
                      "wrong object returned by weak dict!")
     items1 = list(dict.items())
     items2 = list(dict.copy().items())
     items1.sort()
     items2.sort()
     self.assertEqual(items1, items2,
                  "cloning of weak-valued dictionary did not work!")
     del items1, items2
     self.assertEqual(len(dict), self.COUNT)
     del objects[0]
     gc_collect()
     self.assertEqual(len(dict), self.COUNT - 1,
                  "deleting object did not cause dictionary update")
     del objects, o
     gc_collect()
     self.assertEqual(len(dict), 0,
                  "deleting the values did not clear the dictionary")
     # regression on SF bug #447152:
     dict = weakref.WeakValueDictionary()
     self.assertRaises(KeyError, dict.__getitem__, 1)
     dict[2] = C()
     gc_collect()
     self.assertRaises(KeyError, dict.__getitem__, 2)
예제 #21
0
 def test_weak_values(self):
     #
     #  This exercises d.copy(), d.items(), d[], del d[], len(d).
     #
     dict, objects = self.make_weak_valued_dict()
     for o in objects:
         if not test_support.is_jython:  # Such dictionaries now use MapMaker
             self.assertTrue(weakref.getweakrefcount(o) == 1,
                             "wrong number of weak references to %r!" % o)
         self.assertTrue(o is dict[o.arg],
                      "wrong object returned by weak dict!")
     items1 = dict.items()
     items2 = dict.copy().items()
     items1.sort()
     items2.sort()
     self.assertTrue(items1 == items2,
                  "cloning of weak-valued dictionary did not work!")
     del items1, items2
     self.assertTrue(len(dict) == self.COUNT)
     del objects[0]
     gc.collect()
     # underlying Map.size is guaranteed only to be eventually consistent for MapMaker
     self.assertEqual(len(list(dict.iterkeys())), self.COUNT - 1,
                  "deleting object did not cause dictionary update")
     del objects, o
     gc.collect()
     self.assertEqual(len(list(dict.iterkeys())), 0,
                  "deleting the values did not clear the dictionary")
     # regression on SF bug #447152:
     dict = weakref.WeakValueDictionary()
     self.assertRaises(KeyError, dict.__getitem__, 1)
     dict[2] = C()
     gc.collect()
     self.assertRaises(KeyError, dict.__getitem__, 2)
예제 #22
0
 def test_weak_keys(self):
     #
     #  This exercises d.copy(), d.items(), d[] = v, d[], del d[],
     #  len(d), d.has_key().
     #
     import gc
     dict, objects = self.make_weak_keyed_dict()
     for o in objects:
         self.assert_(
             weakref.getweakrefcount(o) == 1,
             "wrong number of weak references to %r!" % o)
         self.assert_(o.arg is dict[o],
                      "wrong object returned by weak dict!")
     items1 = dict.items()
     items2 = dict.copy().items()
     self.assert_(
         set(items1) == set(items2),
         "cloning of weak-keyed dictionary did not work!")
     del items1, items2
     test_support.gc_collect()
     self.assert_(len(dict) == self.COUNT)
     del objects[0]
     test_support.gc_collect()
     self.assert_(
         len(dict) == (self.COUNT - 1),
         "deleting object did not cause dictionary update")
     del objects, o
     test_support.gc_collect()
     self.assert_(
         len(dict) == 0, "deleting the keys did not clear the dictionary")
     o = Object(42)
     dict[o] = "What is the meaning of the universe?"
     self.assert_(dict.has_key(o))
     self.assert_(not dict.has_key(34))
예제 #23
0
def test_comparable_garbage_collection(cache):
    a = Node(name="a")
    b = Node(name="b")
    c = Node(name="c")
    d = Node(name="d")

    cache[pair(a, b)] = True
    cache[pair(a, c)] = False
    cache[pair(c, d)] = True
    cache[pair(b, d)] = False

    assert weakref.getweakrefcount(a) == 2
    del c
    assert weakref.getweakrefcount(a) == 1
    del b
    assert weakref.getweakrefcount(a) == 0
예제 #24
0
 def test_weak_keys(self):
     #
     #  This exercises d.copy(), d.items(), d[] = v, d[], del d[],
     #  len(d), in d.
     #
     dict, objects = self.make_weak_keyed_dict()
     for o in objects:
         if not test_support.is_jython:  # Such dictionaries now use MapMaker
             self.assertTrue(weakref.getweakrefcount(o) == 1,
                             "wrong number of weak references to %r!" % o)
         self.assertTrue(o.arg is dict[o],
                      "wrong object returned by weak dict!")
     items1 = dict.items()
     items2 = dict.copy().items()
     self.assertTrue(set(items1) == set(items2),
                  "cloning of weak-keyed dictionary did not work!")
     del items1, items2
     self.assertEqual(len(list(dict.iterkeys())), self.COUNT)
     del objects[0]
     gc.collect()
     self.assertEqual(len(list(dict.iterkeys())), self.COUNT - 1,
                  "deleting object did not cause dictionary update")
     del objects, o
     gc.collect()
     self.assertEqual(len(list(dict.iterkeys())), 0,
                  "deleting the keys did not clear the dictionary")
     o = Object(42)
     dict[o] = "What is the meaning of the universe?"
     self.assertIn(o, dict)
     self.assertNotIn(34, dict)
예제 #25
0
 def test_weak_keys(self):
     #
     #  This exercises d.copy(), d.items(), d[] = v, d[], del d[],
     #  len(d), in d.
     #
     dict, objects = self.make_weak_keyed_dict()
     for o in objects:
         self.assertEqual(weakref.getweakrefcount(o), 1,
                      "wrong number of weak references to %r!" % o)
         self.assertIs(o.arg, dict[o],
                      "wrong object returned by weak dict!")
     items1 = dict.items()
     items2 = dict.copy().items()
     self.assertEqual(set(items1), set(items2),
                  "cloning of weak-keyed dictionary did not work!")
     del items1, items2
     self.assertEqual(len(dict), self.COUNT)
     del objects[0]
     self.assertEqual(len(dict), (self.COUNT - 1),
                  "deleting object did not cause dictionary update")
     del objects, o
     self.assertEqual(len(dict), 0,
                  "deleting the keys did not clear the dictionary")
     o = Object(42)
     dict[o] = "What is the meaning of the universe?"
     self.assertIn(o, dict)
     self.assertNotIn(34, dict)
예제 #26
0
    def test_ref_reuse(self):
        o = C()
        ref1 = weakref.ref(o)
        # create a proxy to make sure that there's an intervening creation
        # between these two; it should make no difference
        proxy = weakref.proxy(o)
        ref2 = weakref.ref(o)
        self.assert_(ref1 is ref2, "reference object w/out callback should be re-used")

        o = C()
        proxy = weakref.proxy(o)
        ref1 = weakref.ref(o)
        ref2 = weakref.ref(o)
        self.assert_(ref1 is ref2, "reference object w/out callback should be re-used")
        self.assert_(weakref.getweakrefcount(o) == 2, "wrong weak ref count for object")
        del proxy
        self.assert_(weakref.getweakrefcount(o) == 1, "wrong weak ref count for object after deleting proxy")
예제 #27
0
 def test5(self):
     a = arange(20, dtype=int32)
     self.assertEqual(weakref.getweakrefcount(a), 0)
     d = DataArrayInt(a)
     self.assertEqual(weakref.getweakrefcount(a), 1)
     self.assertTrue(not a.flags["OWNDATA"])
     self.assertTrue(d.isIota(20))
     a[:] = 2  # modifying a and d because a and d share the same chunk of data
     self.assertTrue(d.isUniform(2))
     del d  # d is destroyed, a retrieves its ownership of its initial chunk of data
     ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
     import gc
     gc.collect()
     self.assertTrue(a.flags["OWNDATA"])
     a[:] = 4  # a can be used has usual
     self.assertTrue(DataArrayInt(a).isUniform(4))
     pass
예제 #28
0
    def test_ipy2_gh437(self):
        """https://github.com/IronLanguages/ironpython2/issues/437"""
        import weakref
        class SomeWeakReferenceableObject(object): pass

        o = SomeWeakReferenceableObject()
        x = [weakref.ref(o) for i in range(10)]
        self.assertEqual(weakref.getweakrefcount(o), 1)
예제 #29
0
    def test_getweakrefcount(self):
        o = C()
        ref1 = weakref.ref(o)
        ref2 = weakref.ref(o, self.callback)
        self.assertEqual(weakref.getweakrefcount(o), 2, "got wrong number of weak reference objects")

        proxy1 = weakref.proxy(o)
        proxy2 = weakref.proxy(o, self.callback)
        self.assertEqual(weakref.getweakrefcount(o), 4, "got wrong number of weak reference objects")

        del ref1, ref2, proxy1, proxy2
        self.assertEqual(
            weakref.getweakrefcount(o), 0, "weak reference objects not unlinked from" " referent when discarded."
        )

        # assumes ints do not support weakrefs
        self.assertEqual(weakref.getweakrefcount(1), 0, "got wrong number of weak reference objects for int")
예제 #30
0
 def test_getweakrefcount(self):
     o = C()
     ref1 = weakref.ref(o)
     ref2 = weakref.ref(o, self.callback)
     self.assertEqual(weakref.getweakrefcount(o), 2,
                      'got wrong number of weak reference objects')
     proxy1 = weakref.proxy(o)
     proxy2 = weakref.proxy(o, self.callback)
     self.assertEqual(weakref.getweakrefcount(o), 4,
                      'got wrong number of weak reference objects')
     del ref1, ref2, proxy1, proxy2
     self.assertEqual(
         weakref.getweakrefcount(o), 0,
         'weak reference objects not unlinked from referent when discarded.'
     )
     self.assertEqual(weakref.getweakrefcount(1), 0,
                      'got wrong number of weak reference objects for int')
예제 #31
0
    def _pop_idle(self):
        for conn in self.busy_array:
            if weakref.getweakrefcount(conn) > 0:
                continue

            return id(conn), weakref.proxy(conn)

        return None, None
예제 #32
0
    def test_ipy2_gh437(self):
        """https://github.com/IronLanguages/ironpython2/issues/437"""
        import weakref
        class SomeWeakReferenceableObject(object): pass

        o = SomeWeakReferenceableObject()
        x = [weakref.ref(o) for i in range(10)]
        self.assertEqual(weakref.getweakrefcount(o), 1)
예제 #33
0
    def _pop_idle(self):
        for conn in self.busy_array:
            if weakref.getweakrefcount(conn) > 0:
                continue

            return id(conn), weakref.proxy(conn)

        return None, None
예제 #34
0
    def test_weakref_session(self):
        ssh_session = self.app.ssh_session
        ssh_session.command('ls')

        self.assertEqual(
            1,
            weakref.getweakrefcount(
                self.app.session_manager.sessions['ssh_session']))
예제 #35
0
 def test_subclass_refs_dont_replace_standard_refs(self):
     if test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=21116"):
         return
     class MyRef(weakref.ref):
         pass
     o = Object(42)
     r1 = MyRef(o)
     r2 = weakref.ref(o)
     self.assertTrue(r1 is not r2)
     self.assertEqual(weakref.getweakrefs(o), [r2, r1])
     self.assertEqual(weakref.getweakrefcount(o), 2)
     r3 = MyRef(o)
     self.assertEqual(weakref.getweakrefcount(o), 3)
     refs = weakref.getweakrefs(o)
     self.assertEqual(len(refs), 3)
     self.assertTrue(r2 is refs[0])
     self.assertIn(r1, refs[1:])
     self.assertIn(r3, refs[1:])
예제 #36
0
 def test_subclass_refs_dont_replace_standard_refs(self):
     class MyRef(weakref.ref):
         pass
     o = Object(42)
     r1 = MyRef(o)
     r2 = weakref.ref(o)
     self.assertTrue(r1 is not r2)
     self.assertEqual(weakref.getweakrefs(o), [r2, r1])
     self.assertEqual(weakref.getweakrefcount(o), 2)
     r3 = MyRef(o)
     self.assertEqual(weakref.getweakrefcount(o), 3)
     refs = weakref.getweakrefs(o)
     self.assertEqual(len(refs), 3)
     assert set(refs) == set((r1, r2, r3))
     if test_support.check_impl_detail():
         self.assertTrue(r2 is refs[0])
         self.assertIn(r1, refs[1:])
         self.assertIn(r3, refs[1:])
 def test5(self):
     a=arange(20,dtype=int32)
     self.assertEqual(weakref.getweakrefcount(a),0)
     d=DataArrayInt(a)
     self.assertEqual(weakref.getweakrefcount(a),1)
     self.assertTrue(not a.flags["OWNDATA"])
     self.assertTrue(d.isIdentity())
     self.assertEqual(len(d),20)
     a[:]=2 # modifying a and d because a and d share the same chunk of data
     self.assertTrue(d.isUniform(2))
     del d # d is destroyed, a retrieves its ownership of its initial chunk of data
     ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called 
     import gc
     gc.collect()
     self.assertTrue(a.flags["OWNDATA"])
     a[:]=4 # a can be used has usual
     self.assertTrue(DataArrayInt(a).isUniform(4))
     pass
 def test13(self):
     a=arange(20,dtype=float64)
     self.assertEqual(weakref.getweakrefcount(a),0)
     d=DataArrayDouble(a)
     self.assertEqual(weakref.getweakrefcount(a),1)
     self.assertTrue(not a.flags["OWNDATA"])
     self.assertTrue(d.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-14))
     self.assertEqual(len(d),20)
     a[:]=2 # modifying a and d because a and d share the same chunk of data
     self.assertTrue(d.isUniform(2,1e-14))
     del d # d is destroyed, a retrieves its ownership of its initial chunk of data
     ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
     import gc
     gc.collect()
     self.assertTrue(a.flags["OWNDATA"])
     a[:]=4 # a can be used has usual
     self.assertTrue(DataArrayDouble(a).isUniform(4,1e-14))
     pass
예제 #39
0
 def test_subclass_refs_dont_replace_standard_refs(self):
     if test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=21116"):
         return
     class MyRef(weakref.ref):
         pass
     o = Object(42)
     r1 = MyRef(o)
     r2 = weakref.ref(o)
     self.assertTrue(r1 is not r2)
     self.assertEqual(weakref.getweakrefs(o), [r2, r1])
     self.assertEqual(weakref.getweakrefcount(o), 2)
     r3 = MyRef(o)
     self.assertEqual(weakref.getweakrefcount(o), 3)
     refs = weakref.getweakrefs(o)
     self.assertEqual(len(refs), 3)
     self.assertTrue(r2 is refs[0])
     self.assertIn(r1, refs[1:])
     self.assertIn(r3, refs[1:])
예제 #40
0
 def test_subclass_refs_dont_replace_standard_refs(self):
     class MyRef(weakref.ref):
         pass
     o = Object(42)
     r1 = MyRef(o)
     r2 = weakref.ref(o)
     self.assertTrue(r1 is not r2)
     self.assertEqual(weakref.getweakrefs(o), [r2, r1])
     self.assertEqual(weakref.getweakrefcount(o), 2)
     r3 = MyRef(o)
     self.assertEqual(weakref.getweakrefcount(o), 3)
     refs = weakref.getweakrefs(o)
     self.assertEqual(len(refs), 3)
     assert set(refs) == set((r1, r2, r3))
     if test_support.check_impl_detail():
         self.assertTrue(r2 is refs[0])
         self.assertIn(r1, refs[1:])
         self.assertIn(r3, refs[1:])
예제 #41
0
def show_weakref():

    d = {'color': 'blue', 'value': 512, 'weight': 64}
    wd = WeakrefDict({'color': 'red', 'value': 255, 'weight': 128})
    nwc = NotWeakableClass(12345)
    print(d, sys.getrefcount(d))
    print(wd, weakref.getweakrefcount(wd))
    try:
        dref = weakref.ref(d)
    except Exception as e:
        print('create weakref error:', e)
    else:
        print(dref, dref(), weakref.getweakrefcount(dref()))
    try:
        nwcref = weakref.ref(nwc)
    except Exception as e:
        print('create weakref error:', e)
    else:
        print(nwcref, nwcref(), weakref.getweakrefcount(nwcref()))
    try:
        NotWC = weakref.ref(NotWeakableClass)
    except Exception as e:
        print('create weakref error:', e)
    else:
        print(NotWC, NotWC(), weakref.getweakrefcount(NotWC()))
    wdref = weakref.ref(wd)
    print(wdref, wdref(), weakref.getweakrefcount(wdref()))
    del wd
    print(wdref())
    # weakref value dictionary
    wvd = weakref.WeakValueDictionary()
    x = WeakrefDict({'width': 1366, 'height': 768})
    print(wvd, x)
    xid = id(x)
    wvd[xid] = x
    print(wvd[xid])
    # weakref key dictionary
    wkd = weakref.WeakKeyDictionary()
    wkd[nwc] = xid
    del xid
    print(wkd[nwc])
    del nwc
    print([v for v in wkd.values()])
예제 #42
0
 def test_weakref_works_on_coord(self):
     coord = plyparser.Coord(file='a', line=2, column=10,
         end_line=3, end_column=11)
     wr = weakref.ref(coord)
     cref = wr()
     self.assertEqual(cref.line, 2)
     self.assertEqual(cref.column, 10)
     self.assertEqual(cref.end_line, 3)
     self.assertEqual(cref.end_column, 11)
     self.assertEqual(weakref.getweakrefcount(coord), 1)
예제 #43
0
 def test_ref_reuse(self):
     o = C()
     ref1 = weakref.ref(o)
     proxy = weakref.proxy(o)
     ref2 = weakref.ref(o)
     self.assertIs(ref1, ref2,
                   'reference object w/out callback should be re-used')
     o = C()
     proxy = weakref.proxy(o)
     ref1 = weakref.ref(o)
     ref2 = weakref.ref(o)
     self.assertIs(ref1, ref2,
                   'reference object w/out callback should be re-used')
     self.assertEqual(weakref.getweakrefcount(o), 2,
                      'wrong weak ref count for object')
     del proxy
     self.assertEqual(
         weakref.getweakrefcount(o), 1,
         'wrong weak ref count for object after deleting proxy')
예제 #44
0
 def test_defaultInstance(self):
     # create an instance so we can test the rest of the interfaces
     inst = Entity() # Note by default no parameters are possible
     # not implemented by default, results in a type error by design
     self.assertRaises(TypeError, inst.serialize)
     self.assertEquals(str(inst), 'Entity()')
     self.assertListEqual(list(Entity.objects), [inst])
     self.assertEqual(weakref.getweakrefcount(inst), 1)
     self.assertEqual(Entity.isValid(inst), True)
     Entity.delete(inst) #you must reap the instance yourself
     self.assertEqual(Entity.isValid(inst), False)
예제 #45
0
 def show(s = ''):
     print(s)
     print('global dict', dict(d))
     for ref in d.keyrefs():
         k = ref()
         if k is None: continue
         count = weakref.getweakrefcount(k)
         print('weakref count', k, count, ref)
         print('weakref list', weakref.getweakrefs(k))
         print('ref count', k, sys.getrefcount(k)) # == 4??????????
     print('\n'*2)
예제 #46
0
 def test_defaultInstance(self):
     # create an instance so we can test the rest of the interfaces
     inst = Entity()  # Note by default no parameters are possible
     # not implemented by default, results in a type error by design
     self.assertRaises(TypeError, inst.serialize)
     self.assertEquals(str(inst), 'Entity()')
     self.assertListEqual(list(Entity.objects), [inst])
     self.assertEqual(weakref.getweakrefcount(inst), 1)
     self.assertEqual(Entity.isValid(inst), True)
     Entity.delete(inst)  #you must reap the instance yourself
     self.assertEqual(Entity.isValid(inst), False)
예제 #47
0
    def test_ref_reuse(self):
        o = C()
        ref1 = weakref.ref(o)
        # create a proxy to make sure that there's an intervening creation
        # between these two; it should make no difference
        proxy = weakref.proxy(o)
        ref2 = weakref.ref(o)
        self.assert_(ref1 is ref2,
                     "reference object w/out callback should be re-used")

        o = C()
        proxy = weakref.proxy(o)
        ref1 = weakref.ref(o)
        ref2 = weakref.ref(o)
        self.assert_(ref1 is ref2,
                     "reference object w/out callback should be re-used")
        self.assert_(weakref.getweakrefcount(o) == 2,
                     "wrong weak ref count for object")
        del proxy
        self.assert_(weakref.getweakrefcount(o) == 1,
                     "wrong weak ref count for object after deleting proxy")
예제 #48
0
  def testWekRefs(self):
    sysobj = npython.sel('/sys')
    try:
      ref = weakref.ref(sysobj)
    except:
      assert 1 == 0, "Can't create weakref"
    weakrefscount = weakref.getweakrefcount(sysobj)
    assert weakrefscount, "Something strange with weak refs"

    try:
      ref().getcmds()
    except:
      assert 1 == 0, "Can't access weakrefed object"
예제 #49
0
def test_weakref():
    """Weak references allow Platforms to be del'd while Scenarios live."""
    mp = ixmp.Platform(
        backend='jdbc',
        driver='hsqldb',
        url='jdbc:hsqldb:mem:test_weakref',
    )

    # There is one reference to the Platform, and zero weak references
    assert getrefcount(mp) - 1 == 1
    assert getweakrefcount(mp) == 0

    # Create a single Scenario
    s = ixmp.Scenario(mp, 'foo', 'bar', version='new')

    # Still one reference to the Platform
    assert getrefcount(mp) - 1 == 1
    # …but additionally one weak reference
    assert getweakrefcount(mp) == 1

    # Make a local reference to the backend
    backend = mp._backend

    # Delete the Platform. Note that this only has an effect if there are no
    # existing references to it
    del mp

    # s.platform is a dead weak reference, so it can't be accessed
    with pytest.raises(ReferenceError):
        s.platform._backend

    # There is only one remaining reference to the backend: the *backend* name
    # in the local scope
    assert getrefcount(backend) - 1 == 1

    # The backend is garbage-collected at this point

    # The Scenario object still lives, but can't be used for anything
    assert s.model == 'foo'
예제 #50
0
 def test13(self):
     a = arange(20, dtype=float64)
     self.assertEqual(weakref.getweakrefcount(a), 0)
     d = DataArrayDouble(a)
     self.assertEqual(weakref.getweakrefcount(a), 1)
     self.assertTrue(not a.flags["OWNDATA"])
     self.assertTrue(
         d.isEqual(
             DataArrayDouble([
                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
                 17, 18, 19
             ]), 1e-14))
     self.assertEqual(len(d), 20)
     a[:] = 2  # modifying a and d because a and d share the same chunk of data
     self.assertTrue(d.isUniform(2, 1e-14))
     del d  # d is destroyed, a retrieves its ownership of its initial chunk of data
     ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
     import gc
     gc.collect()
     self.assertTrue(a.flags["OWNDATA"])
     a[:] = 4  # a can be used has usual
     self.assertTrue(DataArrayDouble(a).isUniform(4, 1e-14))
     pass
예제 #51
0
 def runTest():
     dict, objects = self.make_weak_keyed_dict()
     for o in objects:
         self.assertTrue(weakref.getweakrefcount(o) == 1,
                      "wrong number of weak references to %r!" % o)
         self.assertTrue(o.arg is dict[o],
                      "wrong object returned by weak dict!")
     items1 = dict.items()
     items2 = dict.copy().items()
     del o
     self.assertTrue(set(items1) == set(items2),
                  "cloning of weak-keyed dictionary did not work!")
     del items1, items2
     self.assertTrue(len(dict) == self.COUNT)
     del objects[0]
     
     return dict, objects
예제 #52
0
 def runTest():
     dict, objects = self.make_weak_keyed_dict()
     for o in objects:
         self.assertTrue(weakref.getweakrefcount(o) == 1,
                      "wrong number of weak references to %r!" % o)
         self.assertTrue(o.arg is dict[o],
                      "wrong object returned by weak dict!")
     items1 = dict.items()
     items2 = dict.copy().items()
     del o
     self.assertTrue(set(items1) == set(items2),
                  "cloning of weak-keyed dictionary did not work!")
     del items1, items2
     self.assertTrue(len(dict) == self.COUNT)
     del objects[0]
     
     return dict, objects
예제 #53
0
파일: gc.py 프로젝트: yuanchenhuan/essay
def main3():
    a = OBJ()
    b = weakref.proxy(a)
    print(a, b, type(a), type(b))
    print(sys.getrefcount(a))
    print(sys.getrefcount(b))
    print(weakref.getweakrefs(a), weakref.getweakrefcount(a))
    print(b is a)  # False
    del a
    # print(b) # ReferenceError: weakly-referenced object no longer exists

    a = OBJ()
    c = weakref.ref(a)
    _c = c()
    print(c)
    print(_c is a)
    del a, _c
    print(c)
예제 #54
0
 def test_weak_values(self):
     #
     #  This exercises d.copy(), d.items(), d[], del d[], len(d).
     #
     dict, objects = self.make_weak_valued_dict()
     for o in objects:
         self.assertTrue(
             weakref.getweakrefcount(o) == 1,
             "wrong number of weak references to %r!" % o)
         self.assertTrue(o is dict[o.arg],
                         "wrong object returned by weak dict!")
     if test_support.due_to_ironpython_incompatibility():
         del o
     items1 = dict.items()
     items2 = dict.copy().items()
     items1.sort()
     items2.sort()
     self.assertTrue(items1 == items2,
                     "cloning of weak-valued dictionary did not work!")
     del items1, items2
     test_support.force_gc_collect()
     self.assertTrue(len(dict) == self.COUNT)
     del objects[0]
     if test_support.due_to_ironpython_incompatibility():
         gc.collect()
         del objects
     else:
         self.assertTrue(
             len(dict) == (self.COUNT - 1),
             "deleting object did not cause dictionary update")
         del objects, o
     test_support.force_gc_collect()
     if not test_support.due_to_ironpython_incompatibility():
         # we currently fail because of some garbage alive on the stack
         self.assertTrue(
             len(dict) == 0,
             "deleting the values did not clear the dictionary")
         # regression on SF bug #447152:
         dict = weakref.WeakValueDictionary()
         self.assertRaises(KeyError, dict.__getitem__, 1)
         dict[2] = C()
         self.assertRaises(KeyError, dict.__getitem__, 2)
예제 #55
0
        def runTest():
            o = C()
            ref1 = weakref.ref(o)
            # create a proxy to make sure that there's an intervening creation
            # between these two; it should make no difference
            proxy = weakref.proxy(o)
            ref2 = weakref.ref(o)
            self.assertTrue(ref1 is ref2,
                         "reference object w/out callback should be re-used")

            o = C()
            proxy = weakref.proxy(o)
            ref1 = weakref.ref(o)
            ref2 = weakref.ref(o)
            self.assertTrue(ref1 is ref2,
                         "reference object w/out callback should be re-used")
            self.assertTrue(weakref.getweakrefcount(o) == 2,
                         "wrong weak ref count for object")
            del proxy
            return o, ref1
예제 #56
0
        def runTest():
            o = C()
            ref1 = weakref.ref(o)
            # create a proxy to make sure that there's an intervening creation
            # between these two; it should make no difference
            proxy = weakref.proxy(o)
            ref2 = weakref.ref(o)
            self.assertTrue(ref1 is ref2,
                         "reference object w/out callback should be re-used")

            o = C()
            proxy = weakref.proxy(o)
            ref1 = weakref.ref(o)
            ref2 = weakref.ref(o)
            self.assertTrue(ref1 is ref2,
                         "reference object w/out callback should be re-used")
            self.assertTrue(weakref.getweakrefcount(o) == 2,
                         "wrong weak ref count for object")
            del proxy
            return o, ref1
예제 #57
0
 def test_weak_values(self):
     #
     #  This exercises d.copy(), d.items(), d[], del d[], len(d).
     #
     dict, objects = self.make_weak_valued_dict()
     for o in objects:
         self.assertTrue(weakref.getweakrefcount(o) == 1,
                      "wrong number of weak references to %r!" % o)
         self.assertTrue(o is dict[o.arg],
                      "wrong object returned by weak dict!")
     if test_support.due_to_ironpython_incompatibility():
         del o
     items1 = dict.items()
     items2 = dict.copy().items()
     items1.sort()
     items2.sort()
     self.assertTrue(items1 == items2,
                  "cloning of weak-valued dictionary did not work!")
     del items1, items2
     test_support.force_gc_collect()
     self.assertTrue(len(dict) == self.COUNT)
     del objects[0]
     if test_support.due_to_ironpython_incompatibility():
         gc.collect()
         del objects
     else:
         self.assertTrue(len(dict) == (self.COUNT - 1),
                     "deleting object did not cause dictionary update")
         del objects, o
     test_support.force_gc_collect()
     if not test_support.due_to_ironpython_incompatibility():
         # we currently fail because of some garbage alive on the stack
         self.assertTrue(len(dict) == 0,
                      "deleting the values did not clear the dictionary")
         # regression on SF bug #447152:
         dict = weakref.WeakValueDictionary()
         self.assertRaises(KeyError, dict.__getitem__, 1)
         dict[2] = C()
         self.assertRaises(KeyError, dict.__getitem__, 2)
예제 #58
0
def testWeakRefTester2():
    # create a weakRefTester2 object called newList
    newList = weakRefTester2('WeakRefTester')
    print("New List Object:", newList)

    # create a weak reference to newList
    weakRef = weakref.ref(newList, refCallback)

    # get a reference to the list referenced by the weak reference
    newWeakList = weakRef()

    # create a proxy to newList which uses a weak reference
    newProxy = weakref.proxy(newList, proxyCallBack)
    print("Weak reference to New List Object:", newWeakList)
    print('The object using a proxy: ' + str(newProxy))
    if newList is newWeakList:
        print("Original NewList is now a weak reference")

    print('The Weak reference count is: ' +
          str(weakref.getweakrefcount(newList)))
    del newList, newWeakList
    print("The weak reference is: " + str(weakRef()))
예제 #59
0
파일: to_yml.py 프로젝트: Py0s/KooC
def to_yml_item(item, pp, name):
    global scalar
    refcount = weakref.getweakrefcount(item)
    if refcount > 0:
        name += " &" + str(id(item))
    if type(item).__name__ in scalar:
        tag = fmt.end(
            '\n',
            fmt.sep(
                "",
                [
                    name,
                    " ",
                    yml_attr(
                        type(item).__name__,
                        repr(item)
                    )
                ]
            )
        )
        pp.append(tag)
        return
    if isinstance(item, weakref.ref):
        name += " *" + str(id(item()))
        tag = fmt.end('\n', fmt.sep("", [name, " ", repr(item)]))
        pp.append(tag)
        return
    if isinstance(item, bytes) or isinstance(item, bytearray):
        inner = fmt.tab([])
        tag = fmt.block(name + " " + str(yml_attr('type', 'bytes')) + ':\n',
                        '----' + name + '----\n', inner)
        inner.lsdata.append(fmt.sep(" ", []))
        bindata = inner.lsdata[-1].lsdata
        i = 0
        for b in item:
            bindata.append("%02X" % b)
            i += 1
            if i > 16:
                bindata.append("\n")
                i = 0
        bindata.append("\n")
        pp.append(tag)
        return
    if isinstance(item, object) and hasattr(item, '__dict__'):
        inner = fmt.tab([])
        tag = fmt.block(name + " " +
                        str(yml_attr('type', item.__class__.__name__))
                        + ':\n',
                        '', inner)
        for attr in sorted(vars(item)):
            to_yml_item(getattr(item, attr), inner.lsdata, attr)
        if len(vars(item)) == 0:
            inner.lsdata.append("\n")
        pp.append(tag)
        return
    if isinstance(item, list):
        inner = fmt.tab([])
        tag = fmt.block(
            name + " " + str(yml_attr('type', 'list')) + ':\n',
            '',
            inner
        )
        i = 0
        for subitem in item:
            idxname = str(fmt.sep(" ", ['[', i, ']']))
            to_yml_item(subitem, inner.lsdata, idxname)
            i += 1
        if len(item) == 0:
            inner.lsdata.append("\n")
        pp.append(tag)
        return
    if isinstance(item, tuple):
        inner = fmt.tab([])
        tag = fmt.block(name + " " + str(yml_attr('type', 'tuple')) + ':\n',
                        '', inner)
        i = 0
        for subitem in item:
            idxname = str(fmt.sep(" ", ["[", i, "]"]))
            to_yml_item(subitem, inner.lsdata, idxname)
            i += 1
        if len(item) == 0:
            inner.lsdata.append("\n")
        pp.append(tag)
        return
    if isinstance(item, dict):
        inner = fmt.tab([])
        tag = fmt.block(name + " " + str(yml_attr('type', 'dict')) + ':\n',
                        '', inner)
        for k in sorted(item.keys()):
            idxname = str(fmt.sep(" ", ["[", repr(k), "]"]))
            to_yml_item(item[k], inner.lsdata, idxname)
        if len(item.keys()) == 0:
            inner.lsdata.append("\n")
        pp.append(tag)
        return
    if isinstance(item, set):
        inner = fmt.tab([])
        tag = fmt.block(name + " " + str(yml_attr('type', 'set')) + ':\n',
                        '', inner)
        for subitem in sorted(item):
            inner.lsdata.append(fmt.sep(", "[repr(subitem)]))
        if len(item) == 0:
            inner.lsdata.append("\n")
        pp.append(tag)
        return
    if item is None:
        tag = fmt.end('\n', [name])
        pp.append(tag)
        return
예제 #60
0
 def idle(self):
     counter = 0
     for conn in self._pool:
         if weakref.getweakrefcount(conn) < 1:
             counter += 1
     return counter