Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r84203:6cac09131559
Date: 2016-05-04 18:29 -0700
http://bitbucket.org/pypy/pypy/changeset/6cac09131559/

Log:    add __dict__ to class/staticmethod

diff --git a/pypy/interpreter/function.py b/pypy/interpreter/function.py
--- a/pypy/interpreter/function.py
+++ b/pypy/interpreter/function.py
@@ -593,6 +593,19 @@
 
     def __init__(self, w_function):
         self.w_function = w_function
+        self.w_dict = None
+
+    def getdict(self, space):
+        if self.w_dict is None:
+            self.w_dict = space.newdict(instance=True)
+        return self.w_dict
+
+    def setdict(self, space, w_dict):
+        if not space.isinstance_w(w_dict, space.w_dict):
+            raise oefmt(space.w_TypeError,
+                        "__dict__ must be set to a dictionary, not a %T",
+                        w_dict)
+        self.w_dict = w_dict
 
     def descr_staticmethod_get(self, w_obj, w_cls=None):
         """staticmethod(x).__get__(obj[, type]) -> x"""
@@ -613,6 +626,19 @@
 
     def __init__(self, w_function):
         self.w_function = w_function
+        self.w_dict = None
+
+    def getdict(self, space):
+        if self.w_dict is None:
+            self.w_dict = space.newdict(instance=True)
+        return self.w_dict
+
+    def setdict(self, space, w_dict):
+        if not space.isinstance_w(w_dict, space.w_dict):
+            raise oefmt(space.w_TypeError,
+                        "__dict__ must be set to a dictionary, not a %T",
+                        w_dict)
+        self.w_dict = w_dict
 
     def descr_classmethod_get(self, space, w_obj, w_klass=None):
         if space.is_none(w_klass):
diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -739,6 +739,8 @@
     __new__ = interp2app(StaticMethod.descr_staticmethod__new__.im_func),
     __func__= interp_attrproperty_w('w_function', cls=StaticMethod),
     __isabstractmethod__ = GetSetProperty(StaticMethod.descr_isabstract),
+    __dict__ = GetSetProperty(descr_get_dict, descr_set_dict,
+                              cls=StaticMethod),
     )
 
 ClassMethod.typedef = TypeDef(
@@ -747,6 +749,7 @@
     __get__ = interp2app(ClassMethod.descr_classmethod_get),
     __func__= interp_attrproperty_w('w_function', cls=ClassMethod),
     __isabstractmethod__ = GetSetProperty(ClassMethod.descr_isabstract),
+    __dict__ = GetSetProperty(descr_get_dict, descr_set_dict, cls=ClassMethod),
     __doc__ = """classmethod(function) -> class method
 
 Convert a function to be a class method.
diff --git a/pypy/module/__builtin__/test/test_descriptor.py 
b/pypy/module/__builtin__/test/test_descriptor.py
--- a/pypy/module/__builtin__/test/test_descriptor.py
+++ b/pypy/module/__builtin__/test/test_descriptor.py
@@ -14,6 +14,17 @@
         assert d.f("abc", "def") == "abcdef"
         assert D.f("abc", "def") == "abcdef"
 
+    def test_staticmethod_dict(self):
+        sm = staticmethod(None)
+        assert sm.__dict__ == {}
+        sm.x = 42
+        assert sm.x == 42
+        assert sm.__dict__ == {"x" : 42}
+        del sm.x
+        assert not hasattr(sm, "x")
+        raises(TypeError, setattr, sm, '__dict__', [])
+        raises((AttributeError, TypeError), delattr, sm, '__dict__')
+
     def test_staticmethod_subclass(self):
         class Static(staticmethod):
             pass
@@ -266,6 +277,20 @@
         meth = classmethod(1).__get__(1)
         raises(TypeError, meth)
 
+    def test_classmethod_dict(self):
+        cm = classmethod(None)
+        assert cm.__dict__ == {}
+        cm.x = 42
+        assert cm.x == 42
+        assert cm.__dict__ == {"x": 42}
+        del cm.x
+        assert not hasattr(cm, "x")
+        cm.x = 42
+        cm.__dict__ = {}
+        assert not hasattr(cm, "x")
+        raises(TypeError, setattr, cm, '__dict__', [])
+        raises((AttributeError, TypeError), delattr, cm, '__dict__')
+
     def test_super_thisclass(self):
         class A(object):
             pass
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to