KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.python.core;
2
3 public abstract class PyBuiltinFunction extends PyObject implements PyType.Newstyle {
4
5     /* type info */
6
7     public static final String JavaDoc exposed_name="builtin_function_or_method";
8
9     public static void typeSetup(PyObject dict,PyType.Newstyle marker) {
10         dict.__setitem__("__name__", new PyGetSetDescr("__name__",
11                 PyBuiltinFunction.class, "fastGetName", null));
12         dict.__setitem__("__self__", new PyGetSetDescr("__self__",
13                 PyBuiltinFunction.class, "getSelf", null));
14         dict.__setitem__("__doc__", new PyGetSetDescr("__doc__",
15                 PyBuiltinFunction.class, "fastGetDoc", null));
16     }
17
18     public interface Info {
19         String JavaDoc getName();
20         int getMaxargs();
21         int getMinargs();
22         PyException unexpectedCall(int nargs, boolean keywords);
23     }
24
25     public static class DefaultInfo implements Info {
26
27         public DefaultInfo(String JavaDoc name,int minargs,int maxargs) {
28             this.name = name;
29             this.minargs = minargs;
30             this.maxargs = maxargs;
31         }
32
33         public DefaultInfo(String JavaDoc name,int nargs) {
34             this(name,nargs,nargs);
35         }
36
37         private String JavaDoc name;
38         private int maxargs, minargs;
39
40         public String JavaDoc getName() {
41             return name;
42         }
43
44         public int getMaxargs() {
45             return maxargs;
46         }
47         public int getMinargs() {
48             return minargs;
49         }
50
51         public static boolean check(int nargs,int minargs,int maxargs) {
52             if (nargs < minargs)
53                 return false;
54             if (maxargs != -1 && nargs > maxargs)
55                 return false;
56             return true;
57         }
58
59         public static PyException unexpectedCall(
60             int nargs,
61             boolean keywords,
62             String JavaDoc name,
63             int minargs,
64             int maxargs) {
65             if (keywords)
66                 return Py.TypeError(name + "() takes no keyword arguments");
67             String JavaDoc argsblurb;
68             if (minargs == maxargs) {
69                 if (minargs == 0)
70                     argsblurb = "no arguments";
71                 else if (minargs == 1)
72                     argsblurb = "exactly one argument";
73                 else
74                     argsblurb = minargs + " arguments";
75             } else if (maxargs == -1) {
76                 return Py.TypeError(name + "() requires at least " +
77                         minargs + " (" + nargs + " given)");
78             } else {
79                 if (minargs <= 0)
80                     argsblurb = "at most "+ maxargs + " arguments";
81                 else
82                     argsblurb = minargs + "-" + maxargs + " arguments";
83             }
84
85             return Py.TypeError(
86                 name + "() takes " + argsblurb + " (" + nargs + " given)");
87         }
88
89         public PyException unexpectedCall(int nargs, boolean keywords) {
90             return unexpectedCall(nargs, keywords, name, minargs, maxargs);
91         }
92
93     }
94
95     protected PyBuiltinFunction() {}
96
97     protected PyBuiltinFunction(Info info) {
98         this.info = info;
99     }
100
101     protected Info info;
102
103     public void setInfo(Info info) {
104         this.info = info;
105     }
106
107     abstract protected PyBuiltinFunction makeBound(PyObject self);
108
109     public PyObject getSelf() {
110         return null;
111     }
112
113     public String JavaDoc toString() {
114         PyObject self = getSelf();
115         if (self == null)
116             return "<built-in function " + info.getName() + ">";
117         else {
118             String JavaDoc typename = self.getType().fastGetName();
119             return "<built-in method "
120                 + info.getName()
121                 + " of "
122                 + typename
123                 + " object>";
124         }
125     }
126
127     abstract public PyObject inst_call(PyObject self);
128     abstract public PyObject inst_call(PyObject self, PyObject arg0);
129     abstract public PyObject inst_call(
130         PyObject self,
131         PyObject arg0,
132         PyObject arg1);
133     abstract public PyObject inst_call(
134         PyObject self,
135         PyObject arg0,
136         PyObject arg1,
137         PyObject arg2);
138     abstract public PyObject inst_call(
139         PyObject self,
140         PyObject arg0,
141         PyObject arg1,
142         PyObject arg2,
143         PyObject arg3);
144     abstract public PyObject inst_call(PyObject self, PyObject[] args);
145     abstract public PyObject inst_call(
146         PyObject self,
147         PyObject[] args,
148         String JavaDoc[] keywords);
149
150     public PyObject fastGetName() {
151         return Py.newString(this.info.getName());
152     }
153
154     public PyObject fastGetDoc() {
155         return Py.None;
156     }
157 }
158
Popular Tags