[pypy-svn] r70721 - in pypy/branch/separate-compilation/pypy/translator/c: . test

afa at codespeak.net afa at codespeak.net
Wed Jan 20 11:33:29 CET 2010


Author: afa
Date: Wed Jan 20 11:33:28 2010
New Revision: 70721

Modified:
   pypy/branch/separate-compilation/pypy/translator/c/separate.py
   pypy/branch/separate-compilation/pypy/translator/c/test/test_separate.py
Log:
Implement attribute access for external classes
Next: implement method call


Modified: pypy/branch/separate-compilation/pypy/translator/c/separate.py
==============================================================================
--- pypy/branch/separate-compilation/pypy/translator/c/separate.py	(original)
+++ pypy/branch/separate-compilation/pypy/translator/c/separate.py	Wed Jan 20 11:33:28 2010
@@ -18,6 +18,7 @@
         self.exported_function = {}
         self.exported_class = {}
         self.class_repr = {}
+        self.classdef = {}
 
     def make_wrapper_for_constructor(self, cls, name):
         nbargs = len(cls.__init__.argtypes)
@@ -95,6 +96,8 @@
         return exported_funcptr
 
     def make_wrapper_for_class(self, name, cls, new_func):
+        classdef = self.classdef[name]
+        attributes = classdef.attrs
         STRUCTPTR = self.class_repr[name]
 
         class C_Controller(Controller):
@@ -103,13 +106,21 @@
             def new(self_, *args):
                 return new_func(*args)
 
+            def __getattr__(self_, name):
+                if name.startswith('get_') and name[4:] in classdef.attrs:
+                    def getter(obj):
+                        return getattr(obj, 'inst_' + name[4:])
+                    return getter
+                if name.startswith('set_') and name[4:] in classdef.attrs:
+                    def setter(obj, value):
+                        setattr(obj, 'inst_' + name[4:], value)
+                    return setter
+                raise AttributeError(name)
+
         class Entry(ControllerEntry):
             _about_ = STRUCTPTR
             _controller_ = C_Controller
 
-        bookkeeper = getbookkeeper()
-        classdef = bookkeeper.getuniqueclassdef(cls)
-
         return STRUCTPTR
 
     def make_import_module(self, builder, node_names):
@@ -118,6 +129,7 @@
         for clsname, cls in self.exported_class.items():
             classdef = bookkeeper.getuniqueclassdef(cls)
             classrepr = rtyper.getrepr(model.SomeInstance(classdef)).lowleveltype
+            self.classdef[clsname] = classdef
             self.class_repr[clsname] = classrepr
 
         forwards = []

Modified: pypy/branch/separate-compilation/pypy/translator/c/test/test_separate.py
==============================================================================
--- pypy/branch/separate-compilation/pypy/translator/c/test/test_separate.py	(original)
+++ pypy/branch/separate-compilation/pypy/translator/c/test/test_separate.py	Wed Jan 20 11:33:28 2010
@@ -15,6 +15,10 @@
             def __init__(self, x):
                 self.x = x
 
+            @export
+            def invert(self):
+                self.x = 1 / self.x
+
         # functions exported from the 'first' module
         @export(float)
         def newS(x):
@@ -166,7 +170,6 @@
         assert c_fn() == 73.5
 
     def test_structure_attributes(self):
-        py.test.skip("WIP")
         firstmodule = self.compile_separated(
             "first", S=self.S)
 
@@ -179,7 +182,25 @@
 
         fn = self.call_exported(secondmodule.g)
 
-        assert fn() == 20.25
+        assert fn() == 20.75
+        c_fn = self.compile_function(fn, [])
+        assert c_fn() == 20.75
+
+    def test_method_call(self):
+        firstmodule = self.compile_separated(
+            "first", S=self.S)
+
+        @export()
+        def g():
+            s = firstmodule.S(8.0)
+            s.invert()
+            return s.x
+
+        secondmodule = self.compile_separated("second", g=g)
+
+        fn = self.call_exported(secondmodule.g)
+
+        assert fn() == 0.125
         c_fn = self.compile_function(fn, [])
-        assert c_fn() == 20.25
+        assert c_fn() == 0.125
 



More information about the Pypy-commit mailing list