From: <pj...@us...> - 2008-10-31 02:05:00
|
Revision: 5533 http://jython.svn.sourceforge.net/jython/?rev=5533&view=rev Author: pjenvey Date: 2008-10-31 02:04:57 +0000 (Fri, 31 Oct 2008) Log Message: ----------- add asDouble (like asInt), fix round conversion Modified Paths: -------------- trunk/jython/Lib/test/test_builtin_jy.py trunk/jython/src/org/python/core/PyFloat.java trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/__builtin__.java Modified: trunk/jython/Lib/test/test_builtin_jy.py =================================================================== --- trunk/jython/Lib/test/test_builtin_jy.py 2008-10-30 22:43:59 UTC (rev 5532) +++ trunk/jython/Lib/test/test_builtin_jy.py 2008-10-31 02:04:57 UTC (rev 5533) @@ -102,11 +102,13 @@ return None self.assert_(not callable(Baz())) -class RangeTest(unittest.TestCase): +class ConversionTest(unittest.TestCase): class Foo(object): def __int__(self): return 3 + def __float__(self): + return 3.14 foo = Foo() def test_range_non_int(self): @@ -115,6 +117,9 @@ def test_xrange_non_int(self): self.assertEqual(list(xrange(self.foo)), [0, 1, 2]) + def test_round_non_float(self): + self.assertEqual(round(self.Foo(), 1), 3.1) + def test_main(): test.test_support.run_unittest(BuiltinTest, LoopTest, @@ -124,7 +129,7 @@ ReturnTest, ReprTest, CallableTest, - RangeTest) + ConversionTest) if __name__ == "__main__": test_main() Modified: trunk/jython/src/org/python/core/PyFloat.java =================================================================== --- trunk/jython/src/org/python/core/PyFloat.java 2008-10-30 22:43:59 UTC (rev 5532) +++ trunk/jython/src/org/python/core/PyFloat.java 2008-10-31 02:04:57 UTC (rev 5533) @@ -620,6 +620,10 @@ return float___getnewargs__(); } + public double asDouble() { + return value; + } + // standard singleton issues apply here to __getformat__/__setformat__, // but this is what Python demands Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2008-10-30 22:43:59 UTC (rev 5532) +++ trunk/jython/src/org/python/core/PyObject.java 2008-10-31 02:04:57 UTC (rev 5533) @@ -3887,8 +3887,26 @@ } /** - * Coerce this object into an index-sized integer. + * Convert this object into a double. Throws a PyException on failure. * + * @return a double value + */ + public double asDouble() { + PyFloat floatObj; + try { + floatObj = __float__(); + } catch (PyException pye) { + if (Py.matchException(pye, Py.AttributeError)) { + throw Py.TypeError("a float is required"); + } + throw pye; + } + return floatObj.asDouble(); + } + + /** + * Convert this object into an index-sized integer. Throws a PyException on failure. + * * @return an index-sized int */ public int asIndex() { @@ -3896,7 +3914,7 @@ } /** - * Coerce this object into an index-sized integer. + * Convert this object into an index-sized integer. * * Throws a Python exception on Overflow if specified an exception type for err. * Modified: trunk/jython/src/org/python/core/__builtin__.java =================================================================== --- trunk/jython/src/org/python/core/__builtin__.java 2008-10-30 22:43:59 UTC (rev 5532) +++ trunk/jython/src/org/python/core/__builtin__.java 2008-10-31 02:04:57 UTC (rev 5533) @@ -1534,7 +1534,7 @@ ArgParser ap = new ArgParser("round", args, kwds, new String[] {"number", "ndigits"}, 0); PyObject number = ap.getPyObject(0); int ndigits = ap.getInt(1, 0); - return round(Py.py2double(number), ndigits); + return round(number.asDouble(), ndigits); } private static PyFloat round(double f, int digits) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |