From: <pj...@us...> - 2010-03-13 17:33:20
|
Revision: 6984 http://jython.svn.sourceforge.net/jython/?rev=6984&view=rev Author: pjenvey Date: 2010-03-13 17:33:14 +0000 (Sat, 13 Mar 2010) Log Message: ----------- fix the cmath module to accept objects implementing __float__ and remove its duplicate implementation of asDouble Modified Paths: -------------- trunk/jython/Lib/test/test_cmath_jy.py trunk/jython/NEWS trunk/jython/src/org/python/modules/cmath.java Modified: trunk/jython/Lib/test/test_cmath_jy.py =================================================================== --- trunk/jython/Lib/test/test_cmath_jy.py 2010-03-13 03:38:40 UTC (rev 6983) +++ trunk/jython/Lib/test/test_cmath_jy.py 2010-03-13 17:33:14 UTC (rev 6984) @@ -99,6 +99,27 @@ self.assertAlmostEqual(complex(1.00071, 0.00490826), cmath.tanh(complex(3, 4))) + def test_faux_float(self): + class Foo: + def __float__(self): + return 1.0 + class Bar(object): + def __float__(self): + return 1.0 + self.assertEqual(cmath.log(Foo()), 0.0) + self.assertEqual(cmath.log(Bar()), 0.0) + + def test_faux_complex(self): + class Foo: + def __complex__(self): + return 1.0j + class Bar(object): + def __complex__(self): + return 1.0j + self.assertEqual(cmath.log(Foo()), cmath.log(1.0j)) + self.assertEqual(cmath.log(Bar()), cmath.log(1.0j)) + + def test_main(): test_support.run_unittest(CmathTestCase) Modified: trunk/jython/NEWS =================================================================== --- trunk/jython/NEWS 2010-03-13 03:38:40 UTC (rev 6983) +++ trunk/jython/NEWS 2010-03-13 17:33:14 UTC (rev 6984) @@ -20,6 +20,7 @@ - Fix runtime issues during exitfuncs triggered via SystemRestart (such as during Django or Pylons development mode reloading) - Fix pickling of collections.defaultdict objects + - Fix the cmath module to accept objects implementing the __float__ method Jython 2.5.1rc3 Bugs Fixed Modified: trunk/jython/src/org/python/modules/cmath.java =================================================================== --- trunk/jython/src/org/python/modules/cmath.java 2010-03-13 03:38:40 UTC (rev 6983) +++ trunk/jython/src/org/python/modules/cmath.java 2010-03-13 17:33:14 UTC (rev 6984) @@ -2,8 +2,8 @@ import org.python.core.Py; import org.python.core.PyComplex; -import org.python.core.PyException; import org.python.core.PyFloat; +import org.python.core.PyInstance; import org.python.core.PyObject; import org.python.modules.math; @@ -24,28 +24,39 @@ return (Math.sqrt(x * x + y * y)); } - private static PyComplex complexFromPyObject(PyObject in) { - try{ - return(in.__complex__()); - } catch(PyException e){ - if(e.type == Py.AttributeError || e.type == Py.ValueError) { - throw Py.TypeError("a float is required"); + private static PyComplex complexFromPyObject(PyObject obj) { + // If op is already of type PyComplex_Type, return its value + if (obj instanceof PyComplex) { + return (PyComplex)obj; + } + + // If not, use op's __complex__ method, if it exists + PyObject newObj = null; + if (obj instanceof PyInstance) { + // this can go away in python 3000 + if (obj.__findattr__("__complex__") != null) { + newObj = obj.invoke("__complex__"); } - throw e; + // else try __float__ + } else { + PyObject complexFunc = obj.getType().lookup("__complex__"); + if (complexFunc != null) { + newObj = complexFunc.__call__(obj); + } } - } - - private static double doubleFromPyObject(PyObject in) { - try{ - return(in.__float__().getValue()); - } catch(PyException e){ - if(e.type == Py.AttributeError || e.type == Py.ValueError) { - throw Py.TypeError("a float is required"); + + if (newObj != null) { + if (!(newObj instanceof PyComplex)) { + throw Py.TypeError("__complex__ should return a complex object"); } - throw e; + return (PyComplex)newObj; } + + // If neither of the above works, interpret op as a float giving the real part of + // the result, and fill in the imaginary part as 0 + return new PyComplex(obj.asDouble(), 0); } - + public static PyObject acos(PyObject in) { PyComplex x = complexFromPyObject(in); return (c_prodi(log(x.__add__(i @@ -146,7 +157,7 @@ } public static PyComplex log(PyObject in, PyObject base) { - return log(complexFromPyObject(in), doubleFromPyObject(base)); + return log(complexFromPyObject(in), base.asDouble()); } public static PyComplex log(PyComplex x, double base) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |