[pypy-svn] r74100 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test
afa at codespeak.net
afa at codespeak.net
Tue Apr 27 10:32:36 CEST 2010
Author: afa
Date: Tue Apr 27 10:32:34 2010
New Revision: 74100
Added:
pypy/branch/cpython-extension/pypy/module/cpyext/test/test_weakref.py (contents, props changed)
pypy/branch/cpython-extension/pypy/module/cpyext/weakrefobject.py (contents, props changed)
Modified:
pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py
pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
Log:
PyWeakref_NewRef, PyWeakref_GetObject
Modified: pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py (original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py Tue Apr 27 10:32:34 2010
@@ -66,6 +66,7 @@
import pypy.module.cpyext.pystate
import pypy.module.cpyext.datetime
import pypy.module.cpyext.complexobject
+import pypy.module.cpyext.weakrefobject
# now that all rffi_platform.Struct types are registered, configure them
api.configure_types()
Modified: pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py (original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py Tue Apr 27 10:32:34 2010
@@ -4782,19 +4782,6 @@
raise NotImplementedError
@cpython_api([PyObject, PyObject], PyObject)
-def PyWeakref_NewRef(space, ob, callback):
- """Return a weak reference object for the object ob. This will always return
- a new reference, but is not guaranteed to create a new object; an existing
- reference object may be returned. The second parameter, callback, can be a
- callable object that receives notification when ob is garbage collected; it
- should accept a single parameter, which will be the weak reference object
- itself. callback may also be None or NULL. If ob is not a
- weakly-referencable object, or if callback is not callable, None, or
- NULL, this will return NULL and raise TypeError.
- """
- raise NotImplementedError
-
- at cpython_api([PyObject, PyObject], PyObject)
def PyWeakref_NewProxy(space, ob, callback):
"""Return a weak reference proxy object for the object ob. This will always
return a new reference, but is not guaranteed to create a new object; an
@@ -4808,13 +4795,6 @@
raise NotImplementedError
@cpython_api([PyObject], PyObject, borrowed=True)
-def PyWeakref_GetObject(space, ref):
- """Return the referenced object from a weak reference, ref. If the referent is
- no longer live, returns None.
- """
- raise NotImplementedError
-
- at cpython_api([PyObject], PyObject, borrowed=True)
def PyWeakref_GET_OBJECT(space, ref):
"""Similar to PyWeakref_GetObject(), but implemented as a macro that does no
error checking.
Added: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_weakref.py
==============================================================================
--- (empty file)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_weakref.py Tue Apr 27 10:32:34 2010
@@ -0,0 +1,14 @@
+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)
+
+ w_obj = space.newtuple([])
+ assert api.PyWeakref_NewRef(w_obj, space.w_None) is None
+ assert api.PyErr_Occurred() is space.w_TypeError
+ api.PyErr_Clear()
Added: pypy/branch/cpython-extension/pypy/module/cpyext/weakrefobject.py
==============================================================================
--- (empty file)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/weakrefobject.py Tue Apr 27 10:32:34 2010
@@ -0,0 +1,26 @@
+from pypy.module.cpyext.api import cpython_api
+from pypy.module.cpyext.pyobject import PyObject, register_container
+from pypy.module._weakref.interp__weakref import W_Weakref
+
+ at cpython_api([PyObject, PyObject], PyObject)
+def PyWeakref_NewRef(space, w_obj, w_callback):
+ """Return a weak reference object for the object ob. This will always return
+ a new reference, but is not guaranteed to create a new object; an existing
+ reference object may be returned. The second parameter, callback, can be a
+ callable object that receives notification when ob is garbage collected; it
+ should accept a single parameter, which will be the weak reference object
+ itself. callback may also be None or NULL. If ob is not a
+ weakly-referencable object, or if callback is not callable, None, or
+ NULL, this will return NULL and raise TypeError.
+ """
+ w_weakref = space.gettypeobject(W_Weakref.typedef)
+ return space.call_function(w_weakref, w_obj, w_callback)
+
+ at cpython_api([PyObject], PyObject, borrowed=True)
+def PyWeakref_GetObject(space, w_ref):
+ """Return the referenced object from a weak reference, ref. If the referent is
+ no longer live, returns None.
+ """
+ register_container(space, w_ref)
+ return space.call_function(w_ref)
+
More information about the Pypy-commit
mailing list