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


Python sys.getrefcount函数代码示例

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


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

示例1: test04_leak_GC

 def test04_leak_GC(self):
     import sys
     a = "example of private object"
     refcount = sys.getrefcount(a)
     self.obj.set_private(a)
     self.obj = None
     self.assertEqual(refcount, sys.getrefcount(a))
开发者ID:Rogerlin2013,项目名称:CrossApp,代码行数:7,代码来源:test_basics.py

示例2: check_set_double_new

 def check_set_double_new(self,level=5):
     a = UserDict()
     key = 1.0
     inline_tools.inline('a[key] = 123.0;',['a','key'])
     assert_equal(sys.getrefcount(key),4) # should be 3
     assert_equal(sys.getrefcount(a[key]),2)
     assert_equal(a[key],123.0)
开发者ID:mbentz80,项目名称:jzigbeercp,代码行数:7,代码来源:test_scxx_object.py

示例3: test_no_refcycle_through_target

    def test_no_refcycle_through_target(self):
        class RunSelfFunction(object):
            def __init__(self, should_raise):
                # The links in this refcycle from Thread back to self
                # should be cleaned up when the thread completes.
                self.should_raise = should_raise
                self.thread = threading.Thread(target=self._run, args=(self,), kwargs={"yet_another": self})
                self.thread.start()

            def _run(self, other_ref, yet_another):
                if self.should_raise:
                    raise SystemExit

        cyclic_object = RunSelfFunction(should_raise=False)
        weak_cyclic_object = weakref.ref(cyclic_object)
        cyclic_object.thread.join()
        del cyclic_object
        self.assertIsNone(
            weak_cyclic_object(), msg=("%d references still around" % sys.getrefcount(weak_cyclic_object()))
        )

        raising_cyclic_object = RunSelfFunction(should_raise=True)
        weak_raising_cyclic_object = weakref.ref(raising_cyclic_object)
        raising_cyclic_object.thread.join()
        del raising_cyclic_object
        self.assertIsNone(
            weak_raising_cyclic_object(),
            msg=("%d references still around" % sys.getrefcount(weak_raising_cyclic_object())),
        )
开发者ID:pykomke,项目名称:Kurz_Python_KE,代码行数:29,代码来源:test_threading.py

示例4: test_memoize_method

    def test_memoize_method(self):
        class foo(object):
            def __init__(self):
                self._count = 0

            @memoize
            def wrapped(self, a, b):
                self._count += 1
                return a + b

        instance = foo()
        refcount = sys.getrefcount(instance)
        self.assertEqual(instance._count, 0)
        self.assertEqual(instance.wrapped(1, 1), 2)
        self.assertEqual(instance._count, 1)
        self.assertEqual(instance.wrapped(1, 1), 2)
        self.assertEqual(instance._count, 1)
        self.assertEqual(instance.wrapped(2, 1), 3)
        self.assertEqual(instance._count, 2)
        self.assertEqual(instance.wrapped(1, 2), 3)
        self.assertEqual(instance._count, 3)
        self.assertEqual(instance.wrapped(1, 2), 3)
        self.assertEqual(instance._count, 3)
        self.assertEqual(instance.wrapped(1, 1), 2)
        self.assertEqual(instance._count, 3)

        # Memoization of methods is expected to not keep references to
        # instances, so the refcount shouldn't have changed after executing the
        # memoized method.
        self.assertEqual(refcount, sys.getrefcount(instance))
开发者ID:shanewfx,项目名称:gecko-dev,代码行数:30,代码来源:test_util.py

示例5: test_set_complex

 def test_set_complex(self):
     a = UserDict()
     key = 1+1j
     inline_tools.inline("a[key] = 1234;",['a','key'])
     assert_equal(sys.getrefcount(key),4) # should be 3
     assert_equal(sys.getrefcount(a[key]),2)
     assert_equal(a[key],1234)
开发者ID:b-t-g,项目名称:Sim,代码行数:7,代码来源:test_scxx_object.py

示例6: test_nrt_gen0_stop_iteration

    def test_nrt_gen0_stop_iteration(self):
        """
        Test cleanup on StopIteration
        """
        pygen = nrt_gen0
        cgen = jit(nopython=True)(pygen)

        py_ary = np.arange(1)
        c_ary = py_ary.copy()

        py_iter = pygen(py_ary)
        c_iter = cgen(c_ary)

        py_res = next(py_iter)
        c_res = next(c_iter)

        with self.assertRaises(StopIteration):
            py_res = next(py_iter)

        with self.assertRaises(StopIteration):
            c_res = next(c_iter)

        del py_iter
        del c_iter

        np.testing.assert_equal(py_ary, c_ary)
        self.assertEqual(py_res, c_res)
        # Check reference count
        self.assertEqual(sys.getrefcount(py_ary),
                         sys.getrefcount(c_ary))
开发者ID:EGQM,项目名称:numba,代码行数:30,代码来源:test_generators.py

示例7: testBogusObject

    def testBogusObject(self):
        def add(key, obj):
            self.cache[key] = obj

        nones = sys.getrefcount(None)

        key = p64(2)
        # value isn't persistent
        self.assertRaises(TypeError, add, key, 12)

        o = StubObject()
        # o._p_oid == None
        self.assertRaises(TypeError, add, key, o)

        o._p_oid = p64(3)
        self.assertRaises(ValueError, add, key, o)

        o._p_oid = key
        # o._p_jar == None
        self.assertRaises(Exception, add, key, o)

        o._p_jar = self.jar
        self.cache[key] = o
        # make sure it can be added multiple times
        self.cache[key] = o

        # same object, different keys
        self.assertRaises(ValueError, add, p64(0), o)

        if sys.gettrace() is None:
            # 'coverage' keeps track of coverage information in a data
            # structure that adds a new reference to None for each executed
            # line of code, which interferes with this test.  So check it
            # only if we're running without coverage tracing.
            self.assertEqual(sys.getrefcount(None), nones)
开发者ID:sarvex,项目名称:ZODB,代码行数:35,代码来源:testCache.py

示例8: test_refs

 def test_refs(self):
     if hasattr(sys, "getrefcount"):
         for tp in self._types:
             m = memoryview(tp(self._source))
             oldrefcount = sys.getrefcount(m)
             m[1:2]
             self.assertEqual(sys.getrefcount(m), oldrefcount)
开发者ID:isaiah,项目名称:jython3,代码行数:7,代码来源:test_memoryview.py

示例9: test_leak_references_on

    def test_leak_references_on(self):
        model = TesterModel()
        obj_ref = weakref.ref(model.root)
        # Initial refcount is 1 for model.root + the temporary
        self.assertEqual(sys.getrefcount(model.root), 2)

        # Iter increases by 1 do to assignment to iter.user_data
        res, it = model.do_get_iter([0])
        self.assertEqual(id(model.root), it.user_data)
        self.assertEqual(sys.getrefcount(model.root), 3)

        # Verify getting a TreeIter more then once does not further increase
        # the ref count.
        res2, it2 = model.do_get_iter([0])
        self.assertEqual(id(model.root), it2.user_data)
        self.assertEqual(sys.getrefcount(model.root), 3)

        # Deleting the iter does not decrease refcount because references
        # leak by default (they are stored in the held_refs pool)
        del it
        gc.collect()
        self.assertEqual(sys.getrefcount(model.root), 3)

        # Deleting a model should free all held references to user data
        # stored by TreeIters
        del model
        gc.collect()
        self.assertEqual(obj_ref(), None)
开发者ID:Distrotech,项目名称:pygobject,代码行数:28,代码来源:test_generictreemodel.py

示例10: test_set_double_new

 def test_set_double_new(self):
     a = UserDict()
     key = 1.0
     inline_tools.inline("a[key] = 123.0;", ["a", "key"])
     assert_equal(sys.getrefcount(key), 4)  # should be 3
     assert_equal(sys.getrefcount(a[key]), 2)
     assert_equal(a[key], 123.0)
开发者ID:mattyhk,项目名称:basketball-django,代码行数:7,代码来源:test_scxx_object.py

示例11: test_commit_refcount

 def test_commit_refcount(self):
     commit = self.repo[COMMIT_SHA]
     start = sys.getrefcount(commit)
     tree = commit.tree
     del tree
     end = sys.getrefcount(commit)
     self.assertEqual(start, end)
开发者ID:feisuzhu,项目名称:pygit2,代码行数:7,代码来源:test_commit.py

示例12: test04_leak_GC

 def test04_leak_GC(self):
     a = 'example of private object'
     refcount = sys.getrefcount(a)
     self.obj.set_private(a)
     self.obj = None
     self.assertEqual(refcount, sys.getrefcount(a))
     return
开发者ID:webiumsk,项目名称:WOT-0.9.14-CT,代码行数:7,代码来源:test_basics.py

示例13: testGetSetUpd

 def testGetSetUpd(self):
     self.assertTrue(self.snes.getUpdate() is None)
     upd = lambda snes, it: None
     refcnt = getrefcount(upd)
     self.snes.setUpdate(upd)
     self.assertEqual(getrefcount(upd), refcnt + 1)
     self.snes.setUpdate(upd)
     self.assertEqual(getrefcount(upd), refcnt + 1)
     self.snes.setUpdate(None)
     self.assertTrue(self.snes.getUpdate() is None)
     self.assertEqual(getrefcount(upd), refcnt)
     self.snes.setUpdate(upd)
     self.assertEqual(getrefcount(upd), refcnt + 1)
     upd2 = lambda snes, it: None
     refcnt2 = getrefcount(upd2)
     self.snes.setUpdate(upd2)
     self.assertEqual(getrefcount(upd),  refcnt)
     self.assertEqual(getrefcount(upd2), refcnt2 + 1)
     tmp = self.snes.getUpdate()[0]
     self.assertTrue(tmp is upd2)
     self.assertEqual(getrefcount(upd2), refcnt2 + 2)
     del tmp
     self.snes.setUpdate(None)
     self.assertTrue(self.snes.getUpdate() is None)
     self.assertEqual(getrefcount(upd2), refcnt2)
开发者ID:cstausland,项目名称:ppde_chris,代码行数:25,代码来源:test_snes.py

示例14: test_to_pandas_zero_copy

def test_to_pandas_zero_copy():
    import gc

    arr = pyarrow.from_pylist(range(10))

    for i in range(10):
        np_arr = arr.to_pandas()
        assert sys.getrefcount(np_arr) == 2
        np_arr = None  # noqa

    assert sys.getrefcount(arr) == 2

    for i in range(10):
        arr = pyarrow.from_pylist(range(10))
        np_arr = arr.to_pandas()
        arr = None
        gc.collect()

        # Ensure base is still valid

        # Because of py.test's assert inspection magic, if you put getrefcount
        # on the line being examined, it will be 1 higher than you expect
        base_refcount = sys.getrefcount(np_arr.base)
        assert base_refcount == 2
        np_arr.sum()
开发者ID:julienledem,项目名称:arrow,代码行数:25,代码来源:test_array.py

示例15: test_swap

    def test_swap(self):

        def pyfunc(x, y, t):
            """Swap array x and y for t number of times
            """
            for i in range(t):
                x, y = y, x

            return x, y


        cfunc = nrtjit(pyfunc)

        x = np.random.random(100)
        y = np.random.random(100)

        t = 100

        initrefct = sys.getrefcount(x), sys.getrefcount(y)
        expect, got = pyfunc(x, y, t), cfunc(x, y, t)
        self.assertIsNone(got[0].base)
        self.assertIsNone(got[1].base)
        np.testing.assert_equal(expect, got)
        del expect, got
        self.assertEqual(initrefct, (sys.getrefcount(x), sys.getrefcount(y)))
开发者ID:gmarkall,项目名称:numba,代码行数:25,代码来源:test_dyn_array.py


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