[pypy-commit] pypy sepcomp2: Implement attribute access

amauryfa noreply at buildbot.pypy.org
Wed Feb 22 23:24:31 CET 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: sepcomp2
Changeset: r52774:094c275fd0b4
Date: 2012-02-22 21:44 +0100
http://bitbucket.org/pypy/pypy/changeset/094c275fd0b4/

Log:	Implement attribute access

diff --git a/pypy/translator/c/exportinfo.py b/pypy/translator/c/exportinfo.py
--- a/pypy/translator/c/exportinfo.py
+++ b/pypy/translator/c/exportinfo.py
@@ -153,8 +153,8 @@
     def save_repr(self, builder):
         rtyper = builder.db.translator.rtyper
         bookkeeper = rtyper.annotator.bookkeeper
-        classdef = bookkeeper.getuniqueclassdef(self.cls)
-        self.classrepr = rtyper.getrepr(model.SomeInstance(classdef)
+        self.classdef = bookkeeper.getuniqueclassdef(self.cls)
+        self.classrepr = rtyper.getrepr(model.SomeInstance(self.classdef)
                                         ).lowleveltype
         
     def make_controller(self, module):
@@ -170,6 +170,16 @@
             def new(self, *args):
                 return constructor(*args)
 
+        def install_attribute(name):
+            def getter(self, obj):
+                return getattr(obj, 'inst_' + name)
+            setattr(ClassController, 'get_' + name, getter)
+            def setter(self, obj, value):
+                return getattr(obj, 'inst_' + name, value)
+            setattr(ClassController, 'set_' + name, getter)
+        for name, attrdef in self.classdef.attrs.items():
+            install_attribute(name)
+
         class Entry(ControllerEntry):
             _about_ = self.cls
             _controller_ = ClassController
diff --git a/pypy/translator/c/test/test_export.py b/pypy/translator/c/test/test_export.py
--- a/pypy/translator/c/test/test_export.py
+++ b/pypy/translator/c/test/test_export.py
@@ -125,3 +125,21 @@
         mod = self.compile_module("second", g=g)
         assert mod.g() == 62.3
         # XXX How can we check that the code of f() was not translated again?
+
+    def test_attribute_access(self):
+        class Struct:
+            @export(float)
+            def __init__(self, x):
+                self.x = x
+        @export(Struct)
+        def increment(s):
+            s.x += 33.2
+        self.compile_module("first", Struct=Struct, increment=increment)
+
+        @export()
+        def g():
+            s = Struct(3.0)
+            increment(s)
+            return s.x
+        mod = self.compile_module("second", g=g)
+        assert mod.g() == 36.2


More information about the pypy-commit mailing list