| 1 | from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 2 | |
| 3 | |
| 4 | class AppTestInstanceMethod(AppTestCpythonExtensionBase): |
| 5 | def test_instancemethod(self): |
| 6 | module = self.import_extension('foo', [ |
| 7 | ("instancemethod", "METH_O", |
| 8 | """ |
| 9 | return PyInstanceMethod_New(args); |
| 10 | """)]) |
| 11 | |
| 12 | def testfunction(self): |
| 13 | """some doc""" |
| 14 | return self |
| 15 | |
| 16 | class InstanceMethod: |
| 17 | id = module.instancemethod(id) |
| 18 | testmethod = module.instancemethod(testfunction) |
| 19 | |
| 20 | inst = InstanceMethod() |
| 21 | assert id(inst) == inst.id() |
| 22 | assert inst.testmethod() is inst |
| 23 | assert InstanceMethod.testmethod(inst) is inst |
| 24 | assert InstanceMethod.__dict__['testmethod'](inst) is inst |
| 25 | assert inst.testmethod.__doc__ == testfunction.__doc__ |
| 26 | assert InstanceMethod.testmethod.__doc__ == testfunction.__doc__ |
| 27 | |
| 28 | InstanceMethod.testmethod.attribute = "test" |
| 29 | assert testfunction.attribute == "test" |
| 30 | raises(AttributeError, setattr, inst.testmethod, "attribute", "test") |
| 31 | |
| 32 | def test_instancemethod_cpyext_attributes(self): |
| 33 | module = self.import_extension('foo', [ |
| 34 | ("instancemethod_get_doc", "METH_O", |
| 35 | """ |
| 36 | PyObject* instancemethod = PyInstanceMethod_New(args); |
| 37 | return PyObject_GetAttrString(instancemethod, "__doc__"); |
| 38 | """), |
| 39 | ("instancemethod_get_name", "METH_O", |
| 40 | """ |
| 41 | PyObject* instancemethod = PyInstanceMethod_New(args); |
| 42 | return PyObject_GetAttrString(instancemethod, "__name__"); |
| 43 | """), |
| 44 | ("instancemethod_get_module", "METH_O", |
| 45 | """ |
| 46 | PyObject* instancemethod = PyInstanceMethod_New(args); |
| 47 | return PyObject_GetAttrString(instancemethod, "__module__"); |
| 48 | """) |
| 49 | ]) |
| 50 | |
| 51 | def testfunction(self): |
| 52 | """some doc""" |
| 53 | return self |
| 54 | assert(module.instancemethod_get_doc(testfunction) == testfunction.__doc__) |
| 55 | assert(module.instancemethod_get_module(testfunction) == testfunction.__module__) |
| 56 | assert(module.instancemethod_get_name(testfunction) == testfunction.__name__) |