From: <pj...@us...> - 2010-04-09 05:06:14
|
Revision: 7009 http://jython.svn.sourceforge.net/jython/?rev=7009&view=rev Author: pjenvey Date: 2010-04-09 05:06:08 +0000 (Fri, 09 Apr 2010) Log Message: ----------- replace __int/long/float__ with asInt/Long/Double where appropriate Modified Paths: -------------- trunk/jython/src/org/python/core/PyString.java trunk/jython/src/org/python/modules/_sre.java trunk/jython/src/org/python/modules/jffi/Util.java trunk/jython/src/org/python/modules/math.java trunk/jython/src/org/python/modules/struct.java Modified: trunk/jython/src/org/python/core/PyString.java =================================================================== --- trunk/jython/src/org/python/core/PyString.java 2010-04-09 04:40:52 UTC (rev 7008) +++ trunk/jython/src/org/python/core/PyString.java 2010-04-09 05:06:08 UTC (rev 7009) @@ -2714,36 +2714,13 @@ return s; } - private String formatFloatDecimal(PyObject arg, boolean truncate) { - PyFloat argAsFloat; - if (arg instanceof PyFloat) { - // Fast path - argAsFloat = (PyFloat)arg; - } else { - // Use __float__ - if (arg instanceof PyInteger || arg instanceof PyLong) { - // Typical cases, safe to call __float__: - argAsFloat = arg.__float__(); - } else { - try { - // We can't simply call arg.__float__() because PyString implements - // it without exposing it to python (i.e, str instances has no - // __float__ attribute). So, we would support strings as arguments - // for %g format, which is forbidden by CPython tests (on - // test_format.py). - argAsFloat = (PyFloat)arg.__getattr__("__float__").__call__(); - } catch (PyException e) { - // XXX: Swallow customs AttributeError throws from __float__ methods - // No better alternative for the moment - if (e.match(Py.AttributeError)) { - throw Py.TypeError("float argument required"); + private double asDouble(PyObject obj) { + try { + return obj.asDouble(); + } catch (PyException pye) { + throw !pye.match(Py.TypeError) ? pye : Py.TypeError("float argument required"); } - throw e; } - } - } - return formatFloatDecimal(argAsFloat.getValue(), truncate); - } private String formatFloatDecimal(double v, boolean truncate) { checkPrecision("decimal"); @@ -2772,7 +2749,7 @@ boolean truncate) { StringBuilder buf = new StringBuilder(); - double v = arg.__float__().getValue(); + double v = asDouble(arg); boolean isNegative = false; if (v < 0) { v = -v; @@ -2971,10 +2948,8 @@ string = formatFloatExponential(arg, c, false); break; case 'f': - case 'F': - string = formatFloatDecimal(arg, false); -// if (altFlag && string.indexOf('.') == -1) -// string += '.'; + case 'F': + string = formatFloatDecimal(asDouble(arg), false); break; case 'g': case 'G': @@ -2983,7 +2958,7 @@ precision = 6; } - double v = arg.__float__().getValue(); + double v = asDouble(arg); int exponent = (int)ExtraMath.closeFloor(Math.log10(Math.abs(v == 0 ? 1 : v))); if (v == Double.POSITIVE_INFINITY) { string = "inf"; @@ -2991,7 +2966,7 @@ string = "-inf"; } else if (exponent >= -4 && exponent < precision) { precision -= exponent + 1; - string = formatFloatDecimal(arg, !altFlag); + string = formatFloatDecimal(v, !altFlag); // XXX: this block may be unnecessary now if (altFlag && string.indexOf('.') == -1) { Modified: trunk/jython/src/org/python/modules/_sre.java =================================================================== --- trunk/jython/src/org/python/modules/_sre.java 2010-04-09 04:40:52 UTC (rev 7008) +++ trunk/jython/src/org/python/modules/_sre.java 2010-04-09 05:06:08 UTC (rev 7009) @@ -12,60 +12,34 @@ * CNRI. Hewlett-Packard provided funding for 1.6 integration and * other compatibility work. */ - package org.python.modules; -import java.util.Iterator; -import org.python.core.Py; -import org.python.core.PyInteger; -import org.python.core.PyList; -import org.python.core.PyLong; import org.python.core.PyObject; import org.python.core.PyString; import org.python.modules.sre.PatternObject; import org.python.modules.sre.SRE_STATE; - public class _sre { public static int MAGIC = SRE_STATE.SRE_MAGIC; // workaround the fact that H, I types are unsigned, but we are not really using them as such - //XXX: May not be the right size, but I suspect it is -- see sre_compile.py + // XXX: May not be the right size, but I suspect it is -- see sre_compile.py public static int CODESIZE = 4; - public static PatternObject compile(PyString pattern, int flags, - PyObject code, int groups, - PyObject groupindex, - PyObject indexgroup) { - int[] ccode = null; - if (code instanceof PyList) { - int n = code.__len__(); - ccode = new int[n]; - int i = 0; - for (PyObject item : code.asIterable()) { - ccode[i] = (int)((PyLong)item.__long__()).getValue().longValue(); - i++; - } - } else { - throw Py.TypeError("Expected list"); + public static PatternObject compile(PyString pattern, int flags, PyObject code, int groups, + PyObject groupindex, PyObject indexgroup) { + int[] ccode = new int[code.__len__()]; + int i = 0; + for (PyObject item : code.asIterable()) { + ccode[i++] = (int)item.asLong(); } - - PatternObject po = new PatternObject(pattern, - flags, - ccode, - groups, - groupindex, - indexgroup); - return po; + return new PatternObject(pattern, flags, ccode, groups, groupindex, indexgroup); } - - public static int getcodesize() { return CODESIZE; } - public static int getlower(int ch, int flags) { return SRE_STATE.getlower(ch, flags); } Modified: trunk/jython/src/org/python/modules/jffi/Util.java =================================================================== --- trunk/jython/src/org/python/modules/jffi/Util.java 2010-04-09 04:40:52 UTC (rev 7008) +++ trunk/jython/src/org/python/modules/jffi/Util.java 2010-04-09 05:06:08 UTC (rev 7009) @@ -88,13 +88,7 @@ } public static final long int64Value(PyObject value) { - if (value instanceof PyLong) { - return ((PyLong) value).getLong(Long.MIN_VALUE, Long.MAX_VALUE); - } else if (value instanceof PyInteger) { - return value.asInt(); - } else { - return ((PyLong) value.__long__()).getLong(Long.MIN_VALUE, Long.MAX_VALUE); - } + return value.asLong(); } public static final long uint64Value(PyObject value) { Modified: trunk/jython/src/org/python/modules/math.java =================================================================== --- trunk/jython/src/org/python/modules/math.java 2010-04-09 04:40:52 UTC (rev 7008) +++ trunk/jython/src/org/python/modules/math.java 2010-04-09 05:06:08 UTC (rev 7009) @@ -53,7 +53,7 @@ } public static double floor(PyObject v) { - return floor(v.__float__().getValue()); + return floor(v.asDouble()); } public static double floor(double v) { @@ -69,7 +69,7 @@ if (v instanceof PyLong) { doubleValue = calculateLongLog((PyLong)v); } else { - doubleValue = log(v.__float__().getValue()); + doubleValue = log(v.asDouble()); } if (base != null) { return check(applyLoggedBase(doubleValue, base)); @@ -89,7 +89,7 @@ if (base instanceof PyLong) { loggedBase = calculateLongLog((PyLong)base); } else { - loggedBase = log(base.__float__().getValue()); + loggedBase = log(base.asDouble()); } return check(loggedValue / loggedBase); } @@ -103,7 +103,7 @@ } public static double sin(PyObject v) { - return sin(v.__float__().getValue()); + return sin(v.asDouble()); } public static double sin(double v) { @@ -111,7 +111,7 @@ } public static double sqrt(PyObject v) { - return sqrt(v.__float__().getValue()); + return sqrt(v.asDouble()); } public static double sqrt(double v) { @@ -129,7 +129,7 @@ if (x <= 0.0) throw Py.ValueError("math domain error"); return log10(x) + (e[0]*8.0)*log10(2.0); } - return log10(v.__float__().getValue()); + return log10(v.asDouble()); } private static double log10(double v) { Modified: trunk/jython/src/org/python/modules/struct.java =================================================================== --- trunk/jython/src/org/python/modules/struct.java 2010-04-09 04:40:52 UTC (rev 7008) +++ trunk/jython/src/org/python/modules/struct.java 2010-04-09 05:06:08 UTC (rev 7009) @@ -351,7 +351,7 @@ } double get_float(PyObject value) { - return value.__float__().getValue(); + return value.asDouble(); } @@ -1000,7 +1000,7 @@ throw Py.TypeError("pack_into takes an array arg"); // as well as a buffer, what else? } PyArray buffer = (PyArray)args[argstart]; - int offset = args[argstart + 1].__int__().asInt(); + int offset = args[argstart + 1].asInt(); ByteStream res = pack(format, f, size, argstart + 2, args); if (res.pos > buffer.__len__()) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |