From: <pj...@us...> - 2010-03-14 21:40:07
|
Revision: 6985 http://jython.svn.sourceforge.net/jython/?rev=6985&view=rev Author: pjenvey Date: 2010-03-14 21:39:57 +0000 (Sun, 14 Mar 2010) Log Message: ----------- coding standards/cleanup Modified Paths: -------------- trunk/jython/src/org/python/core/PyComplex.java trunk/jython/src/org/python/core/PyFloat.java trunk/jython/src/org/python/core/PyInteger.java trunk/jython/src/org/python/core/PyLong.java Modified: trunk/jython/src/org/python/core/PyComplex.java =================================================================== --- trunk/jython/src/org/python/core/PyComplex.java 2010-03-13 17:33:14 UTC (rev 6984) +++ trunk/jython/src/org/python/core/PyComplex.java 2010-03-14 21:39:57 UTC (rev 6985) @@ -1,4 +1,7 @@ -// Copyright (c) Corporation for National Research Initiatives +/* + * Copyright (c) Corporation for National Research Initiatives + * Copyright (c) Jython Developers + */ package org.python.core; import org.python.expose.ExposedGet; @@ -15,14 +18,28 @@ public static final PyType TYPE = PyType.fromClass(PyComplex.class); + static PyComplex J = new PyComplex(0, 1.); + @ExposedGet(doc = BuiltinDocs.complex_real_doc) public double real; @ExposedGet(doc = BuiltinDocs.complex_imag_doc) public double imag; - static PyComplex J = new PyComplex(0, 1.); + public PyComplex(PyType subtype, double r, double i) { + super(subtype); + real = r; + imag = i; + } + public PyComplex(double r, double i) { + this(TYPE, r, i); + } + + public PyComplex(double r) { + this(r, 0.0); + } + @ExposedNew public static PyObject complex_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { @@ -58,7 +75,7 @@ PyComplex complexImag; PyFloat toFloat = null; if (real instanceof PyComplex) { - complexReal = (PyComplex)real; + complexReal = (PyComplex) real; } else { try { toFloat = real.__float__(); @@ -75,7 +92,7 @@ if (imag == null) { complexImag = new PyComplex(0.0); } else if (imag instanceof PyComplex) { - complexImag = (PyComplex)imag; + complexImag = (PyComplex) imag; } else { toFloat = null; try { @@ -98,20 +115,6 @@ return complexReal; } - public PyComplex(PyType subtype, double r, double i) { - super(subtype); - real = r; - imag = i; - } - - public PyComplex(double r, double i) { - this(TYPE, r, i); - } - - public PyComplex(double r) { - this(r, 0.0); - } - public final PyFloat getReal() { return Py.newFloat(real); } @@ -121,14 +124,14 @@ } public static String toString(double value) { - if (value == Math.floor(value) && - value <= Long.MAX_VALUE && value >= Long.MIN_VALUE) { - return Long.toString((long)value); + if (value == Math.floor(value) && value <= Long.MAX_VALUE && value >= Long.MIN_VALUE) { + return Long.toString((long) value); } else { return Double.toString(value); } } + @Override public String toString() { return complex_toString(); } @@ -136,16 +139,17 @@ @ExposedMethod(names = {"__repr__", "__str__"}, doc = BuiltinDocs.complex___str___doc) final String complex_toString() { if (real == 0.) { - return toString(imag)+"j"; + return toString(imag) + "j"; } else { if (imag >= 0) { - return "("+toString(real)+"+"+toString(imag)+"j)"; + return String.format("(%s+%sj)", toString(real), toString(imag)); } else { - return "("+toString(real)+"-"+toString(-imag)+"j)"; + return String.format("(%s-%sj)", toString(real), toString(-imag)); } } } + @Override public int hashCode() { return complex___hash__(); } @@ -155,12 +159,12 @@ if (imag == 0) { return new PyFloat(real).hashCode(); } else { - long v = Double.doubleToLongBits(real) ^ - Double.doubleToLongBits(imag); - return (int)v ^ (int)(v >> 32); + long v = Double.doubleToLongBits(real) ^ Double.doubleToLongBits(imag); + return (int) v ^ (int) (v >> 32); } } + @Override public boolean __nonzero__() { return complex___nonzero__(); } @@ -170,18 +174,17 @@ return real != 0 || imag != 0; } - /*public Object __tojava__(Class c) { - return super.__tojava__(c); - }*/ - + @Override public int __cmp__(PyObject other) { - if (!canCoerce(other)) + if (!canCoerce(other)) { return -2; + } PyComplex c = coerce(other); double oreal = c.real; double oimag = c.imag; - if (real == oreal && imag == oimag) + if (real == oreal && imag == oimag) { return 0; + } if (real != oreal) { return real < oreal ? -1 : 1; } else { @@ -189,42 +192,42 @@ } } - /* - * @see org.python.core.PyObject#__eq__(org.python.core.PyObject) - */ + @Override public PyObject __eq__(PyObject other) { return complex___eq__(other); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___eq___doc) final PyObject complex___eq__(PyObject other) { - if (!canCoerce(other)) + if (!canCoerce(other)) { return null; + } PyComplex c = coerce(other); return Py.newBoolean(real == c.real && imag == c.imag); } - /* - * @see org.python.core.PyObject#__ne__(org.python.core.PyObject) - */ + @Override public PyObject __ne__(PyObject other) { return complex___ne__(other); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___ne___doc) final PyObject complex___ne__(PyObject other) { - if (!canCoerce(other)) + if (!canCoerce(other)) { return null; + } PyComplex c = coerce(other); return Py.newBoolean(real != c.real || imag != c.imag); } private PyObject unsupported_comparison(PyObject other) { - if (!canCoerce(other)) + if (!canCoerce(other)) { return null; + } throw Py.TypeError("cannot compare complex numbers using <, <=, >, >="); } + @Override public PyObject __ge__(PyObject other) { return complex___ge__(other); } @@ -234,6 +237,7 @@ return unsupported_comparison(other); } + @Override public PyObject __gt__(PyObject other) { return complex___gt__(other); } @@ -243,6 +247,7 @@ return unsupported_comparison(other); } + @Override public PyObject __le__(PyObject other) { return complex___le__(other); } @@ -252,6 +257,7 @@ return unsupported_comparison(other); } + @Override public PyObject __lt__(PyObject other) { return complex___lt__(other); } @@ -261,62 +267,72 @@ return unsupported_comparison(other); } + @Override public Object __coerce_ex__(PyObject other) { return complex___coerce_ex__(other); } - + @ExposedMethod(doc = BuiltinDocs.complex___coerce___doc) final PyObject complex___coerce__(PyObject other) { return adaptToCoerceTuple(complex___coerce_ex__(other)); } - /** + /** * Coercion logic for complex. Implemented as a final method to avoid - * invocation of virtual methods from the exposed coerce. - */ + * invocation of virtual methods from the exposed coerce. + */ final PyObject complex___coerce_ex__(PyObject other) { - if (other instanceof PyComplex) + if (other instanceof PyComplex) { return other; - if (other instanceof PyFloat) - return new PyComplex(((PyFloat)other).getValue(), 0); - if (other instanceof PyInteger) - return new PyComplex(((PyInteger)other).getValue(), 0); - if (other instanceof PyLong) - return new PyComplex(((PyLong)other).doubleValue(), 0); + } + if (other instanceof PyFloat) { + return new PyComplex(((PyFloat) other).getValue(), 0); + } + if (other instanceof PyInteger) { + return new PyComplex(((PyInteger) other).getValue(), 0); + } + if (other instanceof PyLong) { + return new PyComplex(((PyLong) other).doubleValue(), 0); + } return Py.None; } private final boolean canCoerce(PyObject other) { - return other instanceof PyComplex || - other instanceof PyFloat || - other instanceof PyInteger || - other instanceof PyLong; + return other instanceof PyComplex || other instanceof PyFloat || other instanceof PyInteger + || other instanceof PyLong; } private final PyComplex coerce(PyObject other) { - if (other instanceof PyComplex) + 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(((PyInteger)other).getValue(), 0); - if (other instanceof PyLong) - return new PyComplex(((PyLong)other).doubleValue(), 0); + } + if (other instanceof PyFloat) { + return new PyComplex(((PyFloat) other).getValue(), 0); + } + if (other instanceof PyInteger) { + return new PyComplex(((PyInteger) other).getValue(), 0); + } + if (other instanceof PyLong) { + return new PyComplex(((PyLong) other).doubleValue(), 0); + } throw Py.TypeError("xxx"); } + @Override public PyObject __add__(PyObject right) { return complex___add__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___add___doc) final PyObject complex___add__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } PyComplex c = coerce(right); - return new PyComplex(real+c.real, imag+c.imag); + return new PyComplex(real + c.real, imag + c.imag); } + @Override public PyObject __radd__(PyObject left) { return complex___radd__(left); } @@ -327,55 +343,63 @@ } private final static PyObject _sub(PyComplex o1, PyComplex o2) { - return new PyComplex(o1.real-o2.real, o1.imag-o2.imag); + return new PyComplex(o1.real - o2.real, o1.imag - o2.imag); } + @Override public PyObject __sub__(PyObject right) { return complex___sub__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___sub___doc) final PyObject complex___sub__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } return _sub(this, coerce(right)); } + @Override public PyObject __rsub__(PyObject left) { return complex___rsub__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rsub___doc) final PyObject complex___rsub__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } return _sub(coerce(left), this); } 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); + return new PyComplex(o1.real * o2.real - o1.imag * o2.imag, + o1.real * o2.imag + o1.imag * o2.real); } + @Override public PyObject __mul__(PyObject right) { return complex___mul__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___mul___doc) final PyObject complex___mul__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } return _mul(this, coerce(right)); } + @Override public PyObject __rmul__(PyObject left) { return complex___rmul__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rmul___doc) final PyObject complex___rmul__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } return _mul(coerce(left), this); } @@ -400,95 +424,113 @@ } } + @Override public PyObject __div__(PyObject right) { return complex___div__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___div___doc) final PyObject complex___div__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; - if (Options.divisionWarning >= 2) + } + if (Options.divisionWarning >= 2) { Py.warning(Py.DeprecationWarning, "classic complex division"); + } return _div(this, coerce(right)); } + @Override public PyObject __rdiv__(PyObject left) { return complex___rdiv__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rdiv___doc) final PyObject complex___rdiv__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; - if (Options.divisionWarning >= 2) + } + if (Options.divisionWarning >= 2) { Py.warning(Py.DeprecationWarning, "classic complex division"); + } return _div(coerce(left), this); } + @Override public PyObject __floordiv__(PyObject right) { return complex___floordiv__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___floordiv___doc) final PyObject complex___floordiv__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } return _divmod(this, coerce(right)).__finditem__(0); } + @Override public PyObject __rfloordiv__(PyObject left) { return complex___rfloordiv__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rfloordiv___doc) final PyObject complex___rfloordiv__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } return _divmod(coerce(left), this).__finditem__(0); } + @Override public PyObject __truediv__(PyObject right) { return complex___truediv__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___truediv___doc) final PyObject complex___truediv__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } return _div(this, coerce(right)); } + @Override public PyObject __rtruediv__(PyObject left) { return complex___rtruediv__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rtruediv___doc) final PyObject complex___rtruediv__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } return _div(coerce(left), this); } + @Override public PyObject __mod__(PyObject right) { return complex___mod__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___mod___doc) final PyObject complex___mod__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } return _mod(this, coerce(right)); } + @Override public PyObject __rmod__(PyObject left) { return complex___rmod__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rmod___doc) final PyObject complex___rmod__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } return _mod(coerce(left), this); } @@ -502,25 +544,29 @@ return value.__sub__(z.__mul__(right)); } + @Override public PyObject __divmod__(PyObject right) { return complex___divmod__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___divmod___doc) final PyObject complex___divmod__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } return _divmod(this, coerce(right)); } + @Override public PyObject __rdivmod__(PyObject left) { return complex___rdivmod__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rdivmod___doc) final PyObject complex___rdivmod__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } return _divmod(coerce(left), this); } @@ -534,10 +580,11 @@ return new PyTuple(z, value.__sub__(z.__mul__(right))); } - private static PyObject ipow(PyComplex value, int iexp) { int pow = iexp; - if (pow < 0) pow = -pow; + if (pow < 0) { + pow = -pow; + } double xr = value.real; double xi = value.imag; @@ -549,25 +596,28 @@ while (pow > 0) { if ((pow & 0x1) != 0) { - tmp = zr*xr - zi*xi; - zi = zi*xr + zr*xi; + tmp = zr * xr - zi * xi; + zi = zi * xr + zr * xi; zr = tmp; } pow >>= 1; - if (pow == 0) + if (pow == 0) { break; - tmp = xr*xr - xi*xi; - xi = xr*xi*2; + } + tmp = xr * xr - xi * xi; + xi = xr * xi * 2; xr = tmp; } PyComplex ret = new PyComplex(zr, zi); - if (iexp < 0) - return new PyComplex(1,0).__div__(ret); + if (iexp < 0) { + return new PyComplex(1, 0).__div__(ret); + } return ret; } + @Override public PyObject __pow__(PyObject right, PyObject modulo) { return complex___pow__(right, modulo); } @@ -577,19 +627,22 @@ if (modulo != null) { throw Py.ValueError("complex modulo"); } - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } return _pow(this, coerce(right)); } + @Override public PyObject __rpow__(PyObject left) { return complex___rpow__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rpow___doc) final PyObject complex___rpow__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } return _pow(coerce(left), this); } @@ -610,7 +663,7 @@ } // Check for integral powers - int iexp = (int)yr; + int iexp = (int) yr; if (yi == 0 && yr == iexp && iexp >= -128 && iexp <= 128) { return ipow(value, iexp); } @@ -619,14 +672,15 @@ double len = Math.pow(abs, yr); double at = Math.atan2(xi, xr); - double phase = at*yr; + double phase = at * yr; if (yi != 0) { - len /= Math.exp(at*yi); - phase += yi*Math.log(abs); + len /= Math.exp(at * yi); + phase += yi * Math.log(abs); } - return new PyComplex(len*Math.cos(phase), len*Math.sin(phase)); + return new PyComplex(len * Math.cos(phase), len * Math.sin(phase)); } + @Override public PyObject __neg__() { return complex___neg__(); } @@ -636,6 +690,7 @@ return new PyComplex(-real, -imag); } + @Override public PyObject __pos__() { return complex___pos__(); } @@ -648,10 +703,12 @@ return new PyComplex(real, imag); } + @Override public PyObject __invert__() { - throw Py.TypeError("bad operand type for unary ~"); + throw Py.TypeError("bad operand type for unary ~"); } + @Override public PyObject __abs__() { return complex___abs__(); } @@ -661,26 +718,27 @@ return new PyFloat(Math.hypot(real, imag)); } + @Override public PyObject __int__() { return complex___int__(); } @ExposedMethod(doc = BuiltinDocs.complex___int___doc) final PyInteger complex___int__() { - throw Py.TypeError( - "can't convert complex to int; use e.g. int(abs(z))"); + throw Py.TypeError("can't convert complex to int; use e.g. int(abs(z))"); } + @Override public PyObject __long__() { return complex___long__(); } @ExposedMethod(doc = BuiltinDocs.complex___long___doc) final PyObject complex___long__() { - throw Py.TypeError( - "can't convert complex to long; use e.g. long(abs(z))"); + throw Py.TypeError("can't convert complex to long; use e.g. long(abs(z))"); } + @Override public PyFloat __float__() { return complex___float__(); } @@ -690,6 +748,7 @@ throw Py.TypeError("can't convert complex to float; use e.g. abs(z)"); } + @Override public PyComplex __complex__() { return new PyComplex(real, imag); } @@ -708,6 +767,7 @@ return new PyTuple(new PyComplex(real, imag)); } + @Override public PyTuple __getnewargs__() { return complex___getnewargs__(); } Modified: trunk/jython/src/org/python/core/PyFloat.java =================================================================== --- trunk/jython/src/org/python/core/PyFloat.java 2010-03-13 17:33:14 UTC (rev 6984) +++ trunk/jython/src/org/python/core/PyFloat.java 2010-03-14 21:39:57 UTC (rev 6985) @@ -1,4 +1,7 @@ -// Copyright (c) Corporation for National Research Initiatives +/* + * Copyright (c) Corporation for National Research Initiatives + * Copyright (c) Jython Developers + */ package org.python.core; import java.io.Serializable; @@ -14,16 +17,33 @@ * A builtin python float. */ @ExposedType(name = "float", doc = BuiltinDocs.float_doc) -public class PyFloat extends PyObject -{ +public class PyFloat extends PyObject { + + public static final PyType TYPE = PyType.fromClass(PyFloat.class); + /** Precisions used by repr() and str(), respectively. */ private static final int PREC_REPR = 17; private static final int PREC_STR = 12; + private double value; + + public PyFloat(PyType subtype, double v) { + super(subtype); + value = v; + } + + public PyFloat(double v) { + this(TYPE, v); + } + + public PyFloat(float v) { + this((double) v); + } + @ExposedNew public static PyObject float_new(PyNewWrapper new_, boolean init, PyType subtype, - PyObject[] args, String[] keywords) { - ArgParser ap = new ArgParser("float", args, keywords, new String[] { "x" }, 0); + PyObject[] args, String[] keywords) { + ArgParser ap = new ArgParser("float", args, keywords, new String[] {"x"}, 0); PyObject x = ap.getPyObject(0, null); if (x == null) { if (new_.for_type == subtype) { @@ -54,23 +74,6 @@ } } - public static final PyType TYPE = PyType.fromClass(PyFloat.class); - - private double value; - - public PyFloat(PyType subtype, double v) { - super(subtype); - value = v; - } - - public PyFloat(double v) { - this(TYPE, v); - } - - public PyFloat(float v) { - this((double)v); - } - /** * Determine if this float is not infinity, nor NaN. */ @@ -82,10 +85,12 @@ return value; } + @Override public String toString() { return __str__().toString(); } + @Override public PyString __str__() { return float___str__(); } @@ -95,6 +100,7 @@ return Py.newString(formatDouble(PREC_STR)); } + @Override public PyString __repr__() { return float___repr__(); } @@ -108,7 +114,7 @@ if (Double.isNaN(value)) { return "nan"; } - + String result = String.format("%%.%dg", precision); result = Py.newString(result).__mod__(this).toString(); @@ -127,6 +133,7 @@ return result; } + @Override public int hashCode() { return float___hash__(); } @@ -134,19 +141,21 @@ @ExposedMethod(doc = BuiltinDocs.float___hash___doc) final int float___hash__() { double intPart = Math.floor(value); - double fractPart = value-intPart; + double fractPart = value - intPart; if (fractPart == 0) { - if (intPart <= Integer.MAX_VALUE && intPart >= Integer.MIN_VALUE) - return (int)value; - else + if (intPart <= Integer.MAX_VALUE && intPart >= Integer.MIN_VALUE) { + return (int) value; + } else { return __long__().hashCode(); + } } else { long v = Double.doubleToLongBits(value); - return (int)v ^ (int)(v >> 32); + return (int) v ^ (int) (v >> 32); } } + @Override public boolean __nonzero__() { return float___nonzero__(); } @@ -156,10 +165,10 @@ return value != 0; } - public Object __tojava__(Class c) { - if (c == Double.TYPE || c == Number.class || - c == Double.class || c == Object.class || c == Serializable.class) - { + @Override + public Object __tojava__(Class<?> c) { + if (c == Double.TYPE || c == Number.class || c == Double.class || c == Object.class + || c == Serializable.class) { return new Double(value); } if (c == Float.TYPE || c == Float.class) { @@ -168,6 +177,7 @@ return super.__tojava__(c); } + @Override public PyObject __eq__(PyObject other) { // preclude _cmp_unsafe's this == other shortcut because NaN != anything, even // itself @@ -177,6 +187,7 @@ return null; } + @Override public PyObject __ne__(PyObject other) { if (Double.isNaN(value)) { return Py.True; @@ -184,18 +195,19 @@ return null; } + @Override public int __cmp__(PyObject other) { return float___cmp__(other); } - //XXX: needs __doc__ + // XXX: needs __doc__ @ExposedMethod(type = MethodType.CMP) final int float___cmp__(PyObject other) { double i = value; double j; if (other instanceof PyFloat) { - j = ((PyFloat)other).value; + j = ((PyFloat) other).value; } else if (!isFinite()) { // we're infinity: our magnitude exceeds any finite // integer, so it doesn't matter which int we compare i @@ -206,10 +218,10 @@ return -2; } } else if (other instanceof PyInteger) { - j = ((PyInteger)other).getValue(); + j = ((PyInteger) other).getValue(); } else if (other instanceof PyLong) { BigDecimal v = new BigDecimal(value); - BigDecimal w = new BigDecimal(((PyLong)other).getValue()); + BigDecimal w = new BigDecimal(((PyLong) other).getValue()); return v.compareTo(w); } else { return -2; @@ -227,6 +239,7 @@ } } + @Override public Object __coerce_ex__(PyObject other) { return float___coerce_ex__(other); } @@ -236,22 +249,23 @@ return adaptToCoerceTuple(float___coerce_ex__(other)); } - /** + /** * Coercion logic for float. Implemented as a final method to avoid - * invocation of virtual methods from the exposed coerce. - */ + * invocation of virtual methods from the exposed coerce. + */ final Object float___coerce_ex__(PyObject other) { - if (other instanceof PyFloat) + if (other instanceof PyFloat) { return other; - else { - if (other instanceof PyInteger) - return new PyFloat((double)((PyInteger)other).getValue()); - if (other instanceof PyLong) - return new PyFloat(((PyLong)other).doubleValue()); - else + } else { + if (other instanceof PyInteger) { + return new PyFloat((double) ((PyInteger) other).getValue()); + } + if (other instanceof PyLong) { + return new PyFloat(((PyLong) other).doubleValue()); + } else { return Py.None; + } } - } private static final boolean canCoerce(PyObject other) { @@ -259,28 +273,32 @@ } private static final double coerce(PyObject other) { - if (other instanceof PyFloat) + if (other instanceof PyFloat) { return ((PyFloat) other).value; - else if (other instanceof PyInteger) + } else if (other instanceof PyInteger) { return ((PyInteger) other).getValue(); - else if (other instanceof PyLong) + } else if (other instanceof PyLong) { return ((PyLong) other).doubleValue(); - else + } else { throw Py.TypeError("xxx"); + } } + @Override public PyObject __add__(PyObject right) { return float___add__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___add___doc) final PyObject float___add__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } double rightv = coerce(right); return new PyFloat(value + rightv); } + @Override public PyObject __radd__(PyObject left) { return float___radd__(left); } @@ -290,42 +308,49 @@ return __add__(left); } + @Override public PyObject __sub__(PyObject right) { return float___sub__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___sub___doc) final PyObject float___sub__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } double rightv = coerce(right); return new PyFloat(value - rightv); } + @Override public PyObject __rsub__(PyObject left) { return float___rsub__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rsub___doc) final PyObject float___rsub__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } double leftv = coerce(left); return new PyFloat(leftv - value); } + @Override public PyObject __mul__(PyObject right) { return float___mul__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___mul___doc) final PyObject float___mul__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } double rightv = coerce(right); return new PyFloat(value * rightv); } + @Override public PyObject __rmul__(PyObject left) { return float___rmul__(left); } @@ -335,151 +360,185 @@ return __mul__(left); } + @Override public PyObject __div__(PyObject right) { return float___div__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___div___doc) final PyObject float___div__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; - if (Options.divisionWarning >= 2) + } + if (Options.divisionWarning >= 2) { Py.warning(Py.DeprecationWarning, "classic float division"); + } + double rightv = coerce(right); - if (rightv == 0) + if (rightv == 0) { throw Py.ZeroDivisionError("float division"); + } return new PyFloat(value / rightv); } + @Override public PyObject __rdiv__(PyObject left) { return float___rdiv__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rdiv___doc) final PyObject float___rdiv__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; - if (Options.divisionWarning >= 2) + } + if (Options.divisionWarning >= 2) { Py.warning(Py.DeprecationWarning, "classic float division"); + } + double leftv = coerce(left); - if (value == 0) + if (value == 0) { throw Py.ZeroDivisionError("float division"); + } return new PyFloat(leftv / value); } + @Override public PyObject __floordiv__(PyObject right) { return float___floordiv__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___floordiv___doc) final PyObject float___floordiv__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } double rightv = coerce(right); - if (rightv == 0) + if (rightv == 0) { throw Py.ZeroDivisionError("float division"); + } return new PyFloat(Math.floor(value / rightv)); } + @Override public PyObject __rfloordiv__(PyObject left) { return float___rfloordiv__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rfloordiv___doc) final PyObject float___rfloordiv__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } double leftv = coerce(left); - if (value == 0) + if (value == 0) { throw Py.ZeroDivisionError("float division"); + } return new PyFloat(Math.floor(leftv / value)); } + @Override public PyObject __truediv__(PyObject right) { return float___truediv__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___truediv___doc) final PyObject float___truediv__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } double rightv = coerce(right); - if (rightv == 0) + if (rightv == 0) { throw Py.ZeroDivisionError("float division"); + } return new PyFloat(value / rightv); } + @Override public PyObject __rtruediv__(PyObject left) { return float___rtruediv__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rtruediv___doc) final PyObject float___rtruediv__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } double leftv = coerce(left); - if (value == 0) + if (value == 0) { throw Py.ZeroDivisionError("float division"); + } return new PyFloat(leftv / value); } private static double modulo(double x, double y) { - if (y == 0) + if (y == 0) { throw Py.ZeroDivisionError("float modulo"); + } double z = Math.IEEEremainder(x, y); - if (z*y < 0) + if (z * y < 0) { z += y; + } return z; } + @Override public PyObject __mod__(PyObject right) { return float___mod__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___mod___doc) final PyObject float___mod__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } double rightv = coerce(right); return new PyFloat(modulo(value, rightv)); } + @Override public PyObject __rmod__(PyObject left) { return float___rmod__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rmod___doc) final PyObject float___rmod__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } double leftv = coerce(left); return new PyFloat(modulo(leftv, value)); } + @Override public PyObject __divmod__(PyObject right) { return float___divmod__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___divmod___doc) final PyObject float___divmod__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } double rightv = coerce(right); - if (rightv == 0) + if (rightv == 0) { throw Py.ZeroDivisionError("float division"); + } double z = Math.floor(value / rightv); return new PyTuple(new PyFloat(z), new PyFloat(value - z * rightv)); } + @Override public PyObject __rdivmod__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } double leftv = coerce(left); - if (value == 0) + if (value == 0) { throw Py.ZeroDivisionError("float division"); + } double z = Math.floor(leftv / value); return new PyTuple(new PyFloat(z), new PyFloat(leftv - z * value)); @@ -487,22 +546,23 @@ @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rdivmod___doc) final PyObject float___rdivmod__(PyObject left) { - return __rdivmod__(left); + return __rdivmod__(left); } - + @Override public PyObject __pow__(PyObject right, PyObject modulo) { return float___pow__(right, modulo); } - @ExposedMethod(type = MethodType.BINARY, defaults = "null", doc = BuiltinDocs.float___pow___doc) + @ExposedMethod(type = MethodType.BINARY, defaults = "null", + doc = BuiltinDocs.float___pow___doc) final PyObject float___pow__(PyObject right, PyObject modulo) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } if (modulo != null) { - throw Py.TypeError("pow() 3rd argument not allowed " + - "unless all arguments are integers"); + throw Py.TypeError("pow() 3rd argument not allowed unless all arguments are integers"); } return _pow(value, coerce(right), modulo); @@ -510,12 +570,14 @@ @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rpow___doc) final PyObject float___rpow__(PyObject left) { - return __rpow__(left); + return __rpow__(left); } - + + @Override public PyObject __rpow__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } return _pow(coerce(left), value, null); } @@ -523,28 +585,27 @@ private static PyFloat _pow(double value, double iw, PyObject modulo) { // Rely completely on Java's pow function if (iw == 0) { - if (modulo != null) + if (modulo != null) { return new PyFloat(modulo(1.0, coerce(modulo))); + } return new PyFloat(1.0); } if (value == 0.0) { - if (iw < 0.0) - throw Py.ZeroDivisionError("0.0 cannot be raised to a " + - "negative power"); + if (iw < 0.0) { + throw Py.ZeroDivisionError("0.0 cannot be raised to a negative power"); + } return new PyFloat(0); } - if (value < 0 && iw != Math.floor(iw)) + if (value < 0 && iw != Math.floor(iw)) { throw Py.ValueError("negative number cannot be raised to a fractional power"); - + } + double ret = Math.pow(value, iw); - if (modulo == null) { - return new PyFloat(ret); - } else { - return new PyFloat(modulo(ret, coerce(modulo))); - } + return new PyFloat(modulo == null ? ret : modulo(ret, coerce(modulo))); } + @Override public PyObject __neg__() { return float___neg__(); } @@ -554,6 +615,7 @@ return new PyFloat(-value); } + @Override public PyObject __pos__() { return float___pos__(); } @@ -563,10 +625,12 @@ return float___float__(); } + @Override public PyObject __invert__() { throw Py.TypeError("bad operand type for unary ~"); } + @Override public PyObject __abs__() { return float___abs__(); } @@ -579,6 +643,7 @@ return float___float__(); } + @Override public PyObject __int__() { return float___int__(); } @@ -586,11 +651,12 @@ @ExposedMethod(doc = BuiltinDocs.float___int___doc) final PyObject float___int__() { if (value <= Integer.MAX_VALUE && value >= Integer.MIN_VALUE) { - return new PyInteger((int)value); + return new PyInteger((int) value); } return __long__(); } + @Override public PyObject __long__() { return float___long__(); } @@ -600,6 +666,7 @@ return new PyLong(value); } + @Override public PyFloat __float__() { return float___float__(); } @@ -611,6 +678,8 @@ } return Py.newFloat(value); } + + @Override public PyComplex __complex__() { return new PyComplex(value, 0.); } @@ -620,10 +689,12 @@ return new PyTuple(new PyObject[] {new PyFloat(getValue())}); } + @Override public PyTuple __getnewargs__() { return float___getnewargs__(); } + @Override public double asDouble() { return value; } @@ -635,23 +706,26 @@ // standard singleton issues apply here to __getformat__/__setformat__, // but this is what Python demands - public enum Format { - UNKNOWN ("unknown"), - BE ("IEEE, big-endian"), - LE ("IEEE, little-endian"); - + + UNKNOWN("unknown"), + BE("IEEE, big-endian"), + LE("IEEE, little-endian"); + private final String format; + Format(String format) { this.format = format; } - public String format() { return format; } + + public String format() { + return format; + } } - - // subset of IEEE-754, the JVM is big-endian + // subset of IEEE-754, the JVM is big-endian public static volatile Format double_format = Format.BE; public static volatile Format float_format = Format.BE; - + @ExposedClassMethod(doc = BuiltinDocs.float___getformat___doc) public static String float___getformat__(PyType type, String typestr) { if ("double".equals(typestr)) { @@ -662,7 +736,7 @@ throw Py.ValueError("__getformat__() argument 1 must be 'double' or 'float'"); } } - + @ExposedClassMethod(doc = BuiltinDocs.float___setformat___doc) public static void float___setformat__(PyType type, String typestr, String format) { Format new_format = null; @@ -670,13 +744,15 @@ throw Py.ValueError("__setformat__() argument 1 must be 'double' or 'float'"); } if (Format.LE.format().equals(format)) { - throw Py.ValueError(String.format("can only set %s format to 'unknown' or the " + "detected platform value", typestr)); + throw Py.ValueError(String.format("can only set %s format to 'unknown' or the " + + "detected platform value", typestr)); } else if (Format.BE.format().equals(format)) { new_format = Format.BE; } else if (Format.UNKNOWN.format().equals(format)) { new_format = Format.UNKNOWN; } else { - throw Py.ValueError("__setformat__() argument 2 must be 'unknown', " + "'IEEE, little-endian' or 'IEEE, big-endian'"); + throw Py.ValueError("__setformat__() argument 2 must be 'unknown', " + + "'IEEE, little-endian' or 'IEEE, big-endian'"); } if (new_format != null) { if ("double".equals(typestr)) { Modified: trunk/jython/src/org/python/core/PyInteger.java =================================================================== --- trunk/jython/src/org/python/core/PyInteger.java 2010-03-13 17:33:14 UTC (rev 6984) +++ trunk/jython/src/org/python/core/PyInteger.java 2010-03-14 21:39:57 UTC (rev 6985) @@ -1,4 +1,7 @@ -// Copyright (c) Corporation for National Research Initiatives +/* + * Copyright (c) Corporation for National Research Initiatives + * Copyright (c) Jython Developers + */ package org.python.core; import java.io.Serializable; @@ -14,7 +17,7 @@ */ @ExposedType(name = "int", doc = BuiltinDocs.int_doc) public class PyInteger extends PyObject { - + public static final PyType TYPE = PyType.fromClass(PyInteger.class); /** The minimum value of an int represented by a BigInteger */ @@ -23,31 +26,41 @@ /** The maximum value of an int represented by a BigInteger */ public static final BigInteger maxInt = BigInteger.valueOf(Integer.MAX_VALUE); + private int value; + + public PyInteger(PyType subType, int v) { + super(subType); + value = v; + } + + public PyInteger(int v) { + this(TYPE, v); + } + @ExposedNew public static PyObject int_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { - ArgParser ap = new ArgParser("int", args, keywords, new String[] { "x", - "base" }, 0); + ArgParser ap = new ArgParser("int", args, keywords, new String[] {"x", "base"}, 0); PyObject x = ap.getPyObject(0, null); int base = ap.getInt(1, -909); if (new_.for_type == subtype) { if (x == null) { return Py.Zero; } - if (base == -909) { - if (x instanceof PyBoolean) { - return (coerce(x) == 0) ? Py.Zero : Py.One; - } - return asPyInteger(x); - } - if (!(x instanceof PyString)) { - throw Py.TypeError("int: can't convert non-string with explicit base"); - } + if (base == -909) { + if (x instanceof PyBoolean) { + return (coerce(x) == 0) ? Py.Zero : Py.One; + } + return asPyInteger(x); + } + if (!(x instanceof PyString)) { + throw Py.TypeError("int: can't convert non-string with explicit base"); + } try { - return Py.newInteger(((PyString)x).atoi(base)); + return Py.newInteger(((PyString) x).atoi(base)); } catch (PyException pye) { if (pye.match(Py.OverflowError)) { - return ((PyString)x).atol(base); + return ((PyString) x).atol(base); } throw pye; } @@ -56,60 +69,51 @@ return new PyIntegerDerived(subtype, 0); } if (base == -909) { - PyObject intOrLong = asPyInteger(x); - if (intOrLong instanceof PyInteger) { - return new PyIntegerDerived(subtype, ((PyInteger) intOrLong).getValue()); - } else { - throw Py.OverflowError("long int too large to convert to int"); - } + PyObject intOrLong = asPyInteger(x); + if (intOrLong instanceof PyInteger) { + return new PyIntegerDerived(subtype, ((PyInteger) intOrLong).getValue()); + } else { + throw Py.OverflowError("long int too large to convert to int"); + } } if (!(x instanceof PyString)) { - throw Py - .TypeError("int: can't convert non-string with explicit base"); + throw Py.TypeError("int: can't convert non-string with explicit base"); } return new PyIntegerDerived(subtype, ((PyString) x).atoi(base)); } } // xxx /** - * @return the result of x.__int__ + * @return the result of x.__int__ * @throws Py.Type error if x.__int__ throws an Py.AttributeError */ - private static PyObject asPyInteger(PyObject x) { - try { - return x.__int__(); - } catch (PyException pye) { - if (!pye.match(Py.AttributeError)) - throw pye; - throw Py.TypeError("int() argument must be a string or a number"); - } - } - - private int value; - - public PyInteger(PyType subType, int v) { - super(subType); - value = v; + private static PyObject asPyInteger(PyObject x) { + try { + return x.__int__(); + } catch (PyException pye) { + if (!pye.match(Py.AttributeError)) { + throw pye; + } + throw Py.TypeError("int() argument must be a string or a number"); + } } - public PyInteger(int v) { - this(TYPE, v); - } - public int getValue() { return value; } + @Override public String toString() { return int_toString(); } - //XXX: need separate __doc__ for __repr__ + // XXX: need separate __doc__ for __repr__ @ExposedMethod(names = {"__str__", "__repr__"}, doc = BuiltinDocs.int___str___doc) final String int_toString() { return Integer.toString(getValue()); } + @Override public int hashCode() { return int_hashCode(); } @@ -119,6 +123,7 @@ return getValue(); } + @Override public boolean __nonzero__() { return int___nonzero__(); } @@ -128,42 +133,50 @@ return getValue() != 0; } + @Override public Object __tojava__(Class<?> c) { - if (c == Integer.TYPE || c == Number.class || - c == Object.class || c == Integer.class || - c == Serializable.class) - { + if (c == Integer.TYPE || c == Number.class || c == Object.class || c == Integer.class + || c == Serializable.class) { return new Integer(getValue()); } - if (c == Boolean.TYPE || c == Boolean.class) + if (c == Boolean.TYPE || c == Boolean.class) { return new Boolean(getValue() != 0); - if (c == Byte.TYPE || c == Byte.class) - return new Byte((byte)getValue()); - if (c == Short.TYPE || c == Short.class) - return new Short((short)getValue()); + } + if (c == Byte.TYPE || c == Byte.class) { + return new Byte((byte) getValue()); + } + if (c == Short.TYPE || c == Short.class) { + return new Short((short) getValue()); + } - if (c == Long.TYPE || c == Long.class) + if (c == Long.TYPE || c == Long.class) { return new Long(getValue()); - if (c == Float.TYPE || c == Float.class) + } + if (c == Float.TYPE || c == Float.class) { return new Float(getValue()); - if (c == Double.TYPE || c == Double.class) + } + if (c == Double.TYPE || c == Double.class) { return new Double(getValue()); + } return super.__tojava__(c); } + @Override public int __cmp__(PyObject other) { return int___cmp__(other); } @ExposedMethod(type = MethodType.CMP, doc = BuiltinDocs.int___cmp___doc) final int int___cmp__(PyObject other) { - if (!canCoerce(other)) - return -2; + if (!canCoerce(other)) { + return -2; + } int v = coerce(other); return getValue() < v ? -1 : getValue() > v ? 1 : 0; } + @Override public Object __coerce_ex__(PyObject other) { return int___coerce_ex__(other); } @@ -173,15 +186,12 @@ return adaptToCoerceTuple(int___coerce_ex__(other)); } - /** + /** * Coercion logic for int. Implemented as a final method to avoid - * invocation of virtual methods from the exposed coerced. - */ + * invocation of virtual methods from the exposed coerced. + */ final Object int___coerce_ex__(PyObject other) { - if (other instanceof PyInteger) - return other; - else - return Py.None; + return other instanceof PyInteger ? other : Py.None; } private static final boolean canCoerce(PyObject other) { @@ -189,30 +199,33 @@ } private static final int coerce(PyObject other) { - if (other instanceof PyInteger) + if (other instanceof PyInteger) { return ((PyInteger) other).getValue(); - else - throw Py.TypeError("xxx"); + } + throw Py.TypeError("xxx"); } - + @Override public PyObject __add__(PyObject right) { return int___add__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___add___doc) final PyObject int___add__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } int rightv = coerce(right); int a = getValue(); int b = rightv; int x = a + b; - if ((x^a) >= 0 || (x^b) >= 0) + if ((x ^ a) >= 0 || (x ^ b) >= 0) { return Py.newInteger(x); - return new PyLong((long) a + (long)b); + } + return new PyLong((long) a + (long) b); } + @Override public PyObject __radd__(PyObject left) { return int___radd__(left); } @@ -224,57 +237,64 @@ private static PyObject _sub(int a, int b) { int x = a - b; - if ((x^a) >= 0 || (x^~b) >= 0) + if ((x ^ a) >= 0 || (x ^ ~b) >= 0) { return Py.newInteger(x); - return new PyLong((long) a - (long)b); + } + return new PyLong((long) a - (long) b); } + @Override public PyObject __sub__(PyObject right) { return int___sub__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___sub___doc) final PyObject int___sub__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } return _sub(getValue(), coerce(right)); } + @Override public PyObject __rsub__(PyObject left) { return int___rsub__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rsub___doc) final PyObject int___rsub__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } return _sub(coerce(left), getValue()); } + @Override public PyObject __mul__(PyObject right) { return int___mul__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___mul___doc) final PyObject int___mul__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } int rightv = coerce(right); double x = getValue(); x *= rightv; - //long x = ((long)getValue())*((PyInteger)right).getValue(); - //System.out.println("mul: "+this+" * "+right+" = "+x); - if (x <= Integer.MAX_VALUE && x >= Integer.MIN_VALUE) - return Py.newInteger((int)x); + if (x <= Integer.MAX_VALUE && x >= Integer.MIN_VALUE) { + return Py.newInteger((int) x); + } return __long__().__mul__(right); } + @Override public PyObject __rmul__(PyObject left) { return int___rmul__(left); } - + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rmul___doc) final PyObject int___rmul__(PyObject left) { return __mul__(left); @@ -288,13 +308,11 @@ } long xdivy = x / y; long xmody = x - xdivy * y; - - /* If the signs of x and y differ, and the remainder is non-0, - * C89 doesn't define whether xdivy is now the floor or the - * ceiling of the infinitely precise quotient. We want the floor, - * and we have it iff the remainder's sign matches y's. - */ - + + // If the signs of x and y differ, and the remainder is non-0, C89 doesn't define + // whether xdivy is now the floor or the ceiling of the infinitely precise + // quotient. We want the floor, and we have it iff the remainder's sign matches + // y's. if (xmody != 0 && ((y < 0 && xmody > 0) || (y > 0 && xmody < 0))) { xmody += y; --xdivy; @@ -307,128 +325,148 @@ public PyObject __div__(PyObject right) { return int___div__(right); } - + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___div___doc) final PyObject int___div__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; - if (Options.divisionWarning > 0) + } + if (Options.divisionWarning > 0) { Py.warning(Py.DeprecationWarning, "classic int division"); + } return Py.newInteger(divide(getValue(), coerce(right))); } + @Override public PyObject __rdiv__(PyObject left) { return int___rdiv__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rdiv___doc) final PyObject int___rdiv__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; - if (Options.divisionWarning > 0) + } + if (Options.divisionWarning > 0) { Py.warning(Py.DeprecationWarning, "classic int division"); + } return Py.newInteger(divide(coerce(left), getValue())); } + @Override public PyObject __floordiv__(PyObject right) { return int___floordiv__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___floordiv___doc) final PyObject int___floordiv__(PyObject right) { - if (!canCoerce(right)) + if (!canCoerce(right)) { return null; + } return Py.newInteger(divide(getValue(), coerce(right))); } + @Override public PyObject __rfloordiv__(PyObject left) { return int___rfloordiv__(left); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rfloordiv___doc) final PyObject int___rfloordiv__(PyObject left) { - if (!canCoerce(left)) + if (!canCoerce(left)) { return null; + } return Py.newInteger(divide(coerce(left), getValue())); } + @Override public PyObject __truediv__(PyObject right) { return int___truediv__(right); } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___truediv___doc) final PyObject int___truediv__(PyObject right) { - if (right instanceof PyInteger) + if (right instanceof PyInteger) { return __float__().__truediv__(right); - else if(right instanceof PyLong) + } else if (right instanceof PyLong) { return int___long__().__truediv__(right); - ... [truncated message content] |