Index: src/org/python/core/__builtin__.java =================================================================== --- src/org/python/core/__builtin__.java (revision 3608) +++ src/org/python/core/__builtin__.java (working copy) @@ -10,294 +10,288 @@ class BuiltinFunctions extends PyBuiltinFunctionSet { - public BuiltinFunctions(String name, int index, int argcount) { - this(name, index, argcount, argcount); - } + public BuiltinFunctions(String name, int index, int argcount) { + this(name, index, argcount, argcount); + } - public BuiltinFunctions(String name, int index, int minargs, int maxargs) { - super(name, index, minargs, maxargs); - } + public BuiltinFunctions(String name, int index, int minargs, int maxargs) { + super(name, index, minargs, maxargs); + } - public PyObject __call__() { - switch (this.index) { - case 4: - return __builtin__.globals(); - case 16: - return __builtin__.dir(); - case 24: - return __builtin__.input(); - case 28: - return __builtin__.locals(); - case 34: - return Py.newString(__builtin__.raw_input()); - case 41: - return __builtin__.vars(); - default: - throw info.unexpectedCall(0, false); - } - } + public PyObject __call__() { + switch (this.index) { + case 4: + return __builtin__.globals(); + case 16: + return __builtin__.dir(); + case 24: + return __builtin__.input(); + case 28: + return __builtin__.locals(); + case 34: + return Py.newString(__builtin__.raw_input()); + case 41: + return __builtin__.vars(); + default: + throw info.unexpectedCall(0, false); + } + } - public PyObject __call__(PyObject arg1) { - switch (this.index) { - case 0: - return Py.newString(__builtin__.chr(Py.py2int(arg1, "chr(): 1st arg can't be coerced to int"))); - case 1: - return Py.newInteger(__builtin__.len(arg1)); - case 2: - return __builtin__.range(arg1); - case 3: - if (!(arg1 instanceof PyString)) - throw Py.TypeError("ord() expected string of length 1, but " + arg1.getType().getFullName() + " found"); - if (arg1.__len__() > 1) - throw Py.TypeError("ord() expected a character, but string of length " + arg1.__len__() + " found"); - return Py.newInteger(__builtin__.ord(Py.py2char(arg1, - "ord(): 1st arg can't be coerced to char"))); - case 5: - return __builtin__.hash(arg1); - case 6: - return Py.newUnicode(__builtin__.chr(Py.py2int(arg1, "unichr(): 1st arg can't be coerced to int"))); - case 7: - return __builtin__.abs(arg1); - case 11: - return Py.newInteger(__builtin__.id(arg1)); - case 12: - return __builtin__.sum(arg1); - case 14: - return Py.newBoolean(__builtin__.callable(arg1)); - case 16: - return __builtin__.dir(arg1); - case 18: - return __builtin__.eval(arg1); - case 19: - try { - __builtin__.execfile(arg1.asString(0)); - } catch (ConversionException e) { - throw Py.TypeError("execfile's first argument must be str"); - } - return Py.None; - case 23: - return __builtin__.hex(arg1); - case 24: - return __builtin__.input(arg1); - case 25: - return __builtin__.intern(arg1.__str__()); - case 27: - return __builtin__.iter(arg1); - case 32: - return __builtin__.oct(arg1); - case 34: - return Py.newString(__builtin__.raw_input(arg1)); - case 36: - Object o = arg1.__tojava__(PyModule.class); - if (o == Py.NoConversion) { - o = arg1.__tojava__(PyJavaClass.class); - if (o == Py.NoConversion) { - Py.TypeError("reload() argument must be a module"); - } - return __builtin__.reload((PyJavaClass) o); - } - return __builtin__.reload((PyModule) o); - case 37: - return __builtin__.repr(arg1); - case 38: - return __builtin__.round(Py.py2double(arg1)); - case 40: - return __builtin__.slice(arg1); - case 41: - return __builtin__.vars(arg1); - case 42: - return __builtin__.xrange(Py.py2int(arg1)); - case 30: - return fancyCall(new PyObject[] { arg1 }); - case 31: - return fancyCall(new PyObject[] { arg1 }); - case 43: - return fancyCall(new PyObject[] { arg1 }); - case 45: - return __builtin__.reversed(arg1); - default: - throw info.unexpectedCall(1, false); - } - } + public PyObject __call__(PyObject arg1) { + switch (this.index) { + case 0: + return Py.newString(__builtin__.chr(Py.py2int(arg1, "chr(): 1st arg can't be coerced to int"))); + case 1: + return Py.newInteger(__builtin__.len(arg1)); + case 2: + return __builtin__.range(arg1); + case 3: + if (!(arg1 instanceof PyString)) + throw Py.TypeError("ord() expected string of length 1, but " + arg1.getType().getFullName() + " found"); + if (arg1.__len__() > 1) + throw Py.TypeError("ord() expected a character, but string of length " + arg1.__len__() + " found"); + return Py.newInteger(__builtin__.ord(Py.py2char(arg1, + "ord(): 1st arg can't be coerced to char"))); + case 5: + return __builtin__.hash(arg1); + case 6: + return Py.newUnicode(__builtin__.chr(Py.py2int(arg1, "unichr(): 1st arg can't be coerced to int"))); + case 7: + return __builtin__.abs(arg1); + case 11: + return Py.newInteger(__builtin__.id(arg1)); + case 12: + return __builtin__.sum(arg1); + case 14: + return Py.newBoolean(__builtin__.callable(arg1)); + case 16: + return __builtin__.dir(arg1); + case 18: + return __builtin__.eval(arg1); + case 19: + try { + __builtin__.execfile(arg1.asString(0)); + } catch (ConversionException e) { + throw Py.TypeError("execfile's first argument must be str"); + } + return Py.None; + case 23: + return __builtin__.hex(arg1); + case 24: + return __builtin__.input(arg1); + case 25: + return __builtin__.intern(arg1.__str__()); + case 27: + return __builtin__.iter(arg1); + case 32: + return __builtin__.oct(arg1); + case 34: + return Py.newString(__builtin__.raw_input(arg1)); + case 36: + Object o = arg1.__tojava__(PyModule.class); + if (o == Py.NoConversion) { + o = arg1.__tojava__(PyJavaClass.class); + if (o == Py.NoConversion) { + Py.TypeError("reload() argument must be a module"); + } + return __builtin__.reload((PyJavaClass) o); + } + return __builtin__.reload((PyModule) o); + case 37: + return __builtin__.repr(arg1); + case 38: + return __builtin__.round(Py.py2double(arg1)); + case 41: + return __builtin__.vars(arg1); + case 42: + return __builtin__.xrange(Py.py2int(arg1)); + case 30: + return fancyCall(new PyObject[] { arg1 }); + case 31: + return fancyCall(new PyObject[] { arg1 }); + case 43: + return fancyCall(new PyObject[] { arg1 }); + case 45: + return __builtin__.reversed(arg1); + default: + throw info.unexpectedCall(1, false); + } + } - public PyObject __call__(PyObject arg1, PyObject arg2) { - switch (this.index) { - case 2: - return __builtin__.range(arg1, arg2); - case 6: - return Py.newInteger(__builtin__.cmp(arg1, arg2)); - case 9: - return __builtin__.apply(arg1, arg2); - case 10: - return Py.newBoolean(__builtin__.isinstance(arg1, arg2)); - case 12: - return __builtin__.sum(arg1, arg2); - case 13: - return __builtin__.coerce(arg1, arg2); - case 15: - __builtin__.delattr(arg1, asString(arg2, "delattr(): attribute name must be string")); - return Py.None; - case 17: - return __builtin__.divmod(arg1, arg2); - case 18: - return __builtin__.eval(arg1, arg2); - case 19: - try { - __builtin__.execfile(arg1.asString(0), arg2); - } catch (ConversionException e) { - throw Py.TypeError("execfile's first argument must be str"); - } - return Py.None; - case 20: - return __builtin__.filter(arg1, arg2); - case 21: - return __builtin__.getattr(arg1, asString(arg2, "getattr(): attribute name must be string")); - case 22: - return Py.newBoolean(__builtin__.hasattr(arg1, asString(arg2, "hasattr(): attribute name must be string"))); - case 26: - return Py.newBoolean(__builtin__.issubclass(arg1, arg2)); - case 27: - return __builtin__.iter(arg1, arg2); - case 33: - return __builtin__.pow(arg1, arg2); - case 35: - return __builtin__.reduce(arg1, arg2); - case 38: - return __builtin__.round(Py.py2double(arg1), Py.py2int(arg2)); - case 40: - return __builtin__.slice(arg1, arg2); - case 42: - return __builtin__.xrange(Py.py2int(arg1), Py.py2int(arg2)); - case 29: - return fancyCall(new PyObject[] { arg1, arg2 }); - case 30: - return fancyCall(new PyObject[] { arg1, arg2 }); - case 31: - return fancyCall(new PyObject[] { arg1, arg2 }); - case 43: - return fancyCall(new PyObject[] { arg1, arg2 }); - default: - throw info.unexpectedCall(2, false); - } - } + public PyObject __call__(PyObject arg1, PyObject arg2) { + switch (this.index) { + case 2: + return __builtin__.range(arg1, arg2); + case 6: + return Py.newInteger(__builtin__.cmp(arg1, arg2)); + case 9: + return __builtin__.apply(arg1, arg2); + case 10: + return Py.newBoolean(__builtin__.isinstance(arg1, arg2)); + case 12: + return __builtin__.sum(arg1, arg2); + case 13: + return __builtin__.coerce(arg1, arg2); + case 15: + __builtin__.delattr(arg1, asString(arg2, "delattr(): attribute name must be string")); + return Py.None; + case 17: + return __builtin__.divmod(arg1, arg2); + case 18: + return __builtin__.eval(arg1, arg2); + case 19: + try { + __builtin__.execfile(arg1.asString(0), arg2); + } catch (ConversionException e) { + throw Py.TypeError("execfile's first argument must be str"); + } + return Py.None; + case 20: + return __builtin__.filter(arg1, arg2); + case 21: + return __builtin__.getattr(arg1, asString(arg2, "getattr(): attribute name must be string")); + case 22: + return Py.newBoolean(__builtin__.hasattr(arg1, asString(arg2, "hasattr(): attribute name must be string"))); + case 26: + return Py.newBoolean(__builtin__.issubclass(arg1, arg2)); + case 27: + return __builtin__.iter(arg1, arg2); + case 33: + return __builtin__.pow(arg1, arg2); + case 35: + return __builtin__.reduce(arg1, arg2); + case 38: + return __builtin__.round(Py.py2double(arg1), Py.py2int(arg2)); + case 42: + return __builtin__.xrange(Py.py2int(arg1), Py.py2int(arg2)); + case 29: + return fancyCall(new PyObject[] { arg1, arg2 }); + case 30: + return fancyCall(new PyObject[] { arg1, arg2 }); + case 31: + return fancyCall(new PyObject[] { arg1, arg2 }); + case 43: + return fancyCall(new PyObject[] { arg1, arg2 }); + default: + throw info.unexpectedCall(2, false); + } + } - public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) { - switch (this.index) { - case 2: - return __builtin__.range(arg1, arg2, arg3); - case 9: - try { - if (arg3 instanceof PyStringMap) { - PyDictionary d = new PyDictionary(); - d.update(arg3); - arg3 = d; - } - // this catches both casts of arg3 to a PyDictionary, and - // all casts of keys in the dictionary to PyStrings inside - // apply(PyObject, PyObject, PyDictionary) - PyDictionary d = (PyDictionary) arg3; - return __builtin__.apply(arg1, arg2, d); - } catch (ClassCastException e) { - throw Py.TypeError("apply() 3rd argument must be a " + "dictionary with string keys"); - } - case 18: - return __builtin__.eval(arg1, arg2, arg3); - case 19: - __builtin__.execfile(asString(arg1, "execfile's first argument must be str", false), arg2, arg3); - return Py.None; - case 21: - return __builtin__.getattr(arg1, asString(arg2, "getattr(): attribute name must be string"), arg3); - case 33: - return __builtin__.pow(arg1, arg2, arg3); - case 35: - return __builtin__.reduce(arg1, arg2, arg3); - case 39: - __builtin__.setattr(arg1, asString(arg2, "setattr(): attribute name must be string"), arg3); - return Py.None; - case 40: - return __builtin__.slice(arg1, arg2, arg3); - case 42: - return __builtin__.xrange(Py.py2int(arg1), Py.py2int(arg2), Py.py2int(arg3)); - case 44: - return fancyCall(new PyObject[] { arg1, arg2, arg3 }); - case 29: - return fancyCall(new PyObject[] { arg1, arg2, arg3 }); - case 30: - return fancyCall(new PyObject[] { arg1, arg2, arg3 }); - case 31: - return fancyCall(new PyObject[] { arg1, arg2, arg3 }); - case 43: - return fancyCall(new PyObject[] { arg1, arg2, arg3 }); - default: - throw info.unexpectedCall(3, false); - } - } + public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) { + switch (this.index) { + case 2: + return __builtin__.range(arg1, arg2, arg3); + case 9: + try { + if (arg3 instanceof PyStringMap) { + PyDictionary d = new PyDictionary(); + d.update(arg3); + arg3 = d; + } + // this catches both casts of arg3 to a PyDictionary, and + // all casts of keys in the dictionary to PyStrings inside + // apply(PyObject, PyObject, PyDictionary) + PyDictionary d = (PyDictionary) arg3; + return __builtin__.apply(arg1, arg2, d); + } catch (ClassCastException e) { + throw Py.TypeError("apply() 3rd argument must be a " + "dictionary with string keys"); + } + case 18: + return __builtin__.eval(arg1, arg2, arg3); + case 19: + __builtin__.execfile(asString(arg1, "execfile's first argument must be str", false), arg2, arg3); + return Py.None; + case 21: + return __builtin__.getattr(arg1, asString(arg2, "getattr(): attribute name must be string"), arg3); + case 33: + return __builtin__.pow(arg1, arg2, arg3); + case 35: + return __builtin__.reduce(arg1, arg2, arg3); + case 39: + __builtin__.setattr(arg1, asString(arg2, "setattr(): attribute name must be string"), arg3); + return Py.None; + case 42: + return __builtin__.xrange(Py.py2int(arg1), Py.py2int(arg2), Py.py2int(arg3)); + case 44: + return fancyCall(new PyObject[] { arg1, arg2, arg3 }); + case 29: + return fancyCall(new PyObject[] { arg1, arg2, arg3 }); + case 30: + return fancyCall(new PyObject[] { arg1, arg2, arg3 }); + case 31: + return fancyCall(new PyObject[] { arg1, arg2, arg3 }); + case 43: + return fancyCall(new PyObject[] { arg1, arg2, arg3 }); + default: + throw info.unexpectedCall(3, false); + } + } - /** - * @return arg as an interned String, or throws TypeError with mesage if asString throws a ConversionException - */ - private String asString(PyObject arg, String message) { - return asString(arg, message, true); - } + /** + * @return arg as an interned String, or throws TypeError with mesage if asString throws a ConversionException + */ + private String asString(PyObject arg, String message) { + return asString(arg, message, true); + } - /** - * @param intern - should the resulting string be interned - * @return arg as a String, or throws TypeError with message if asString throws a ConversionException. - */ - private String asString(PyObject arg, String message, boolean intern) { + /** + * @param intern - should the resulting string be interned + * @return arg as a String, or throws TypeError with message if asString throws a ConversionException. + */ + private String asString(PyObject arg, String message, boolean intern) { - try { - return intern ? arg.asString(0).intern() : arg.asString(0); - } catch (ConversionException e) { - throw Py.TypeError(message); - } - } + try { + return intern ? arg.asString(0).intern() : arg.asString(0); + } catch (ConversionException e) { + throw Py.TypeError(message); + } + } - public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3, PyObject arg4) { - switch (this.index) { - case 44: - return fancyCall(new PyObject[] { arg1, arg2, arg3, arg4 }); - case 29: - return fancyCall(new PyObject[] { arg1, arg2, arg3, arg4 }); - case 30: - return fancyCall(new PyObject[] { arg1, arg2, arg3, arg4 }); - case 31: - return fancyCall(new PyObject[] { arg1, arg2, arg3, arg4 }); - case 43: - return fancyCall(new PyObject[] { arg1, arg2, arg3, arg4 }); - default: - throw info.unexpectedCall(4, false); - } - } + public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3, PyObject arg4) { + switch (this.index) { + case 44: + return fancyCall(new PyObject[] { arg1, arg2, arg3, arg4 }); + case 29: + return fancyCall(new PyObject[] { arg1, arg2, arg3, arg4 }); + case 30: + return fancyCall(new PyObject[] { arg1, arg2, arg3, arg4 }); + case 31: + return fancyCall(new PyObject[] { arg1, arg2, arg3, arg4 }); + case 43: + return fancyCall(new PyObject[] { arg1, arg2, arg3, arg4 }); + default: + throw info.unexpectedCall(4, false); + } + } - public PyObject fancyCall(PyObject[] args) { - switch (this.index) { - case 44: - if (args.length > 5) { - throw info.unexpectedCall(args.length, false); - } - int flags = 0; - if (args.length > 3) { - flags = Py.py2int(args[3]); - } - boolean dont_inherit = false; - if (args.length > 4) { - dont_inherit = Py.py2boolean(args[4]); - } + public PyObject fancyCall(PyObject[] args) { + switch (this.index) { + case 44: + if (args.length > 5) { + throw info.unexpectedCall(args.length, false); + } + int flags = 0; + if (args.length > 3) { + flags = Py.py2int(args[3]); + } + boolean dont_inherit = false; + if (args.length > 4) { + dont_inherit = Py.py2boolean(args[4]); + } - return __builtin__.compile(args[0].toString(), args[1].toString(), args[2].toString(), flags, dont_inherit); - case 29: - return __builtin__.map(args); - case 30: - return __builtin__.max(args); - case 31: - return __builtin__.min(args); - case 43: - return __builtin__.zip(args); - default: - throw info.unexpectedCall(args.length, false); - } - } + return __builtin__.compile(args[0].toString(), args[1].toString(), args[2].toString(), flags, dont_inherit); + case 29: + return __builtin__.map(args); + case 30: + return __builtin__.max(args); + case 31: + return __builtin__.min(args); + case 43: + return __builtin__.zip(args); + default: + throw info.unexpectedCall(args.length, false); + } + } } @@ -305,474 +299,474 @@ * The builtin module. All builtin functions are defined here */ public class __builtin__ { - public static void fillWithBuiltins(PyObject dict) { - /* newstyle */ - dict.__setitem__("object", PyType.fromClass(PyObject.class)); - dict.__setitem__("type", PyType.fromClass(PyType.class)); - dict.__setitem__("bool", PyType.fromClass(PyBoolean.class)); - dict.__setitem__("int", PyType.fromClass(PyInteger.class)); - dict.__setitem__("enumerate", PyType.fromClass(PyEnumerate.class)); - dict.__setitem__("float", PyType.fromClass(PyFloat.class)); - dict.__setitem__("long", PyType.fromClass(PyLong.class)); - dict.__setitem__("complex", PyType.fromClass(PyComplex.class)); - dict.__setitem__("dict", PyType.fromClass(PyDictionary.class)); - dict.__setitem__("list", PyType.fromClass(PyList.class)); - dict.__setitem__("tuple", PyType.fromClass(PyTuple.class)); - dict.__setitem__("set", PyType.fromClass(PySet.class)); - dict.__setitem__("frozenset", PyType.fromClass(PyImmutableSet.class)); - - dict.__setitem__("property", PyType.fromClass(PyProperty.class)); - dict.__setitem__("staticmethod", PyType.fromClass(PyStaticMethod.class)); - dict.__setitem__("classmethod", PyType.fromClass(PyClassMethod.class)); - dict.__setitem__("super", PyType.fromClass(PySuper.class)); - dict.__setitem__("str", PyType.fromClass(PyString.class)); - dict.__setitem__("unicode", PyType.fromClass(PyUnicode.class)); - dict.__setitem__("basestring", PyType.fromClass(PyBaseString.class)); - dict.__setitem__("file", PyType.fromClass(PyFile.class)); - dict.__setitem__("open", PyType.fromClass(PyFile.class)); - - /* - */ + public static void fillWithBuiltins(PyObject dict) { + /* newstyle */ + dict.__setitem__("object", PyType.fromClass(PyObject.class)); + dict.__setitem__("type", PyType.fromClass(PyType.class)); + dict.__setitem__("bool", PyType.fromClass(PyBoolean.class)); + dict.__setitem__("int", PyType.fromClass(PyInteger.class)); + dict.__setitem__("enumerate", PyType.fromClass(PyEnumerate.class)); + dict.__setitem__("float", PyType.fromClass(PyFloat.class)); + dict.__setitem__("long", PyType.fromClass(PyLong.class)); + dict.__setitem__("complex", PyType.fromClass(PyComplex.class)); + dict.__setitem__("dict", PyType.fromClass(PyDictionary.class)); + dict.__setitem__("list", PyType.fromClass(PyList.class)); + dict.__setitem__("tuple", PyType.fromClass(PyTuple.class)); + dict.__setitem__("set", PyType.fromClass(PySet.class)); + dict.__setitem__("frozenset", PyType.fromClass(PyImmutableSet.class)); + + dict.__setitem__("property", PyType.fromClass(PyProperty.class)); + dict.__setitem__("staticmethod", PyType.fromClass(PyStaticMethod.class)); + dict.__setitem__("classmethod", PyType.fromClass(PyClassMethod.class)); + dict.__setitem__("super", PyType.fromClass(PySuper.class)); + dict.__setitem__("str", PyType.fromClass(PyString.class)); + dict.__setitem__("unicode", PyType.fromClass(PyUnicode.class)); + dict.__setitem__("basestring", PyType.fromClass(PyBaseString.class)); + dict.__setitem__("file", PyType.fromClass(PyFile.class)); + dict.__setitem__("open", PyType.fromClass(PyFile.class)); + dict.__setitem__("slice",PyType.fromClass(PySlice.class)); + + /* - */ dict.__setitem__("None", Py.None); dict.__setitem__("NotImplemented", Py.NotImplemented); dict.__setitem__("Ellipsis", Py.Ellipsis); dict.__setitem__("True", Py.True); dict.__setitem__("False", Py.False); - - // Work in debug mode by default - // Hopefully add -O option in the future to change this - dict.__setitem__("__debug__", Py.One); - - dict.__setitem__("abs", new BuiltinFunctions("abs", 7, 1)); - dict.__setitem__("apply", new BuiltinFunctions("apply", 9, 2, 3)); - dict.__setitem__("callable", new BuiltinFunctions("callable", 14, 1)); - dict.__setitem__("coerce", new BuiltinFunctions("coerce", 13, 2)); - dict.__setitem__("chr", new BuiltinFunctions("chr", 0, 1)); - dict.__setitem__("cmp", new BuiltinFunctions("cmp", 6, 2)); - dict.__setitem__("globals", new BuiltinFunctions("globals", 4, 0)); - dict.__setitem__("hash", new BuiltinFunctions("hash", 5, 1)); - dict.__setitem__("id", new BuiltinFunctions("id", 11, 1)); - dict.__setitem__("isinstance", new BuiltinFunctions("isinstance", 10, 2)); - dict.__setitem__("len", new BuiltinFunctions("len", 1, 1)); - dict.__setitem__("ord", new BuiltinFunctions("ord", 3, 1)); - dict.__setitem__("range", new BuiltinFunctions("range", 2, 1, 3)); - dict.__setitem__("sum", new BuiltinFunctions("sum", 12, 1, 2)); - dict.__setitem__("unichr", new BuiltinFunctions("unichr", 6, 1)); - dict.__setitem__("compile", new BuiltinFunctions("compile", 44, 3, -1)); - dict.__setitem__("delattr", new BuiltinFunctions("delattr", 15, 2)); - dict.__setitem__("dir", new BuiltinFunctions("dir", 16, 0, 1)); - dict.__setitem__("divmod", new BuiltinFunctions("divmod", 17, 2)); - dict.__setitem__("eval", new BuiltinFunctions("eval", 18, 1, 3)); - dict.__setitem__("execfile", new BuiltinFunctions("execfile", 19, 1, 3)); - dict.__setitem__("filter", new BuiltinFunctions("filter", 20, 2)); - dict.__setitem__("getattr", new BuiltinFunctions("getattr", 21, 2, 3)); - dict.__setitem__("hasattr", new BuiltinFunctions("hasattr", 22, 2)); - dict.__setitem__("hex", new BuiltinFunctions("hex", 23, 1)); - dict.__setitem__("input", new BuiltinFunctions("input", 24, 0, 1)); - dict.__setitem__("intern", new BuiltinFunctions("intern", 25, 1)); - dict.__setitem__("issubclass", new BuiltinFunctions("issubclass", 26, 2)); - 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", new BuiltinFunctions("max", 30, 1, -1)); - dict.__setitem__("min", new BuiltinFunctions("min", 31, 1, -1)); - 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", new BuiltinFunctions("round", 38, 1, 2)); - dict.__setitem__("setattr", new BuiltinFunctions("setattr", 39, 3)); - dict.__setitem__("slice", new BuiltinFunctions("slice", 40, 1, 3)); - dict.__setitem__("vars", new BuiltinFunctions("vars", 41, 0, 1)); - dict.__setitem__("xrange", new BuiltinFunctions("xrange", 42, 1, 3)); - dict.__setitem__("zip", new BuiltinFunctions("zip", 43, 1, -1)); - dict.__setitem__("reversed", new BuiltinFunctions("reversed", 45, 1)); - dict.__setitem__("__import__", new ImportFunction()); - } + + // Work in debug mode by default + // Hopefully add -O option in the future to change this + dict.__setitem__("__debug__", Py.One); + + dict.__setitem__("abs", new BuiltinFunctions("abs", 7, 1)); + dict.__setitem__("apply", new BuiltinFunctions("apply", 9, 2, 3)); + dict.__setitem__("callable", new BuiltinFunctions("callable", 14, 1)); + dict.__setitem__("coerce", new BuiltinFunctions("coerce", 13, 2)); + dict.__setitem__("chr", new BuiltinFunctions("chr", 0, 1)); + dict.__setitem__("cmp", new BuiltinFunctions("cmp", 6, 2)); + dict.__setitem__("globals", new BuiltinFunctions("globals", 4, 0)); + dict.__setitem__("hash", new BuiltinFunctions("hash", 5, 1)); + dict.__setitem__("id", new BuiltinFunctions("id", 11, 1)); + dict.__setitem__("isinstance", new BuiltinFunctions("isinstance", 10, 2)); + dict.__setitem__("len", new BuiltinFunctions("len", 1, 1)); + dict.__setitem__("ord", new BuiltinFunctions("ord", 3, 1)); + dict.__setitem__("range", new BuiltinFunctions("range", 2, 1, 3)); + dict.__setitem__("sum", new BuiltinFunctions("sum", 12, 1, 2)); + dict.__setitem__("unichr", new BuiltinFunctions("unichr", 6, 1)); + dict.__setitem__("compile", new BuiltinFunctions("compile", 44, 3, -1)); + dict.__setitem__("delattr", new BuiltinFunctions("delattr", 15, 2)); + dict.__setitem__("dir", new BuiltinFunctions("dir", 16, 0, 1)); + dict.__setitem__("divmod", new BuiltinFunctions("divmod", 17, 2)); + dict.__setitem__("eval", new BuiltinFunctions("eval", 18, 1, 3)); + dict.__setitem__("execfile", new BuiltinFunctions("execfile", 19, 1, 3)); + dict.__setitem__("filter", new BuiltinFunctions("filter", 20, 2)); + dict.__setitem__("getattr", new BuiltinFunctions("getattr", 21, 2, 3)); + dict.__setitem__("hasattr", new BuiltinFunctions("hasattr", 22, 2)); + dict.__setitem__("hex", new BuiltinFunctions("hex", 23, 1)); + dict.__setitem__("input", new BuiltinFunctions("input", 24, 0, 1)); + dict.__setitem__("intern", new BuiltinFunctions("intern", 25, 1)); + dict.__setitem__("issubclass", new BuiltinFunctions("issubclass", 26, 2)); + 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", new BuiltinFunctions("max", 30, 1, -1)); + dict.__setitem__("min", new BuiltinFunctions("min", 31, 1, -1)); + 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", new BuiltinFunctions("round", 38, 1, 2)); + dict.__setitem__("setattr", new BuiltinFunctions("setattr", 39, 3)); + dict.__setitem__("vars", new BuiltinFunctions("vars", 41, 0, 1)); + dict.__setitem__("xrange", new BuiltinFunctions("xrange", 42, 1, 3)); + dict.__setitem__("zip", new BuiltinFunctions("zip", 43, 1, -1)); + dict.__setitem__("reversed", new BuiltinFunctions("reversed", 45, 1)); + dict.__setitem__("__import__", new ImportFunction()); + } - public static PyObject abs(PyObject o) { - if (o.isNumberType()) { - return o.__abs__(); - } - throw Py.TypeError("bad operand type for abs()"); - } + public static PyObject abs(PyObject o) { + if (o.isNumberType()) { + return o.__abs__(); + } + throw Py.TypeError("bad operand type for abs()"); + } - public static PyObject apply(PyObject o, PyObject args) { - return o.__call__(Py.make_array(args)); - } + public static PyObject apply(PyObject o, PyObject args) { + return o.__call__(Py.make_array(args)); + } - public static PyObject apply(PyObject o, PyObject args, PyDictionary kws) { - PyObject[] a; - String[] kw; - Hashtable table = kws.table; - if (table.size() > 0) { - java.util.Enumeration ek = table.keys(); - java.util.Enumeration ev = table.elements(); - int n = table.size(); - kw = new String[n]; - PyObject[] aargs = Py.make_array(args); - a = new PyObject[n + aargs.length]; - System.arraycopy(aargs, 0, a, 0, aargs.length); - int offset = aargs.length; + public static PyObject apply(PyObject o, PyObject args, PyDictionary kws) { + PyObject[] a; + String[] kw; + Hashtable table = kws.table; + if (table.size() > 0) { + java.util.Enumeration ek = table.keys(); + java.util.Enumeration ev = table.elements(); + int n = table.size(); + kw = new String[n]; + PyObject[] aargs = Py.make_array(args); + a = new PyObject[n + aargs.length]; + System.arraycopy(aargs, 0, a, 0, aargs.length); + int offset = aargs.length; - for (int i = 0; i < n; i++) { - kw[i] = ((PyString) ek.nextElement()).internedString(); - a[i + offset] = (PyObject) ev.nextElement(); - } - return o.__call__(a, kw); - } else { - return apply(o, args); - } - } - - public static boolean callable(PyObject o) { - return o.__findattr__("__call__") != null; - } + for (int i = 0; i < n; i++) { + kw[i] = ((PyString) ek.nextElement()).internedString(); + a[i + offset] = (PyObject) ev.nextElement(); + } + return o.__call__(a, kw); + } else { + return apply(o, args); + } + } + + public static boolean callable(PyObject o) { + return o.__findattr__("__call__") != null; + } - public static char unichr(int i) { - return chr(i); - } + public static char unichr(int i) { + return chr(i); + } - public static char chr(int i) { - if (i < 0 || i > 65535) { - throw Py.ValueError("chr() arg not in range(65535)"); - } - return (char) i; - } + public static char chr(int i) { + if (i < 0 || i > 65535) { + throw Py.ValueError("chr() arg not in range(65535)"); + } + return (char) i; + } - public static int cmp(PyObject x, PyObject y) { - return x._cmp(y); - } + public static int cmp(PyObject x, PyObject y) { + return x._cmp(y); + } - public static PyTuple coerce(PyObject o1, PyObject o2) { - PyObject[] result = o1._coerce(o2); - if (result != null) { - return new PyTuple(result); - } - throw Py.TypeError("number coercion failed"); - } + public static PyTuple coerce(PyObject o1, PyObject o2) { + PyObject[] result = o1._coerce(o2); + if (result != null) { + return new PyTuple(result); + } + throw Py.TypeError("number coercion failed"); + } - public static PyCode compile(String data, String filename, String type) { - return Py.compile_flags(data, filename, type, Py.getCompilerFlags()); - } + public static PyCode compile(String data, String filename, String type) { + return Py.compile_flags(data, filename, type, Py.getCompilerFlags()); + } - public static PyCode compile(String data, String filename, String type, int flags, boolean dont_inherit) { - if ((flags & ~PyTableCode.CO_ALL_FEATURES) != 0) { - throw Py.ValueError("compile(): unrecognised flags"); - } - return Py.compile_flags(data, filename, type, Py.getCompilerFlags(flags, dont_inherit)); - } + public static PyCode compile(String data, String filename, String type, int flags, boolean dont_inherit) { + if ((flags & ~PyTableCode.CO_ALL_FEATURES) != 0) { + throw Py.ValueError("compile(): unrecognised flags"); + } + return Py.compile_flags(data, filename, type, Py.getCompilerFlags(flags, dont_inherit)); + } - public static void delattr(PyObject o, String n) { - o.__delattr__(n); - } + public static void delattr(PyObject o, String n) { + o.__delattr__(n); + } - public static PyObject dir(PyObject o) { - PyList ret = (PyList) o.__dir__(); - ret.sort(); - return ret; - } + public static PyObject dir(PyObject o) { + PyList ret = (PyList) o.__dir__(); + ret.sort(); + return ret; + } - public static PyObject dir() { - PyObject l = locals(); - PyList ret; + public static PyObject dir() { + PyObject l = locals(); + PyList ret; - if (l instanceof PyStringMap) { - ret = ((PyStringMap) l).keys(); - } else if (l instanceof PyDictionary) { - ret = ((PyDictionary) l).keys(); - } + if (l instanceof PyStringMap) { + ret = ((PyStringMap) l).keys(); + } else if (l instanceof PyDictionary) { + ret = ((PyDictionary) l).keys(); + } - ret = (PyList) l.invoke("keys"); - ret.sort(); - return ret; - } + ret = (PyList) l.invoke("keys"); + ret.sort(); + return ret; + } - public static PyObject divmod(PyObject x, PyObject y) { - return x._divmod(y); - } + public static PyObject divmod(PyObject x, PyObject y) { + return x._divmod(y); + } - public static PyEnumerate enumerate(PyObject seq) { - return new PyEnumerate(seq); - } + public static PyEnumerate enumerate(PyObject seq) { + return new PyEnumerate(seq); + } - public static PyObject eval(PyObject o, PyObject globals, PyObject locals) { - PyCode code; - if (o instanceof PyCode) { - code = (PyCode) o; - } else { - if (o instanceof PyString) { - code = compile(o.toString(), "", "eval"); - } else { - throw Py.TypeError("eval: argument 1 must be string or code object"); - } - } - return Py.runCode(code, locals, globals); - } + public static PyObject eval(PyObject o, PyObject globals, PyObject locals) { + PyCode code; + if (o instanceof PyCode) { + code = (PyCode) o; + } else { + if (o instanceof PyString) { + code = compile(o.toString(), "", "eval"); + } else { + throw Py.TypeError("eval: argument 1 must be string or code object"); + } + } + return Py.runCode(code, locals, globals); + } - public static PyObject eval(PyObject o, PyObject globals) { - return eval(o, globals, globals); - } + public static PyObject eval(PyObject o, PyObject globals) { + return eval(o, globals, globals); + } - public static PyObject eval(PyObject o) { - if (o instanceof PyTableCode && ((PyTableCode) o).hasFreevars()) { - throw Py.TypeError("code object passed to eval() may not contain free variables"); - } - return eval(o, null, null); - } + public static PyObject eval(PyObject o) { + if (o instanceof PyTableCode && ((PyTableCode) o).hasFreevars()) { + throw Py.TypeError("code object passed to eval() may not contain free variables"); + } + return eval(o, null, null); + } - public static void execfile(String name, PyObject globals, PyObject locals) { - execfile_flags(name, globals, locals, Py.getCompilerFlags()); - } + public static void execfile(String name, PyObject globals, PyObject locals) { + execfile_flags(name, globals, locals, Py.getCompilerFlags()); + } - public static void execfile_flags(String name, PyObject globals, PyObject locals, CompilerFlags cflags) { - java.io.FileInputStream file; - try { - file = new java.io.FileInputStream(name); - } catch (java.io.FileNotFoundException e) { - throw Py.IOError(e); - } - PyCode code; + public static void execfile_flags(String name, PyObject globals, PyObject locals, CompilerFlags cflags) { + java.io.FileInputStream file; + try { + file = new java.io.FileInputStream(name); + } catch (java.io.FileNotFoundException e) { + throw Py.IOError(e); + } + PyCode code; - try { - code = Py.compile_flags(file, name, "exec", cflags); - } finally { - try { - file.close(); - } catch (java.io.IOException e) { - throw Py.IOError(e); - } - } - Py.runCode(code, locals, globals); - } + try { + code = Py.compile_flags(file, name, "exec", cflags); + } finally { + try { + file.close(); + } catch (java.io.IOException e) { + throw Py.IOError(e); + } + } + Py.runCode(code, locals, globals); + } - public static void execfile(String name, PyObject globals) { - execfile(name, globals, globals); - } + public static void execfile(String name, PyObject globals) { + execfile(name, globals, globals); + } - public static void execfile(String name) { - execfile(name, null, null); - } + public static void execfile(String name) { + execfile(name, null, null); + } - public static PyObject filter(PyObject f, PyString s) { - if (f == Py.None) { - return s; - } - PyObject[] args = new PyObject[1]; - char[] chars = s.toString().toCharArray(); - int i; - int j; - int n = chars.length; - for (i = 0, j = 0; i < n; i++) { - args[0] = Py.makeCharacter(chars[i]); - if (!f.__call__(args).__nonzero__()) { - continue; - } - chars[j++] = chars[i]; - } - return new PyString(new String(chars, 0, j)); - } + public static PyObject filter(PyObject f, PyString s) { + if (f == Py.None) { + return s; + } + PyObject[] args = new PyObject[1]; + char[] chars = s.toString().toCharArray(); + int i; + int j; + int n = chars.length; + for (i = 0, j = 0; i < n; i++) { + args[0] = Py.makeCharacter(chars[i]); + if (!f.__call__(args).__nonzero__()) { + continue; + } + chars[j++] = chars[i]; + } + return new PyString(new String(chars, 0, j)); + } - public static PyObject filter(PyObject f, PyObject l) { - if (l instanceof PyString) { - return filter(f, (PyString) l); - } - PyList list = new PyList(); - PyObject iter = l.__iter__(); - for (PyObject item = null; (item = iter.__iternext__()) != null;) { - if (f == Py.None) { - if (!item.__nonzero__()) { - continue; - } - } else if (!f.__call__(item).__nonzero__()) { - continue; - } - list.append(item); - } - if (l instanceof PyTuple) { - return tuple(list); - } - return list; - } + public static PyObject filter(PyObject f, PyObject l) { + if (l instanceof PyString) { + return filter(f, (PyString) l); + } + PyList list = new PyList(); + PyObject iter = l.__iter__(); + for (PyObject item = null; (item = iter.__iternext__()) != null;) { + if (f == Py.None) { + if (!item.__nonzero__()) { + continue; + } + } else if (!f.__call__(item).__nonzero__()) { + continue; + } + list.append(item); + } + if (l instanceof PyTuple) { + return tuple(list); + } + return list; + } - public static PyObject getattr(PyObject o, String n) { - return o.__getattr__(n); - } + public static PyObject getattr(PyObject o, String n) { + return o.__getattr__(n); + } - public static PyObject getattr(PyObject o, String n, PyObject def) { - PyObject val = o.__findattr__(n); - if (val != null) { - return val; - } - return def; - } + public static PyObject getattr(PyObject o, String n, PyObject def) { + PyObject val = o.__findattr__(n); + if (val != null) { + return val; + } + return def; + } - public static PyObject globals() { - return Py.getFrame().f_globals; - } + public static PyObject globals() { + return Py.getFrame().f_globals; + } - public static boolean hasattr(PyObject o, String n) { - try { - return o.__findattr__(n) != null; - } catch (PyException exc) { - if (Py.matchException(exc, Py.AttributeError)) { - return false; - } - throw exc; - } - } + public static boolean hasattr(PyObject o, String n) { + try { + return o.__findattr__(n) != null; + } catch (PyException exc) { + if (Py.matchException(exc, Py.AttributeError)) { + return false; + } + throw exc; + } + } - public static PyInteger hash(PyObject o) { - return o.__hash__(); - } + public static PyInteger hash(PyObject o) { + return o.__hash__(); + } - public static PyString hex(PyObject o) { - try { - return o.__hex__(); - } catch (PyException e) { - if (Py.matchException(e, Py.AttributeError)) - throw Py.TypeError("hex() argument can't be converted to hex"); - throw e; - } + public static PyString hex(PyObject o) { + try { + return o.__hex__(); + } catch (PyException e) { + if (Py.matchException(e, Py.AttributeError)) + throw Py.TypeError("hex() argument can't be converted to hex"); + throw e; + } } - public static long id(PyObject o) { - return Py.id(o); - } + public static long id(PyObject o) { + return Py.id(o); + } - public static PyObject input(PyObject prompt) { - String line = raw_input(prompt); - return eval(new PyString(line)); - } + public static PyObject input(PyObject prompt) { + String line = raw_input(prompt); + return eval(new PyString(line)); + } - public static PyObject input() { - return input(new PyString("")); - } + public static PyObject input() { + return input(new PyString("")); + } - private static PyStringMap internedStrings; + private static PyStringMap internedStrings; - public static PyString intern(PyString s) { - if (internedStrings == null) { - internedStrings = new PyStringMap(); - } + public static PyString intern(PyString s) { + if (internedStrings == null) { + internedStrings = new PyStringMap(); + } - String istring = s.internedString(); - PyObject ret = internedStrings.__finditem__(istring); - if (ret != null) { - return (PyString) ret; - } - if (s instanceof PyStringDerived) { - s = s.__str__(); - } - internedStrings.__setitem__(istring, s); - return s; - } + String istring = s.internedString(); + PyObject ret = internedStrings.__finditem__(istring); + if (ret != null) { + return (PyString) ret; + } + if (s instanceof PyStringDerived) { + s = s.__str__(); + } + internedStrings.__setitem__(istring, s); + return s; + } - // xxx find where used, modify with more appropriate if necessary - public static boolean isinstance(PyObject obj, PyObject cls) { - return Py.isInstance(obj, cls); - } + // xxx find where used, modify with more appropriate if necessary + public static boolean isinstance(PyObject obj, PyObject cls) { + return Py.isInstance(obj, cls); + } - // xxx find where used, modify with more appropriate if necessary - public static boolean issubclass(PyObject derived, PyObject cls) { - return Py.isSubClass(derived, cls); - } + // xxx find where used, modify with more appropriate if necessary + public static boolean issubclass(PyObject derived, PyObject cls) { + return Py.isSubClass(derived, cls); + } - public static PyObject iter(PyObject obj) { - return obj.__iter__(); - } + public static PyObject iter(PyObject obj) { + return obj.__iter__(); + } - public static PyObject iter(PyObject callable, PyObject sentinel) { - return new PyCallIter(callable, sentinel); - } + public static PyObject iter(PyObject callable, PyObject sentinel) { + return new PyCallIter(callable, sentinel); + } - public static int len(PyObject o) { - try { - return o.__len__(); - } catch (PyException e) { - // Make this work like CPython where - // - // a = 7; len(a) raises a TypeError, - // a.__len__() raises an AttributeError - // and - // class F: pass - // f = F(); len(f) also raises an AttributeError - // - // Testing the type of o feels unclean though - if (e.type == Py.AttributeError && !(o instanceof PyInstance)) { - throw Py.TypeError("len() of unsized object"); - } - throw e; - } - } + public static int len(PyObject o) { + try { + return o.__len__(); + } catch (PyException e) { + // Make this work like CPython where + // + // a = 7; len(a) raises a TypeError, + // a.__len__() raises an AttributeError + // and + // class F: pass + // f = F(); len(f) also raises an AttributeError + // + // Testing the type of o feels unclean though + if (e.type == Py.AttributeError && !(o instanceof PyInstance)) { + throw Py.TypeError("len() of unsized object"); + } + throw e; + } + } - public static PyObject locals() { - return Py.getFrame().getf_locals(); - } + public static PyObject locals() { + return Py.getFrame().getf_locals(); + } - public static PyObject map(PyObject[] argstar) { - int n = argstar.length - 1; - if (n < 1) { - throw Py.TypeError("map requires at least two arguments"); - } - PyObject element; - PyObject f = argstar[0]; - PyList list = new PyList(); - PyObject[] args = new PyObject[n]; - PyObject[] iters = new PyObject[n]; + public static PyObject map(PyObject[] argstar) { + int n = argstar.length - 1; + if (n < 1) { + throw Py.TypeError("map requires at least two arguments"); + } + PyObject element; + PyObject f = argstar[0]; + PyList list = new PyList(); + PyObject[] args = new PyObject[n]; + PyObject[] iters = new PyObject[n]; - for (int j = 0; j < n; j++) { - iters[j] = Py.iter(argstar[j + 1], "argument " + (j + 1) + " to map() must support iteration"); - } + for (int j = 0; j < n; j++) { + iters[j] = Py.iter(argstar[j + 1], "argument " + (j + 1) + " to map() must support iteration"); + } - while (true) { - boolean any_items = false; - for (int j = 0; j < n; j++) { - if ((element = iters[j].__iternext__()) != null) { - args[j] = element; - any_items = true; - } else { - args[j] = Py.None; - } - } - if (!any_items) { - break; - } - if (f == Py.None) { - if (n == 1) { - list.append(args[0]); - } else { - list.append(new PyTuple(args.clone())); - } - } else { - list.append(f.__call__(args)); - } - } - return list; - } + while (true) { + boolean any_items = false; + for (int j = 0; j < n; j++) { + if ((element = iters[j].__iternext__()) != null) { + args[j] = element; + any_items = true; + } else { + args[j] = Py.None; + } + } + if (!any_items) { + break; + } + if (f == Py.None) { + if (n == 1) { + list.append(args[0]); + } else { + list.append(new PyTuple(args.clone())); + } + } else { + list.append(f.__call__(args)); + } + } + return list; + } - public static PyObject max(PyObject[] l) { - if (l.length == 1) { - return max(l[0]); - } - return max(new PyTuple(l)); - } + public static PyObject max(PyObject[] l) { + if (l.length == 1) { + return max(l[0]); + } + return max(new PyTuple(l)); + } - private static PyObject max(PyObject o) { - PyObject max = null; - PyObject iter = o.__iter__(); - for (PyObject item; (item = iter.__iternext__()) != null;) { - if (max == null || item._gt(max).__nonzero__()) { - max = item; - } - } - if (max == null) { - throw Py.ValueError("max of empty sequence"); - } - return max; - } + private static PyObject max(PyObject o) { + PyObject max = null; + PyObject iter = o.__iter__(); + for (PyObject item; (item = iter.__iternext__()) != null;) { + if (max == null || item._gt(max).__nonzero__()) { + max = item; + } + } + if (max == null) { + throw Py.ValueError("max of empty sequence"); + } + return max; + } public static PyObject min(PyObject[] l) { if (l.length == 0) { throw Py.TypeError("min expected 1 arguments, got 0"); @@ -783,19 +777,19 @@ return min(new PyTuple(l)); } - private static PyObject min(PyObject o) { - PyObject min = null; - PyObject iter = o.__iter__(); - for (PyObject item; (item = iter.__iternext__()) != null;) { - if (min == null || item._lt(min).__nonzero__()) { - min = item; - } - } - if (min == null) { - throw Py.ValueError("min of empty sequence"); - } - return min; - } + private static PyObject min(PyObject o) { + PyObject min = null; + PyObject iter = o.__iter__(); + for (PyObject item; (item = iter.__iternext__()) != null;) { + if (min == null || item._lt(min).__nonzero__()) { + min = item; + } + } + if (min == null) { + throw Py.ValueError("min of empty sequence"); + } + return min; + } public static PyString oct(PyObject o) { try { @@ -808,81 +802,81 @@ } } - public static final int ord(char c) { - return c; - } + public static final int ord(char c) { + return c; + } - public static PyObject pow(PyObject x, PyObject y) { - return x._pow(y); - } + public static PyObject pow(PyObject x, PyObject y) { + return x._pow(y); + } - private static boolean coerce(PyObject[] objs) { - PyObject x = objs[0]; - PyObject y = objs[1]; - PyObject[] result; - result = x._coerce(y); - if (result != null) { - objs[0] = result[0]; - objs[1] = result[1]; - return true; - } - result = y._coerce(x); - if (result != null) { - objs[0] = result[1]; - objs[1] = result[0]; - return true; - } - return false; - } + private static boolean coerce(PyObject[] objs) { + PyObject x = objs[0]; + PyObject y = objs[1]; + PyObject[] result; + result = x._coerce(y); + if (result != null) { + objs[0] = result[0]; + objs[1] = result[1]; + return true; + } + result = y._coerce(x); + if (result != null) { + objs[0] = result[1]; + objs[1] = result[0]; + return true; + } + return false; + } - public static PyObject pow(PyObject xi, PyObject yi, PyObject zi) { - PyObject x = xi; - PyObject y = yi; - PyObject z = zi; + public static PyObject pow(PyObject xi, PyObject yi, PyObject zi) { + PyObject x = xi; + PyObject y = yi; + PyObject z = zi; - PyObject[] tmp = new PyObject[2]; + PyObject[] tmp = new PyObject[2]; - tmp[0] = x; - tmp[1] = y; - if (coerce(tmp)) { - x = tmp[0]; - y = tmp[1]; - tmp[1] = z; - if (coerce(tmp)) { - x = tmp[0]; - z = tmp[1]; - tmp[0] = y; - if (coerce(tmp)) { - z = tmp[1]; - y = tmp[0]; - } - } - } else { - tmp[1] = z; - if (coerce(tmp)) { - x = tmp[0]; - z = tmp[1]; - tmp[0] = y; - if (coerce(tmp)) { - y = tmp[0]; - z = tmp[1]; - tmp[1] = x; - if (coerce(tmp)) { - x = tmp[1]; - y = tmp[0]; - } - } - } - } + tmp[0] = x; + tmp[1] = y; + if (coerce(tmp)) { + x = tmp[0]; + y = tmp[1]; + tmp[1] = z; + if (coerce(tmp)) { + x = tmp[0]; + z = tmp[1]; + tmp[0] = y; + if (coerce(tmp)) { + z = tmp[1]; + y = tmp[0]; + } + } + } else { + tmp[1] = z; + if (coerce(tmp)) { + x = tmp[0]; + z = tmp[1]; + tmp[0] = y; + if (coerce(tmp)) { + y = tmp[0]; + z = tmp[1]; + tmp[1] = x; + if (coerce(tmp)) { + x = tmp[1]; + y = tmp[0]; + } + } + } + } - if (x.getType() == y.getType() && x.getType() == z.getType()) { - x = x.__pow__(y, z); - if (x != null) { - return x; - } - } - throw Py.TypeError("__pow__ not defined for these operands"); - } + if (x.getType() == y.getType() && x.getType() == z.getType()) { + x = x.__pow__(y, z); + if (x != null) { + return x; + } + } + throw Py.TypeError("__pow__ not defined for these operands"); + } public static PyObject range(PyObject start, PyObject stop, PyObject step) { // Check that step is valid. @@ -930,161 +924,150 @@ return new PyList(objs); } - public static PyObject range(PyObject n) { - return range(Py.Zero, n, Py.One); - } + public static PyObject range(PyObject n) { + return range(Py.Zero, n, Py.One); + } - public static PyObject range(PyObject start, PyObject stop) { - return range(start, stop, Py.One); - } + public static PyObject range(PyObject start, PyObject stop) { + return range(start, stop, Py.One); + } - private static PyString readline(PyObject file) { - if (file instanceof PyFile) { - return new PyString(((PyFile) file).readline()); - } else { - PyObject ret = file.invoke("readline"); - if (!(ret instanceof PyString)) { - throw Py.TypeError("object.readline() returned non-string"); - } - return (PyString) ret; - } - } + private static PyString readline(PyObject file) { + if (file instanceof PyFile) { + return new PyString(((PyFile) file).readline()); + } else { + PyObject ret = file.invoke("readline"); + if (!(ret instanceof PyString)) { + throw Py.TypeError("object.readline() returned non-string"); + } + return (PyString) ret; + } + } - public static String raw_input(PyObject prompt) { - Py.print(prompt); - PyObject stdin = Py.getSystemState().stdin; - String data = readline(stdin).toString(); - if (data.endsWith("\n")) { - return data.substring(0, data.length() - 1); - } else { - if (data.length() == 0) { - throw Py.EOFError("raw_input()"); - } - } - return data; - } + public static String raw_input(PyObject prompt) { + Py.print(prompt); + PyObject stdin = Py.getSystemState().stdin; + String data = readline(stdin).toString(); + if (data.endsWith("\n")) { + return data.substring(0, data.length() - 1); + } else { + if (data.length() == 0) { + throw Py.EOFError("raw_input()"); + } + } + return data; + } - public static String raw_input() { - return raw_input(new PyString("")); - } + public static String raw_input() { + return raw_input(new PyString("")); + } - public static PyObject reduce(PyObject f, PyObject l, PyObject z) { - PyObject result = z; - PyObject iter = Py.iter(l, "reduce() arg 2 must support iteration"); + public static PyObject reduce(PyObject f, PyObject l, PyObject z) { + PyObject result = z; + PyObject iter = Py.iter(l, "reduce() arg 2 must support iteration"); - for (PyObject item; (item = iter.__iternext__()) != null;) { - if (result == null) { - result = item; - } else { - result = f.__call__(result, item); - } - } - if (result == null) { - throw Py.TypeError("reduce of empty sequence with no initial value"); - } - return result; - } + for (PyObject item; (item = iter.__iternext__()) != null;) { + if (result == null) { + result = item; + } else { + result = f.__call__(result, item); + } + } + if (result == null) { + throw Py.TypeError("reduce of empty sequence with no initial value"); + } + return result; + } - public static PyObject reduce(PyObject f, PyObject l) { - return reduce(f, l, null); - } + public static PyObject reduce(PyObject f, PyObject l) { + return reduce(f, l, null); + } - public static PyObject reload(PyModule o) { - return imp.reload(o); - } + public static PyObject reload(PyModule o) { + return imp.reload(o); + } - public static PyObject reload(PyJavaClass o) { - return imp.reload(o); - } + public static PyObject reload(PyJavaClass o) { + return imp.reload(o); + } - public static PyString repr(PyObject o) { - return o.__repr__(); - } + public static PyString repr(PyObject o) { + return o.__repr__(); + } - // This seems awfully special purpose... - public static PyFloat round(double f, int digits) { - boolean neg = f < 0; - double multiple = Math.pow(10., digits); - if (neg) { - f = -f; - } - double tmp = Math.floor(f * multiple + 0.5); - if (neg) { - tmp = -tmp; - } - return new PyFloat(tmp / multiple); - } + // This seems awfully special purpose... + public static PyFloat round(double f, int digits) { + boolean neg = f < 0; + double multiple = Math.pow(10., digits); + if (neg) { + f = -f; + } + double tmp = Math.floor(f * multiple + 0.5); + if (neg) { + tmp = -tmp; + } + return new PyFloat(tmp / multiple); + } - public static PyFloat round(double f) { - return round(f, 0); - } + public static PyFloat round(double f) { + return round(f, 0); + } - public static void setattr(PyObject o, String n, PyObject v) { - o.__setattr__(n, v); - } + public static void setattr(PyObject o, String n, PyObject v) { + o.__setattr__(n, v); + } - public static PySlice slice(PyObject start, PyObject stop, PyObject step) { - return new PySlice(start, stop, step); - } - public static PySlice slice(PyObject start, PyObject stop) { - return slice(start, stop, Py.None); - } + public static PyObject sum(PyObject seq, PyObject result) { - public static PySlice slice(PyObject stop) { - return slice(Py.None, stop, Py.None); - } + if (result instanceof PyString) { + throw Py.TypeError("sum() can't sum strings [use ''.join(seq) instead]"); + } - public static PyObject sum(PyObject seq, PyObject result) { - - if (result instanceof PyString) { - throw Py.TypeError("sum() can't sum strings [use ''.join(seq) instead]"); - } - - PyObject item; - PyObject iter = seq.__iter__(); - while ((item = iter.__iternext__()) != null) { - result = result._add(item); - } - return result; - } - - public static PyObject reversed(PyObject seq) { + PyObject item; + PyObject iter = seq.__iter__(); + while ((item = iter.__iternext__()) != null) { + result = result._add(item); + } + return result; + } + + public static PyObject reversed(PyObject seq) { if(hasattr(seq, "__getitem__") && hasattr(seq, "__len__") && !hasattr(seq, "keys")) { return new PyReversedIterator(seq); } else { throw Py.TypeError("argument to reversed() must be a sequence"); } - } - - public static PyObject sum(PyObject seq) { - return sum(seq, Py.Zero); - } + } + + public static PyObject sum(PyObject seq) { + return sum(seq, Py.Zero); + } - public static PyTuple tuple(PyObject o) { - if (o instanceof PyTuple) { - return (PyTuple) o; - } - if (o instanceof PyList) { - // always make a copy, otherwise the tuple will share the - // underlying data structure with the list object, which - // renders the tuple mutable! - PyList l = (PyList) o; - PyObject[] a = new PyObject[l.size()]; - System.arraycopy(l.getArray(), 0, a, 0, a.length); - return new PyTuple(a); - } - return new PyTuple(Py.make_array(o)); - } + public static PyTuple tuple(PyObject o) { + if (o instanceof PyTuple) { + return (PyTuple) o; + } + if (o instanceof PyList) { + // always make a copy, otherwise the tuple will share the + // underlying data structure with the list object, which + // renders the tuple mutable! + PyList l = (PyList) o; + PyObject[] a = new PyObject[l.size()]; + System.arraycopy(l.getArray(), 0, a, 0, a.length); + return new PyTuple(a); + } + return new PyTuple(Py.make_array(o)); + } - public static PyType type(PyObject o) { - return o.getType(); - } + public static PyType type(PyObject o) { + return o.getType(); + } - public static PyObject vars() { - return locals(); - } + public static PyObject vars() { + return locals(); + } public static PyObject vars(PyObject o) { try { @@ -1096,125 +1079,125 @@ } } - public static PyObject xrange(int start, int stop, int step) { - return new PyXRange(start, stop, step); - } + public static PyObject xrange(int start, int stop, int step) { + return new PyXRange(start, stop, step); + } - public static PyObject xrange(int n) { - return xrange(0, n, 1); - } + public static PyObject xrange(int n) { + return xrange(0, n, 1); + } - public static PyObject xrange(int start, int stop) { - return xrange(start, stop, 1); - } + public static PyObject xrange(int start, int stop) { + return xrange(start, stop, 1); + } - public static PyString __doc__zip = new PyString("zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n" - + "\n" + "Return a list of tuples, where each tuple contains the i-th element\n" - + "from each of the argument sequences. The returned list is\n" - + "truncated in length to the length of the shortest argument sequence."); + public static PyString __doc__zip = new PyString("zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n" + + "\n" + "Return a list of tuples, where each tuple contains the i-th element\n" + + "from each of the argument sequences. The returned list is\n" + + "truncated in length to the length of the shortest argument sequence."); - public static PyObject zip(PyObject[] argstar) { - int itemsize = argstar.length; - if (itemsize < 1) { - throw Py.TypeError("zip requires at least one sequence"); - } + public static PyObject zip(PyObject[] argstar) { + int itemsize = argstar.length; + if (itemsize < 1) { + throw Py.TypeError("zip requires at least one sequence"); + } - // Type check the arguments; they must be sequences. Might as well - // cache the __iter__() methods. - PyObject[] iters = new PyObject[itemsize]; + // Type check the arguments; they must be sequences. Might as well + // cache the __iter__() methods. + PyObject[] iters = new PyObject[itemsize]; - for (int j = 0; j < itemsize; j++) { - PyObject iter = argstar[j].__iter__(); - if (iter == null) { - throw Py.TypeError("zip argument #" + (j + 1) + " must support iteration"); - } - iters[j] = iter; - } + for (int j = 0; j < itemsize; j++) { + PyObject iter = argstar[j].__iter__(); + if (iter == null) { + throw Py.TypeError("zip argument #" + (j + 1) + " must support iteration"); + } + iters[j] = iter; + } - PyList ret = new PyList(); + PyList ret = new PyList(); - for (int i = 0;; i++) { - PyObject[] next = new PyObject[itemsize]; - PyObject item; + for (int i = 0;; i++) { + PyObject[] next = new PyObject[itemsize]; + PyObject item; - for (int j = 0; j < itemsize; j++) { - try { - item = iters[j].__iternext__(); - } catch (PyException e) { - if (Py.matchException(e, Py.StopIteration)) { - return ret; - } - throw e; - } - if (item == null) { - return ret; - } - next[j] = item; - } - ret.append(new PyTuple(next)); - } - } + for (int j = 0; j < itemsize; j++) { + try { + item = iters[j].__iternext__(); + } catch (PyException e) { + if (Py.matchException(e, Py.StopIteration)) { + return ret; + } + throw e; + } + if (item == null) { + return ret; + } + next[j] = item; + } + ret.append(new PyTuple(next)); + } + } - public static PyObject __import__(String name) { - return __import__(name, null, null, null); - } + public static PyObject __import__(String name) { + return __import__(name, null, null, null); + } - public static PyObject __import__(String name, PyObject globals) { - return __import__(name, globals, null, null); - } + public static PyObject __import__(String name, PyObject globals) { + return __import__(name, globals, null, null); + } - public static PyObject __import__(String name, PyObject globals, PyObject locals) { - return __import__(name, globals, locals, null); - } + public static PyObject __import__(String name, PyObject globals, PyObject locals) { + return __import__(name, globals, locals, null); + } - public static PyObject __import__(String name, PyObject globals, PyObject locals, PyObject fromlist) { - PyFrame frame = Py.getFrame(); - if (frame == null) { - return null; - } - PyObject builtins = frame.f_builtins; - if (builtins == null) { - builtins = PySystemState.builtins; - } + public static PyObject __import__(String name, PyObject globals, PyObject locals, PyObject fromlist) { + PyFrame frame = Py.getFrame(); + if (frame == null) { + return null; + } + PyObject builtins = frame.f_builtins; + if (builtins == null) { + builtins = PySystemState.builtins; + } - PyObject __import__ = builtins.__finditem__("__import__"); - if (__import__ == null) { - return null; - } + PyObject __import__ = builtins.__finditem__("__import__"); + if (__import__ == null) { + return null; + } - PyObject module = __import__.__call__(new PyObject[] { Py.newString(name), globals, locals, fromlist }); - return module; - } + PyObject module = __import__.__call__(new PyObject[] { Py.newString(name), globals, locals, fromlist }); + return module; + } } class ImportFunction extends PyObject { - public ImportFunction() { - } + public ImportFunction() { + } - public PyObject __call__(PyObject args[], String keywords[]) { - if (!(args.length < 1 || args[0] instanceof PyString)) { - throw Py.TypeError("first argument must be a string"); - } - if (keywords.length > 0) { - throw Py.TypeError("__import__() takes no keyword arguments"); - } + public PyObject __call__(PyObject args[], String keywords[]) { + if (!(args.length < 1 || args[0] instanceof PyString)) { + throw Py.TypeError("first argument must be a string"); + } + if (keywords.length > 0) { + throw Py.TypeError("__import__() takes no keyword arguments"); + } - int argc = args.length; - String module = args[0].__str__().toString(); + int argc = args.length; + String module = args[0].__str__().toString(); - PyObject globals = (argc > 1 && args[1] != null) ? args[1] : null; - PyObject fromlist = (argc > 3 && args[3] != null) ? args[3] : Py.EmptyTuple; + PyObject globals = (argc > 1 && args[1] != null) ? args[1] : null; + PyObject fromlist = (argc > 3 && args[3] != null) ? args[3] : Py.EmptyTuple; - return load(module, globals, fromlist); - } + return load(module, globals, fromlist); + } - private PyObject load(String module, PyObject globals, PyObject fromlist) { - PyObject mod = imp.importName(module.intern(), fromlist.__len__() == 0, globals, fromlist); - return mod; - } + private PyObject load(String module, PyObject globals, PyObject fromlist) { + PyObject mod = imp.importName(module.intern(), fromlist.__len__() == 0, globals, fromlist); + return mod; + } - public String toString() { - return ""; - } + public String toString() { + return ""; + } } Index: src/templates/slice.expose =================================================================== --- src/templates/slice.expose (revision 0) +++ src/templates/slice.expose (revision 0) @@ -0,0 +1,17 @@ +# setup +type_name: slice +type_class: PySlice + +# getsets +expose_getset: start getStart +expose_getset: stop getStop +expose_getset: step getStep + +# exposed methods +expose_meth: indices o +expose_meth: __hash__ + throw Py.TypeError("unhashable type"); +expose_wide_meth: __init__ -1 -1 + `vdeleg`(init); + `void; +expose_new_mutable: Index: src/templates/slice.derived =================================================================== --- src/templates/slice.derived (revision 0) +++ src/templates/slice.derived (revision 0) @@ -0,0 +1,5 @@ +base_class: PySlice +want_dict: true +ctr: +incl: object + Index: src/org/python/core/PySlice.java =================================================================== --- src/org/python/core/PySlice.java (revision 3608) +++ src/org/python/core/PySlice.java (working copy) @@ -1,34 +1,289 @@ -// Copyright (c) Corporation for National Research Initiatives +//Copyright (c) Corporation for National Research Initiatives package org.python.core; - /** * A python slice object. */ public class PySlice extends PyObject { - public PyObject start, stop, step; + //~ BEGIN GENERATED REGION -- DO NOT EDIT SEE gexpose.py + /* type info */ + + public static final String exposed_name="slice"; + + public static void typeSetup(PyObject dict,PyType.Newstyle marker) { + dict.__setitem__("start",new PyGetSetDescr("start",PySlice.class,"getStart",null,null)); + dict.__setitem__("stop",new PyGetSetDescr("stop",PySlice.class,"getStop",null,null)); + dict.__setitem__("step",new PyGetSetDescr("step",PySlice.class,"getStep",null,null)); + class exposed_indices extends PyBuiltinMethodNarrow { + + exposed_indices(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed_indices(self,info); + } + + public PyObject __call__(PyObject arg0) { + return((PySlice)self).slice_indices(arg0); + } + + } + dict.__setitem__("indices",new PyMethodDescr("indices",PySlice.class,1,1,new exposed_indices(null,null))); + class exposed___hash__ extends PyBuiltinMethodNarrow { + + exposed___hash__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___hash__(self,info); + } + + public PyObject __call__() { + throw Py.TypeError("unhashable type"); + } + + } + dict.__setitem__("__hash__",new PyMethodDescr("__hash__",PySlice.class,0,0,new exposed___hash__(null,null))); + class exposed___init__ extends PyBuiltinMethod { + + exposed___init__(PyObject self,PyBuiltinFunction.Info info) { + super(self,info); + } + + public PyBuiltinFunction bind(PyObject self) { + return new exposed___init__(self,info); + } + + public PyObject __call__(PyObject[]args) { + return __call__(args,Py.NoKeywords); + } + + public PyObject __call__(PyObject[]args,String[]keywords) { + ((PySlice)self).slice_init(args,keywords); + return Py.None; + } + + } + dict.__setitem__("__init__",new PyMethodDescr("__init__",PySlice.class,-1,-1,new exposed___init__(null,null))); + dict.__setitem__("__new__",new PyNewWrapper(PySlice.class,"__new__",-1,-1) { + + public PyObject new_impl(boolean init,PyType subtype,PyObject[]args,String[]keywords) { + PySlice newobj; + if (for_type==subtype) { + newobj=new PySlice(); + if (init) + newobj.slice_init(args,keywords); + } else { + newobj=new PySliceDerived(subtype); + } + return newobj; + } + + }); + } + //~ END GENERATED REGION -- DO NOT EDIT SEE gexpose.py + + private static final PyType SLICE_TYPE = PyType.fromClass(PySlice.class); + + public PyObject start = Py.None; + public PyObject stop = Py.None; + public PyObject step = Py.None; + public PySlice(PyObject start, PyObject stop, PyObject step) { - if (start == null) start = Py.None; - if (stop == null) stop = Py.None; - if (step == null) step = Py.One; - + this(SLICE_TYPE); this.start = start; this.stop = stop; this.step = step; } + public PySlice(PyType type) { + super(type); + } + + public PySlice() { + this(SLICE_TYPE); + } + + public final PyObject getStart() { + return start; + } + + public final PyObject getStop() { + return stop; + } + + public final PyObject getStep() { + return step; + } + + public int hashCode() { + return slice_hashCode(); + } + + final int slice_hashCode() { + throw Py.TypeError("unhashable type"); + } + public PyString __str__() { - return new PyString(start.__repr__()+":"+stop.__repr__()+":"+ - step.__repr__()); + return new PyString(start.__repr__() + ":" + stop.__repr__() + ":" + + step.__repr__()); } public PyString __repr__() { - return new PyString("slice("+start.__repr__()+", "+ - stop.__repr__()+", "+ - step.__repr__()+")"); + return new PyString("slice(" + start.__repr__() + ", " + + stop.__repr__() + ", " + + step.__repr__() + ")"); } - public boolean isSequenceType() { return false; } + public synchronized PyObject __eq__(PyObject o) { + if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { + return null; + } + int i = cmp(this, o); + return (i == 0) ? Py.True : Py.False; + } + + public synchronized PyObject __ne__(PyObject o) { + if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) { + return null; + } + int i = cmp(this, o); + return (i != 0) ? Py.True : Py.False; + } + + protected static int cmp(PyObject o1, PyObject o2) { + PySlice s1 = (PySlice)o1; + PySlice s2 = (PySlice)o2; + + if (s1 == s2) { + return 0; + } + + int result = 0; + result = s1.getStart()._cmp(s2.getStart()); + if (result != 0) { + return result; + } + result = s1.getStop()._cmp(s2.getStop()); + if (result != 0) { + return result; + } + result = s1.getStep()._cmp(s2.getStep()); + if (result != 0) { + return result; + } + + return result; + } + + public int calculateSliceIndex(PyObject v){ + long x = 0; + + if (v != null) { + if (v instanceof PyInteger) { + x = ((PyInteger)v.__int__()).getValue(); + } + else if (v instanceof PyLong ) { + x = ((PyInteger)v.__int__()).getValue(); + } + else { + Py.TypeError("slice indices must be integers or None"); + return 0; + } + } + + if (x > Integer.MAX_VALUE) { + x = Integer.MAX_VALUE; + } else if(x < -Integer.MAX_VALUE) { + x = -Integer.MAX_VALUE; + } + + return new PyLong(x).getValue().intValue(); + } + + public PyObject slice_indices(PyObject len){ + int ilen, tmpStart, tmpStop, tmpStep, sliceLength, defStart, defStop; + + if (len instanceof PyInteger || len instanceof PyLong) { + ilen = ((PyInteger)len.__int__()).getValue(); + } else { + throw Py.TypeError("an integer is required"); + } + + if (this.step == Py.None) { + tmpStep = 1; + } else { + tmpStep = calculateSliceIndex(this.step); + if (tmpStep == 0) { + Py.ValueError("slice step cannot be zero"); + return null; + } + } + + defStart = tmpStep < 0 ? ilen - 1 : 0; + defStop = tmpStep < 0 ? -1 : ilen; + + if (this.start == Py.None) { + tmpStart = defStart; + } else { + tmpStart = calculateSliceIndex(this.start); + if(tmpStart == 0) { + return null; + + } + if (tmpStart < 0) tmpStart += ilen; + if (tmpStart < 0) tmpStart = (tmpStep < 0) ? -1 : 0; + if (tmpStart >= ilen) + tmpStart = (tmpStep < 0) ? ilen - 1 : ilen; + } + + if (this.stop == Py.None) { + tmpStop = defStop; + } else { + tmpStop = calculateSliceIndex(this.stop); + if (tmpStop == 0) { + return null; + } + if (tmpStop < 0) tmpStop += ilen; + if (tmpStop < 0) tmpStop = -1; + if (tmpStop > ilen) tmpStop = ilen; + } + + if ((tmpStep < 0 && tmpStop >= tmpStart) + || (tmpStep > 0 && tmpStart >= tmpStop)) { + + sliceLength = 0; + } else if (tmpStep < 0) { + sliceLength = (tmpStop - tmpStart + 1) / (tmpStep) + 1; + } else { + sliceLength = (tmpStop - tmpStart - 1) / (tmpStep) + 1; + } + + return new PyTuple(new PyObject[] { new PyInteger(tmpStart), + new PyInteger(tmpStop), new PyInteger(tmpStep)}); + } + + final void slice_init(PyObject[] args, String[] keywords) { + if (args.length == 0) { + throw Py.TypeError("slice expected at least 1 arguments, got " + args.length); + } else if(args.length > 3) { + throw Py.TypeError("slice expected at most 3 arguments, got " + args.length); + } else { + ArgParser ap = new ArgParser("slice", args, keywords, "start", "stop", "step"); + if(args.length == 1) { + stop = ap.getPyObject(0); + } else if(args.length == 2) { + start = ap.getPyObject(0); + stop = ap.getPyObject(1); + } else if(args.length == 3) { + start = ap.getPyObject(0); + stop = ap.getPyObject(1); + step = ap.getPyObject(2); + } + } + } } Index: src/org/python/core/PySliceDerived.java =================================================================== --- src/org/python/core/PySliceDerived.java (revision 0) +++ src/org/python/core/PySliceDerived.java (revision 0) @@ -0,0 +1,954 @@ +package org.python.core; + +public class PySliceDerived extends PySlice implements Slotted { + + public PyObject getSlot(int index) { + return slots[index]; + } + + public void setSlot(int index,PyObject value) { + slots[index]=value; + } + + private PyObject[]slots; + + private PyObject dict; + + public PyObject fastGetDict() { + return dict; + } + + public PyObject getDict() { + return dict; + } + + public void setDict(PyObject newDict) { + if (newDict instanceof PyStringMap||newDict instanceof PyDictionary) { + dict=newDict; + } else { + throw Py.TypeError("__dict__ must be set to a Dictionary "+newDict.getClass().getName()); + } + } + + public void delDict() { + // deleting an object's instance dict makes it grow a new one + dict=new PyStringMap(); + } + + public PySliceDerived(PyType subtype) { + super(subtype); + slots=new PyObject[subtype.getNumSlots()]; + dict=subtype.instDict(); + } + + public PyString __str__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__str__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__str__"+" should return a "+"string"); + } + return super.__str__(); + } + + public PyString __repr__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__repr__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__repr__"+" should return a "+"string"); + } + return super.__repr__(); + } + + public PyString __hex__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__hex__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__hex__"+" should return a "+"string"); + } + return super.__hex__(); + } + + public PyString __oct__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__oct__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyString) + return(PyString)res; + throw Py.TypeError("__oct__"+" should return a "+"string"); + } + return super.__oct__(); + } + + public PyFloat __float__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__float__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyFloat) + return(PyFloat)res; + throw Py.TypeError("__float__"+" should return a "+"float"); + } + return super.__float__(); + } + + public PyLong __long__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__long__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyLong) + return(PyLong)res; + throw Py.TypeError("__long__"+" should return a "+"long"); + } + return super.__long__(); + } + + public PyComplex __complex__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__complex__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyComplex) + return(PyComplex)res; + throw Py.TypeError("__complex__"+" should return a "+"complex"); + } + return super.__complex__(); + } + + public PyObject __pos__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__pos__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__pos__(); + } + + public PyObject __neg__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__neg__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__neg__(); + } + + public PyObject __abs__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__abs__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__abs__(); + } + + public PyObject __invert__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__invert__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__invert__(); + } + + public PyObject __reduce__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__reduce__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + return super.__reduce__(); + } + + public PyObject __add__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__add__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__add__(other); + } + + public PyObject __radd__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__radd__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__radd__(other); + } + + public PyObject __sub__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__sub__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__sub__(other); + } + + public PyObject __rsub__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rsub__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rsub__(other); + } + + public PyObject __mul__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__mul__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__mul__(other); + } + + public PyObject __rmul__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rmul__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rmul__(other); + } + + public PyObject __div__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__div__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__div__(other); + } + + public PyObject __rdiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rdiv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rdiv__(other); + } + + public PyObject __floordiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__floordiv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__floordiv__(other); + } + + public PyObject __rfloordiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rfloordiv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rfloordiv__(other); + } + + public PyObject __truediv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__truediv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__truediv__(other); + } + + public PyObject __rtruediv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rtruediv__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rtruediv__(other); + } + + public PyObject __mod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__mod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__mod__(other); + } + + public PyObject __rmod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rmod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rmod__(other); + } + + public PyObject __divmod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__divmod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__divmod__(other); + } + + public PyObject __rdivmod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rdivmod__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rdivmod__(other); + } + + public PyObject __pow__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__pow__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__pow__(other); + } + + public PyObject __rpow__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rpow__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rpow__(other); + } + + public PyObject __lshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__lshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__lshift__(other); + } + + public PyObject __rlshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rlshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rlshift__(other); + } + + public PyObject __rshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rshift__(other); + } + + public PyObject __rrshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rrshift__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rrshift__(other); + } + + public PyObject __and__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__and__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__and__(other); + } + + public PyObject __rand__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rand__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rand__(other); + } + + public PyObject __or__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__or__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__or__(other); + } + + public PyObject __ror__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ror__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ror__(other); + } + + public PyObject __xor__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__xor__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__xor__(other); + } + + public PyObject __rxor__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__rxor__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__rxor__(other); + } + + public PyObject __lt__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__lt__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__lt__(other); + } + + public PyObject __le__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__le__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__le__(other); + } + + public PyObject __gt__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__gt__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__gt__(other); + } + + public PyObject __ge__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ge__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ge__(other); + } + + public PyObject __eq__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__eq__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__eq__(other); + } + + public PyObject __ne__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ne__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res==Py.NotImplemented) + return null; + return res; + } + return super.__ne__(other); + } + + public PyObject __iadd__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__iadd__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__iadd__(other); + } + + public PyObject __isub__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__isub__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__isub__(other); + } + + public PyObject __imul__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__imul__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__imul__(other); + } + + public PyObject __idiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__idiv__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__idiv__(other); + } + + public PyObject __ifloordiv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ifloordiv__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ifloordiv__(other); + } + + public PyObject __itruediv__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__itruediv__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__itruediv__(other); + } + + public PyObject __imod__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__imod__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__imod__(other); + } + + public PyObject __ipow__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ipow__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ipow__(other); + } + + public PyObject __ilshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ilshift__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ilshift__(other); + } + + public PyObject __irshift__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__irshift__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__irshift__(other); + } + + public PyObject __iand__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__iand__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__iand__(other); + } + + public PyObject __ior__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ior__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ior__(other); + } + + public PyObject __ixor__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__ixor__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(other); + return super.__ixor__(other); + } + + public PyObject __int__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__int__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyInteger||res instanceof PyLong) + return(PyObject)res; + throw Py.TypeError("__int__"+" should return an integer"); + } + return super.__int__(); + } + + public String toString() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__repr__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (!(res instanceof PyString)) + throw Py.TypeError("__repr__ should return a string"); + return((PyString)res).toString(); + } + return super.toString(); + } + + public int hashCode() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__hash__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyInteger) + return((PyInteger)res).getValue(); + throw Py.TypeError("__hash__ should return a int"); + } + if (self_type.lookup("__eq__")!=null||self_type.lookup("__cmp__")!=null) + throw Py.TypeError("unhashable type"); + return super.hashCode(); + } + + public PyUnicode __unicode__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__unicode__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyUnicode) + return(PyUnicode)res; + if (res instanceof PyString) + return new PyUnicode((PyString)res); + throw Py.TypeError("__unicode__"+" should return a "+"unicode"); + } + return super.__unicode__(); + } + + public int __cmp__(PyObject other) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__cmp__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(other); + if (res instanceof PyInteger) { + int v=((PyInteger)res).getValue(); + return v<0?-1:v>0?1:0; + } + throw Py.TypeError("__cmp__ should return a int"); + } + return super.__cmp__(other); + } + + public boolean __nonzero__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__nonzero__"); + if (impl==null) { + impl=self_type.lookup("__len__"); + if (impl==null) + return super.__nonzero__(); + } + return impl.__get__(this,self_type).__call__().__nonzero__(); + } + + public boolean __contains__(PyObject o) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__contains__"); + if (impl==null) + return super.__contains__(o); + return impl.__get__(this,self_type).__call__(o).__nonzero__(); + } + + public int __len__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__len__"); + if (impl!=null) { + PyObject res=impl.__get__(this,self_type).__call__(); + if (res instanceof PyInteger) + return((PyInteger)res).getValue(); + throw Py.TypeError("__len__ should return a int"); + } + return super.__len__(); + } + + public PyObject __iter__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__iter__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(); + impl=self_type.lookup("__getitem__"); + if (impl==null) + return super.__iter__(); + return new PySequenceIter(this); + } + + public PyObject __iternext__() { + PyType self_type=getType(); + PyObject impl=self_type.lookup("next"); + if (impl!=null) { + try { + return impl.__get__(this,self_type).__call__(); + } catch (PyException exc) { + if (Py.matchException(exc,Py.StopIteration)) + return null; + throw exc; + } + } + return super.__iternext__(); // ??? + } + + public PyObject __finditem__(PyObject key) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__getitem__"); + if (impl!=null) + try { + return impl.__get__(this,self_type).__call__(key); + } catch (PyException exc) { + if (Py.matchException(exc,Py.LookupError)) + return null; + throw exc; + } + return super.__finditem__(key); + } + + public void __setitem__(PyObject key,PyObject value) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__setitem__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(key,value); + return; + } + super.__setitem__(key,value); + } + + public PyObject __getslice__(PyObject start,PyObject stop,PyObject step) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__getslice__"); + if (impl!=null) + try { + return impl.__get__(this,self_type).__call__(start,stop); + } catch (PyException exc) { + if (Py.matchException(exc,Py.LookupError)) + return null; + throw exc; + } + return super.__getslice__(start,stop,step); + } + + public void __delitem__(PyObject key) { // ??? + PyType self_type=getType(); + PyObject impl=self_type.lookup("__delitem__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(key); + return; + } + super.__delitem__(key); + } + + public PyObject __call__(PyObject args[],String keywords[]) { + ThreadState ts=Py.getThreadState(); + if (ts.recursion_depth++>ts.systemState.getrecursionlimit()) + throw Py.RuntimeError("maximum __call__ recursion depth exceeded"); + try { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__call__"); + if (impl!=null) + return impl.__get__(this,self_type).__call__(args,keywords); + return super.__call__(args,keywords); + } finally { + --ts.recursion_depth; + } + } + + public PyObject __findattr__(String name) { + PyType self_type=getType(); + PyObject getattribute=self_type.lookup("__getattribute__"); + PyString py_name=null; + try { + if (getattribute!=null) { + return getattribute.__get__(this,self_type).__call__(py_name=new PyString(name)); + } else { + return super.__findattr__(name); + } + } catch (PyException e) { + if (Py.matchException(e,Py.AttributeError)) { + PyObject getattr=self_type.lookup("__getattr__"); + if (getattr!=null) + try { + return getattr.__get__(this,self_type).__call__(py_name!=null?py_name:new PyString(name)); + } catch (PyException e1) { + if (!Py.matchException(e1,Py.AttributeError)) + throw e1; + } + return null; + } + throw e; + } + } + + public void __setattr__(String name,PyObject value) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__setattr__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(new PyString(name),value); + return; + } + super.__setattr__(name,value); + } + + public void __delattr__(String name) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__delattr__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(new PyString(name)); + return; + } + super.__delattr__(name); + } + + public PyObject __get__(PyObject obj,PyObject type) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__get__"); + if (impl!=null) { + if (obj==null) + obj=Py.None; + if (type==null) + type=Py.None; + return impl.__get__(this,self_type).__call__(obj,type); + } + return super.__get__(obj,type); + } + + public void __set__(PyObject obj,PyObject value) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__set__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(obj,value); + return; + } + super.__set__(obj,value); + } + + public void __delete__(PyObject obj) { + PyType self_type=getType(); + PyObject impl=self_type.lookup("__delete__"); + if (impl!=null) { + impl.__get__(this,self_type).__call__(obj); + return; + } + super.__delete__(obj); + } + + public void dispatch__init__(PyType type,PyObject[]args,String[]keywords) { + PyType self_type=getType(); + if (self_type.isSubType(type)) { + PyObject impl=self_type.lookup("__init__"); + if (impl!=null) + impl.__get__(this,self_type).__call__(args,keywords); + } + } + +} Index: Lib/test/test_slice.py =================================================================== --- Lib/test/test_slice.py (revision 0) +++ Lib/test/test_slice.py (revision 0) @@ -0,0 +1,101 @@ +# tests for slice objects; in particular the indices method. + +import unittest +from test import test_support + +import sys + +class SliceTest(unittest.TestCase): + + def test_constructor(self): + self.assertRaises(TypeError, slice) + self.assertRaises(TypeError, slice, 1, 2, 3, 4) + + def test_repr(self): + self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)") + + def test_hash(self): + # Verify clearing of SF bug #800796 + self.assertRaises(TypeError, hash, slice(5)) + self.assertRaises(TypeError, slice(5).__hash__) + + def test_cmp(self): + s1 = slice(1, 2, 3) + s2 = slice(1, 2, 3) + s3 = slice(1, 2, 4) + self.assertEqual(s1, s2) + self.assertNotEqual(s1, s3) + + class Exc(Exception): + pass + + class BadCmp(object): + def __eq__(self, other): + raise Exc + + s1 = slice(BadCmp()) + s2 = slice(BadCmp()) + self.assertRaises(Exc, cmp, s1, s2) + self.assertEqual(s1, s1) + + s1 = slice(1, BadCmp()) + s2 = slice(1, BadCmp()) + self.assertEqual(s1, s1) + self.assertRaises(Exc, cmp, s1, s2) + + s1 = slice(1, 2, BadCmp()) + s2 = slice(1, 2, BadCmp()) + self.assertEqual(s1, s1) + self.assertRaises(Exc, cmp, s1, s2) + + def test_members(self): + s = slice(1) + self.assertEqual(s.start, None) + self.assertEqual(s.stop, 1) + self.assertEqual(s.step, None) + + s = slice(1, 2) + self.assertEqual(s.start, 1) + self.assertEqual(s.stop, 2) + self.assertEqual(s.step, None) + + s = slice(1, 2, 3) + self.assertEqual(s.start, 1) + self.assertEqual(s.stop, 2) + self.assertEqual(s.step, 3) + + class AnyClass: + pass + + obj = AnyClass() + s = slice(obj) + self.assert_(s.stop is obj) + + def test_indices(self): + self.assertEqual(slice(None ).indices(10), (0, 10, 1)) + self.assertEqual(slice(None, None, 2).indices(10), (0, 10, 2)) + self.assertEqual(slice(1, None, 2).indices(10), (1, 10, 2)) + self.assertEqual(slice(None, None, -1).indices(10), (9, -1, -1)) + self.assertEqual(slice(None, None, -2).indices(10), (9, -1, -2)) + self.assertEqual(slice(3, None, -2).indices(10), (3, -1, -2)) + self.assertEqual( + slice(-100, 100 ).indices(10), + slice(None).indices(10) + ) + self.assertEqual( + slice(100, -100, -1).indices(10), + slice(None, None, -1).indices(10) + ) + self.assertEqual(slice(-100L, 100L, 2L).indices(10), (0, 10, 2)) +# Jython transition 2.3 +# sliceLength of PySequence returns 0 when step is sys.maxint +# http://jython.org/1818353 +# self.assertEqual(range(10)[::sys.maxint - 1], [0]) + + self.assertRaises(OverflowError, slice(None).indices, 1L<<100) + +def test_main(): + test_support.run_unittest(SliceTest) + +if __name__ == "__main__": + test_main()