Index: Py.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/Py.java,v retrieving revision 2.32 diff -u -r2.32 Py.java --- Py.java 2001/01/21 14:00:33 2.32 +++ Py.java 2001/01/25 19:54:21 @@ -31,6 +31,8 @@ /** A tuple with zero elements **/ public static PyTuple EmptyTuple; + + public static PyObject NotImplemented; /** The Python integer 0 - also used as false **/ public static PyInteger Zero; Index: PyComplex.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyComplex.java,v retrieving revision 2.1 diff -u -r2.1 PyComplex.java --- PyComplex.java 1999/05/17 19:59:38 2.1 +++ PyComplex.java 2001/01/25 19:54:22 @@ -6,10 +6,7 @@ static PyComplex J = new PyComplex(0, 1.); - public static PyClass __class__; - public PyComplex(double r, double i) { - super(__class__); real = r; imag = i; } @@ -88,57 +85,139 @@ return Py.None; } + private final boolean canCoerce(PyObject other) { + return other instanceof PyComplex || + other instanceof PyFloat || + other instanceof PyInteger || + other instanceof PyLong; + } + + private final PyComplex coerce(PyObject other) { + if (other instanceof PyComplex) + return (PyComplex) other; + if (other instanceof PyFloat) + return new PyComplex(((PyFloat)other).getValue(), 0); + if (other instanceof PyInteger) + return new PyComplex((double)((PyInteger)other).getValue(), 0); + if (other instanceof PyLong) + return new PyComplex(((PyLong)other).doubleValue(), 0); + throw Py.TypeError("xxx"); + } - public PyObject __add__(PyObject o) { - PyComplex c = (PyComplex)o; + public PyObject __add__(PyObject right) { + if (!canCoerce(right)) + return Py.NotImplemented; + PyComplex c = coerce(right); return new PyComplex(real+c.real, imag+c.imag); } + + public PyObject __radd__(PyObject left) { + return __add__(left); + } + + private final static PyObject _sub(PyComplex o1, PyComplex o2) { + return new PyComplex(o1.real-o2.real, o1.imag-o2.imag); + } + + public PyObject __sub__(PyObject right) { + if (!canCoerce(right)) + return Py.NotImplemented; + return _sub(this, coerce(right)); + } + + public PyObject __rsub__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + return _sub(coerce(left), this); + } - public PyObject __sub__(PyObject o) { - PyComplex c = (PyComplex)o; - return new PyComplex(real-c.real, imag-c.imag); + private final static PyObject _mul(PyComplex o1, PyComplex o2) { + return new PyComplex(o1.real*o2.real-o1.imag*o2.imag, + o1.real*o2.imag+o1.imag*o2.real); } - public PyObject __mul__(PyObject o) { - PyComplex c = (PyComplex)o; - return new PyComplex(real*c.real-imag*c.imag, real*c.imag+imag*c.real); + public PyObject __mul__(PyObject right) { + if (!canCoerce(right)) + return Py.NotImplemented; + return _mul(this, coerce(right)); } - public PyObject __div__(PyObject o) { - PyComplex c = (PyComplex)o; - double denom = c.real*c.real+c.imag*c.imag; + public PyObject __rmul__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + return _mul(coerce(left), this); + } + + private final static PyObject _div(PyComplex o1, PyComplex o2) { + double denom = o2.real*o2.real+o2.imag*o2.imag; if (denom == 0) throw Py.ZeroDivisionError("complex division"); - return new PyComplex((real*c.real + imag*c.imag)/denom, - (imag*c.real - real*c.imag)/denom); + return new PyComplex((o1.real*o2.real + o1.imag*o2.imag)/denom, + (o1.imag*o2.real - o1.real*o2.imag)/denom); + } + + public PyObject __div__(PyObject right) { + if (!canCoerce(right)) + return Py.NotImplemented; + return _div(this, coerce(right)); + } + + public PyObject __rdiv__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + return _div(coerce(left), this); } + + public PyObject __mod__(PyObject right) { + if (!canCoerce(right)) + return Py.NotImplemented; + return _mod(this, coerce(right)); + } + + public PyObject __rmod__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + return _mod(coerce(left), this); + } - public PyObject __mod__(PyObject o) { - PyComplex z = (PyComplex)__div__(o); + private static PyObject _mod(PyComplex value, PyComplex right) { + PyComplex z = (PyComplex)value.__div__(right); z.real = Math.floor(z.real); z.imag = 0.0; - return __sub__(z.__mul__(o)); + return value.__sub__(z.__mul__(right)); } - public PyObject __divmod__(PyObject o) { - PyComplex z = (PyComplex)__div__(o); + public PyObject __divmod__(PyObject right) { + if (!canCoerce(right)) + return Py.NotImplemented; + return _divmod(this, coerce(right)); + } + + public PyObject __rdivmod__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + return _divmod(coerce(left), this); + } + private static PyObject _divmod(PyComplex value, PyComplex right) { + PyComplex z = (PyComplex)value.__div__(right); + z.real = Math.floor(z.real); z.imag = 0.0; - return new PyTuple(new PyObject[] {z, __sub__(z.__mul__(o))}); + return new PyTuple(new PyObject[] {z, value.__sub__(z.__mul__(right))}); } - private PyObject ipow(int iexp) { + private static PyObject ipow(PyComplex value, int iexp) { int pow = iexp; if (pow < 0) pow = -pow; - double xr = real; - double xi = imag; + double xr = value.real; + double xi = value.imag; double zr = 1; double zi = 0; @@ -170,11 +249,22 @@ if (modulo != null) { throw Py.ValueError("complex modulo"); } - - double xr = real; - double xi = imag; - double yr = ((PyComplex)right).real; - double yi = ((PyComplex)right).imag; + if (!canCoerce(right)) + return Py.NotImplemented; + return _pow(this, coerce(right)); + } + + public PyObject __rpow__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + return _pow(coerce(left), this); + } + + public static PyObject _pow(PyComplex value, PyComplex right) { + double xr = value.real; + double xi = value.imag; + double yr = right.real; + double yi = right.imag; if (yr == 0 && yi == 0) { return new PyComplex(1, 0); @@ -189,7 +279,7 @@ // Check for integral powers int iexp = (int)yr; if (yi == 0 && yr == (double)iexp && iexp >= -128 && iexp <= 128) { - return ipow(iexp); + return ipow(value, iexp); } double abs = ExtraMath.hypot(xr, xi); @@ -235,5 +325,15 @@ public PyComplex conjugate() { return new PyComplex(real, -imag); + } + + public boolean isMappingType() { return false; } + public boolean isSequenceType() { return false; } + + // __class__ boilerplate -- see PyObject for details + public static PyClass __class__; + + protected PyClass getPyClass() { + return __class__; } } Index: PyFloat.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyFloat.java,v retrieving revision 2.3 diff -u -r2.3 PyFloat.java --- PyFloat.java 2000/12/11 18:39:45 2.3 +++ PyFloat.java 2001/01/25 19:54:24 @@ -89,26 +89,79 @@ } } + private static final boolean canCoerce(PyObject other) { + return other instanceof PyFloat || other instanceof PyInteger || + other instanceof PyLong; + } + + private static final double coerce(PyObject other) { + if (other instanceof PyFloat) + return ((PyFloat) other).value; + else if (other instanceof PyInteger) + return ((PyInteger) other).getValue(); + else if (other instanceof PyLong) + return ((PyLong) other).doubleValue(); + else + throw Py.TypeError("xxx"); + } + + + public PyObject __add__(PyObject right) { - return new PyFloat(value+((PyFloat)right).value); + if (!canCoerce(right)) + return Py.NotImplemented; + double rightv = coerce(right); + return new PyFloat(value + rightv); } + public PyObject __radd__(PyObject left) { + return __add__(left); + } + public PyObject __sub__(PyObject right) { - return new PyFloat(value-((PyFloat)right).value); + if (!canCoerce(right)) + return Py.NotImplemented; + double rightv = coerce(right); + return new PyFloat(value - rightv); } + public PyObject __rsub__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + double leftv = coerce(left); + return new PyFloat(leftv - value); + } + public PyObject __mul__(PyObject right) { - return new PyFloat(value*((PyFloat)right).value); + if (!canCoerce(right)) + return Py.NotImplemented; + double rightv = coerce(right); + return new PyFloat(value * rightv); + } + + public PyObject __rmul__(PyObject left) { + return __mul__(left); } public PyObject __div__(PyObject right) { - double y = ((PyFloat)right).value; - if (y == 0) + if (!canCoerce(right)) + return Py.NotImplemented; + double rightv = coerce(right); + if (rightv == 0) + throw Py.ZeroDivisionError("float division"); + return new PyFloat(value / rightv); + } + + public PyObject __rdiv__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + double leftv = coerce(left); + if (value == 0) throw Py.ZeroDivisionError("float division"); - return new PyFloat(value/y); + return new PyFloat(leftv / value); } - private double modulo(double x, double y) { + private static double modulo(double x, double y) { if (y == 0) throw Py.ZeroDivisionError("float modulo"); double z = Math.IEEEremainder(x, y); @@ -118,27 +171,70 @@ } public PyObject __mod__(PyObject right) { - return new PyFloat(modulo(value, ((PyFloat)right).value)); + if (!canCoerce(right)) + return Py.NotImplemented; + double rightv = coerce(right); + return new PyFloat(modulo(value, rightv)); } + public PyObject __rmod__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + double leftv = coerce(left); + return new PyFloat(modulo(leftv, value)); + } + public PyObject __divmod__(PyObject right) { - double y = ((PyFloat)right).value; - if (y == 0) + if (!canCoerce(right)) + return Py.NotImplemented; + double rightv = coerce(right); + + if (rightv == 0) throw Py.ZeroDivisionError("float division"); - double z = Math.floor(value/y); + double z = Math.floor(value / rightv); return new PyTuple( - new PyObject[] {new PyFloat(z), new PyFloat(value-z*y)} + new PyObject[] {new PyFloat(z), new PyFloat(value-z*rightv)} ); } + public PyObject __rdivmod__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + double leftv = coerce(left); + + if (value == 0) + throw Py.ZeroDivisionError("float division"); + double z = Math.floor(leftv / value); + + return new PyTuple( + new PyObject[] {new PyFloat(z), new PyFloat(leftv-z*value)} + ); + } + + public PyObject __pow__(PyObject right, PyObject modulo) { - // Rely completely on Java's pow function + if (!canCoerce(right)) + return Py.NotImplemented; - double iw = ((PyFloat)right).value; + if (modulo != null && !canCoerce(modulo)) + return Py.NotImplemented; + + return _pow(value, coerce(right), modulo); + } + + public PyObject __rpow__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + + return _pow(coerce(left), value, null); + } + + private static PyFloat _pow(double value, double iw, PyObject modulo) { + // Rely completely on Java's pow function if (iw == 0) { if (modulo != null) - return new PyFloat(modulo(1.0, ((PyFloat)modulo).value)); + return new PyFloat(modulo(1.0, coerce(modulo))); return new PyFloat(1.0); } if (value == 0.0) { @@ -151,7 +247,7 @@ if (modulo == null) { return new PyFloat(ret); } else { - return new PyFloat(modulo(ret, ((PyFloat)modulo).value)); + return new PyFloat(modulo(ret, coerce(modulo))); } } @@ -187,5 +283,15 @@ public PyComplex __complex__() { return new PyComplex(value, 0.); + } + + public boolean isMappingType() { return false; } + public boolean isSequenceType() { return false; } + + // __class__ boilerplate -- see PyObject for details + public static PyClass __class__; + + protected PyClass getPyClass() { + return __class__; } } Index: PyInstance.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyInstance.java,v retrieving revision 2.13 diff -u -r2.13 PyInstance.java --- PyInstance.java 2000/10/08 18:20:09 2.13 +++ PyInstance.java 2001/01/25 19:54:29 @@ -129,10 +129,10 @@ if (tmp != null) c = tmp; } - if (javaProxy == null && __class__.proxyClass != null) { makeProxy(); } + if (c.isInstance(javaProxy)) return javaProxy; @@ -678,7 +678,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __add__(PyObject o) { - return invoke_ex("__add__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__add__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__add__", o2); + else + return o1._add(o2); + } } /** @@ -686,7 +696,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __radd__(PyObject o) { - return invoke_ex("__radd__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__radd__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__radd__", o2); + else + return o2._add(o1); + } } /** @@ -705,7 +725,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __sub__(PyObject o) { - return invoke_ex("__sub__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__sub__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__sub__", o2); + else + return o1._sub(o2); + } } /** @@ -713,7 +743,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __rsub__(PyObject o) { - return invoke_ex("__rsub__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rsub__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rsub__", o2); + else + return o2._sub(o1); + } } /** @@ -732,7 +772,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __mul__(PyObject o) { - return invoke_ex("__mul__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__mul__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__mul__", o2); + else + return o1._mul(o2); + } } /** @@ -740,7 +790,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __rmul__(PyObject o) { - return invoke_ex("__rmul__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rmul__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rmul__", o2); + else + return o2._mul(o1); + } } /** @@ -759,7 +819,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __div__(PyObject o) { - return invoke_ex("__div__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__div__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__div__", o2); + else + return o1._div(o2); + } } /** @@ -767,7 +837,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __rdiv__(PyObject o) { - return invoke_ex("__rdiv__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rdiv__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rdiv__", o2); + else + return o2._div(o1); + } } /** @@ -786,7 +866,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __mod__(PyObject o) { - return invoke_ex("__mod__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__mod__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__mod__", o2); + else + return o1._mod(o2); + } } /** @@ -794,7 +884,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __rmod__(PyObject o) { - return invoke_ex("__rmod__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rmod__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rmod__", o2); + else + return o2._mod(o1); + } } /** @@ -813,7 +913,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __divmod__(PyObject o) { - return invoke_ex("__divmod__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__divmod__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__divmod__", o2); + else + return o1._divmod(o2); + } } /** @@ -821,7 +931,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __rdivmod__(PyObject o) { - return invoke_ex("__rdivmod__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rdivmod__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rdivmod__", o2); + else + return o2._divmod(o1); + } } /** @@ -829,7 +949,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __pow__(PyObject o) { - return invoke_ex("__pow__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__pow__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__pow__", o2); + else + return o1._pow(o2); + } } /** @@ -837,7 +967,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __rpow__(PyObject o) { - return invoke_ex("__rpow__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rpow__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rpow__", o2); + else + return o2._pow(o1); + } } /** @@ -856,7 +996,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __lshift__(PyObject o) { - return invoke_ex("__lshift__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__lshift__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__lshift__", o2); + else + return o1._lshift(o2); + } } /** @@ -864,7 +1014,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __rlshift__(PyObject o) { - return invoke_ex("__rlshift__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rlshift__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rlshift__", o2); + else + return o2._lshift(o1); + } } /** @@ -883,7 +1043,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __rshift__(PyObject o) { - return invoke_ex("__rshift__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rshift__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rshift__", o2); + else + return o1._rshift(o2); + } } /** @@ -891,7 +1061,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __rrshift__(PyObject o) { - return invoke_ex("__rrshift__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rrshift__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rrshift__", o2); + else + return o2._rshift(o1); + } } /** @@ -910,7 +1090,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __and__(PyObject o) { - return invoke_ex("__and__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__and__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__and__", o2); + else + return o1._and(o2); + } } /** @@ -918,7 +1108,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __rand__(PyObject o) { - return invoke_ex("__rand__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rand__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rand__", o2); + else + return o2._and(o1); + } } /** @@ -937,7 +1137,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __or__(PyObject o) { - return invoke_ex("__or__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__or__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__or__", o2); + else + return o1._or(o2); + } } /** @@ -945,7 +1155,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __ror__(PyObject o) { - return invoke_ex("__ror__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__ror__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__ror__", o2); + else + return o2._or(o1); + } } /** @@ -964,7 +1184,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __xor__(PyObject o) { - return invoke_ex("__xor__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__xor__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__xor__", o2); + else + return o1._xor(o2); + } } /** @@ -972,7 +1202,17 @@ * in the instance's dictionary and calling it if it is found. **/ public PyObject __rxor__(PyObject o) { - return invoke_ex("__rxor__", o); + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rxor__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rxor__", o2); + else + return o2._xor(o1); + } } /** @@ -985,4 +1225,5 @@ return ret; return super.__ixor__(o); } + } Index: PyInteger.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyInteger.java,v retrieving revision 2.5 diff -u -r2.5 PyInteger.java --- PyInteger.java 2000/12/11 18:39:45 2.5 +++ PyInteger.java 2001/01/25 19:54:30 @@ -64,27 +64,62 @@ return Py.None; } + private static final boolean canCoerce(PyObject other) { + return other instanceof PyInteger; + } + + private static final int coerce(PyObject other) { + if (other instanceof PyInteger) + return ((PyInteger) other).value; + else + throw Py.TypeError("xxx"); + } + + public PyObject __add__(PyObject right) { + if (!canCoerce(right)) + return Py.NotImplemented; + int rightv = coerce(right); int a = value; - int b = ((PyInteger)right).value; + int b = rightv; int x = a + b; if ((x^a) < 0 && (x^b) < 0) throw Py.OverflowError("integer addition: "+this+" + "+right); - return Py.newInteger(x); //new PyInteger(x); + return Py.newInteger(x); + } + public PyObject __radd__(PyObject left) { + return __add__(left); } - public PyObject __sub__(PyObject right) { - int a = value; - int b = ((PyInteger)right).value; + private static PyInteger _sub(int a, int b) { int x = a - b; if ((x^a) < 0 && (x^~b) < 0) - throw Py.OverflowError("integer subtraction: "+this+" - "+right); + throw Py.OverflowError("integer subtraction: "+a+" - "+b); return Py.newInteger(x); } + public PyObject __sub__(PyObject right) { + if (!canCoerce(right)) + return Py.NotImplemented; + return _sub(value, coerce(right)); + } + + public PyObject __rsub__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + return _sub(coerce(left), value); + } + public PyObject __mul__(PyObject right) { + if (right instanceof PySequence) + return ((PySequence) right).repeat(value); + + if (!canCoerce(right)) + return Py.NotImplemented; + int rightv = coerce(right); + double x = (double)value; - x *= ((PyInteger)right).value; + x *= rightv; //long x = ((long)value)*((PyInteger)right).value; //System.out.println("mul: "+this+" * "+right+" = "+x); @@ -94,9 +129,13 @@ return Py.newInteger((int)x); } + public PyObject __rmul__(PyObject left) { + return __mul__(left); + } + // Getting signs correct for integer division // This convention makes sense when you consider it in tandem with modulo - private int divide(int x, int y) { + private static int divide(int x, int y) { if (y == 0) throw Py.ZeroDivisionError("integer division or modulo"); @@ -111,30 +150,69 @@ } public PyObject __div__(PyObject right) { - return Py.newInteger(divide(value, ((PyInteger)right).value)); + if (!canCoerce(right)) + return Py.NotImplemented; + return Py.newInteger(divide(value, coerce(right))); } - private int modulo(int x, int y, int xdivy) { + public PyObject __rdiv__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + return Py.newInteger(divide(coerce(left), value)); + } + + private static int modulo(int x, int y, int xdivy) { return x - xdivy*y; } public PyObject __mod__(PyObject right) { - int x = ((PyInteger)right).value; - return Py.newInteger(modulo(value, x, divide(value, x))); + if (!canCoerce(right)) + return Py.NotImplemented; + int rightv = coerce(right); + return Py.newInteger(modulo(value, rightv, divide(value, rightv))); } + public PyObject __rmod__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + int leftv = coerce(left); + return Py.newInteger(modulo(leftv, value, divide(leftv, value))); + } + public PyObject __divmod__(PyObject right) { - int x = ((PyInteger)right).value; - int xdivy = divide(value, x); + if (!canCoerce(right)) + return Py.NotImplemented; + int rightv = coerce(right); + + int xdivy = divide(value, rightv); return new PyTuple(new PyObject[] { new PyInteger(xdivy), - new PyInteger(modulo(value, x, xdivy)) + new PyInteger(modulo(value, rightv, xdivy)) }); } public PyObject __pow__(PyObject right, PyObject modulo) { + if (!canCoerce(right)) + return Py.NotImplemented; + + if (modulo != null && !canCoerce(modulo)) + return Py.NotImplemented; + + return _pow(value, coerce(right), modulo); + } + + public PyObject __rpow__(PyObject left, PyObject modulo) { + if (!canCoerce(left)) + return Py.NotImplemented; + + if (modulo != null && !canCoerce(modulo)) + return Py.NotImplemented; + + return _pow(coerce(left), value, modulo); + } + + private static PyInteger _pow(int value, int pow, PyObject modulo) { int mod = 0; - int pow = ((PyInteger)right).value; long tmp = value; boolean neg = false; if (tmp < 0) { @@ -151,7 +229,7 @@ } if (modulo != null) { - mod = ((PyInteger)modulo).value; + mod = coerce(modulo); if (mod == 0) { throw Py.ValueError("pow(x, y, z) with z==0"); } @@ -195,26 +273,55 @@ } public PyObject __lshift__(PyObject right) { - int shift = ((PyInteger)right).value; - if (shift > 31) + int rightv; + if (right instanceof PyInteger) + rightv = ((PyInteger)right).value; + else + return Py.NotImplemented; + + if (rightv > 31) return new PyInteger(0); - return Py.newInteger(value << shift); + return Py.newInteger(value << rightv); } public PyObject __rshift__(PyObject right) { - return Py.newInteger(value >>> ((PyInteger)right).value); + int rightv; + if (right instanceof PyInteger) + rightv = ((PyInteger)right).value; + else + return Py.NotImplemented; + + return Py.newInteger(value >>> rightv); } public PyObject __and__(PyObject right) { - return Py.newInteger(value & ((PyInteger)right).value); + int rightv; + if (right instanceof PyInteger) + rightv = ((PyInteger)right).value; + else + return Py.NotImplemented; + + return Py.newInteger(value & rightv); } public PyObject __xor__(PyObject right) { - return Py.newInteger(value ^ ((PyInteger)right).value); + int rightv; + if (right instanceof PyInteger) + rightv = ((PyInteger)right).value; + else + return Py.NotImplemented; + + return Py.newInteger(value ^ rightv); } public PyObject __or__(PyObject right) { - return Py.newInteger(value | ((PyInteger)right).value); + int rightv; + if (right instanceof PyInteger) + rightv = ((PyInteger)right).value; + else + return Py.NotImplemented; + + return Py.newInteger(value | rightv); } public PyObject __neg__() { Index: PyList.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyList.java,v retrieving revision 2.15 diff -u -r2.15 PyList.java --- PyList.java 2000/10/09 14:45:14 2.15 +++ PyList.java 2001/01/25 19:54:34 @@ -256,6 +256,8 @@ } public PyObject __imul__(PyObject o) { + if (!(o instanceof PyInteger || o instanceof PyLong)) + throw Py.TypeError("can't multiply sequence to non-int"); int l = length; int count = o.__int__().getValue(); @@ -387,7 +389,7 @@ } public PyObject __iadd__(PyObject o) { - extend(o); + extend(fastSequence(o, "argument to += must be a sequence")); return this; } Index: PyLong.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyLong.java,v retrieving revision 2.6 diff -u -r2.6 PyLong.java --- PyLong.java 2000/12/20 13:44:43 2.6 +++ PyLong.java 2001/01/25 19:54:35 @@ -108,16 +108,55 @@ } } + private static final boolean canCoerce(PyObject other) { + return other instanceof PyLong || other instanceof PyInteger; + } + + private static final BigInteger coerce(PyObject other) { + if (other instanceof PyLong) + return ((PyLong) other).value; + else if (other instanceof PyInteger) + return java.math.BigInteger.valueOf(((PyInteger) other).getValue()); + else + throw Py.TypeError("xxx"); + } + + public PyObject __add__(PyObject right) { - return new PyLong(value.add(((PyLong)right).value)); + if (!canCoerce(right)) + return Py.NotImplemented; + return new PyLong(value.add(coerce(right))); } + public PyObject __radd__(PyObject left) { + return __add__(left); + } + public PyObject __sub__(PyObject right) { - return new PyLong(value.subtract(((PyLong)right).value)); + if (!canCoerce(right)) + return Py.NotImplemented; + return new PyLong(value.subtract(coerce(right))); + } + + public PyObject __rsub__(PyObject left) { + return new PyLong(coerce(left).subtract(value)); } public PyObject __mul__(PyObject right) { - return new PyLong(value.multiply(((PyLong)right).value)); + if (right instanceof PySequence) + return ((PySequence) right).repeat(coerceInt(this)); + + if (!canCoerce(right)) + return Py.NotImplemented; + return new PyLong(value.multiply(coerce(right))); + } + + public PyObject __rmul__(PyObject left) { + if (left instanceof PySequence) + return ((PySequence) left).repeat(coerceInt(this)); + if (!canCoerce(left)) + return Py.NotImplemented; + return new PyLong(coerce(left).multiply(value)); } // Getting signs correct for integer division @@ -138,7 +177,15 @@ } public PyObject __div__(PyObject right) { - return new PyLong(divide(value, ((PyLong)right).value)); + if (!canCoerce(right)) + return Py.NotImplemented; + return new PyLong(divide(value, coerce(right))); + } + + public PyObject __rdiv__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + return new PyLong(divide(coerce(left), value)); } private BigInteger modulo(BigInteger x, BigInteger y, BigInteger xdivy) { @@ -146,21 +193,59 @@ } public PyObject __mod__(PyObject right) { - BigInteger y = ((PyLong)right).value; - return new PyLong(modulo(value, y, divide(value, y))); + if (!canCoerce(right)) + return Py.NotImplemented; + BigInteger rightv = coerce(right); + return new PyLong(modulo(value, rightv, divide(value, rightv))); } + public PyObject __rmod__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + BigInteger leftv = coerce(left); + return new PyLong(modulo(leftv, value, divide(leftv, value))); + } + public PyObject __divmod__(PyObject right) { - BigInteger y = ((PyLong)right).value; - BigInteger xdivy = divide(value, y); + if (!canCoerce(right)) + return Py.NotImplemented; + BigInteger rightv = coerce(right); + + BigInteger xdivy = divide(value, rightv); return new PyTuple(new PyObject[] { new PyLong(xdivy), - new PyLong(modulo(value, y, xdivy)) + new PyLong(modulo(value, rightv, xdivy)) }); } + public PyObject __rdivmod__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + BigInteger leftv = coerce(left); + + BigInteger xdivy = divide(leftv, value); + return new PyTuple(new PyObject[] { + new PyLong(xdivy), + new PyLong(modulo(leftv, value, xdivy)) + }); + } + public PyObject __pow__(PyObject right, PyObject modulo) { - BigInteger y = ((PyLong)right).value; + if (!canCoerce(right)) + return Py.NotImplemented; + + if (modulo != null && !canCoerce(right)) + return Py.NotImplemented; + return _pow(value, coerce(right), modulo); + } + + public PyObject __rpow__(PyObject left) { + if (!canCoerce(left)) + return Py.NotImplemented; + return _pow(coerce(left), value, null); + } + + public static PyLong _pow(BigInteger value, BigInteger y, PyObject modulo) { if (y.compareTo(BigInteger.valueOf(0)) < 0) { if (value.compareTo(BigInteger.valueOf(0)) != 0) throw Py.ValueError("long integer to a negative power"); @@ -173,7 +258,7 @@ // This whole thing can be trivially rewritten after bugs in modPow // are fixed by SUN - BigInteger z = ((PyLong)modulo).value; + BigInteger z = coerce(modulo); int zi = z.intValue(); // Clear up some special cases right away if (zi == 0) @@ -202,24 +287,43 @@ } } + private static final int coerceInt(PyObject other) { + if (other instanceof PyLong) + return (int) ((PyLong) other).getLong(Integer.MIN_VALUE, Integer.MAX_VALUE); + else if (other instanceof PyInteger) + return ((PyInteger) other).getValue(); + else + throw Py.TypeError("xxx"); + } + public PyObject __lshift__(PyObject right) { - return new PyLong(value.shiftLeft(((PyLong)right).value.intValue())); + if (!canCoerce(right)) + return Py.NotImplemented; + return new PyLong(value.shiftLeft(coerceInt(right))); } public PyObject __rshift__(PyObject right) { - return new PyLong(value.shiftRight(((PyLong)right).value.intValue())); + if (!canCoerce(right)) + return Py.NotImplemented; + return new PyLong(value.shiftRight(coerceInt(right))); } public PyObject __and__(PyObject right) { - return new PyLong(value.and(((PyLong)right).value)); + if (!canCoerce(right)) + return Py.NotImplemented; + return new PyLong(value.and(coerce(right))); } public PyObject __xor__(PyObject right) { - return new PyLong(value.xor(((PyLong)right).value)); + if (!canCoerce(right)) + return Py.NotImplemented; + return new PyLong(value.xor(coerce(right))); } public PyObject __or__(PyObject right) { - return new PyLong(value.or(((PyLong)right).value)); + if (!canCoerce(right)) + return Py.NotImplemented; + return new PyLong(value.or(coerce(right))); } public PyObject __neg__() { @@ -274,5 +378,15 @@ public PyString __str__() { return Py.newString(value.toString()); + } + + public boolean isMappingType() { return false; } + public boolean isSequenceType() { return false; } + + // __class__ boilerplate -- see PyObject for details + public static PyClass __class__; + + protected PyClass getPyClass() { + return __class__; } } Index: PyNone.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyNone.java,v retrieving revision 2.3 diff -u -r2.3 PyNone.java --- PyNone.java 1999/10/04 20:44:28 2.3 +++ PyNone.java 2001/01/25 19:54:35 @@ -23,4 +23,14 @@ protected String safeRepr() { return "'None' object"; } + + public boolean isMappingType() { return false; } + public boolean isSequenceType() { return false; } + + // __class__ boilerplate -- see PyObject for details + public static PyClass __class__; + + protected PyClass getPyClass() { + return __class__; + } } Index: PyObject.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyObject.java,v retrieving revision 2.16 diff -u -r2.16 PyObject.java --- PyObject.java 2001/01/21 16:19:52 2.16 +++ PyObject.java 2001/01/25 19:54:43 @@ -1086,6 +1086,8 @@ **/ public PyObject __pow__(PyObject o2, PyObject o3) throws PyException { return null; } + + // Generated by make_binops.py (Begin) /** @@ -1122,45 +1124,13 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _add(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__add__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__add__(o2); - else - o1 = o2.__radd__(o1); - if (o1 != null) - return o1; - } + public final PyObject _add(PyObject o2) { + PyObject x = __add__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__radd__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__add__ nor __radd__ defined for these operands"); } @@ -1199,45 +1169,13 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _sub(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__sub__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__sub__(o2); - else - o1 = o2.__rsub__(o1); - if (o1 != null) - return o1; - } + public final PyObject _sub(PyObject o2) { + PyObject x = __sub__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__rsub__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__sub__ nor __rsub__ defined for these operands"); } @@ -1276,45 +1214,13 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _mul(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__mul__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__mul__(o2); - else - o1 = o2.__rmul__(o1); - if (o1 != null) - return o1; - } + public final PyObject _mul(PyObject o2) { + PyObject x = __mul__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__rmul__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__mul__ nor __rmul__ defined for these operands"); } @@ -1353,45 +1259,13 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _div(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__div__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__div__(o2); - else - o1 = o2.__rdiv__(o1); - if (o1 != null) - return o1; - } + public final PyObject _div(PyObject o2) { + PyObject x = __div__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__rdiv__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__div__ nor __rdiv__ defined for these operands"); } @@ -1430,45 +1304,13 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _mod(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__mod__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__mod__(o2); - else - o1 = o2.__rmod__(o1); - if (o1 != null) - return o1; - } + public final PyObject _mod(PyObject o2) { + PyObject x = __mod__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__rmod__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__mod__ nor __rmod__ defined for these operands"); } @@ -1507,45 +1349,13 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _divmod(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__divmod__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__divmod__(o2); - else - o1 = o2.__rdivmod__(o1); - if (o1 != null) - return o1; - } + public final PyObject _divmod(PyObject o2) { + PyObject x = __divmod__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__rdivmod__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__divmod__ nor __rdivmod__ defined for these operands"); } @@ -1584,45 +1394,13 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _pow(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__pow__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__pow__(o2); - else - o1 = o2.__rpow__(o1); - if (o1 != null) - return o1; - } + public final PyObject _pow(PyObject o2) { + PyObject x = __pow__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__rpow__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__pow__ nor __rpow__ defined for these operands"); } @@ -1661,45 +1439,13 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _lshift(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__lshift__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__lshift__(o2); - else - o1 = o2.__rlshift__(o1); - if (o1 != null) - return o1; - } + public final PyObject _lshift(PyObject o2) { + PyObject x = __lshift__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__rlshift__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__lshift__ nor __rlshift__ defined for these operands"); } @@ -1738,45 +1484,13 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _rshift(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__rshift__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__rshift__(o2); - else - o1 = o2.__rrshift__(o1); - if (o1 != null) - return o1; - } + public final PyObject _rshift(PyObject o2) { + PyObject x = __rshift__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__rrshift__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__rshift__ nor __rrshift__ defined for these operands"); } @@ -1815,45 +1529,13 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _and(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__and__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__and__(o2); - else - o1 = o2.__rand__(o1); - if (o1 != null) - return o1; - } + public final PyObject _and(PyObject o2) { + PyObject x = __and__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__rand__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__and__ nor __rand__ defined for these operands"); } @@ -1892,45 +1574,13 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _or(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__or__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__or__(o2); - else - o1 = o2.__ror__(o1); - if (o1 != null) - return o1; - } + public final PyObject _or(PyObject o2) { + PyObject x = __or__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__ror__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__or__ nor __ror__ defined for these operands"); } @@ -1969,50 +1619,19 @@ * @exception PyTypeError if this operation can't be performed * with these operands. **/ - public final PyObject _xor(PyObject o2_in) { - PyObject o2 = o2_in; - PyObject o1 = this; - Object ctmp; - if (o1.__class__ != o2.__class__) { - ctmp = o1.__coerce_ex__(o2); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o1 = ((PyObject[])ctmp)[0]; - o2 = ((PyObject[])ctmp)[1]; - } else { - o2 = (PyObject)ctmp; - } - } - } else ctmp = null; - - if (ctmp != Py.None && (o1 = o1.__xor__(o2)) != null) - return o1; - o1 = this; - o2 = o2_in; - if (o1.__class__ != o2.__class__) { - ctmp=o2.__coerce_ex__(o1); - if (ctmp != null) { - if (ctmp instanceof PyObject[]) { - o2 = ((PyObject[])ctmp)[0]; - o1 = ((PyObject[])ctmp)[1]; - } else { - o1 = (PyObject)ctmp; - } - } - } - if (ctmp != Py.None) { - if (o1.__class__ == o2.__class__) - o1 = o1.__xor__(o2); - else - o1 = o2.__rxor__(o1); - if (o1 != null) - return o1; - } + public final PyObject _xor(PyObject o2) { + PyObject x = __xor__(o2); + if (x != null && x != Py.NotImplemented) + return x; + x = o2.__rxor__(this); + if (x != null && x != Py.NotImplemented) + return x; throw Py.TypeError( "__xor__ nor __rxor__ defined for these operands"); } // Generated by make_binops.py (End) + /* A convenience function for PyProxy's */ Index: PySequence.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PySequence.java,v retrieving revision 2.12 diff -u -r2.12 PySequence.java --- PySequence.java 2000/10/09 14:45:14 2.12 +++ PySequence.java 2001/01/25 19:54:47 @@ -201,6 +201,23 @@ return s1 < s2 ? -1 : (s1 > s2 ? 1: 0); } + protected static PyObject fastSequence(PyObject seq, String msg) { + if (seq instanceof PyList || seq instanceof PyTuple) + return seq; + + if (!seq.isSequenceType()) + throw Py.TypeError(msg); + + int n = seq.__len__(); + + PyList list = new PyList(); + PyObject item; + for (int i = 0; (item = list.__finditem__(i)) != null; i++) { + list.append(item); + } + return list; + } + protected static final int sliceLength(int start, int stop, int step) { //System.err.println("slice: "+start+", "+stop+", "+step); int ret; @@ -395,7 +412,7 @@ } } } - +/* public PyObject __mul__(PyObject count) { int repeats = Py.py2int(count, "can't multiply sequence with non-int"); return repeat(repeats >= 0 ? repeats : 0); @@ -404,7 +421,7 @@ public PyObject __rmul__(PyObject count) { return __mul__(count); } - +*/ public synchronized Object __tojava__(Class c) { if (c.isArray()) { Class component = c.getComponentType();