[pypy-svn] r37748 - in pypy/dist/pypy/module/clr: . test

antocuni at codespeak.net antocuni at codespeak.net
Thu Feb 1 16:41:42 CET 2007


Author: antocuni
Date: Thu Feb  1 16:41:41 2007
New Revision: 37748

Modified:
   pypy/dist/pypy/module/clr/app_clr.py
   pypy/dist/pypy/module/clr/interp_clr.py
   pypy/dist/pypy/module/clr/test/test_clr.py
Log:
Support for static properties.



Modified: pypy/dist/pypy/module/clr/app_clr.py
==============================================================================
--- pypy/dist/pypy/module/clr/app_clr.py	(original)
+++ pypy/dist/pypy/module/clr/app_clr.py	Thu Feb  1 16:41:41 2007
@@ -69,6 +69,22 @@
         return '<bound CLI method %s.%s of %s>' % (self.im_self.__class__.__cliclass__, self.im_name, self.im_self)
 
 
+class StaticProperty(object):
+    def __init__(self, fget=None, fset=None):
+        self.fget = fget
+        self.fset = fset
+
+    def __get__(self, obj, type_):
+        return self.fget()
+
+class MetaCliClassWrapper(type):
+    def __setattr__(cls, name, value):
+        obj = cls.__dict__.get(name, None)
+        if isinstance(obj, StaticProperty):
+            obj.fset(value)
+        else:
+            type.__setattr__(cls, name, value)
+
 class CliClassWrapper(object):
     __slots__ = ('__cliobj__',)
 
@@ -88,22 +104,27 @@
 
     assert len(indexers) <= 1
     if indexers:
-        name, getter, setter = indexers[0]
+        name, getter, setter, is_static = indexers[0]
+        assert not is_static
         if getter:
             d['__getitem__'] = d[getter]
         if setter:
             d['__setitem__'] = d[setter]
-    cls = type(classname, (CliClassWrapper,), d)
-    
+    cls = MetaCliClassWrapper(classname, (CliClassWrapper,), d)
+
     # we must add properties *after* the class has been created
     # because we need to store UnboundMethods as getters and setters
-    for (name, getter, setter) in properties:
+    for (name, getter, setter, is_static) in properties:
         fget = None
         fset = None
         if getter:
             fget = getattr(cls, getter)
         if setter:
             fset = getattr(cls, setter)
-        setattr(cls, name, property(fget, fset))
+        if is_static:
+            prop = StaticProperty(fget, fset)
+        else:
+            prop = property(fget, fset)
+        setattr(cls, name, prop)
 
     return cls

Modified: pypy/dist/pypy/module/clr/interp_clr.py
==============================================================================
--- pypy/dist/pypy/module/clr/interp_clr.py	(original)
+++ pypy/dist/pypy/module/clr/interp_clr.py	Thu Feb  1 16:41:41 2007
@@ -93,8 +93,8 @@
 
 def wrap_list_of_tuples(space, lst):
     list_w = []
-    for (a,b,c) in lst:
-        items_w = [space.wrap(a), space.wrap(b), space.wrap(c)]
+    for (a,b,c,d) in lst:
+        items_w = [space.wrap(a), space.wrap(b), space.wrap(c), space.wrap(d)]
         list_w.append(space.newtuple(items_w))
     return space.newlist(list_w)
 
@@ -126,14 +126,18 @@
         get_name = None
         set_name = None
         if b_prop.get_CanRead():
-            get_name = b_prop.GetGetMethod().get_Name()
+            get_meth = b_prop.GetGetMethod()
+            get_name = get_meth.get_Name()
+            is_static = get_meth.get_IsStatic()
         if b_prop.get_CanWrite():
-            set_name = b_prop.GetSetMethod().get_Name()
+            set_meth = b_prop.GetSetMethod()
+            set_name = set_meth.get_Name()
+            is_static = set_meth.get_IsStatic()
         b_indexparams = b_prop.GetIndexParameters()
         if len(b_indexparams) == 0:
-            properties.append((b_prop.get_Name(), get_name, set_name))
+            properties.append((b_prop.get_Name(), get_name, set_name, is_static))
         else:
-            indexers.append((b_prop.get_Name(), get_name, set_name))
+            indexers.append((b_prop.get_Name(), get_name, set_name, is_static))
     w_properties = wrap_list_of_tuples(space, properties)
     w_indexers = wrap_list_of_tuples(space, indexers)
     return w_properties, w_indexers

Modified: pypy/dist/pypy/module/clr/test/test_clr.py
==============================================================================
--- pypy/dist/pypy/module/clr/test/test_clr.py	(original)
+++ pypy/dist/pypy/module/clr/test/test_clr.py	Thu Feb  1 16:41:41 2007
@@ -122,3 +122,11 @@
         x.Add("bar")
         s = x[0]
         assert s == "bar"
+
+    def test_static_property(self):
+        import clr
+        import os
+        Environment = clr.load_cli_class('System', 'Environment')
+        assert Environment.CurrentDirectory == os.getcwd()
+        Environment.CurrentDirectory == '/'
+        assert Environment.CurrentDirectory == os.getcwd()



More information about the Pypy-commit mailing list