pypy / pypy / module / cpyext / test /

from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.module.cpyext.test.test_api import BaseApiTest

class TestWeakReference(BaseApiTest):
    def test_weakref(self, space, api):
        w_obj = space.w_Exception
        w_ref = api.PyWeakref_NewRef(w_obj, space.w_None)
        assert w_ref is not None
        assert space.is_w(api.PyWeakref_GetObject(w_ref), w_obj)
        assert space.is_w(api.PyWeakref_GET_OBJECT(w_ref), w_obj)
        assert space.is_w(api.PyWeakref_LockObject(w_ref), w_obj)

        w_obj = space.newtuple([])
        assert api.PyWeakref_NewRef(w_obj, space.w_None) is None
        assert api.PyErr_Occurred() is space.w_TypeError

    def test_weakref_proxy(self, space, api):
        w_obj = space.w_Exception
        w_proxy = api.PyWeakref_NewProxy(w_obj, space.w_None)
        assert w_proxy is not None
        assert space.is_w(w_proxy.dereference(), w_obj)
        ret = api.PyWeakref_CheckProxy(w_proxy)
        assert ret == 1

    def test_weakref_lockobject(self, space, api):
        # some new weakrefable object
        w_obj = space.call_function(space.w_type, space.wrap("newtype"),
                                    space.newtuple([]), space.newdict())
        assert w_obj is not None

        w_ref = api.PyWeakref_NewRef(w_obj, space.w_None)
        assert w_obj is not None

        assert space.is_w(api.PyWeakref_LockObject(w_ref), w_obj)
        del w_obj
        import gc; gc.collect()
        assert space.is_w(api.PyWeakref_LockObject(w_ref), space.w_None)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.