KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 /**
5  * A helper class for faster implementations of commonly called
6  * methods.
7  * <p>
8  * Subclasses of PyBuiltinFunctionSet will implement some or all
9  * of the __call__ method with a switch on the index number.
10  *
11  */

12 public class PyBuiltinFunctionSet extends PyObject implements Cloneable JavaDoc
13 {
14     // part of the public interface for built-in functions
15
public PyObject __name__;
16     public PyObject __doc__;
17     public PyObject __self__;
18     public static PyObject __members__;
19
20     // internal implementation
21
protected String JavaDoc name;
22     protected int minargs, maxargs;
23     protected boolean isMethod;
24     // used as an index into a big switch statement in the various derived
25
// class's __call__() methods.
26
protected int index;
27     protected String JavaDoc doc;
28
29     static {
30         PyString[] members = new PyString[3];
31         members[0] = new PyString("__doc__");
32         members[1] = new PyString("__name__");
33         members[2] = new PyString("__self__");
34         __members__ = new PyList(members);
35     }
36
37     // full-blown constructor, specifying everything
38
public PyBuiltinFunctionSet(String JavaDoc name, int index, int minargs,
39                                 int maxargs, boolean isMethod, String JavaDoc doc)
40     {
41         this.name = name;
42         this.index = index;
43         this.minargs = minargs;
44         this.maxargs = maxargs;
45         this.isMethod = isMethod;
46         this.doc = doc;
47
48         __name__ = new PyString(name);
49         if (doc == null)
50             __doc__ = Py.None;
51         else
52             __doc__ = new PyString(doc);
53         __self__ = Py.None;
54     }
55
56     public PyObject _doget(PyObject container) {
57         return _doget(container, null);
58     }
59
60     public PyObject _doget(PyObject container, PyObject wherefound) {
61         // Eventually we may want to allow rebinding of builtins
62
// when container is a subclass of __self__.__class__.
63
if (isMethod && __self__ == Py.None) {
64             // TBD: is there a better way?
65
try {
66                 PyBuiltinFunctionSet unique = (PyBuiltinFunctionSet)clone();
67                 unique.__self__ = container;
68                 return unique;
69             }
70             catch (CloneNotSupportedException JavaDoc e) {}
71         }
72         return this;
73     }
74
75     public String JavaDoc toString() {
76         if (isMethod)
77             return "<built-in method "+name+">";
78         else
79             return "<built-in function "+name+">";
80     }
81
82     public boolean isMappingType() { return false; }
83     public boolean isNumberType() { return false; }
84     public boolean isSequenceType() { return false; }
85
86     public PyException argCountError(int nargs) {
87         if (minargs == maxargs) {
88             return Py.TypeError(name+"(): expected "+minargs+" args; got "+
89                                 nargs);
90         } else {
91             return Py.TypeError(name+"(): expected "+minargs+"-"+maxargs+
92                                 " args; got "+nargs);
93         }
94     }
95
96     public PyObject fancyCall(PyObject[] args) {
97         throw Py.TypeError("surprising call");
98     }
99
100     public PyObject __call__(PyObject[] args) {
101         int nargs = args.length;
102         if (minargs != -1 && (nargs > maxargs || nargs < minargs)) {
103             throw argCountError(nargs);
104         }
105         switch (nargs) {
106         case 0:
107             return __call__();
108         case 1:
109             return __call__(args[0]);
110         case 2:
111             return __call__(args[0], args[1]);
112         case 3:
113             return __call__(args[0], args[1], args[2]);
114         case 4:
115             return __call__(args[0], args[1], args[2], args[3]);
116         default:
117             return fancyCall(args);
118         }
119     }
120
121     public PyObject __call__(PyObject[] args, String JavaDoc[] kws) {
122         if (kws.length != 0)
123             throw Py.TypeError(
124                     name+"(): this function takes no keyword arguments");
125         return __call__(args);
126     }
127
128     public PyObject __call__() {
129         throw argCountError(0);
130     }
131
132     public PyObject __call__(PyObject arg1) {
133         throw argCountError(1);
134     }
135
136     public PyObject __call__(PyObject arg1, PyObject arg2) {
137         throw argCountError(2);
138     }
139
140     public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) {
141         throw argCountError(3);
142     }
143
144     public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3,
145                              PyObject arg4)
146     {
147         throw argCountError(4);
148     }
149 }
150
Popular Tags