Index: Lib/marshal.py =================================================================== --- Lib/marshal.py (revision 2796) +++ Lib/marshal.py (working copy) @@ -15,6 +15,8 @@ TYPE_NULL = '0' TYPE_NONE = 'N' +TYPE_FALSE = 'F' +TYPE_TRUE = 'T' TYPE_ELLIPSIS = '.' TYPE_INT = 'i' TYPE_INT64 = 'I' @@ -59,6 +61,13 @@ self.f.write(TYPE_NONE) dispatch[NoneType] = dump_none + def dump_bool(self, x): + if x: + self.f.write(TYPE_TRUE) + else: + self.f.write(TYPE_FALSE) + dispatch[BooleanType] = dump_bool + def dump_ellipsis(self, x): self.f.write(TYPE_ELLIPSIS) try: @@ -210,6 +219,14 @@ return None dispatch[TYPE_NONE] = load_none + def load_False(self): + return False + dispatch[TYPE_FALSE] = load_False + + def load_True(self): + return True + dispatch[TYPE_TRUE] = load_True + def load_ellipsis(self): return EllipsisType dispatch[TYPE_ELLIPSIS] = load_ellipsis Index: src/org/python/core/PyInteger.java =================================================================== --- src/org/python/core/PyInteger.java (revision 2796) +++ src/org/python/core/PyInteger.java (working copy) @@ -1191,6 +1191,9 @@ return Py.Zero; } if (base == -909) { + if (x instanceof PyBoolean) { + return (coerce(x) == 0) ? Py.Zero : Py.One; + } return x.__int__(); } if (!(x instanceof PyString)) { @@ -1320,7 +1323,9 @@ } private static final int coerce(PyObject other) { - if (other instanceof PyInteger) + if (other instanceof PyBoolean) + return ((PyBoolean) other).asInt(0); + else if (other instanceof PyInteger) return ((PyInteger) other).value; else throw Py.TypeError("xxx"); @@ -1335,7 +1340,7 @@ if (!canCoerce(right)) return null; int rightv = coerce(right); - int a = value; + int a = asInt(0); int b = rightv; int x = a + b; if ((x^a) >= 0 || (x^b) >= 0) @@ -1367,7 +1372,7 @@ final PyObject int___sub__(PyObject right) { if (!canCoerce(right)) return null; - return _sub(value, coerce(right)); + return _sub(asInt(0), coerce(right)); } public PyObject __rsub__(PyObject left) { @@ -1377,7 +1382,7 @@ final PyObject int___rsub__(PyObject left) { if (!canCoerce(left)) return null; - return _sub(coerce(left), value); + return _sub(coerce(left), asInt(0)); } public PyObject __mul__(PyObject right) { @@ -1386,13 +1391,13 @@ final PyObject int___mul__(PyObject right) { if (right instanceof PySequence) - return ((PySequence) right).repeat(value); + return ((PySequence) right).repeat(asInt(0)); if (!canCoerce(right)) return null; int rightv = coerce(right); - double x = (double)value; + double x = (double)asInt(0); x *= rightv; //long x = ((long)value)*((PyInteger)right).value; //System.out.println("mul: "+this+" * "+right+" = "+x); @@ -1444,7 +1449,7 @@ return null; if (Options.divisionWarning > 0) Py.warning(Py.DeprecationWarning, "classic int division"); - return Py.newInteger(divide(value, coerce(right))); + return Py.newInteger(divide(asInt(0), coerce(right))); } public PyObject __rdiv__(PyObject left) { @@ -1456,7 +1461,7 @@ return null; if (Options.divisionWarning > 0) Py.warning(Py.DeprecationWarning, "classic int division"); - return Py.newInteger(divide(coerce(left), value)); + return Py.newInteger(divide(coerce(left), asInt(0))); } public PyObject __floordiv__(PyObject right) { @@ -1466,7 +1471,7 @@ final PyObject int___floordiv__(PyObject right) { if (!canCoerce(right)) return null; - return Py.newInteger(divide(value, coerce(right))); + return Py.newInteger(divide(asInt(0), coerce(right))); } public PyObject __rfloordiv__(PyObject left) { @@ -1476,7 +1481,7 @@ final PyObject int___rfloordiv__(PyObject left) { if (!canCoerce(left)) return null; - return Py.newInteger(divide(coerce(left), value)); + return Py.newInteger(divide(coerce(left), asInt(0))); } public PyObject __truediv__(PyObject right) { @@ -1511,7 +1516,8 @@ if (!canCoerce(right)) return null; int rightv = coerce(right); - return Py.newInteger(modulo(value, rightv, divide(value, rightv))); + int v = asInt(0); + return Py.newInteger(modulo(v, rightv, divide(v, rightv))); } public PyObject __rmod__(PyObject left) { @@ -1522,7 +1528,8 @@ if (!canCoerce(left)) return null; int leftv = coerce(left); - return Py.newInteger(modulo(leftv, value, divide(leftv, value))); + int v = asInt(0); + return Py.newInteger(modulo(leftv, v, divide(leftv, v))); } public PyObject __divmod__(PyObject right) { @@ -1534,10 +1541,11 @@ return null; int rightv = coerce(right); - int xdivy = divide(value, rightv); + int v = asInt(0); + int xdivy = divide(v, rightv); return new PyTuple(new PyObject[] { new PyInteger(xdivy), - new PyInteger(modulo(value, rightv, xdivy)) + new PyInteger(modulo(v, rightv, xdivy)) }); } @@ -1552,7 +1560,7 @@ if (modulo != null && !canCoerce(modulo)) return null; - return _pow(value, coerce(right), modulo, this, right); + return _pow(asInt(0), coerce(right), modulo, this, right); } public PyObject __rpow__(PyObject left, PyObject modulo) { @@ -1562,7 +1570,7 @@ if (modulo != null && !canCoerce(modulo)) return null; - return _pow(coerce(left), value, modulo, left, this); + return _pow(coerce(left), asInt(0), modulo, left, this); } private static PyObject _pow(int value, int pow, PyObject modulo, @@ -1637,7 +1645,7 @@ final PyObject int___lshift__(PyObject right) { int rightv; if (right instanceof PyInteger) - rightv = ((PyInteger)right).value; + rightv = ((PyInteger)right).asInt(0); else return null; @@ -1655,7 +1663,7 @@ final PyObject int___rshift__(PyObject right) { int rightv; if (right instanceof PyInteger) - rightv = ((PyInteger)right).value; + rightv = ((PyInteger)right).asInt(0); else return null; @@ -1670,13 +1678,9 @@ } final PyObject int___and__(PyObject right) { - int rightv; - if (right instanceof PyInteger) - rightv = ((PyInteger)right).value; - else - return null; - - return Py.newInteger(value & rightv); + if (!canCoerce(right)) + return null; + return Py.newInteger(asInt(0) & coerce(right)); } public PyObject __xor__(PyObject right) { @@ -1742,7 +1746,7 @@ } final PyObject int___invert__() { - return Py.newInteger(~value); + return Py.newInteger(~asInt(0)); } public PyObject __int__() { @@ -1803,11 +1807,11 @@ public boolean isMappingType() { return false; } public boolean isSequenceType() { return false; } - public long asLong(int index) throws PyObject.ConversionException { + public long asLong(int index) { return getValue(); } - public int asInt(int index) throws PyObject.ConversionException { + public int asInt(int index) { return getValue(); } Index: src/org/python/core/PyObject.java =================================================================== --- src/org/python/core/PyObject.java (revision 2796) +++ src/org/python/core/PyObject.java (working copy) @@ -11,6 +11,7 @@ **/ public class PyObject implements java.io.Serializable { + //~ BEGIN GENERATED REGION -- DO NOT EDIT SEE gexpose.py /* type info */ @@ -1574,7 +1575,7 @@ res = o.__eq__(this); if (res != null) return res; - return _cmpeq_unsafe(o) == 0 ? Py.One : Py.Zero; + return _cmpeq_unsafe(o) == 0 ? Py.True : Py.False; } catch (PyException e) { if (Py.matchException(e, Py.AttributeError)) { return Py.Zero; @@ -1607,7 +1608,7 @@ res = o.__ne__(this); if (res != null) return res; - return _cmpeq_unsafe(o) != 0 ? Py.One : Py.Zero; + return _cmpeq_unsafe(o) != 0 ? Py.True : Py.False; } finally { delete_token(ts, token); ts.compareStateNesting--; @@ -1635,7 +1636,7 @@ res = o.__ge__(this); if (res != null) return res; - return _cmp_unsafe(o) <= 0 ? Py.One : Py.Zero; + return _cmp_unsafe(o) <= 0 ? Py.True : Py.False; } finally { delete_token(ts, token); ts.compareStateNesting--; @@ -1663,7 +1664,7 @@ res = o.__gt__(this); if (res != null) return res; - return _cmp_unsafe(o) < 0 ? Py.One : Py.Zero; + return _cmp_unsafe(o) < 0 ? Py.True : Py.False; } finally { delete_token(ts, token); ts.compareStateNesting--; @@ -1691,7 +1692,7 @@ res = o.__le__(this); if (res != null) return res; - return _cmp_unsafe(o) >= 0 ? Py.One : Py.Zero; + return _cmp_unsafe(o) >= 0 ? Py.True : Py.False; } finally { delete_token(ts, token); ts.compareStateNesting--; @@ -1719,7 +1720,7 @@ res = o.__lt__(this); if (res != null) return res; - return _cmp_unsafe(o) > 0 ? Py.One : Py.Zero; + return _cmp_unsafe(o) > 0 ? Py.True : Py.False; } finally { delete_token(ts, token); ts.compareStateNesting--; @@ -1734,7 +1735,7 @@ * @return the result of the comparison **/ public PyObject _is(PyObject o) { - return this == o ? Py.One : Py.Zero; + return this == o ? Py.True : Py.False; } /** @@ -1744,7 +1745,7 @@ * @return the result of the comparison **/ public PyObject _isnot(PyObject o) { - return this != o ? Py.One : Py.Zero; + return this != o ? Py.True : Py.False; } /** @@ -1792,7 +1793,7 @@ * @return not this. **/ public PyObject __not__() { - return __nonzero__() ? Py.Zero : Py.One; + return __nonzero__() ? Py.False : Py.True; } /* The basic numeric operations */ Index: src/org/python/core/PySystemState.java =================================================================== --- src/org/python/core/PySystemState.java (revision 2796) +++ src/org/python/core/PySystemState.java (working copy) @@ -434,6 +434,9 @@ Py.Zero = new PyInteger(0); Py.One = new PyInteger(1); + + Py.False = new PyBoolean(false); + Py.True = new PyBoolean(true); Py.EmptyString = new PyString(""); Py.Newline = new PyString("\n"); Index: src/org/python/core/PyJavaInstance.java =================================================================== --- src/org/python/core/PyJavaInstance.java (revision 2796) +++ src/org/python/core/PyJavaInstance.java (working copy) @@ -94,9 +94,9 @@ public PyObject _is(PyObject o) { if (o instanceof PyJavaInstance) { return javaProxy == ((PyJavaInstance)o).javaProxy - ? Py.One : Py.Zero; + ? Py.True : Py.False; } - return Py.Zero; + return Py.False; } public PyObject _isnot(PyObject o) { Index: src/org/python/core/PyBoolean.java =================================================================== --- src/org/python/core/PyBoolean.java (revision 0) +++ src/org/python/core/PyBoolean.java (revision 0) @@ -0,0 +1,489 @@ +package org.python.core; + +import java.io.Serializable; + +public class PyBoolean extends PyInteger { + +/** + * A builtin python bool. + */ + //~ BEGIN GENERATED REGION -- DO NOT EDIT SEE gexpose.py + /* type info */ + + public static final String exposed_name="bool"; + + public static void typeSetup(PyObject dict,PyType.Newstyle marker) { + class exposed___abs__ extends PyBuiltinFunctionNarrow { + + private PyBoolean self; + + public PyObject getSelf() { + return self; + } + + exposed___abs__(PyBoolean self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___abs__((PyBoolean)self,info); + } + + public PyObject __call__() { + return self.bool___abs__(); + } + + public PyObject inst_call(PyObject gself) { + PyBoolean self=(PyBoolean)gself; + return self.bool___abs__(); + } + + } + dict.__setitem__("__abs__",new PyMethodDescr("__abs__",PyBoolean.class,0,0,new exposed___abs__(null,null))); + class exposed___neg__ extends PyBuiltinFunctionNarrow { + + private PyBoolean self; + + public PyObject getSelf() { + return self; + } + + exposed___neg__(PyBoolean self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___neg__((PyBoolean)self,info); + } + + public PyObject __call__() { + return self.bool___neg__(); + } + + public PyObject inst_call(PyObject gself) { + PyBoolean self=(PyBoolean)gself; + return self.bool___neg__(); + } + + } + dict.__setitem__("__neg__",new PyMethodDescr("__neg__",PyBoolean.class,0,0,new exposed___neg__(null,null))); + class exposed___pos__ extends PyBuiltinFunctionNarrow { + + private PyBoolean self; + + public PyObject getSelf() { + return self; + } + + exposed___pos__(PyBoolean self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___pos__((PyBoolean)self,info); + } + + public PyObject __call__() { + return self.bool___pos__(); + } + + public PyObject inst_call(PyObject gself) { + PyBoolean self=(PyBoolean)gself; + return self.bool___pos__(); + } + + } + dict.__setitem__("__pos__",new PyMethodDescr("__pos__",PyBoolean.class,0,0,new exposed___pos__(null,null))); + class exposed___and__ extends PyBuiltinFunctionNarrow { + + private PyBoolean self; + + public PyObject getSelf() { + return self; + } + + exposed___and__(PyBoolean self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___and__((PyBoolean)self,info); + } + + public PyObject __call__(PyObject arg0) { + PyObject ret=self.bool___and__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + public PyObject inst_call(PyObject gself,PyObject arg0) { + PyBoolean self=(PyBoolean)gself; + PyObject ret=self.bool___and__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + } + dict.__setitem__("__and__",new PyMethodDescr("__and__",PyBoolean.class,1,1,new exposed___and__(null,null))); + class exposed___or__ extends PyBuiltinFunctionNarrow { + + private PyBoolean self; + + public PyObject getSelf() { + return self; + } + + exposed___or__(PyBoolean self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___or__((PyBoolean)self,info); + } + + public PyObject __call__(PyObject arg0) { + PyObject ret=self.bool___or__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + public PyObject inst_call(PyObject gself,PyObject arg0) { + PyBoolean self=(PyBoolean)gself; + PyObject ret=self.bool___or__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + } + dict.__setitem__("__or__",new PyMethodDescr("__or__",PyBoolean.class,1,1,new exposed___or__(null,null))); + class exposed___xor__ extends PyBuiltinFunctionNarrow { + + private PyBoolean self; + + public PyObject getSelf() { + return self; + } + + exposed___xor__(PyBoolean self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___xor__((PyBoolean)self,info); + } + + public PyObject __call__(PyObject arg0) { + PyObject ret=self.bool___xor__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + public PyObject inst_call(PyObject gself,PyObject arg0) { + PyBoolean self=(PyBoolean)gself; + PyObject ret=self.bool___xor__(arg0); + if (ret==null) + return Py.NotImplemented; + return ret; + } + + } + dict.__setitem__("__xor__",new PyMethodDescr("__xor__",PyBoolean.class,1,1,new exposed___xor__(null,null))); + class exposed___nonzero__ extends PyBuiltinFunctionNarrow { + + private PyBoolean self; + + public PyObject getSelf() { + return self; + } + + exposed___nonzero__(PyBoolean self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___nonzero__((PyBoolean)self,info); + } + + public PyObject __call__() { + return Py.newBoolean(self.bool___nonzero__()); + } + + public PyObject inst_call(PyObject gself) { + PyBoolean self=(PyBoolean)gself; + return Py.newBoolean(self.bool___nonzero__()); + } + + } + dict.__setitem__("__nonzero__",new PyMethodDescr("__nonzero__",PyBoolean.class,0,0,new exposed___nonzero__(null,null))); + class exposed___repr__ extends PyBuiltinFunctionNarrow { + + private PyBoolean self; + + public PyObject getSelf() { + return self; + } + + exposed___repr__(PyBoolean self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___repr__((PyBoolean)self,info); + } + + public PyObject __call__() { + return new PyString(self.bool_toString()); + } + + public PyObject inst_call(PyObject gself) { + PyBoolean self=(PyBoolean)gself; + return new PyString(self.bool_toString()); + } + + } + dict.__setitem__("__repr__",new PyMethodDescr("__repr__",PyBoolean.class,0,0,new exposed___repr__(null,null))); + class exposed___str__ extends PyBuiltinFunctionNarrow { + + private PyBoolean self; + + public PyObject getSelf() { + return self; + } + + exposed___str__(PyBoolean self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___str__((PyBoolean)self,info); + } + + public PyObject __call__() { + return new PyString(self.bool_toString()); + } + + public PyObject inst_call(PyObject gself) { + PyBoolean self=(PyBoolean)gself; + return new PyString(self.bool_toString()); + } + + } + dict.__setitem__("__str__",new PyMethodDescr("__str__",PyBoolean.class,0,0,new exposed___str__(null,null))); + class exposed___hash__ extends PyBuiltinFunctionNarrow { + + private PyBoolean self; + + public PyObject getSelf() { + return self; + } + + exposed___hash__(PyBoolean self,PyBuiltinFunction.Info info) { + super(info); + this.self=self; + } + + public PyBuiltinFunction makeBound(PyObject self) { + return new exposed___hash__((PyBoolean)self,info); + } + + public PyObject __call__() { + return Py.newInteger(self.bool_hashCode()); + } + + public PyObject inst_call(PyObject gself) { + PyBoolean self=(PyBoolean)gself; + return Py.newInteger(self.bool_hashCode()); + } + + } + dict.__setitem__("__hash__",new PyMethodDescr("__hash__",PyBoolean.class,0,0,new exposed___hash__(null,null))); + dict.__setitem__("__new__",new PyNewWrapper(PyBoolean.class,"__new__",-1,-1) { + + public PyObject new_impl(boolean init,PyType subtype,PyObject[]args,String[]keywords) { + return bool_new(this,init,subtype,args,keywords); + } + + }); + } + //~ END GENERATED REGION -- DO NOT EDIT SEE gexpose.py + + public static PyObject bool_new(PyNewWrapper new_, boolean init, PyType subtype, + PyObject[] args, String[] keywords) { + ArgParser ap = new ArgParser("bool", args, keywords, new String[] { "x" }, 0); + PyObject x = ap.getPyObject(0, null); + if (x == null) { + return Py.False; + } + if (new_.for_type == subtype) { + return x.__nonzero__() ? Py.True : Py.False; + } else { + return new PyBooleanDerived(subtype, x.__nonzero__()); + } + } // xxx + + private static final PyType BOOLTYPE = PyType.fromClass(PyBoolean.class); + + private boolean value; + + public PyBoolean(PyType subType, boolean v) { + super(subType, v ? 1 : 0); + value = v; + } + + public PyBoolean(boolean v) { + this(BOOLTYPE, v); + } + + public int getValue() { + return value ? 1 : 0; + } + + public String safeRepr() throws PyIgnoreMethodTag { + return "'bool' object"; + } + + public String toString() { + return bool_toString(); + } + + final String bool_toString() { + return value ? "True" : "False"; + } + + public int hashCode() { + return bool_hashCode(); + } + + final int bool_hashCode() { + return value ? 1 : 0; + } + + private static void err_ovf(String msg) { + try { + Py.OverflowWarning(msg); + } catch (PyException exc) { + if (Py.matchException(exc, Py.OverflowWarning)) + throw Py.OverflowError(msg); + } + } + + public boolean __nonzero__() { + return bool___nonzero__(); + } + + final boolean bool___nonzero__() { + return value; + } + + public Object __tojava__(Class c) { + if (c == Integer.TYPE || c == Number.class || + c == Object.class || c == Integer.class || + c == Serializable.class) + { + return new Integer(value ? 1 : 0); + } + + if (c == Boolean.TYPE || c == Boolean.class) + return new Boolean(value); + if (c == Byte.TYPE || c == Byte.class) + return new Byte((byte)(value ? 1 : 0)); + if (c == Short.TYPE || c == Short.class) + return new Short((short)(value ? 1 : 0)); + if (c == Long.TYPE || c == Long.class) + return new Long(value ? 1 : 0); + if (c == Float.TYPE || c == Float.class) + return new Float(value ? 1 : 0); + if (c == Double.TYPE || c == Double.class) + return new Double(value ? 1 : 0); + return super.__tojava__(c); + } + + public PyObject __and__(PyObject right) { + return bool___and__(right); + } + + final PyObject bool___and__(PyObject right) { + if (right instanceof PyBoolean) { + return Py.newBoolean(value & ((PyBoolean)right).value); + } else if (right instanceof PyInteger) { + return Py.newInteger(asInt(0) & ((PyInteger)right).getValue()); + } else { + return null; + } + } + + public PyObject __xor__(PyObject right) { + return bool___xor__(right); + } + + final PyObject bool___xor__(PyObject right) { + if (right instanceof PyBoolean) { + return Py.newBoolean(value ^ ((PyBoolean)right).value); + } else if (right instanceof PyInteger) { + return Py.newInteger(asInt(0) ^ ((PyInteger)right).getValue()); + } else { + return null; + } + } + + public PyObject __or__(PyObject right) { + return bool___or__(right); + } + + final PyObject bool___or__(PyObject right) { + if (right instanceof PyBoolean) { + return Py.newBoolean(value | ((PyBoolean)right).value); + } else if (right instanceof PyInteger) { + return Py.newInteger(asInt(0) | ((PyInteger)right).getValue()); + } else { + return null; + } + } + + public PyObject __neg__() { + return bool___neg__(); + } + + final PyObject bool___neg__() { + return Py.newInteger(value ? -1 : 0); + } + + public PyObject __pos__() { + return bool___pos__(); + } + + final PyObject bool___pos__() { + return Py.newInteger(value ? 1 : 0); + } + + public PyObject __abs__() { + return bool___abs__(); + } + + final PyObject bool___abs__() { + return Py.newInteger(value ? 1 : 0); + } + + public long asLong(int index) { + return value ? 1 : 0; + } + + public int asInt(int index) { + return value ? 1 : 0; + } + +} Index: src/org/python/core/__builtin__.java =================================================================== --- src/org/python/core/__builtin__.java (revision 2796) +++ src/org/python/core/__builtin__.java (working copy) @@ -106,6 +106,7 @@ 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)); @@ -131,8 +132,8 @@ dict.__setitem__("None", Py.None); dict.__setitem__("NotImplemented", Py.NotImplemented); dict.__setitem__("Ellipsis", Py.Ellipsis); - dict.__setitem__("True", Py.One); - dict.__setitem__("False", Py.Zero); + 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 @@ -190,9 +191,9 @@ } } - public static PyObject bool(PyObject o) { - return (o == null ? Py.Zero : o.__nonzero__() ? Py.One : Py.Zero); - } +// public static PyObject bool(PyObject o) { +// return (o == null ? Py.False : o.__nonzero__() ? Py.True : Py.False); +// } public static boolean callable(PyObject o) { return o.__findattr__("__call__") != null; Index: src/org/python/core/Py.java =================================================================== --- src/org/python/core/Py.java (revision 2796) +++ src/org/python/core/Py.java (working copy) @@ -55,12 +55,18 @@ /** A tuple with zero elements **/ public static PyTuple EmptyTuple; - /** The Python integer 0 - also used as false **/ + /** The Python integer 0 **/ public static PyInteger Zero; - /** The Python integer 1 - also used as true **/ + /** The Python integer 1 **/ public static PyInteger One; + /** The Python boolean False **/ + public static PyBoolean False; + + /** The Python boolean True **/ + public static PyBoolean True; + /** A zero-length Python string **/ public static PyString EmptyString; @@ -490,8 +496,8 @@ return new PyUnicode(s); } - public static PyInteger newBoolean(boolean t) { - return t ? Py.One : Py.Zero; + public static PyBoolean newBoolean(boolean t) { + return t ? Py.True : Py.False; } // nested scopes: @@ -1491,7 +1497,7 @@ } } if (o instanceof Boolean) { - return ((Boolean)o).booleanValue() ? Py.One : Py.Zero; + return ((Boolean)o).booleanValue() ? Py.True : Py.False; } if (o == null) return Py.None; if (o instanceof String) return new PyString((String)o); Index: src/org/python/core/PyBooleanDerived.java =================================================================== --- src/org/python/core/PyBooleanDerived.java (revision 0) +++ src/org/python/core/PyBooleanDerived.java (revision 0) @@ -0,0 +1,945 @@ +package org.python.core; + +public class PyBooleanDerived extends PyBoolean { + + private PyObject dict; + + public PyObject fastGetDict() { + return dict; + } + + public PyObject getDict() { + return dict; + } + + public PyBooleanDerived(PyType subtype, boolean v) { + super(subtype, v); + 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 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 PyObject) + return(PyObject)res; + throw Py.TypeError("__int__"+" should return a "+"int"); + } + return super.__int__(); + } + + 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 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 __getitem__(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.__getitem__(key); + } + + 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: src/org/python/modules/types.java =================================================================== --- src/org/python/modules/types.java (revision 2796) +++ src/org/python/modules/types.java (working copy) @@ -14,6 +14,7 @@ // xxx change some of these public static void classDictInit(PyObject dict) { dict.__setitem__("ArrayType", PyType.fromClass(PyArray.class)); + dict.__setitem__("BooleanType", PyType.fromClass(PyBoolean.class)); dict.__setitem__("BuiltinFunctionType", PyType.fromClass(PyReflectedFunction.class)); dict.__setitem__("BuiltinMethodType",