[pypy-commit] pypy reflex-support: added bind_object method
wlav
noreply at buildbot.pypy.org
Fri Aug 19 02:54:58 CEST 2011
Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: reflex-support
Changeset: r46625:d4e404da778d
Date: 2011-08-18 18:02 -0700
http://bitbucket.org/pypy/pypy/changeset/d4e404da778d/
Log: added bind_object method
diff --git a/pypy/module/cppyy/__init__.py b/pypy/module/cppyy/__init__.py
--- a/pypy/module/cppyy/__init__.py
+++ b/pypy/module/cppyy/__init__.py
@@ -9,6 +9,7 @@
'_template_byname' : 'interp_cppyy.template_byname',
'CPPInstance' : 'interp_cppyy.W_CPPInstance',
'addressof' : 'interp_cppyy.addressof',
+ 'bind_object' : 'interp_cppyy.bind_object',
}
appleveldefs = {
diff --git a/pypy/module/cppyy/converter.py b/pypy/module/cppyy/converter.py
--- a/pypy/module/cppyy/converter.py
+++ b/pypy/module/cppyy/converter.py
@@ -560,7 +560,6 @@
from pypy.module.cppyy import interp_cppyy
# 1) full, exact match
- print 'asking for: ',
try:
return _converters[name](space, -1)
except KeyError, k:
diff --git a/pypy/module/cppyy/interp_cppyy.py b/pypy/module/cppyy/interp_cppyy.py
--- a/pypy/module/cppyy/interp_cppyy.py
+++ b/pypy/module/cppyy/interp_cppyy.py
@@ -1,7 +1,7 @@
import pypy.module.cppyy.capi as capi
from pypy.interpreter.error import OperationError
-from pypy.interpreter.gateway import ObjSpace, interp2app
+from pypy.interpreter.gateway import ObjSpace, interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef, interp_attrproperty
from pypy.interpreter.baseobjspace import Wrappable, W_Root
@@ -17,19 +17,20 @@
NULL_VOIDP = lltype.nullptr(rffi.VOIDP.TO)
+ at unwrap_spec(name=str)
def load_dictionary(space, name):
try:
cdll = capi.c_load_dictionary(name)
except rdynload.DLOpenError, e:
raise OperationError(space.w_RuntimeError, space.wrap(str(e)))
return W_CPPLibrary(space, cdll)
-load_dictionary.unwrap_spec = [ObjSpace, str]
class State(object):
def __init__(self, space):
self.cpptype_cache = { "void" : W_CPPType(space, "void", NULL_VOIDP) }
self.cpptemplatetype_cache = {}
+ at unwrap_spec(name=str)
def type_byname(space, name):
state = space.fromcache(State)
try:
@@ -50,8 +51,8 @@
return cpptype
return None
-type_byname.unwrap_spec = [ObjSpace, str]
+ at unwrap_spec(name=str)
def template_byname(space, name):
state = space.fromcache(State)
try:
@@ -66,13 +67,6 @@
return template
return None
-template_byname.unwrap_spec = [ObjSpace, str]
-
-def addressof(space, w_cppinstance):
- cppinstance = space.interp_w(W_CPPInstance, w_cppinstance, can_be_None=False)
- address = rffi.cast(rffi.LONG, cppinstance.rawobject)
- return space.wrap(address)
-addressof.unwrap_spec = [ObjSpace, W_Root]
class W_CPPLibrary(Wrappable):
@@ -525,6 +519,14 @@
raise OperationError(self.space.w_ReferenceError,
self.space.wrap("trying to access a NULL pointer"))
+ def instance__eq__(self, w_other):
+ other = self.space.interp_w(W_CPPInstance, w_other, can_be_None=False)
+ iseq = self.rawobject == other.rawobject
+ return self.space.wrap(iseq)
+
+ def instance__ne__(self, w_other):
+ return self.space.not_(self.instance__eq__(w_other))
+
def destruct(self):
assert isinstance(self, W_CPPInstance)
if self.rawobject:
@@ -540,6 +542,8 @@
W_CPPInstance.typedef = TypeDef(
'CPPInstance',
cppclass = interp_attrproperty('cppclass', cls=W_CPPInstance),
+ __eq__ = interp2app(W_CPPInstance.instance__eq__, unwrap_spec=['self', W_Root]),
+ __ne__ = interp2app(W_CPPInstance.instance__ne__, unwrap_spec=['self', W_Root]),
destruct = interp2app(W_CPPInstance.destruct, unwrap_spec=['self']),
)
W_CPPInstance.typedef.acceptable_as_base_class = True
@@ -549,3 +553,16 @@
cppinstance = space.interp_w(W_CPPInstance, w_cppinstance, can_be_None=False)
W_CPPInstance.__init__(cppinstance, space, cpptype, rawptr, owns)
return w_cppinstance
+
+
+ at unwrap_spec(cppinstance=W_CPPInstance)
+def addressof(space, cppinstance):
+ address = rffi.cast(rffi.LONG, cppinstance.rawobject)
+ return space.wrap(address)
+
+ at unwrap_spec(address=int, owns=bool)
+def bind_object(space, address, w_type, owns=False):
+ rawobject = rffi.cast(rffi.VOIDP, address)
+ w_cpptype = space.findattr(w_type, space.wrap("_cpp_proxy"))
+ cpptype = space.interp_w(W_CPPType, w_cpptype, can_be_None=False)
+ return new_instance(space, w_type, cpptype, rawobject, owns)
diff --git a/pypy/module/cppyy/test/test_advancedcpp.py b/pypy/module/cppyy/test/test_advancedcpp.py
--- a/pypy/module/cppyy/test/test_advancedcpp.py
+++ b/pypy/module/cppyy/test/test_advancedcpp.py
@@ -335,3 +335,24 @@
assert cppyy.addressof(o) == pp.gime_address_ptr(o)
assert cppyy.addressof(o) == pp.gime_address_ptr_ptr(o)
assert cppyy.addressof(o) == pp.gime_address_ptr_ref(o)
+
+ def test09_opaque_pointer_assing( self ):
+ """Test passing around of opaque pointers"""
+
+ import cppyy
+ some_concrete_class = cppyy.gbl.some_concrete_class
+
+ o = some_concrete_class()
+
+ #cobj = cppyy.as_cobject(o)
+ addr = cppyy.addressof(o)
+
+ #assert o == cppyy.bind_object(cobj, some_concrete_class)
+ #assert o == cppyy.bind_object(cobj, type(o))
+ #assert o == cppyy.bind_object(cobj, o.__class__)
+ #assert o == cppyy.bind_object(cobj, "some_concrete_class")
+ assert cppyy.addressof(o) == cppyy.addressof(cppyy.bind_object(addr, some_concrete_class))
+ assert o == cppyy.bind_object(addr, some_concrete_class)
+ assert o == cppyy.bind_object(addr, type(o))
+ assert o == cppyy.bind_object(addr, o.__class__)
+ #assert o == cppyy.bind_object(addr, "some_concrete_class")
More information about the pypy-commit
mailing list