File: classobject.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (88 lines) | stat: -rw-r--r-- 3,373 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from rpython.rtyper.lltypesystem import rffi, lltype
from pypy.module.cpyext.api import CANNOT_FAIL, cpython_api
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, GetSetProperty
from pypy.interpreter.gateway import interp2app


class InstanceMethod(W_Root):
    """The instancemethod facade."""
    _immutable_fields_ = ['w_function']

    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
        return InstanceMethod(w_function)

    def descr_get(self, space, w_obj, w_klass=None):
        if space.is_none(w_obj):
            return self.w_function
        return Method(space, self.w_function, w_obj)

    def descr_call(self, space, __args__):
        return space.call_args(self.w_function, __args__)

    def descr_repr(self, space):
        return self.getrepr(space, '<instancemethod %s>' %
                            (self.w_function.getname(space),))

InstanceMethod.typedef = TypeDef(
    "cinstancemethod",
    __new__ = interp2app(InstanceMethod.descr_new),
    __call__ = interp2app(InstanceMethod.descr_call,
                          descrmismatch='__call__'),
    __get__ = interp2app(InstanceMethod.descr_get),
    __repr__ = interp2app(InstanceMethod.descr_repr,
                          descrmismatch='__repr__'),
    __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

@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
def PyInstanceMethod_Check(space, w_o):
    """Return true if o is an instance method object (has type
    PyInstanceMethod_Type).  The parameter must not be NULL."""
    return space.isinstance_w(w_o,
                              space.gettypeobject(InstanceMethod.typedef))
    

@cpython_api([PyObject], PyObject)
def PyInstanceMethod_New(space, w_func):
    """Return a new instance method object, with func being any
    callable object func is the function that will be called when the
    instance method is called."""
    return InstanceMethod(w_func)
    

@cpython_api([PyObject], PyObject)
def PyInstanceMethod_Function(space, w_im):
    """Return the function object associated with the instance method im."""
    return space.interp_w(InstanceMethod, w_im).w_function
    

@cpython_api([PyObject], PyObject)
def PyInstanceMethod_GET_FUNCTION(space, w_im):
    """Macro version of PyInstanceMethod_Function() which avoids error
    checking."""
    return space.interp_w(InstanceMethod, w_im).w_function
    
    if w_bases is None:
        w_bases = space.newtuple([])