| 1 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 2 |
from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w |
| 3 |
from pypy.module.cpyext.weakrefobject import PyWeakref_NewRef |
| 4 |
|
| 5 |
class TestWeakReference(BaseApiTest): |
| 6 |
def test_weakref(self, space, api): |
| 7 |
w_obj = space.w_Exception |
| 8 |
w_ref = api.PyWeakref_NewRef(w_obj, space.w_None) |
| 9 |
assert w_ref is not None |
| 10 |
assert space.is_w(api.PyWeakref_GetObject(w_ref), w_obj) |
| 11 |
assert space.is_w(api.PyWeakref_LockObject(w_ref), w_obj) |
| 12 |
|
| 13 |
w_obj = space.newtuple([]) |
| 14 |
with raises_w(space, TypeError): |
| 15 |
PyWeakref_NewRef(space, w_obj, space.w_None) |
| 16 |
|
| 17 |
def test_proxy(self, space, api): |
| 18 |
w_obj = space.w_Warning |
| 19 |
w_proxy = api.PyWeakref_NewProxy(w_obj, None) |
| 20 |
assert space.unwrap(space.str(w_proxy)) == "<class 'Warning'>" |
| 21 |
assert space.unwrap(space.repr(w_proxy)).startswith('<weak') |
| 22 |
|
| 23 |
def test_weakref_lockobject(self, space, api): |
| 24 |
|
| 25 |
w_obj = space.call_function(space.w_type, space.wrap("newtype"), |
| 26 |
space.newtuple([]), space.newdict()) |
| 27 |
assert w_obj is not None |
| 28 |
|
| 29 |
w_ref = api.PyWeakref_NewRef(w_obj, space.w_None) |
| 30 |
assert w_obj is not None |
| 31 |
|
| 32 |
assert space.is_w(api.PyWeakref_LockObject(w_ref), w_obj) |
| 33 |
del w_obj |
| 34 |
import gc; gc.collect() |
| 35 |
assert space.is_w(api.PyWeakref_LockObject(w_ref), space.w_None) |
| 36 |
|
| 37 |
|
| 38 |
class AppTestWeakReference(AppTestCpythonExtensionBase): |
| 39 |
|
| 40 |
def test_weakref_macro(self): |
| 41 |
module = self.import_extension('foo', [ |
| 42 |
("test_macro_cast", "METH_NOARGS", |
| 43 |
""" |
| 44 |
// PyExc_Warning is some weak-reffable PyObject*. |
| 45 |
char* dumb_pointer; |
| 46 |
PyObject* weakref_obj = PyWeakref_NewRef(PyExc_Warning, NULL); |
| 47 |
if (!weakref_obj) return weakref_obj; |
| 48 |
// No public PyWeakReference type. |
| 49 |
dumb_pointer = (char*) weakref_obj; |
| 50 |
|
| 51 |
PyWeakref_GET_OBJECT(weakref_obj); |
| 52 |
PyWeakref_GET_OBJECT(dumb_pointer); |
| 53 |
|
| 54 |
return weakref_obj; |
| 55 |
""" |
| 56 |
) |
| 57 |
]) |
| 58 |
module.test_macro_cast() |
| 59 |
|
| 60 |
def test_weakref_check(self): |
| 61 |
module = self.import_extension('foo', [ |
| 62 |
("test_weakref_cast", "METH_O", |
| 63 |
""" |
| 64 |
return Py_BuildValue("iiii", |
| 65 |
(int)PyWeakref_Check(args), |
| 66 |
(int)PyWeakref_CheckRef(args), |
| 67 |
(int)PyWeakref_CheckRefExact(args), |
| 68 |
(int)PyWeakref_CheckProxy(args)); |
| 69 |
""" |
| 70 |
) |
| 71 |
]) |
| 72 |
import weakref |
| 73 |
def foo(): pass |
| 74 |
class Bar(object): |
| 75 |
pass |
| 76 |
bar = Bar() |
| 77 |
assert module.test_weakref_cast([]) == (0, 0, 0, 0) |
| 78 |
assert module.test_weakref_cast(weakref.ref(foo)) == (1, 1, 1, 0) |
| 79 |
assert module.test_weakref_cast(weakref.ref(bar)) == (1, 1, 1, 0) |
| 80 |
assert module.test_weakref_cast(weakref.proxy(foo)) == (1, 0, 0, 1) |
| 81 |
assert module.test_weakref_cast(weakref.proxy(bar)) == (1, 0, 0, 1) |
| 82 |
class X(weakref.ref): |
| 83 |
pass |
| 84 |
assert module.test_weakref_cast(X(foo)) == (1, 1, 0, 0) |
| 85 |
assert module.test_weakref_cast(X(bar)) == (1, 1, 0, 0) |