KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PyMethodDescr


1 package org.python.core;
2
3 public class PyMethodDescr extends PyDescriptor implements PyBuiltinFunction.Info
4 {
5
6     protected int minargs, maxargs;
7        
8     protected PyBuiltinFunction func;
9
10     public PyMethodDescr(String JavaDoc name, Class JavaDoc c, int minargs,
11                                 int maxargs, PyBuiltinFunction func)
12     {
13         this.name = name;
14         this.dtype = PyType.fromClass(c);
15         this.minargs = minargs;
16         this.maxargs = maxargs;
17         this.func = func;
18         this.func.setInfo(this);
19     }
20
21     public String JavaDoc getName() {
22         return name;
23     }
24
25     public int getMaxargs() {
26         return maxargs;
27     }
28     public int getMinargs() {
29         return minargs;
30     }
31     
32     public String JavaDoc toString() {
33         return "<method '"+name+"' of '"+dtype.fastGetName()+"' objects>";
34     }
35
36     public PyObject __call__(PyObject[] args) {
37         PyObject self = args[0];
38         PyType objtype = self.getType();
39         if (objtype != dtype && !objtype.isSubType(dtype))
40             throw call_wrongtype(objtype);
41         int n = args.length;
42         PyObject[] rest = new PyObject[n-1];
43         System.arraycopy(args,1,rest,0,n-1);
44         return func.inst_call(self,rest);
45     }
46
47     public PyObject __call__(PyObject[] args, String JavaDoc[] kws) {
48         int n = args.length;
49         if (kws.length == args.length)
50             throw Py.TypeError(blurb()+" needs an argument");
51         PyObject self = args[0];
52         PyType objtype = self.getType();
53         if (objtype != dtype && !objtype.isSubType(dtype))
54             throw call_wrongtype(objtype);
55         PyObject[] rest = new PyObject[n-1];
56         System.arraycopy(args,1,rest,0,n-1);
57         return func.inst_call(self,rest,kws);
58     }
59
60     public PyObject __call__() {
61         throw Py.TypeError(blurb()+" needs an argument");
62     }
63
64     public PyObject __call__(PyObject arg1) {
65         PyType objtype = arg1.getType();
66         if (objtype != dtype && !objtype.isSubType(dtype))
67             throw call_wrongtype(objtype);
68         return func.inst_call(arg1);
69     }
70
71     public PyObject __call__(PyObject arg1, PyObject arg2) {
72         PyType objtype = arg1.getType();
73         if (objtype != dtype && !objtype.isSubType(dtype))
74             throw call_wrongtype(objtype);
75         return func.inst_call(arg1,arg2);
76     }
77
78     public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) {
79         PyType objtype = arg1.getType();
80         if (objtype != dtype && !objtype.isSubType(dtype))
81             throw call_wrongtype(objtype);
82         return func.inst_call(arg1,arg2,arg3);
83     }
84
85     public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3,
86                              PyObject arg4)
87     {
88         PyType objtype = arg1.getType();
89         if (objtype != dtype && !objtype.isSubType(dtype))
90             throw call_wrongtype(objtype);
91         return func.inst_call(arg1,arg2,arg3,arg4);
92     }
93  
94     public PyException unexpectedCall(int nargs,boolean keywords) {
95         return PyBuiltinFunction.DefaultInfo.unexpectedCall(nargs,keywords,name,minargs,maxargs);
96     }
97
98     public PyObject __get__(PyObject obj, PyObject type) {
99         if (obj != null) {
100             PyType objtype = obj.getType();
101             if (objtype != dtype && !objtype.isSubType(dtype))
102                 throw get_wrongtype(objtype);
103             return func.makeBound(obj);
104         }
105         return this;
106     }
107
108 }
109
Popular Tags