Author: Armin Rigo <ar...@tunes.org>
Branch: ffi-backend
Changeset: r55781:dfa048d6ad13
Date: 2012-06-23 16:01 +0200
http://bitbucket.org/pypy/pypy/changeset/dfa048d6ad13/

Log:    read/write attributes.

diff --git a/pypy/module/_ffi_backend/cdataobj.py 
b/pypy/module/_ffi_backend/cdataobj.py
--- a/pypy/module/_ffi_backend/cdataobj.py
+++ b/pypy/module/_ffi_backend/cdataobj.py
@@ -134,6 +134,43 @@
         #
         return self._add_or_sub(w_other, -1)
 
+    def getattr(self, w_attr):
+        from pypy.module._ffi_backend import ctypeobj
+        space = self.space
+        ctype = self.ctype
+        attr = space.str_w(w_attr)
+        if (isinstance(ctype, ctypeobj.W_CTypeStructOrUnion) and
+                ctype.fields_dict is not None):
+            try:
+                cfield = ctype.fields_dict[attr]
+            except KeyError:
+                pass
+            else:
+                w_res = cfield.read(self._cdata)
+                keepalive_until_here(self)
+                return w_res
+        raise operationerrfmt(space.w_AttributeError,
+                              "cdata '%s' has no attribute '%s'",
+                              ctype.name, attr)
+
+    def setattr(self, w_attr, w_value):
+        from pypy.module._ffi_backend import ctypeobj
+        space = self.space
+        ctype = self.ctype
+        attr = space.str_w(w_attr)
+        if (isinstance(ctype, ctypeobj.W_CTypeStructOrUnion) and
+                ctype.fields_dict is not None):
+            try:
+                cfield = ctype.fields_dict[attr]
+            except KeyError:
+                pass
+            else:
+                cfield.write(self._cdata, w_value)
+                return
+        raise operationerrfmt(space.w_AttributeError,
+                              "cdata '%s' has no attribute '%s'",
+                              ctype.name, attr)
+
 ##    def read_raw_signed_data(self):
 ##        result = misc.read_raw_signed_data(self._cdata, self.ctype.size)
 ##        keepalive_until_here(self)
@@ -214,5 +251,7 @@
     __setitem__ = interp2app(W_CData.setitem),
     __add__ = interp2app(W_CData.add),
     __sub__ = interp2app(W_CData.sub),
+    __getattr__ = interp2app(W_CData.getattr),
+    __setattr__ = interp2app(W_CData.setattr),
     )
 W_CData.typedef.acceptable_as_base_class = False
diff --git a/pypy/module/_ffi_backend/ctypeobj.py 
b/pypy/module/_ffi_backend/ctypeobj.py
--- a/pypy/module/_ffi_backend/ctypeobj.py
+++ b/pypy/module/_ffi_backend/ctypeobj.py
@@ -410,6 +410,14 @@
                                         space.wrap(field)])
         return space.newlist(result)
 
+    def convert_to_object(self, cdata):
+        space = self.space
+        if self.size < 0:
+            raise operationerrfmt(space.w_TypeError,
+                                  "cannot return an incomplete cdata '%s'",
+                                  self.name)
+        return cdataobj.W_CData(space, cdata, self)
+
 
 class W_CTypeStruct(W_CTypeStructOrUnion):
     kind = "struct"
@@ -425,6 +433,20 @@
         self.bitshift = bitshift
         self.bitsize = bitsize
 
+    def read(self, cdata):
+        cdata = rffi.ptradd(cdata, self.offset)
+        if self.bitshift >= 0:
+            xxx
+        else:
+            return self.ctype.convert_to_object(cdata)
+
+    def write(self, cdata, w_ob):
+        cdata = rffi.ptradd(cdata, self.offset)
+        if self.bitshift >= 0:
+            xxx
+        else:
+            self.ctype.convert_from_object(cdata, w_ob)
+
 
 W_CType.typedef = TypeDef(
     '_ffi_backend.CTypeDescr',
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to