| 1 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 |
from pypy.module.cpyext.api import CANNOT_FAIL, cpython_api |
| 3 |
from pypy.module.cpyext.pyobject import PyObject |
| 4 |
from pypy.interpreter.baseobjspace import W_Root |
| 5 |
from pypy.interpreter.function import Method |
| 6 |
from pypy.interpreter.typedef import TypeDef, interp_attrproperty_w, GetSetProperty |
| 7 |
from pypy.interpreter.gateway import interp2app |
| 8 |
|
| 9 |
|
| 10 |
class InstanceMethod(W_Root): |
| 11 |
"""The instancemethod facade.""" |
| 12 |
_immutable_fields_ = ['w_function'] |
| 13 |
|
| 14 |
def __init__(self, w_function): |
| 15 |
self.w_function = w_function |
| 16 |
|
| 17 |
def fget_name(self, space): |
| 18 |
return space.getattr(self.w_function, space.newtext("__name__")) |
| 19 |
|
| 20 |
def fget_module(self, space): |
| 21 |
return space.getattr(self.w_function, space.newtext("__module__")) |
| 22 |
|
| 23 |
def fget_docstring(self, space): |
| 24 |
return space.getattr(self.w_function, space.newtext("__doc__")) |
| 25 |
|
| 26 |
@staticmethod |
| 27 |
def descr_new(space, w_subtype, w_function): |
| 28 |
|
| 29 |
return InstanceMethod(w_function) |
| 30 |
|
| 31 |
def descr_get(self, space, w_obj, w_klass=None): |
| 32 |
if space.is_none(w_obj): |
| 33 |
return self.w_function |
| 34 |
return Method(space, self.w_function, w_obj) |
| 35 |
|
| 36 |
def descr_call(self, space, __args__): |
| 37 |
return space.call_args(self.w_function, __args__) |
| 38 |
|
| 39 |
def descr_repr(self, space): |
| 40 |
return self.getrepr(space, '<instancemethod %s>' % |
| 41 |
(self.w_function.getname(space),)) |
| 42 |
|
| 43 |
InstanceMethod.typedef = TypeDef( |
| 44 |
"cinstancemethod", |
| 45 |
__new__ = interp2app(InstanceMethod.descr_new), |
| 46 |
__call__ = interp2app(InstanceMethod.descr_call, |
| 47 |
descrmismatch='__call__'), |
| 48 |
__get__ = interp2app(InstanceMethod.descr_get), |
| 49 |
__repr__ = interp2app(InstanceMethod.descr_repr, |
| 50 |
descrmismatch='__repr__'), |
| 51 |
__func__ = interp_attrproperty_w('w_function', cls=InstanceMethod), |
| 52 |
__name__ = GetSetProperty(InstanceMethod.fget_name, cls=InstanceMethod), |
| 53 |
__module__ = GetSetProperty(InstanceMethod.fget_module, cls=InstanceMethod), |
| 54 |
__doc__ = GetSetProperty(InstanceMethod.fget_docstring, cls=InstanceMethod), |
| 55 |
) |
| 56 |
InstanceMethod.typedef.acceptable_as_base_class = False |
| 57 |
|
| 58 |
@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) |
| 59 |
def PyInstanceMethod_Check(space, w_o): |
| 60 |
"""Return true if o is an instance method object (has type |
| 61 |
PyInstanceMethod_Type). The parameter must not be NULL.""" |
| 62 |
return space.isinstance_w(w_o, |
| 63 |
space.gettypeobject(InstanceMethod.typedef)) |
| 64 |
|
| 65 |
|
| 66 |
@cpython_api([PyObject], PyObject) |
| 67 |
def PyInstanceMethod_New(space, w_func): |
| 68 |
"""Return a new instance method object, with func being any |
| 69 |
callable object func is the function that will be called when the |
| 70 |
instance method is called.""" |
| 71 |
return InstanceMethod(w_func) |
| 72 |
|
| 73 |
|
| 74 |
@cpython_api([PyObject], PyObject) |
| 75 |
def PyInstanceMethod_Function(space, w_im): |
| 76 |
"""Return the function object associated with the instance method im.""" |
| 77 |
return space.interp_w(InstanceMethod, w_im).w_function |
| 78 |
|
| 79 |
|
| 80 |
@cpython_api([PyObject], PyObject) |
| 81 |
def PyInstanceMethod_GET_FUNCTION(space, w_im): |
| 82 |
"""Macro version of PyInstanceMethod_Function() which avoids error |
| 83 |
checking.""" |
| 84 |
return space.interp_w(InstanceMethod, w_im).w_function |
| 85 |
|
| 86 |
if w_bases is None: |
| 87 |
w_bases = space.newtuple([]) |
| 88 |
|