Author: Armin Rigo <ar...@tunes.org>
Branch: cffi-1.0
Changeset: r77194:36252eaabebf
Date: 2015-05-08 09:39 +0200
http://bitbucket.org/pypy/pypy/changeset/36252eaabebf/

Log:    Writing to global vars

diff --git a/pypy/module/_cffi_backend/cglob.py 
b/pypy/module/_cffi_backend/cglob.py
--- a/pypy/module/_cffi_backend/cglob.py
+++ b/pypy/module/_cffi_backend/cglob.py
@@ -12,5 +12,8 @@
     def read_global_var(self):
         return self.w_ctype.convert_to_object(self.ptr)
 
+    def write_global_var(self, w_newvalue):
+        self.w_ctype.convert_from_object(self.ptr, w_newvalue)
+
 W_GlobSupport.typedef = TypeDef("FFIGlobSupport")
 W_GlobSupport.typedef.acceptable_as_base_class = False
diff --git a/pypy/module/_cffi_backend/lib_obj.py 
b/pypy/module/_cffi_backend/lib_obj.py
--- a/pypy/module/_cffi_backend/lib_obj.py
+++ b/pypy/module/_cffi_backend/lib_obj.py
@@ -26,7 +26,7 @@
         XXX
 
     @jit.elidable_promote()
-    def _get_attr(self, attr):
+    def _get_attr_elidable(self, attr):
         try:
             w_result = self.dict_w[attr]
         except KeyError:
@@ -73,26 +73,42 @@
             self.dict_w[attr] = w_result
         return w_result
 
-    def _no_such_attr(self, attr):
-        raise oefmt(self.space.w_AttributeError,
-                    "cffi lib '%s' has no function,"
-                    " global variable or constant named '%s'",
-                    self.libname, attr)
+    def _get_attr(self, w_attr):
+        attr = self.space.str_w(w_attr)
+        w_value = self._get_attr_elidable(attr)
+        if w_value is None:
+            raise oefmt(self.space.w_AttributeError,
+                        "cffi lib '%s' has no function,"
+                        " global variable or constant named '%s'",
+                        self.libname, attr)
+        return w_value
 
     def descr_getattribute(self, w_attr):
-        space = self.space
-        attr = space.str_w(w_attr)
-        w_value = self._get_attr(attr)
-        if w_value is None:
-            raise self._no_such_attr(attr)
-        elif isinstance(w_value, cglob.W_GlobSupport):
+        w_value = self._get_attr(w_attr)
+        if isinstance(w_value, cglob.W_GlobSupport):
             w_value = w_value.read_global_var()
         return w_value
 
+    def descr_setattr(self, w_attr, w_newvalue):
+        w_value = self._get_attr(w_attr)
+        if isinstance(w_value, cglob.W_GlobSupport):
+            w_value.write_global_var(w_newvalue)
+        else:
+            raise oefmt(self.space.w_AttributeError,
+                        "cannot write to function or constant '%s'",
+                        self.space.str_w(w_attr))
+
+    def descr_delattr(self, w_attr):
+        self._get_attr(w_attr)    # for the possible AttributeError
+        raise oefmt(self.space.w_AttributeError,
+                    "C attribute cannot be deleted")
+
 
 W_LibObject.typedef = TypeDef(
         'CompiledLib',
         __repr__ = interp2app(W_LibObject.descr_repr),
         __getattribute__ = interp2app(W_LibObject.descr_getattribute),
+        __setattr__ = interp2app(W_LibObject.descr_setattr),
+        __delattr__ = interp2app(W_LibObject.descr_delattr),
         )
 W_LibObject.typedef.acceptable_as_base_class = False
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to