From: <cg...@us...> - 2008-10-19 05:14:29
|
Revision: 5465 http://jython.svn.sourceforge.net/jython/?rev=5465&view=rev Author: cgroves Date: 2008-10-19 05:14:22 +0000 (Sun, 19 Oct 2008) Log Message: ----------- Rename PyBuiltinFunction to PyBuiltinCallable, and remake PyBuiltinFunction as a base for functions with the basic function stuff from PyBuiltinFunctionSet. Extend that with PyBuiltinFunctionNarrow for functions with a fixed set of args to match PyBuiltinMethodNarrow. Use the new function stuff to implement the builtin functions that were using ExtendedBuiltinFunction as real functions instead of PyObjects. Modified Paths: -------------- trunk/jython/src/org/python/core/ArgParser.java trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/core/PyBuiltinFunction.java trunk/jython/src/org/python/core/PyBuiltinFunctionSet.java trunk/jython/src/org/python/core/PyBuiltinMethod.java trunk/jython/src/org/python/core/PyBuiltinMethodNarrow.java trunk/jython/src/org/python/core/PyBuiltinMethodSet.java trunk/jython/src/org/python/core/PyClassMethodDescr.java trunk/jython/src/org/python/core/PyDictionary.java trunk/jython/src/org/python/core/PyEnumerate.java trunk/jython/src/org/python/core/PyInstance.java trunk/jython/src/org/python/core/PyMethodDescr.java trunk/jython/src/org/python/core/PyNewWrapper.java trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/PySet.java trunk/jython/src/org/python/core/PyStringMap.java trunk/jython/src/org/python/core/PySuper.java trunk/jython/src/org/python/core/PyType.java trunk/jython/src/org/python/core/__builtin__.java trunk/jython/src/org/python/core/exceptions.java trunk/jython/src/org/python/expose/generate/PyTypes.java trunk/jython/src/org/python/modules/_collections/PyDeque.java trunk/jython/src/org/python/modules/_jython.java trunk/jython/src/org/python/modules/_weakref/ReferenceType.java trunk/jython/src/org/python/modules/cPickle.java trunk/jython/tests/java/org/python/expose/generate/ExposedTypeProcessorTest.java trunk/jython/tests/java/org/python/expose/generate/MethodExposerTest.java Added Paths: ----------- trunk/jython/src/org/python/core/PyBuiltinCallable.java trunk/jython/src/org/python/core/PyBuiltinFunctionNarrow.java Modified: trunk/jython/src/org/python/core/ArgParser.java =================================================================== --- trunk/jython/src/org/python/core/ArgParser.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/ArgParser.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -112,9 +112,9 @@ this(funcname, args, kws); this.params = paramnames; check(); - if (!PyBuiltinFunction.DefaultInfo.check(args.length, minargs, + if (!PyBuiltinCallable.DefaultInfo.check(args.length, minargs, this.params.length)) { - throw PyBuiltinFunction.DefaultInfo.unexpectedCall(args.length, + throw PyBuiltinCallable.DefaultInfo.unexpectedCall(args.length, false, funcname, minargs, this.params.length); } } Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/Py.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -86,7 +86,7 @@ static { BOOTSTRAP_TYPES.add(PyObject.class); BOOTSTRAP_TYPES.add(PyType.class); - BOOTSTRAP_TYPES.add(PyBuiltinFunction.class); + BOOTSTRAP_TYPES.add(PyBuiltinCallable.class); BOOTSTRAP_TYPES.add(PyDataDescr.class); } Added: trunk/jython/src/org/python/core/PyBuiltinCallable.java =================================================================== --- trunk/jython/src/org/python/core/PyBuiltinCallable.java (rev 0) +++ trunk/jython/src/org/python/core/PyBuiltinCallable.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -0,0 +1,144 @@ +package org.python.core; + +import org.python.expose.ExposedGet; +import org.python.expose.ExposedType; + +// XXX: not subclassable +@ExposedType(name = "builtin_function_or_method") +public abstract class PyBuiltinCallable extends PyObject { + + protected Info info; + + protected PyBuiltinCallable(PyType type, Info info) { + super(type); + this.info = info; + } + + protected PyBuiltinCallable(Info info) { + this.info = info; + } + + /** + * @return a new instance of this type of PyBuiltinFunction bound to self + */ + abstract public PyBuiltinCallable bind(PyObject self); + + @ExposedGet(name = "__name__") + public PyObject fastGetName() { + return Py.newString(this.info.getName()); + } + + @ExposedGet(name = "__doc__") + public PyObject fastGetDoc() { + return Py.None; + } + + @ExposedGet(name = "__module__") + public PyObject getModule() { + return Py.None; + } + + @ExposedGet(name = "__call__") + public PyObject makeCall() { + return this; + } + + @ExposedGet(name = "__self__") + public PyObject getSelf() { + return Py.None; + } + + public void setInfo(Info info) { + this.info = info; + } + + public String toString() { + PyObject self = getSelf(); + if (self == null) { + return String.format("<built-in function %s>", info.getName()); + } else { + return String.format("<built-in method %s of %s object at %s>", info.getName(), + self.getType().fastGetName(), Py.idstr(self)); + } + } + + public interface Info { + + String getName(); + + int getMaxargs(); + + int getMinargs(); + + PyException unexpectedCall(int nargs, boolean keywords); + } + + public static class DefaultInfo implements Info { + + public DefaultInfo(String name, int minargs, int maxargs) { + this.name = name; + this.minargs = minargs; + this.maxargs = maxargs; + } + + public DefaultInfo(String name) { + this(name, -1, -1); + } + + private String name; + + private int maxargs, minargs; + + public String getName() { + return name; + } + + public int getMaxargs() { + return maxargs; + } + + public int getMinargs() { + return minargs; + } + + public static boolean check(int nargs, int minargs, int maxargs) { + if(nargs < minargs) + return false; + if(maxargs != -1 && nargs > maxargs) + return false; + return true; + } + + public static PyException unexpectedCall(int nargs, + boolean keywords, + String name, + int minargs, + int maxargs) { + if(keywords) + return Py.TypeError(name + "() takes no keyword arguments"); + String argsblurb; + if(minargs == maxargs) { + if(minargs == 0) + argsblurb = "no arguments"; + else if(minargs == 1) + argsblurb = "exactly one argument"; + else + argsblurb = minargs + " arguments"; + } else if(maxargs == -1) { + return Py.TypeError(name + "() requires at least " + minargs + + " (" + nargs + " given)"); + } else { + if(minargs <= 0) + argsblurb = "at most " + maxargs + " arguments"; + else + argsblurb = minargs + "-" + maxargs + " arguments"; + } + return Py.TypeError(name + "() takes " + argsblurb + " (" + nargs + + " given)"); + } + + public PyException unexpectedCall(int nargs, boolean keywords) { + return unexpectedCall(nargs, keywords, name, minargs, maxargs); + } + } +} Modified: trunk/jython/src/org/python/core/PyBuiltinFunction.java =================================================================== --- trunk/jython/src/org/python/core/PyBuiltinFunction.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyBuiltinFunction.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -1,144 +1,41 @@ package org.python.core; -import org.python.expose.ExposedGet; -import org.python.expose.ExposedType; +import org.python.expose.ExposeAsSuperclass; -// XXX: not subclassable -@ExposedType(name = "builtin_function_or_method") -public abstract class PyBuiltinFunction extends PyObject { +public class PyBuiltinFunction extends PyBuiltinCallable implements ExposeAsSuperclass { - protected Info info; + private PyString doc; - protected PyBuiltinFunction(PyType type, Info info) { - super(type); - this.info = info; + protected PyBuiltinFunction(String name, String doc) { + this(name, -1, -1, doc); } - protected PyBuiltinFunction(Info info) { - this.info = info; + protected PyBuiltinFunction(String name, int minargs, int maxargs, String doc) { + super(new DefaultInfo(name, minargs, maxargs)); + this.doc = doc == null ? null : Py.newString(doc); } - /** - * @return a new instance of this type of PyBuiltinFunction bound to self - */ - abstract public PyBuiltinFunction bind(PyObject self); - - @ExposedGet(name = "__name__") - public PyObject fastGetName() { - return Py.newString(this.info.getName()); - } - - @ExposedGet(name = "__doc__") public PyObject fastGetDoc() { - return Py.None; + return doc; } - @ExposedGet(name = "__module__") - public PyObject getModule() { - return Py.None; + public boolean isMappingType() { + return false; } - @ExposedGet(name = "__call__") - public PyObject makeCall() { - return this; + public boolean isNumberType() { + return false; } - @ExposedGet(name = "__self__") - public PyObject getSelf() { - return Py.None; + public boolean isSequenceType() { + return false; } - public void setInfo(Info info) { - this.info = info; + public PyBuiltinCallable bind(PyObject self) { + throw Py.TypeError("Can't bind a builtin function"); } public String toString() { - PyObject self = getSelf(); - if (self == null) { - return String.format("<built-in function %s>", info.getName()); - } else { - return String.format("<built-in method %s of %s object at %s>", info.getName(), - self.getType().fastGetName(), Py.idstr(self)); - } + return "<built-in function " + info.getName() + ">"; } - - public interface Info { - - String getName(); - - int getMaxargs(); - - int getMinargs(); - - PyException unexpectedCall(int nargs, boolean keywords); - } - - public static class DefaultInfo implements Info { - - public DefaultInfo(String name, int minargs, int maxargs) { - this.name = name; - this.minargs = minargs; - this.maxargs = maxargs; - } - - public DefaultInfo(String name) { - this(name, -1, -1); - } - - private String name; - - private int maxargs, minargs; - - public String getName() { - return name; - } - - public int getMaxargs() { - return maxargs; - } - - public int getMinargs() { - return minargs; - } - - public static boolean check(int nargs, int minargs, int maxargs) { - if(nargs < minargs) - return false; - if(maxargs != -1 && nargs > maxargs) - return false; - return true; - } - - public static PyException unexpectedCall(int nargs, - boolean keywords, - String name, - int minargs, - int maxargs) { - if(keywords) - return Py.TypeError(name + "() takes no keyword arguments"); - String argsblurb; - if(minargs == maxargs) { - if(minargs == 0) - argsblurb = "no arguments"; - else if(minargs == 1) - argsblurb = "exactly one argument"; - else - argsblurb = minargs + " arguments"; - } else if(maxargs == -1) { - return Py.TypeError(name + "() requires at least " + minargs - + " (" + nargs + " given)"); - } else { - if(minargs <= 0) - argsblurb = "at most " + maxargs + " arguments"; - else - argsblurb = minargs + "-" + maxargs + " arguments"; - } - return Py.TypeError(name + "() takes " + argsblurb + " (" + nargs - + " given)"); - } - - public PyException unexpectedCall(int nargs, boolean keywords) { - return unexpectedCall(nargs, keywords, name, minargs, maxargs); - } - } } Added: trunk/jython/src/org/python/core/PyBuiltinFunctionNarrow.java =================================================================== --- trunk/jython/src/org/python/core/PyBuiltinFunctionNarrow.java (rev 0) +++ trunk/jython/src/org/python/core/PyBuiltinFunctionNarrow.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -0,0 +1,58 @@ +package org.python.core; + +public class PyBuiltinFunctionNarrow extends PyBuiltinFunction { + + protected PyBuiltinFunctionNarrow(String name, int minargs, int maxargs, String doc) { + super(name, minargs, maxargs, doc); + } + + public PyObject fancyCall(PyObject[] args) { + throw info.unexpectedCall(args.length, false); + } + + public PyObject __call__(PyObject[] args) { + int nargs = args.length; + switch(nargs){ + case 0: + return __call__(); + case 1: + return __call__(args[0]); + case 2: + return __call__(args[0], args[1]); + case 3: + return __call__(args[0], args[1], args[2]); + case 4: + return __call__(args[0], args[1], args[2], args[3]); + default: + return fancyCall(args); + } + } + + public PyObject __call__(PyObject[] args, String[] kws) { + if (kws.length != 0) { + throw Py.TypeError(fastGetName() + "() takes no keyword arguments"); + } + return __call__(args); + } + + + public PyObject __call__() { + throw info.unexpectedCall(0, false); + } + + public PyObject __call__(PyObject arg1) { + throw info.unexpectedCall(1, false); + } + + public PyObject __call__(PyObject arg1, PyObject arg2) { + throw info.unexpectedCall(2, false); + } + + public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) { + throw info.unexpectedCall(3, false); + } + + public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3, PyObject arg4) { + throw info.unexpectedCall(4, false); + } +} Modified: trunk/jython/src/org/python/core/PyBuiltinFunctionSet.java =================================================================== --- trunk/jython/src/org/python/core/PyBuiltinFunctionSet.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyBuiltinFunctionSet.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -1,34 +1,30 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; -import org.python.expose.ExposeAsSuperclass; - /** * A helper class for faster implementations of commonly called methods. * <p> * Subclasses of PyBuiltinFunctionSet will implement some or all of the __call__ * method with a switch on the index number. - * + * */ -public class PyBuiltinFunctionSet extends PyBuiltinFunction implements ExposeAsSuperclass { +public class PyBuiltinFunctionSet extends PyBuiltinFunctionNarrow { // used as an index into a big switch statement in the various derived // class's __call__() methods. - protected int index; + protected final int index; - private PyObject doc = Py.None; - /** * Creates a PyBuiltinFunctionSet that expects 1 argument. */ public PyBuiltinFunctionSet(String name, int index){ this(name, index, 1); } - + public PyBuiltinFunctionSet(String name, int index, int numargs){ this(name, index, numargs, numargs); } - + public PyBuiltinFunctionSet(String name, int index, int minargs, int maxargs){ this(name, index, minargs, maxargs, null); } @@ -39,87 +35,8 @@ int minargs, int maxargs, String doc) { - super(new DefaultInfo(name, minargs, maxargs)); + super(name, minargs, maxargs, doc); this.index = index; - if(doc != null) { - this.doc = Py.newString(doc); - } } - public PyObject fastGetDoc() { - return doc; - } - - public boolean isMappingType() { - return false; - } - - public boolean isNumberType() { - return false; - } - - public boolean isSequenceType() { - return false; - } - - public PyObject fancyCall(PyObject[] args) { - throw info.unexpectedCall(args.length, false); - } - - public PyObject __call__(PyObject[] args) { - int nargs = args.length; - switch(nargs){ - case 0: - return __call__(); - case 1: - return __call__(args[0]); - case 2: - return __call__(args[0], args[1]); - case 3: - return __call__(args[0], args[1], args[2]); - case 4: - return __call__(args[0], args[1], args[2], args[3]); - default: - return fancyCall(args); - } - } - - public PyObject __call__(PyObject[] args, String[] kws) { - if (kws.length != 0) { - throw Py.TypeError(fastGetName() + "() takes no keyword arguments"); - } - return __call__(args); - } - - public PyObject __call__() { - throw info.unexpectedCall(0, false); - } - - public PyObject __call__(PyObject arg1) { - throw info.unexpectedCall(1, false); - } - - public PyObject __call__(PyObject arg1, PyObject arg2) { - throw info.unexpectedCall(2, false); - } - - public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) { - throw info.unexpectedCall(3, false); - } - - public PyObject __call__(PyObject arg1, - PyObject arg2, - PyObject arg3, - PyObject arg4) { - throw info.unexpectedCall(4, false); - } - - public PyBuiltinFunction bind(PyObject self) { - throw Py.TypeError("Can't bind a builtin function"); - } - - public String toString(){ - return "<built-in function "+info.getName()+">"; - } - } Modified: trunk/jython/src/org/python/core/PyBuiltinMethod.java =================================================================== --- trunk/jython/src/org/python/core/PyBuiltinMethod.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyBuiltinMethod.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -2,7 +2,7 @@ import org.python.expose.ExposeAsSuperclass; -public abstract class PyBuiltinMethod extends PyBuiltinFunction implements ExposeAsSuperclass { +public abstract class PyBuiltinMethod extends PyBuiltinCallable implements ExposeAsSuperclass { protected PyObject self; Modified: trunk/jython/src/org/python/core/PyBuiltinMethodNarrow.java =================================================================== --- trunk/jython/src/org/python/core/PyBuiltinMethodNarrow.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyBuiltinMethodNarrow.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -1,7 +1,5 @@ package org.python.core; -import org.python.core.PyBuiltinFunction.DefaultInfo; - public abstract class PyBuiltinMethodNarrow extends PyBuiltinMethod { protected PyBuiltinMethodNarrow(String name, int minArgs, int maxArgs) { Modified: trunk/jython/src/org/python/core/PyBuiltinMethodSet.java =================================================================== --- trunk/jython/src/org/python/core/PyBuiltinMethodSet.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyBuiltinMethodSet.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -25,7 +25,7 @@ return this; } - public PyBuiltinFunction bind(PyObject bindTo) { + public PyBuiltinCallable bind(PyObject bindTo) { if(__self__ == Py.None) { PyBuiltinMethodSet bindable; try { Modified: trunk/jython/src/org/python/core/PyClassMethodDescr.java =================================================================== --- trunk/jython/src/org/python/core/PyClassMethodDescr.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyClassMethodDescr.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -8,7 +8,7 @@ public static final PyType TYPE = PyType.fromClass(PyClassMethodDescr.class); - PyClassMethodDescr(PyType t, PyBuiltinFunction meth) { + PyClassMethodDescr(PyType t, PyBuiltinCallable meth) { super(t, meth); } Modified: trunk/jython/src/org/python/core/PyDictionary.java =================================================================== --- trunk/jython/src/org/python/core/PyDictionary.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyDictionary.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -422,7 +422,7 @@ private void updateCommon(PyObject[] args, String[] keywords, String methName) { int nargs = args.length - keywords.length; if (nargs > 1) { - throw PyBuiltinFunction.DefaultInfo.unexpectedCall(nargs, false, methName, 0, 1); + throw PyBuiltinCallable.DefaultInfo.unexpectedCall(nargs, false, methName, 0, 1); } if (nargs == 1) { PyObject arg = args[0]; Modified: trunk/jython/src/org/python/core/PyEnumerate.java =================================================================== --- trunk/jython/src/org/python/core/PyEnumerate.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyEnumerate.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -51,7 +51,7 @@ public final static PyObject enumerate_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { if (args.length != 1) { - throw PyBuiltinFunction.DefaultInfo.unexpectedCall(args.length, false, "enumerate", 0, + throw PyBuiltinCallable.DefaultInfo.unexpectedCall(args.length, false, "enumerate", 0, 1); } if (new_.for_type == subtype) { Modified: trunk/jython/src/org/python/core/PyInstance.java =================================================================== --- trunk/jython/src/org/python/core/PyInstance.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyInstance.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -12,12 +12,12 @@ { // xxx doc, final name public transient PyClass instclass; - + // xxx public PyObject fastGetClass() { return instclass; } - + //This field is only used by Python subclasses of Java classes Object javaProxy; @@ -31,7 +31,7 @@ throws java.io.IOException, ClassNotFoundException { in.defaultReadObject(); - + String module = in.readUTF(); String name = in.readUTF(); @@ -153,9 +153,8 @@ return javaProxy; if (instclass.__tojava__ != null) { - //try { - PyObject ret = - instclass.__tojava__.__call__(this, PyJavaClass.lookup(c)); + // try { + PyObject ret = instclass.__tojava__.__call__(this, PyJavaClass.lookup(c)); if (ret == Py.None) return Py.NoConversion; @@ -221,8 +220,8 @@ PyObject[] result2 = instclass.lookupGivingClass(name, stopAtJava); if (result2[0] != null) // xxx do we need to use result2[1] (wherefound) for java cases for backw comp? - return result2[0].__get__(this, instclass); - // xxx do we need to use + return result2[0].__get__(this, instclass); + // xxx do we need to use return ifindfunction(name); } @@ -429,7 +428,7 @@ smod = ((PyString)mod).toString() + '.'; } } - return new PyString("<" + smod + instclass.__name__ + " instance at " + + return new PyString("<" + smod + instclass.__name__ + " instance at " + Py.idstr(this) + ">"); } @@ -483,8 +482,8 @@ if (coerced != null) { v = coerced[0]; w = coerced[1]; - if (!(v instanceof PyInstance) && - !(w instanceof PyInstance)) + if (!(v instanceof PyInstance) && + !(w instanceof PyInstance)) return v._cmp(w); } else { v = this; @@ -497,8 +496,8 @@ int result = ((PyInteger)ret).getValue(); return result < 0 ? -1 : result > 0 ? 1 : 0; } - throw Py.TypeError("__cmp__() must return int"); - } + throw Py.TypeError("__cmp__() must return int"); + } } if (w instanceof PyInstance) { ret = ((PyInstance)w).invoke_ex("__cmp__",v); @@ -507,9 +506,9 @@ int result = ((PyInteger)ret).getValue(); return -(result < 0 ? -1 : result > 0 ? 1 : 0); } - throw Py.TypeError("__cmp__() must return int"); - } - + throw Py.TypeError("__cmp__() must return int"); + } + } return -2; } Modified: trunk/jython/src/org/python/core/PyMethodDescr.java =================================================================== --- trunk/jython/src/org/python/core/PyMethodDescr.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyMethodDescr.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -5,13 +5,13 @@ import org.python.expose.ExposedType; @ExposedType(name = "method_descriptor", base = PyObject.class) -public class PyMethodDescr extends PyDescriptor implements PyBuiltinFunction.Info { +public class PyMethodDescr extends PyDescriptor implements PyBuiltinCallable.Info { protected int minargs, maxargs; - protected PyBuiltinFunction meth; + protected PyBuiltinCallable meth; - public PyMethodDescr(PyType t, PyBuiltinFunction func) { + public PyMethodDescr(PyType t, PyBuiltinCallable func) { name = func.info.getName(); dtype = t; minargs = func.info.getMinargs(); @@ -50,7 +50,7 @@ } public PyException unexpectedCall(int nargs, boolean keywords) { - return PyBuiltinFunction.DefaultInfo.unexpectedCall(nargs, keywords, name, minargs, + return PyBuiltinCallable.DefaultInfo.unexpectedCall(nargs, keywords, name, minargs, maxargs); } Modified: trunk/jython/src/org/python/core/PyNewWrapper.java =================================================================== --- trunk/jython/src/org/python/core/PyNewWrapper.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyNewWrapper.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -29,7 +29,7 @@ PyObject[] args, String[] keywords); - public PyBuiltinFunction bind(PyObject self) { + public PyBuiltinCallable bind(PyObject self) { throw Py.SystemError("__new__ wrappers are already bound"); } Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyObject.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -332,8 +332,8 @@ String name; if (this instanceof PyFunction) { name = ((PyFunction) this).__name__ + "() "; - } else if (this instanceof PyBuiltinFunction) { - name = ((PyBuiltinFunction)this).fastGetName().toString() + "() "; + } else if (this instanceof PyBuiltinCallable) { + name = ((PyBuiltinCallable)this).fastGetName().toString() + "() "; } else { name = getType().fastGetName() + " "; } Modified: trunk/jython/src/org/python/core/PySet.java =================================================================== --- trunk/jython/src/org/python/core/PySet.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PySet.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -30,7 +30,7 @@ final void set___init__(PyObject[] args, String[] kwds) { int nargs = args.length - kwds.length; if (nargs > 1) { - throw PyBuiltinFunction.DefaultInfo.unexpectedCall(nargs, false, "Set", 0, 1); + throw PyBuiltinCallable.DefaultInfo.unexpectedCall(nargs, false, "Set", 0, 1); } if (nargs == 0) { return; Modified: trunk/jython/src/org/python/core/PyStringMap.java =================================================================== --- trunk/jython/src/org/python/core/PyStringMap.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyStringMap.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -242,7 +242,7 @@ public void update(PyObject[] args, String[] keywords) { int nargs = args.length - keywords.length; if (nargs > 1) { - throw PyBuiltinFunction.DefaultInfo.unexpectedCall(nargs, false, "update", 0, 1); + throw PyBuiltinCallable.DefaultInfo.unexpectedCall(nargs, false, "update", 0, 1); } if (nargs == 1) { PyObject arg = args[0]; Modified: trunk/jython/src/org/python/core/PySuper.java =================================================================== --- trunk/jython/src/org/python/core/PySuper.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PySuper.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -34,8 +34,8 @@ @ExposedMethod @ExposedNew public void super___init__(PyObject[] args, String[] keywords) { - if (keywords.length != 0 || !PyBuiltinFunction.DefaultInfo.check(args.length, 1, 2)) { - throw PyBuiltinFunction.DefaultInfo.unexpectedCall(args.length, keywords.length != 0, + if (keywords.length != 0 || !PyBuiltinCallable.DefaultInfo.check(args.length, 1, 2)) { + throw PyBuiltinCallable.DefaultInfo.unexpectedCall(args.length, keywords.length != 0, "super", 1, 2); } if (!(args[0] instanceof PyType)) { Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/PyType.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -13,6 +13,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; import org.python.core.util.StringUtil; import org.python.expose.ExposeAsSuperclass; @@ -260,8 +261,7 @@ newtype.has_delete = newtype.lookup("__delete__") != null; newtype.needs_finalizer = newtype.lookup("__del__") != null; - for (int i = 0; i < bases_list.length; i++) { - PyObject cur = bases_list[i]; + for (PyObject cur : bases_list) { if (cur instanceof PyType) ((PyType)cur).attachSubclass(newtype); } @@ -338,10 +338,9 @@ // BOOTSTRAP_TYPES will be filled in by addBuilder later return; } - HashMap<String, Object> propnames = new HashMap<String, Object>(); + Map<String, Object> propnames = new HashMap<String, Object>(); Method[] methods = c.getMethods(); - for (int i = 0; i < methods.length; i++) { - Method meth = methods[i]; + for (Method meth : methods) { Class declaring = meth.getDeclaringClass(); if (declaring != base && base.isAssignableFrom(declaring) && !ignore(meth)) { String methname = meth.getName(); @@ -369,8 +368,7 @@ } } } - for (int i = 0; i < methods.length; i++) { - Method meth = methods[i]; + for (Method meth : methods) { String nmethname = normalize_name(meth.getName()); PyReflectedFunction reflfunc = (PyReflectedFunction)dict.__finditem__(nmethname); if (reflfunc != null) { @@ -378,8 +376,7 @@ } } Field[] fields = c.getFields(); - for (int i = 0; i < fields.length; i++) { - Field field = fields[i]; + for (Field field : fields) { Class declaring = field.getDeclaringClass(); if (declaring != base && base.isAssignableFrom(declaring)) { String fldname = field.getName(); @@ -404,8 +401,7 @@ dict.__setitem__(normalize_name(fldname), new PyReflectedField(field)); } } - for (Iterator iter = propnames.keySet().iterator(); iter.hasNext();) { - String propname = (String)iter.next(); + for (String propname : propnames.keySet()) { String npropname = normalize_name(StringUtil.decapitalize(propname)); PyObject prev = dict.__finditem__(npropname); if (prev != null && prev instanceof PyReflectedFunction) { @@ -437,8 +433,8 @@ Constructor[] ctrs = c.getConstructors(); if (ctrs.length != 0) { final PyReflectedConstructor reflctr = new PyReflectedConstructor("_new_impl"); - for (int i = 0; i < ctrs.length; i++) { - reflctr.addConstructor(ctrs[i]); + for (Constructor ctr : ctrs) { + reflctr.addConstructor(ctr); } PyObject new_ = new PyNewWrapper(c, "__new__", -1, -1) { public PyObject new_impl(boolean init, PyType subtype, PyObject[] args, @@ -557,14 +553,14 @@ base = newBase; mro_internal(); mro_subclasses(savedSubMros); - for (int i = 0; i < savedBases.length; i++) { - if (savedBases[i] instanceof PyType) { - ((PyType)savedBases[i]).detachSubclass(this); + for (PyObject saved : savedBases) { + if (saved instanceof PyType) { + ((PyType)saved).detachSubclass(this); } } - for (int i = 0; i < newBases.length; i++) { - if (newBases[i] instanceof PyType) { - ((PyType)newBases[i]).attachSubclass(this); + for (PyObject newb : newBases) { + if (newb instanceof PyType) { + ((PyType)newb).attachSubclass(this); } } } catch (PyException t) { @@ -703,8 +699,8 @@ acc.add(classic_cl); } PyObject[] bases = classic_cl.__bases__.getArray(); - for (int i = 0; i < bases.length; i++) { - fill_classic_mro(acc,(PyClass)bases[i]); + for (PyObject base : bases) { + fill_classic_mro(acc,(PyClass)base); } } @@ -850,24 +846,22 @@ PyType winner = null; PyType candidate = null; PyType best = null; - for (int i = 0; i < bases.length; i++) { - PyObject base_proto = bases[i]; - if (base_proto instanceof PyClass) { + for (PyObject base : bases) { + if (base instanceof PyClass) { continue; } - if (!(base_proto instanceof PyType)) { + if (!(base instanceof PyType)) { throw Py.TypeError("bases must be types"); } - PyType base = (PyType)base_proto; - candidate = solid_base(base); + candidate = solid_base((PyType)base); if (winner == null) { winner = candidate; - best = base; + best = (PyType)base; } else if (winner.isSubType(candidate)) { ; } else if (candidate.isSubType(winner)) { winner = candidate; - best = base; + best = (PyType)base; } else { throw Py.TypeError("multiple bases have instance lay-out conflict"); } @@ -889,18 +883,17 @@ */ private static PyType findMostDerivedMetatype(PyObject[] bases_list, PyType initialMetatype) { PyType winner = initialMetatype; - for (int i = 0; i < bases_list.length; i++) { - PyObject bases_i = bases_list[i]; - if (bases_i instanceof PyJavaClass) { + for (PyObject base : bases_list) { + if (base instanceof PyJavaClass) { throw Py.TypeError("can't mix new-style and java classes"); } - if (bases_i instanceof PyClass) { - if (((PyClass)bases_i).proxyClass != null) { + if (base instanceof PyClass) { + if (((PyClass)base).proxyClass != null) { throw Py.TypeError("can't mix new-style and java classes"); } continue; } - PyType curtype = bases_i.getType(); + PyType curtype = base.getType(); if (winner.isSubType(curtype)) { continue; } @@ -962,8 +955,8 @@ if (mro == null) { return null; } - for (int i = 0; i < mro.length; i++) { - PyObject dict = mro[i].fastGetDict(); + for (PyObject element : mro) { + PyObject dict = element.fastGetDict(); if (dict != null) { PyObject obj = dict.__finditem__(name); if (obj != null) @@ -978,8 +971,7 @@ if (mro == null) { return null; } - for (int i = 0; i < mro.length; i++) { - PyObject t = mro[i]; + for (PyObject t : mro) { PyObject dict = t.fastGetDict(); if (dict != null) { PyObject obj = dict.__finditem__(name); @@ -1047,8 +1039,8 @@ private static boolean ignore(Method meth) { Class[] exceptions = meth.getExceptionTypes(); - for (int j = 0; j < exceptions.length; j++) { - if (exceptions[j] == PyIgnoreMethodTag.class) { + for (Class exception : exceptions) { + if (exception == PyIgnoreMethodTag.class) { return true; } } @@ -1261,8 +1253,8 @@ if (mro == null) { return; } - for (int i = 0; i < mro.length; i++) { - mro[i].addKeys(accum, "__dict__"); + for (PyObject element : mro) { + element.addKeys(accum, "__dict__"); } } Modified: trunk/jython/src/org/python/core/__builtin__.java =================================================================== --- trunk/jython/src/org/python/core/__builtin__.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/__builtin__.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -1,15 +1,11 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; -import java.util.Arrays; import java.util.Iterator; import java.util.Map; import org.python.antlr.ast.modType; import org.python.core.util.RelativeFile; -import org.python.expose.ExposedGet; -import org.python.expose.ExposedNew; -import org.python.expose.ExposedType; class BuiltinFunctions extends PyBuiltinFunctionSet { @@ -236,7 +232,7 @@ /** * @param intern - should the resulting string be interned - * @return arg as a String, or throws TypeError with message if asString throws a ConversionException. + * @return arg as a String, or throws TypeError with message if asString throws a ConversionException. */ private String asString(PyObject arg, String message, boolean intern) { @@ -280,7 +276,7 @@ } if (args[0] instanceof PyUnicode) { - flags += PyTableCode.PyCF_SOURCE_IS_UTF8; + flags += PyTableCode.PyCF_SOURCE_IS_UTF8; } return __builtin__.compile(args[0].toString(), args[1].toString(), args[2].toString(), flags, dont_inherit); case 29: @@ -373,23 +369,23 @@ dict.__setitem__("iter", new BuiltinFunctions("iter", 27, 1, 2)); dict.__setitem__("locals", new BuiltinFunctions("locals", 28, 0)); dict.__setitem__("map", new BuiltinFunctions("map", 29, 2, -1)); - dict.__setitem__("max", MaxFunction.INSTANCE); - dict.__setitem__("min", MinFunction.INSTANCE); + dict.__setitem__("max", new MaxFunction()); + dict.__setitem__("min", new MinFunction()); dict.__setitem__("oct", new BuiltinFunctions("oct", 32, 1)); dict.__setitem__("pow", new BuiltinFunctions("pow", 33, 2, 3)); dict.__setitem__("raw_input", new BuiltinFunctions("raw_input", 34, 0, 1)); dict.__setitem__("reduce", new BuiltinFunctions("reduce", 35, 2, 3)); dict.__setitem__("reload", new BuiltinFunctions("reload", 36, 1)); dict.__setitem__("repr", new BuiltinFunctions("repr", 37, 1)); - dict.__setitem__("round", RoundFunction.INSTANCE); + dict.__setitem__("round", new RoundFunction()); dict.__setitem__("setattr", new BuiltinFunctions("setattr", 39, 3)); dict.__setitem__("vars", new BuiltinFunctions("vars", 41, 0, 1)); dict.__setitem__("zip", new BuiltinFunctions("zip", 43, 0, -1)); dict.__setitem__("reversed", new BuiltinFunctions("reversed", 45, 1)); - dict.__setitem__("__import__", ImportFunction.INSTANCE); - dict.__setitem__("sorted", SortedFunction.INSTANCE); - dict.__setitem__("all", AllFunction.INSTANCE); - dict.__setitem__("any", AnyFunction.INSTANCE); + dict.__setitem__("__import__", new ImportFunction()); + dict.__setitem__("sorted", new SortedFunction()); + dict.__setitem__("all", new AllFunction()); + dict.__setitem__("any", new AnyFunction()); } public static PyObject abs(PyObject o) { @@ -399,7 +395,7 @@ public static PyObject apply(PyObject o) { return o.__call__(); } - + public static PyObject apply(PyObject o, PyObject args) { return o.__call__(Py.make_array(args)); } @@ -465,7 +461,7 @@ public static PyObject compile(modType node, String filename, String kind) { return Py.compile_flags(node, filename, kind, Py.getCompilerFlags()); } - + public static PyObject compile(String data, String filename, String kind, int flags, boolean dont_inherit) { if ((flags & ~PyTableCode.CO_ALL_FEATURES) != 0) { throw Py.ValueError("compile(): unrecognised flags"); @@ -518,16 +514,16 @@ (o.__findattr__("__getitem__") != null && (!rw || o.__findattr__("__setitem__") != null)); } - + private static void verify_mappings(PyObject globals, PyObject locals, boolean rw) { if (!PyMapping_check(globals, rw)) { throw Py.TypeError("globals must be a mapping"); - } + } if (!PyMapping_check(locals, rw)) { throw Py.TypeError("locals must be a mapping"); } } - + public static PyObject eval(PyObject o, PyObject globals, PyObject locals) { verify_mappings(globals, locals, false); PyCode code; @@ -900,7 +896,7 @@ throw Py.TypeError("ord() expected a character, but string of length " + length + " found"); } - + public static PyObject pow(PyObject x, PyObject y) { return x._pow(y); } @@ -1037,7 +1033,7 @@ return (PyString) ret; } } - + public static String raw_input(PyObject prompt, PyObject file) { PyObject stdout = Py.getSystemState().stdout; if (stdout instanceof PyAttributeDeleted) { @@ -1157,7 +1153,7 @@ public static PyObject zip() { return new PyList(); } - + public static PyObject zip(PyObject[] argstar) { int itemsize = argstar.length; @@ -1233,81 +1229,40 @@ } } -// simulates a PyBuiltinFunction for functions not using the PyBuiltinFunctionSet approach of above -abstract class ExtendedBuiltinFunction extends PyObject { - public static final PyType TYPE = PyType.fromClass(PyBuiltinFunction.class); - @ExposedGet(name = "__class__") - @Override - public PyType getType() { - return TYPE; +class ImportFunction extends PyBuiltinFunction { + ImportFunction() { + super("__import__", + "__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\n" + + "Import a module. The globals are only used to determine the context;\n" + + "they are not modified. The locals are currently unused. The fromlist\n" + + "should be a list of names to emulate ``from name import ...'', or an\n" + + "empty list to emulate ``import name''.\n" + + "When importing a module from a package, note that __import__('A.B', ...)\n" + + "returns package A when fromlist is empty, but its submodule B when\n" + + "fromlist is not empty. Level is used to determine whether to perform \n" + + "absolute or relative imports. -1 is the original strategy of attempting\n" + + "both absolute and relative imports, 0 is absolute, a positive number\n" + + "is the number of parent directories to search relative to the current module."); } -} -class ImportFunction extends ExtendedBuiltinFunction { - static final ImportFunction INSTANCE = new ImportFunction(); - - private ImportFunction() {} - - @ExposedNew - public static PyObject __new__(PyObject[] args, String[] keyword) { - return INSTANCE; - } - - @ExposedGet(name = "__doc__") - public PyObject getDoc() { - return new PyString("__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\n" + - "Import a module. The globals are only used to determine the context;\n" + - "they are not modified. The locals are currently unused. The fromlist\n" + - "should be a list of names to emulate ``from name import ...'', or an\n" + - "empty list to emulate ``import name''.\n" + - "When importing a module from a package, note that __import__('A.B', ...)\n" + - "returns package A when fromlist is empty, but its submodule B when\n" + - "fromlist is not empty. Level is used to determine whether to perform \n" + - "absolute or relative imports. -1 is the original strategy of attempting\n" + - "both absolute and relative imports, 0 is absolute, a positive number\n" + - "is the number of parent directories to search relative to the current module."); - } - public PyObject __call__(PyObject args[], String keywords[]) { - ArgParser ap = new ArgParser("__import__", args, keywords, new String[]{"name", "globals", "locals", "fromlist", "level"}, 1); - + ArgParser ap = new ArgParser("__import__", args, keywords, + new String[]{"name", "globals", "locals", "fromlist", "level"}, + 1); String module = ap.getString(0); PyObject globals = ap.getPyObject(1, null); PyObject fromlist = ap.getPyObject(3, Py.EmptyTuple); int level = ap.getInt(4, imp.DEFAULT_LEVEL); - - return load(module, globals, fromlist, level); + return imp.importName(module.intern(), fromlist.__len__() == 0, globals, fromlist, level); } +} - private PyObject load(String module, PyObject globals, PyObject fromlist, int level) { - PyObject mod = imp.importName(module.intern(), fromlist.__len__() == 0, globals, fromlist, level); - return mod; +class SortedFunction extends PyBuiltinFunction { + SortedFunction() { + super("sorted", "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"); } - public String toString() { - return "<built-in function __import__>"; - } - - -} - -class SortedFunction extends ExtendedBuiltinFunction { - static final SortedFunction INSTANCE = new SortedFunction(); - - private SortedFunction() {} - - @ExposedNew - public static PyObject __new__(PyObject[] args, String[] keyword) { - return INSTANCE; - } - - @ExposedGet(name = "__doc__") @Override - public PyObject getDoc() { - return new PyString("sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"); - } - - @Override public PyObject __call__(PyObject args[], String kwds[]) { if (args.length == 0) { throw Py.TypeError(" sorted() takes at least 1 argument (0 given)"); @@ -1333,37 +1288,20 @@ seq.sort(cmp, key, reverse); return seq; } - - @Override - public String toString() { - return "<built-in function sorted>"; - } } -class AllFunction extends ExtendedBuiltinFunction { - static final AllFunction INSTANCE = new AllFunction(); - - private AllFunction() {} - - @ExposedNew - public static PyObject __new__(PyObject[] args, String[] keyword) { - return INSTANCE; - } - - @ExposedGet(name = "__doc__") - @Override - public PyObject getDoc() { - return new PyString("all(iterable) -> bool\n\nReturn True if bool(x) is True for all values x in the iterable."); +class AllFunction extends PyBuiltinFunctionNarrow { + AllFunction() { + super("all", 1, 1, + "all(iterable) -> bool\n\n" + + "Return True if bool(x) is True for all values x in the iterable."); } @Override - public PyObject __call__(PyObject args[], String kwds[]) { - if (args.length !=1) { - throw Py.TypeError(" all() takes exactly one argument (" + args.length + " given)"); - } - PyObject iter = args[0].__iter__(); + public PyObject __call__(PyObject arg) { + PyObject iter = arg.__iter__(); if (iter == null) { - throw Py.TypeError("'" + args[0].getType().fastGetName() + "' object is not iterable"); + throw Py.TypeError("'" + arg.getType().fastGetName() + "' object is not iterable"); } for (PyObject item : iter.asIterable()) { if (!item.__nonzero__()) { @@ -1372,78 +1310,44 @@ } return Py.True; } - - @Override - public String toString() { - return "<built-in function all>"; - } } -class AnyFunction extends ExtendedBuiltinFunction { - static final AnyFunction INSTANCE = new AnyFunction(); - - private AnyFunction() {} - - @ExposedNew - public static PyObject __new__(PyObject[] args, String[] keyword) { - return INSTANCE; - } - - @ExposedGet(name = "__doc__") - @Override - public PyObject getDoc() { - return new PyString("any(iterable) -> bool\n\nReturn True if bool(x) is True for any x in the iterable."); +class AnyFunction extends PyBuiltinFunctionNarrow { + AnyFunction() { + super("any", 1, 1, + "any(iterable) -> bool\n\nReturn True if bool(x) is True for any x in the iterable."); } @Override - public PyObject __call__(PyObject args[], String kwds[]) { - if (args.length !=1) { - throw Py.TypeError(" any() takes exactly one argument (" + args.length + " given)"); - } - PyObject iter = args[0].__iter__(); + public PyObject __call__(PyObject arg) { + PyObject iter = arg.__iter__(); if (iter == null) { - throw Py.TypeError("'" + args[0].getType().fastGetName() + "' object is not iterable"); + throw Py.TypeError("'" + arg.getType().fastGetName() + "' object is not iterable"); } for (PyObject item : iter.asIterable()) { if (item.__nonzero__()) { return Py.True; } } - return Py.False; + return Py.False; } - - @Override - public String toString() { - return "<built-in function any>"; - } } -class MaxFunction extends ExtendedBuiltinFunction { - static final MaxFunction INSTANCE = new MaxFunction(); - - private MaxFunction() {} - - @ExposedNew - public static PyObject __new__(PyObject[] args, String[] keyword) { - return INSTANCE; - } - - @ExposedGet(name = "__doc__") - @Override - public PyObject getDoc() { - return new PyString( - "max(iterable[, key=func]) -> value\nmax(a, b, c, ...[, key=func]) -> value\n\n" + - "With a single iterable argument, return its largest item.\n" + - "With two or more arguments, return the largest argument."); +class MaxFunction extends PyBuiltinFunction { + MaxFunction() { + super("max", + "max(iterable[, key=func]) -> value\nmax(a, b, c, ...[, key=func]) -> value\n\n" + + "With a single iterable argument, return its largest item.\n" + + "With two or more arguments, return the largest argument."); } @Override public PyObject __call__(PyObject args[], String kwds[]) { int argslen = args.length; PyObject key = null; - + if (args.length - kwds.length == 0) { - throw Py.TypeError(" max() expected 1 arguments, got 0"); + throw Py.TypeError("max() expected 1 arguments, got 0"); } if (kwds.length > 0) { if (kwds[0].equals("key")) { @@ -1453,23 +1357,18 @@ args = newargs; } else { - throw Py.TypeError(" max() got an unexpected keyword argument"); + throw Py.TypeError("max() got an unexpected keyword argument"); } } - + if (args.length > 1) { return max(new PyTuple(args), key); } else { return max(args[0], key); } - } - - @Override - public String toString() { - return "<built-in function max>"; } - + private static PyObject max(PyObject o, PyObject key) { PyObject max = null; PyObject maxKey = null; @@ -1494,31 +1393,19 @@ } -class MinFunction extends ExtendedBuiltinFunction { - static final MinFunction INSTANCE = new MinFunction(); - - private MinFunction() {} - - @ExposedNew - public static PyObject __new__(PyObject[] args, String[] keyword) { - return INSTANCE; - } - - - @ExposedGet(name = "__doc__") - @Override - public PyObject getDoc() { - return new PyString( - "min(iterable[, key=func]) -> value\nmin(a, b, c, ...[, key=func]) -> value\n\n" + - "With a single iterable argument, return its smallest item.\n" + - "With two or more arguments, return the smallest argument.'"); +class MinFunction extends PyBuiltinFunction { + MinFunction() { + super("min", + "min(iterable[, key=func]) -> value\nmin(a, b, c, ...[, key=func]) -> value\n\n" + + "With a single iterable argument, return its smallest item.\n" + + "With two or more arguments, return the smallest argument.'"); } @Override public PyObject __call__(PyObject args[], String kwds[]) { int argslen = args.length; PyObject key = null; - + if (args.length - kwds.length == 0) { throw Py.TypeError(" min() expected 1 arguments, got 0"); } @@ -1530,10 +1417,10 @@ args = newargs; } else { - throw Py.TypeError(" min() got an unexpected keyword argument"); + throw Py.TypeError("min() got an unexpected keyword argument"); } } - + if (args.length > 1) { return min(new PyTuple(args), key); } @@ -1541,12 +1428,7 @@ return min(args[0], key); } } - - @Override - public String toString() { - return "<built-in function min>"; - } - + private static PyObject min(PyObject o, PyObject key) { PyObject min = null; PyObject minKey = null; @@ -1570,37 +1452,20 @@ } } -class RoundFunction extends ExtendedBuiltinFunction { - static final RoundFunction INSTANCE = new RoundFunction(); - private RoundFunction() {} - - @ExposedNew - public static PyObject __new__(PyObject[] args, String[] keyword) { - return INSTANCE; - } - - @ExposedGet(name = "__doc__") - @Override - public PyObject getDoc() { - return new PyString( - "round(number[, ndigits]) -> floating point number\n\n" + - "Round a number to a given precision in decimal digits (default 0 digits).\n" + - "This always returns a floating point number. Precision may be negative."); +class RoundFunction extends PyBuiltinFunction { + RoundFunction() { + super("round", "round(number[, ndigits]) -> floating point number\n\n" + + "Round a number to a given precision in decimal digits (default 0 digits).\n" + + "This always returns a floating point number. Precision may be negative."); } - - @Override - public String toString() { - return "<built-in function round>"; - } - - @Override + public PyObject __call__(PyObject args[], String kwds[]) { - ArgParser ap = new ArgParser("round", args, kwds, new String[]{"number", "ndigits"}, 0); + ArgParser ap = new ArgParser("round", args, kwds, new String[] {"number", "ndigits"}, 0); PyObject number = ap.getPyObject(0); int ndigits = ap.getInt(1, 0); return round(Py.py2double(number), ndigits); } - + private static PyFloat round(double f, int digits) { boolean neg = f < 0; double multiple = Math.pow(10., digits); Modified: trunk/jython/src/org/python/core/exceptions.java =================================================================== --- trunk/jython/src/org/python/core/exceptions.java 2008-10-19 03:04:44 UTC (rev 5464) +++ trunk/jython/src/org/python/core/exceptions.java 2008-10-19 05:14:22 UTC (rev 5465) @@ -661,7 +661,7 @@ this.javaMethod = javaMethod; } - public PyBuiltinFunction bind(PyObject self) { + public PyBuiltinCallable bind(PyObject self) { return new BoundStaticJavaMethod(getType(), self, info, javaMethod); } Modified: trunk/jython/src/org/python/expose/generate/Py... [truncated message content] |