Author: Andrew Stepanov <karn9...@gmail.com>
Branch: cpyext-py3-instancemethod-attributes
Changeset: r94054:a178c81744b1
Date: 2018-03-21 15:37 +0300
http://bitbucket.org/pypy/pypy/changeset/a178c81744b1/

Log:    [cpyext] Add missing 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
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to