01: package org.python.core;
02:
03: import java.util.Arrays;
04:
05: public class PyMethodDescr extends PyDescriptor implements
06: PyBuiltinFunction.Info {
07:
08: protected int minargs, maxargs;
09:
10: protected PyBuiltinFunction meth;
11:
12: public PyMethodDescr(String name, Class c, int minargs,
13: int maxargs, PyBuiltinFunction func) {
14: this .name = name;
15: this .dtype = PyType.fromClass(c);
16: this .minargs = minargs;
17: this .maxargs = maxargs;
18: this .meth = func;
19: this .meth.setInfo(this );
20: }
21:
22: public String getName() {
23: return name;
24: }
25:
26: public int getMaxargs() {
27: return maxargs;
28: }
29:
30: public int getMinargs() {
31: return minargs;
32: }
33:
34: public String toString() {
35: return "<method '" + name + "' of '" + dtype.fastGetName()
36: + "' objects>";
37: }
38:
39: public PyObject __call__(PyObject[] args) {
40: return __call__(args, Py.NoKeywords);
41: }
42:
43: public PyObject __call__(PyObject[] args, String[] kwargs) {
44: if (args.length == kwargs.length) {
45: throw Py
46: .TypeError(name + " requires at least one argument");
47: }
48: checkCallerType(args[0]);
49: PyObject[] actualArgs = new PyObject[args.length - 1];
50: System.arraycopy(args, 1, actualArgs, 0, actualArgs.length);
51: return meth.bind(args[0]).__call__(actualArgs, kwargs);
52:
53: }
54:
55: public PyException unexpectedCall(int nargs, boolean keywords) {
56: return PyBuiltinFunction.DefaultInfo.unexpectedCall(nargs,
57: keywords, name, minargs, maxargs);
58: }
59:
60: public PyObject __get__(PyObject obj, PyObject type) {
61: if (obj != null) {
62: checkCallerType(obj);
63: return meth.bind(obj);
64: }
65: return this ;
66: }
67:
68: protected void checkCallerType(PyObject obj) {
69: PyType objtype = obj.getType();
70: if (objtype != dtype && !objtype.isSubType(dtype)) {
71: throw get_wrongtype(objtype);
72: }
73: }
74: }
|