当前位置: 首页>>代码示例>>Python>>正文


Python weakref.getweakrefcount函数代码示例

本文整理汇总了Python中weakref.getweakrefcount函数的典型用法代码示例。如果您正苦于以下问题:Python getweakrefcount函数的具体用法?Python getweakrefcount怎么用?Python getweakrefcount使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了getweakrefcount函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_getweakrefcount

    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")
开发者ID:BillyboyD,项目名称:main,代码行数:25,代码来源:test_weakref.py

示例2: test_getweakrefcount

    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")
开发者ID:mancoast,项目名称:CPythonPyc_test,代码行数:11,代码来源:212_test_weakref.py

示例3: runTest

        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
开发者ID:BillyboyD,项目名称:main,代码行数:12,代码来源:test_weakref.py

示例4: test_weak_keys

 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))
开发者ID:alkorzt,项目名称:pypy,代码行数:31,代码来源:test_weakref.py

示例5: test_ref_reuse

    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")
开发者ID:BillyboyD,项目名称:main,代码行数:26,代码来源:test_weakref.py

示例6: test_weak_keys

 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)
开发者ID:pekkaklarck,项目名称:jython,代码行数:30,代码来源:test_weakref.py

示例7: test_weak_values

 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)
开发者ID:wdv4758h,项目名称:ZipPy,代码行数:31,代码来源:test_weakref.py

示例8: test_weak_values

 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)
开发者ID:pekkaklarck,项目名称:jython,代码行数:34,代码来源:test_weakref.py

示例9: test_weak_keys

 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)
开发者ID:dr4ke616,项目名称:custom_python,代码行数:27,代码来源:test_weakref.py

示例10: test_weak_keys

    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")
开发者ID:mancoast,项目名称:CPythonPyc_test,代码行数:25,代码来源:212_test_weakref.py

示例11: get

    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
开发者ID:cedricporter,项目名称:smartpool,代码行数:35,代码来源:smartpool.py

示例12: test_subclass_refs_dont_replace_standard_refs

 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:])
开发者ID:alkorzt,项目名称:pypy,代码行数:16,代码来源:test_weakref.py

示例13: test_weak_values

 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)
开发者ID:alkorzt,项目名称:pypy,代码行数:34,代码来源:test_weakref.py

示例14: test_getweakrefcount

    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")
开发者ID:svanschalkwyk,项目名称:datafari,代码行数:17,代码来源:test_weakref.py

示例15: test_ref_reuse

    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")
开发者ID:fabianonunes,项目名称:plone-4.1,代码行数:17,代码来源:test_weakref.py


注:本文中的weakref.getweakrefcount函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。