001: package org.python.core;
002:
003: public abstract class PyBuiltinFunction extends PyObject implements
004: PyType.Newstyle {
005:
006: public static final String exposed_name = "builtin_function_or_method";
007:
008: public static void typeSetup(PyObject dict, PyType.Newstyle marker) {
009: dict.__setitem__("__name__", new PyGetSetDescr("__name__",
010: PyBuiltinFunction.class, "fastGetName", null));
011: dict.__setitem__("__self__", new PyGetSetDescr("__self__",
012: PyBuiltinFunction.class, "getSelf", null));
013: dict.__setitem__("__doc__", new PyGetSetDescr("__doc__",
014: PyBuiltinFunction.class, "fastGetDoc", null));
015: dict.__setitem__("__call__", new PyGetSetDescr("__call__",
016: PyBuiltinFunction.class, "makeCall", null));
017: }
018:
019: public interface Info {
020:
021: String getName();
022:
023: int getMaxargs();
024:
025: int getMinargs();
026:
027: PyException unexpectedCall(int nargs, boolean keywords);
028: }
029:
030: public static class DefaultInfo implements Info {
031:
032: public DefaultInfo(String name, int minargs, int maxargs) {
033: this .name = name;
034: this .minargs = minargs;
035: this .maxargs = maxargs;
036: }
037:
038: public DefaultInfo(String name, int nargs) {
039: this (name, nargs, nargs);
040: }
041:
042: private String name;
043:
044: private int maxargs, minargs;
045:
046: public String getName() {
047: return name;
048: }
049:
050: public int getMaxargs() {
051: return maxargs;
052: }
053:
054: public int getMinargs() {
055: return minargs;
056: }
057:
058: public static boolean check(int nargs, int minargs, int maxargs) {
059: if (nargs < minargs)
060: return false;
061: if (maxargs != -1 && nargs > maxargs)
062: return false;
063: return true;
064: }
065:
066: public static PyException unexpectedCall(int nargs,
067: boolean keywords, String name, int minargs, int maxargs) {
068: if (keywords)
069: return Py.TypeError(name
070: + "() takes no keyword arguments");
071: String argsblurb;
072: if (minargs == maxargs) {
073: if (minargs == 0)
074: argsblurb = "no arguments";
075: else if (minargs == 1)
076: argsblurb = "exactly one argument";
077: else
078: argsblurb = minargs + " arguments";
079: } else if (maxargs == -1) {
080: return Py.TypeError(name + "() requires at least "
081: + minargs + " (" + nargs + " given)");
082: } else {
083: if (minargs <= 0)
084: argsblurb = "at most " + maxargs + " arguments";
085: else
086: argsblurb = minargs + "-" + maxargs + " arguments";
087: }
088: return Py.TypeError(name + "() takes " + argsblurb + " ("
089: + nargs + " given)");
090: }
091:
092: public PyException unexpectedCall(int nargs, boolean keywords) {
093: return unexpectedCall(nargs, keywords, name, minargs,
094: maxargs);
095: }
096: }
097:
098: protected PyBuiltinFunction(Info info) {
099: this .info = info;
100: }
101:
102: protected Info info;
103:
104: public void setInfo(Info info) {
105: this .info = info;
106: }
107:
108: /**
109: * @return a new instance of this type of PyBuiltinFunction bound to self
110: */
111: abstract protected PyBuiltinFunction bind(PyObject self);
112:
113: public PyObject getSelf() {
114: return Py.None;
115: }
116:
117: public String toString() {
118: PyObject self = getSelf();
119: if (self == null)
120: return "<built-in function " + info.getName() + ">";
121: else {
122: String typename = self.getType().fastGetName();
123: return "<built-in method " + info.getName() + " of "
124: + typename + " object>";
125: }
126: }
127:
128: public PyObject fastGetName() {
129: return Py.newString(this .info.getName());
130: }
131:
132: public PyObject fastGetDoc() {
133: return Py.None;
134: }
135:
136: public PyObject makeCall() {
137: return this;
138: }
139: }
|