def test_new_handle():
import _weakref
BVoidP = new_pointer_type(new_void_type())
BCharP = new_pointer_type(new_primitive_type("char"))
class mylist(list):
pass
o = mylist([2, 3, 4])
x = newp_handle(BVoidP, o)
assert repr(x) == "<cdata 'void *' handle to [2, 3, 4]>"
assert x
assert from_handle(x) is o
assert from_handle(cast(BCharP, x)) is o
wr = _weakref.ref(o)
del o
import gc; gc.collect()
assert wr() is not None
assert from_handle(x) == list((2, 3, 4))
assert from_handle(cast(BCharP, x)) == list((2, 3, 4))
del x
for i in range(3):
if wr() is not None:
import gc; gc.collect()
assert wr() is None
py.test.raises(RuntimeError, from_handle, cast(BCharP, 0))
评论列表
文章目录