Update of /cvsroot/jython/jython/org/python/core In directory usw-pr-cvs1:/tmp/cvs-serv18939 Modified Files: PyComplex.java PyFloat.java PyInstance.java PyInteger.java PyLong.java PyObject.java imp.java Log Message: Added __truediv__ and __floordiv__ methods. Emit warnings when classic __div__ is called. Index: PyComplex.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyComplex.java,v retrieving revision 2.9 retrieving revision 2.10 diff -C2 -d -r2.9 -r2.10 *** PyComplex.java 2001/10/28 17:13:43 2.9 --- PyComplex.java 2002/01/13 18:20:31 2.10 *************** *** 178,181 **** --- 178,183 ---- if (!canCoerce(right)) return null; + if (Options.divisionWarning >= 2) + Py.warning(Py.DeprecationWarning, "classic complex division"); return _div(this, coerce(right)); } *************** *** 184,191 **** --- 186,218 ---- if (!canCoerce(left)) return null; + if (Options.divisionWarning >= 2) + Py.warning(Py.DeprecationWarning, "classic complex division"); return _div(coerce(left), this); } + public PyObject __floordiv__(PyObject right) { + if (!canCoerce(right)) + return null; + return _divmod(this, coerce(right)).__finditem__(0); + } + + public PyObject __rfloordiv__(PyObject left) { + if (!canCoerce(left)) + return null; + return _divmod(coerce(left), this).__finditem__(0); + } + public PyObject __truediv__(PyObject right) { + if (!canCoerce(right)) + return null; + return _div(this, coerce(right)); + } + + public PyObject __rtruediv__(PyObject left) { + if (!canCoerce(left)) + return null; + return _div(coerce(left), this); + } + public PyObject __mod__(PyObject right) { if (!canCoerce(right)) *************** *** 201,205 **** private static PyObject _mod(PyComplex value, PyComplex right) { ! PyComplex z = (PyComplex)value.__div__(right); z.real = Math.floor(z.real); --- 228,232 ---- private static PyObject _mod(PyComplex value, PyComplex right) { ! PyComplex z = (PyComplex) _div(value, right); z.real = Math.floor(z.real); *************** *** 222,226 **** private static PyObject _divmod(PyComplex value, PyComplex right) { ! PyComplex z = (PyComplex)value.__div__(right); z.real = Math.floor(z.real); --- 249,253 ---- private static PyObject _divmod(PyComplex value, PyComplex right) { ! PyComplex z = (PyComplex) _div(value, right); z.real = Math.floor(z.real); Index: PyFloat.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyFloat.java,v retrieving revision 2.10 retrieving revision 2.11 diff -C2 -d -r2.10 -r2.11 *** PyFloat.java 2001/10/28 17:13:43 2.10 --- PyFloat.java 2002/01/13 18:20:31 2.11 *************** *** 152,155 **** --- 152,157 ---- if (!canCoerce(right)) return null; + if (Options.divisionWarning >= 2) + Py.warning(Py.DeprecationWarning, "classic float division"); double rightv = coerce(right); if (rightv == 0) *************** *** 159,162 **** --- 161,202 ---- public PyObject __rdiv__(PyObject left) { + if (!canCoerce(left)) + return null; + if (Options.divisionWarning >= 2) + Py.warning(Py.DeprecationWarning, "classic float division"); + double leftv = coerce(left); + if (value == 0) + throw Py.ZeroDivisionError("float division"); + return new PyFloat(leftv / value); + } + + public PyObject __floordiv__(PyObject right) { + if (!canCoerce(right)) + return null; + double rightv = coerce(right); + if (rightv == 0) + throw Py.ZeroDivisionError("float division"); + return new PyFloat(Math.floor(value / rightv)); + } + + public PyObject __rfloordiv__(PyObject left) { + if (!canCoerce(left)) + return null; + double leftv = coerce(left); + if (value == 0) + throw Py.ZeroDivisionError("float division"); + return new PyFloat(Math.floor(leftv / value)); + } + + public PyObject __truediv__(PyObject right) { + if (!canCoerce(right)) + return null; + double rightv = coerce(right); + if (rightv == 0) + throw Py.ZeroDivisionError("float division"); + return new PyFloat(value / rightv); + } + + public PyObject __rtruediv__(PyObject left) { if (!canCoerce(left)) return null; Index: PyInstance.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyInstance.java,v retrieving revision 2.30 retrieving revision 2.31 diff -C2 -d -r2.30 -r2.31 *** PyInstance.java 2002/01/07 20:07:53 2.30 --- PyInstance.java 2002/01/13 18:20:31 2.31 *************** *** 976,979 **** --- 976,1073 ---- /** + * Implements the __floordiv__ method by looking it up + * in the instance's dictionary and calling it if it is found. + **/ + public PyObject __floordiv__(PyObject o) { + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__floordiv__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__floordiv__", o2); + else + return o1._floordiv(o2); + } + } + + /** + * Implements the __rfloordiv__ method by looking it up + * in the instance's dictionary and calling it if it is found. + **/ + public PyObject __rfloordiv__(PyObject o) { + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rfloordiv__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rfloordiv__", o2); + else + return o2._floordiv(o1); + } + } + + /** + * Implements the __ifloordiv__ method by looking it up + * in the instance's dictionary and calling it if it is found. + **/ + public PyObject __ifloordiv__(PyObject o) { + PyObject ret = invoke_ex("__ifloordiv__", o); + if (ret != null) + return ret; + return super.__ifloordiv__(o); + } + + /** + * Implements the __truediv__ method by looking it up + * in the instance's dictionary and calling it if it is found. + **/ + public PyObject __truediv__(PyObject o) { + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__truediv__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__truediv__", o2); + else + return o1._truediv(o2); + } + } + + /** + * Implements the __rtruediv__ method by looking it up + * in the instance's dictionary and calling it if it is found. + **/ + public PyObject __rtruediv__(PyObject o) { + Object ctmp = __coerce_ex__(o); + if (ctmp == null || ctmp == Py.None) + return invoke_ex("__rtruediv__", o); + else { + PyObject o1 = ((PyObject[])ctmp)[0]; + PyObject o2 = ((PyObject[])ctmp)[1]; + if (this == o1) // Prevent recusion if __coerce__ return self + return invoke_ex("__rtruediv__", o2); + else + return o2._truediv(o1); + } + } + + /** + * Implements the __itruediv__ method by looking it up + * in the instance's dictionary and calling it if it is found. + **/ + public PyObject __itruediv__(PyObject o) { + PyObject ret = invoke_ex("__itruediv__", o); + if (ret != null) + return ret; + return super.__itruediv__(o); + } + + /** * Implements the __mod__ method by looking it up * in the instance's dictionary and calling it if it is found. Index: PyInteger.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyInteger.java,v retrieving revision 2.13 retrieving revision 2.14 diff -C2 -d -r2.13 -r2.14 *** PyInteger.java 2001/10/28 17:13:43 2.13 --- PyInteger.java 2002/01/13 18:20:31 2.14 *************** *** 168,171 **** --- 168,173 ---- if (!canCoerce(right)) return null; + if (Options.divisionWarning > 0) + Py.warning(Py.DeprecationWarning, "classic int division"); return Py.newInteger(divide(value, coerce(right))); } *************** *** 174,178 **** --- 176,206 ---- if (!canCoerce(left)) return null; + if (Options.divisionWarning > 0) + Py.warning(Py.DeprecationWarning, "classic int division"); + return Py.newInteger(divide(coerce(left), value)); + } + + public PyObject __floordiv__(PyObject right) { + if (!canCoerce(right)) + return null; + return Py.newInteger(divide(value, coerce(right))); + } + + public PyObject __rfloordiv__(PyObject left) { + if (!canCoerce(left)) + return null; return Py.newInteger(divide(coerce(left), value)); + } + + public PyObject __truediv__(PyObject right) { + if (right instanceof PyInteger) + return __float__().__truediv__(right); + return null; + } + + public PyObject __rtruediv__(PyObject left) { + if (left instanceof PyInteger) + return left.__float__().__truediv__(this); + return null; } Index: PyLong.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyLong.java,v retrieving revision 2.13 retrieving revision 2.14 diff -C2 -d -r2.13 -r2.14 *** PyLong.java 2001/10/28 17:13:43 2.13 --- PyLong.java 2002/01/13 18:20:31 2.14 *************** *** 189,192 **** --- 189,194 ---- if (!canCoerce(right)) return null; + if (Options.divisionWarning > 0) + Py.warning(Py.DeprecationWarning, "classic long division"); return new PyLong(divide(value, coerce(right))); } *************** *** 195,199 **** --- 197,227 ---- if (!canCoerce(left)) return null; + if (Options.divisionWarning > 0) + Py.warning(Py.DeprecationWarning, "classic long division"); + return new PyLong(divide(coerce(left), value)); + } + + public PyObject __floordiv__(PyObject right) { + if (!canCoerce(right)) + return null; + return new PyLong(divide(value, coerce(right))); + } + + public PyObject __rfloordiv__(PyObject left) { + if (!canCoerce(left)) + return null; return new PyLong(divide(coerce(left), value)); + } + + public PyObject __truediv__(PyObject right) { + if (!canCoerce(right)) + return null; + return new PyFloat(doubleValue() / coerce(right).doubleValue()); + } + + public PyObject __rtruediv__(PyObject left) { + if (!canCoerce(left)) + return null; + return new PyFloat(coerce(left).doubleValue() / doubleValue()); } Index: PyObject.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyObject.java,v retrieving revision 2.27 retrieving revision 2.28 diff -C2 -d -r2.27 -r2.28 *** PyObject.java 2002/01/06 21:19:13 2.27 --- PyObject.java 2002/01/13 18:20:31 2.28 *************** *** 1609,1612 **** --- 1609,1614 ---- **/ public final PyObject _div(PyObject o2) { + if (Options.Qnew) + return _truediv(o2); PyObject x = __div__(o2); if (x != null) *************** *** 1617,1620 **** --- 1619,1712 ---- throw Py.TypeError( "__div__ nor __rdiv__ defined for these operands"); + } + + /** + * Equivalent to the standard Python __floordiv__ method + * @param other the object to perform this binary operation with + * (the right-hand operand). + * @return the result of the floordiv, or null if this operation + * is not defined + **/ + public PyObject __floordiv__(PyObject other) { return null; } + + /** + * Equivalent to the standard Python __rfloordiv__ method + * @param other the object to perform this binary operation with + * (the left-hand operand). + * @return the result of the floordiv, or null if this operation + * is not defined. + **/ + public PyObject __rfloordiv__(PyObject other) { return null; } + + /** + * Equivalent to the standard Python __ifloordiv__ method + * @param other the object to perform this binary operation with + * (the right-hand operand). + * @return the result of the floordiv, or null if this operation + * is not defined + **/ + public PyObject __ifloordiv__(PyObject other) { return _floordiv(other); } + + /** + * Implements the Python expression <code>this // other</code> + * @param other the object to perform this binary operation with. + * @return the result of the floordiv. + * @exception PyTypeError if this operation can't be performed + * with these operands. + **/ + public final PyObject _floordiv(PyObject o2) { + PyObject x = __floordiv__(o2); + if (x != null) + return x; + x = o2.__rfloordiv__(this); + if (x != null) + return x; + throw Py.TypeError( + "__floordiv__ nor __rfloordiv__ defined for these operands"); + } + + /** + * Equivalent to the standard Python __truediv__ method + * @param other the object to perform this binary operation with + * (the right-hand operand). + * @return the result of the truediv, or null if this operation + * is not defined + **/ + public PyObject __truediv__(PyObject other) { return null; } + + /** + * Equivalent to the standard Python __rtruediv__ method + * @param other the object to perform this binary operation with + * (the left-hand operand). + * @return the result of the truediv, or null if this operation + * is not defined. + **/ + public PyObject __rtruediv__(PyObject other) { return null; } + + /** + * Equivalent to the standard Python __itruediv__ method + * @param other the object to perform this binary operation with + * (the right-hand operand). + * @return the result of the truediv, or null if this operation + * is not defined + **/ + public PyObject __itruediv__(PyObject other) { return _truediv(other); } + + /** + * Implements the Python expression <code>this / other</code> + * @param other the object to perform this binary operation with. + * @return the result of the truediv. + * @exception PyTypeError if this operation can't be performed + * with these operands. + **/ + public final PyObject _truediv(PyObject o2) { + PyObject x = __truediv__(o2); + if (x != null) + return x; + x = o2.__rtruediv__(this); + if (x != null) + return x; + throw Py.TypeError( + "__truediv__ nor __rtruediv__ defined for these operands"); } Index: imp.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/imp.java,v retrieving revision 2.60 retrieving revision 2.61 diff -C2 -d -r2.60 -r2.61 *** imp.java 2002/01/06 21:19:13 2.60 --- imp.java 2002/01/13 18:20:31 2.61 *************** *** 13,17 **** public class imp { ! public static final int APIVersion = 10; private imp() { ; } --- 13,17 ---- public class imp { ! public static final int APIVersion = 11; private imp() { ; } |