[pypy-commit] pypy py3.5: merge branch which adds missing dunder-atributes to instancemethods

mattip pypy.commits at gmail.com
Wed Mar 21 12:46:28 EDT 2018


Author: Matti Picus <matti.picus at gmail.com>
Branch: py3.5
Changeset: r94056:93baaf2e069a
Date: 2018-03-21 18:45 +0200
http://bitbucket.org/pypy/pypy/changeset/93baaf2e069a/

Log:	merge branch which adds missing dunder-atributes to instancemethods

diff --git a/pypy/doc/whatsnew-pypy3-head.rst b/pypy/doc/whatsnew-pypy3-head.rst
--- a/pypy/doc/whatsnew-pypy3-head.rst
+++ b/pypy/doc/whatsnew-pypy3-head.rst
@@ -13,4 +13,7 @@
 
 Update winreg module to use unicode, wide-strings
 
+.. branch: cpyext-py3-instancemethod-attributes
 
+Add missing ``__doc__``, ``__module__``, ``__name__`` attributes to 
+``instancemethod``
diff --git a/pypy/module/cpyext/classobject.py b/pypy/module/cpyext/classobject.py
--- a/pypy/module/cpyext/classobject.py
+++ b/pypy/module/cpyext/classobject.py
@@ -3,7 +3,7 @@
 from pypy.module.cpyext.pyobject import PyObject
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.function import Method
-from pypy.interpreter.typedef import TypeDef, interp_attrproperty_w
+from pypy.interpreter.typedef import TypeDef, interp_attrproperty_w, GetSetProperty
 from pypy.interpreter.gateway import interp2app
 
 
@@ -14,6 +14,15 @@
     def __init__(self, w_function):
         self.w_function = w_function
 
+    def fget_name(self, space):
+        return space.getattr(self.w_function, space.newtext("__name__"))
+
+    def fget_module(self, space):
+        return space.getattr(self.w_function, space.newtext("__module__"))
+
+    def fget_docstring(self, space):
+        return space.getattr(self.w_function, space.newtext("__doc__"))
+
     @staticmethod
     def descr_new(space, w_subtype, w_function):
         # instancemethod is not subclassable
@@ -38,7 +47,10 @@
     __get__ = interp2app(InstanceMethod.descr_get),
     __repr__ = interp2app(InstanceMethod.descr_repr,
                           descrmismatch='__repr__'),
-    __func__= interp_attrproperty_w('w_function', cls=InstanceMethod),
+    __func__ = interp_attrproperty_w('w_function', cls=InstanceMethod),
+    __name__ = GetSetProperty(InstanceMethod.fget_name, cls=InstanceMethod),
+    __module__ = GetSetProperty(InstanceMethod.fget_module, cls=InstanceMethod),
+    __doc__ = GetSetProperty(InstanceMethod.fget_docstring, cls=InstanceMethod),
 )
 InstanceMethod.typedef.acceptable_as_base_class = False
 
diff --git a/pypy/module/cpyext/test/test_classobject.py b/pypy/module/cpyext/test/test_classobject.py
--- a/pypy/module/cpyext/test/test_classobject.py
+++ b/pypy/module/cpyext/test/test_classobject.py
@@ -1,5 +1,6 @@
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 
+
 class AppTestInstanceMethod(AppTestCpythonExtensionBase):
     def test_instancemethod(self):
         module = self.import_extension('foo', [
@@ -27,3 +28,29 @@
         InstanceMethod.testmethod.attribute = "test"
         assert testfunction.attribute == "test"
         raises(AttributeError, setattr, inst.testmethod, "attribute", "test")
+
+    def test_instancemethod_cpyext_attributes(self):
+        module = self.import_extension('foo', [
+            ("instancemethod_get_doc", "METH_O",
+             """
+                 PyObject* instancemethod = PyInstanceMethod_New(args);
+                 return PyObject_GetAttrString(instancemethod, "__doc__");
+             """),
+            ("instancemethod_get_name", "METH_O",
+             """
+                 PyObject* instancemethod = PyInstanceMethod_New(args);
+                 return PyObject_GetAttrString(instancemethod, "__name__");
+             """),
+            ("instancemethod_get_module", "METH_O",
+             """
+                 PyObject* instancemethod = PyInstanceMethod_New(args);
+                 return PyObject_GetAttrString(instancemethod, "__module__");
+             """)
+        ])
+
+        def testfunction(self):
+            """some doc"""
+            return self
+        assert(module.instancemethod_get_doc(testfunction) == testfunction.__doc__)
+        assert(module.instancemethod_get_module(testfunction) == testfunction.__module__)
+        assert(module.instancemethod_get_name(testfunction) == testfunction.__name__)
\ No newline at end of file


More information about the pypy-commit mailing list