[pypy-commit] pypy py3.5: Compatibility: implement staticmethod.__init__ and classmethod.__init__

rlamy pypy.commits at gmail.com
Tue Oct 25 13:06:34 EDT 2016


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: py3.5
Changeset: r87935:6961be5cf15d
Date: 2016-10-25 18:05 +0100
http://bitbucket.org/pypy/pypy/changeset/6961be5cf15d/

Log:	Compatibility: implement staticmethod.__init__ and
	classmethod.__init__

diff --git a/pypy/interpreter/function.py b/pypy/interpreter/function.py
--- a/pypy/interpreter/function.py
+++ b/pypy/interpreter/function.py
@@ -628,8 +628,11 @@
 
     def descr_staticmethod__new__(space, w_subtype, w_function):
         instance = space.allocate_instance(StaticMethod, w_subtype)
-        instance.__init__(w_function)
-        return space.wrap(instance)
+        instance.__init__(space.w_None)
+        return instance
+
+    def descr_init(self, space, w_function):
+        self.w_function = w_function
 
     def descr_isabstract(self, space):
         return space.newbool(space.isabstractmethod_w(self.w_function))
@@ -662,8 +665,11 @@
 
     def descr_classmethod__new__(space, w_subtype, w_function):
         instance = space.allocate_instance(ClassMethod, w_subtype)
-        instance.__init__(w_function)
-        return space.wrap(instance)
+        instance.__init__(space.w_None)
+        return instance
+
+    def descr_init(self, space, w_function):
+        self.w_function = w_function
 
     def descr_isabstract(self, space):
         return space.newbool(space.isabstractmethod_w(self.w_function))
diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -724,6 +724,7 @@
 (e.g. C().f()).  The instance is ignored except for its class.""",
     __get__ = interp2app(StaticMethod.descr_staticmethod_get),
     __new__ = interp2app(StaticMethod.descr_staticmethod__new__.im_func),
+    __init__=interp2app(StaticMethod.descr_init),
     __func__= interp_attrproperty_w('w_function', cls=StaticMethod),
     __isabstractmethod__ = GetSetProperty(StaticMethod.descr_isabstract),
     __dict__ = GetSetProperty(descr_get_dict, descr_set_dict,
@@ -732,12 +733,13 @@
 
 ClassMethod.typedef = TypeDef(
     'classmethod',
-    __new__ = interp2app(ClassMethod.descr_classmethod__new__.im_func),
-    __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
+    __new__=interp2app(ClassMethod.descr_classmethod__new__.im_func),
+    __init__=interp2app(ClassMethod.descr_init),
+    __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
@@ -31,6 +31,12 @@
         x = Static(1)
         assert isinstance(x, Static)
 
+        class C(Static):
+            def __init__(self, callable):
+                super().__init__(callable)
+        y = C(1)
+        assert isinstance(y, C)
+
     def test_classmethod(self):
         class C(object):
             def f(cls, stuff):
@@ -52,6 +58,12 @@
         x = Classm(1)
         assert isinstance(x, Classm)
 
+        class C(Classm):
+            def __init__(self, callable):
+                super().__init__(callable)
+        y = C(1)
+        assert isinstance(y, C)
+
     def test_property_simple(self):
 
         class a(object):


More information about the pypy-commit mailing list