[pypy-commit] pypy reflex-support: fix rtyper error and proper bindings of instance data members

wlav noreply at buildbot.pypy.org
Wed Jul 13 15:18:00 CEST 2011


Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: reflex-support
Changeset: r45550:95fe0aabf5d4
Date: 2011-07-13 06:17 -0700
http://bitbucket.org/pypy/pypy/changeset/95fe0aabf5d4/

Log:	fix rtyper error and proper bindings of instance data members

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
@@ -7,6 +7,7 @@
         '_load_lib'              : 'interp_cppyy.load_lib',
         '_type_byname'           : 'interp_cppyy.type_byname',
         '_template_byname'       : 'interp_cppyy.template_byname',
+        'CPPInstance'            : 'interp_cppyy.W_CPPInstance',
     }
 
     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
@@ -511,7 +511,7 @@
         address = self._get_raw_address(space, w_obj, offset)
         obj_address = rffi.cast(rffi.VOIDP, address)
         from pypy.module.cppyy import interp_cppyy
-        return interp_cppyy.W_CPPInstance(space, self.cpptype, obj_address)
+        return interp_cppyy.W_CPPInstance(space, self.cpptype, obj_address, False)
 
     def free_argument(self, arg):
         pass
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
@@ -2,7 +2,7 @@
 
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import ObjSpace, interp2app
-from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.typedef import TypeDef, interp_attrproperty
 from pypy.interpreter.baseobjspace import Wrappable
 
 from pypy.rpython.lltypesystem import rffi, lltype
@@ -497,6 +497,7 @@
 
 W_CPPType.typedef = TypeDef(
     'CPPType',
+    type_name = interp_attrproperty('name', W_CPPType),
     get_base_names = interp2app(W_CPPType.get_base_names, unwrap_spec=['self']),
     get_method_names = interp2app(W_CPPType.get_method_names, unwrap_spec=['self']),
     get_overload = interp2app(W_CPPType.get_overload, unwrap_spec=['self', str]),
@@ -555,6 +556,7 @@
 
 W_CPPInstance.typedef = TypeDef(
     'CPPInstance',
+    cppclass = interp_attrproperty('cppclass', W_CPPInstance),
     invoke = interp2app(W_CPPInstance.invoke, unwrap_spec=['self', W_CPPOverload, 'args_w']),
     destruct = interp2app(W_CPPInstance.destruct, unwrap_spec=['self']),
 )
diff --git a/pypy/module/cppyy/pythonify.py b/pypy/module/cppyy/pythonify.py
--- a/pypy/module/cppyy/pythonify.py
+++ b/pypy/module/cppyy/pythonify.py
@@ -82,6 +82,16 @@
     return method
 
 
+def make_datamember(cppdm):
+    import cppyy
+    def binder(obj, owner=None):
+        value = cppdm.__get__(obj, owner)
+        if isinstance(value, cppyy.CPPInstance):
+             cppclass = get_cppclass(value.cppclass.type_name)
+             return bind_object(value, cppclass)
+        return value
+    return property(binder, cppdm.__set__)
+
 def make_cppnamespace(namespace_name, cppns):
     d = {"_cpp_proxy" : cppns}
 
@@ -97,8 +107,9 @@
     # static ones also to the meta class (needed for property setters)
     for dm in cppns.get_data_member_names():
         cppdm = cppns.get_data_member(dm)
-        d[dm] = cppdm
-        setattr(metans, dm, cppdm)
+        pydm = make_datamember(cppdm)
+        d[dm] = pydm
+        setattr(metans, dm, pydm)
 
     # create the python-side C++ namespace representation
     pycppns = metans(namespace_name, (object,), d)
@@ -147,10 +158,11 @@
     # static ones also to the meta class (needed for property setters)
     for dm_name in cpptype.get_data_member_names():
         cppdm = cpptype.get_data_member(dm_name)
+        pydm = make_datamember(cppdm)
 
-        setattr(pycpptype, dm_name, cppdm)
+        setattr(pycpptype, dm_name, pydm)
         if cppdm.is_static():
-            setattr(metacpp, dm_name, cppdm)
+            setattr(metacpp, dm_name, pydm)
 
     _pythonize(pycpptype)
     return pycpptype
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
@@ -175,9 +175,9 @@
 
         #-----
         t2 = gbl.T2(gbl.T1(int))(gbl.T1(int)(32))
-#        t2.m_t2.m_t1 = 32
-#        assert t2.m_t2.value() == 32
-#        assert t2.m_t2.m_t1    == 32
+        t2.m_t2.m_t1 = 32
+        assert t2.m_t2.value() == 32
+        assert t2.m_t2.m_t1    == 32
         t2.destruct()
 
     def test05_abstract_classes(self):


More information about the pypy-commit mailing list