From: <pj...@us...> - 2009-11-22 20:24:52
|
Revision: 6947 http://jython.svn.sourceforge.net/jython/?rev=6947&view=rev Author: pjenvey Date: 2009-11-22 20:24:31 +0000 (Sun, 22 Nov 2009) Log Message: ----------- o add PyObject.asLong, remove asLong(int) uses o fix PyString not overriding asDouble o asDouble already __float__s for us in Condition.wait Modified Paths: -------------- trunk/jython/Lib/test/test_builtin_jy.py trunk/jython/src/org/python/core/PyFile.java trunk/jython/src/org/python/core/PyInteger.java trunk/jython/src/org/python/core/PyLong.java trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/PyString.java trunk/jython/src/org/python/modules/_threading/Condition.java trunk/jython/src/org/python/modules/random/PyRandom.java Modified: trunk/jython/Lib/test/test_builtin_jy.py =================================================================== --- trunk/jython/Lib/test/test_builtin_jy.py 2009-11-17 16:16:14 UTC (rev 6946) +++ trunk/jython/Lib/test/test_builtin_jy.py 2009-11-22 20:24:31 UTC (rev 6947) @@ -136,6 +136,8 @@ def test_round_non_float(self): self.assertEqual(round(self.Foo(), 1), 3.1) + # 2.5/2.5.1 regression + self.assertRaises(TypeError, round, '1.5') class ExecEvalTest(unittest.TestCase): Modified: trunk/jython/src/org/python/core/PyFile.java =================================================================== --- trunk/jython/src/org/python/core/PyFile.java 2009-11-17 16:16:14 UTC (rev 6946) +++ trunk/jython/src/org/python/core/PyFile.java 2009-11-22 20:24:31 UTC (rev 6947) @@ -464,11 +464,7 @@ file_truncate(); return; } - try { - file_truncate(position.asLong(0)); - } catch (PyObject.ConversionException ce) { - throw Py.TypeError("an integer is required"); - } + file_truncate(position.asLong()); } final synchronized void file_truncate(long position) { Modified: trunk/jython/src/org/python/core/PyInteger.java =================================================================== --- trunk/jython/src/org/python/core/PyInteger.java 2009-11-17 16:16:14 UTC (rev 6946) +++ trunk/jython/src/org/python/core/PyInteger.java 2009-11-22 20:24:31 UTC (rev 6947) @@ -854,4 +854,9 @@ public int asInt() { return getValue(); } + + @Override + public long asLong() { + return getValue(); + } } Modified: trunk/jython/src/org/python/core/PyLong.java =================================================================== --- trunk/jython/src/org/python/core/PyLong.java 2009-11-17 16:16:14 UTC (rev 6946) +++ trunk/jython/src/org/python/core/PyLong.java 2009-11-22 20:24:31 UTC (rev 6947) @@ -199,7 +199,7 @@ } public long asLong(int index) { - return getLong(Long.MIN_VALUE, Long.MAX_VALUE, "long too big to convert"); + return asLong(); } public int asInt(int index) { @@ -213,6 +213,11 @@ "long int too large to convert to int"); } + @Override + public long asLong() { + return getLong(Long.MIN_VALUE, Long.MAX_VALUE, "long too big to convert"); + } + public Object __tojava__(Class c) { try { if (c == Byte.TYPE || c == Byte.class) { Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2009-11-17 16:16:14 UTC (rev 6946) +++ trunk/jython/src/org/python/core/PyObject.java 2009-11-22 20:24:31 UTC (rev 6947) @@ -4006,6 +4006,11 @@ throw new ConversionException(index); } + /** + * Convert this object into an int. Throws a PyException on failure. + * + * @return an int value + */ public int asInt() { PyObject intObj; try { @@ -4016,7 +4021,7 @@ } throw pye; } - if (!(intObj instanceof PyInteger) && !(intObj instanceof PyLong)) { + if (!(intObj instanceof PyInteger || intObj instanceof PyLong)) { // Shouldn't happen except with buggy builtin types throw Py.TypeError("nb_int should return int object"); } @@ -4028,6 +4033,28 @@ } /** + * Convert this object longo an long. Throws a PyException on failure. + * + * @return an long value + */ + public long asLong() { + PyObject longObj; + try { + longObj = __long__(); + } catch (PyException pye) { + if (pye.match(Py.AttributeError)) { + throw Py.TypeError("an integer is required"); + } + throw pye; + } + if (!(longObj instanceof PyLong)) { + // Shouldn't happen except with buggy builtin types + throw Py.TypeError("integer conversion failed"); + } + return longObj.asLong(); + } + + /** * Convert this object into a double. Throws a PyException on failure. * * @return a double value Modified: trunk/jython/src/org/python/core/PyString.java =================================================================== --- trunk/jython/src/org/python/core/PyString.java 2009-11-17 16:16:14 UTC (rev 6946) +++ trunk/jython/src/org/python/core/PyString.java 2009-11-22 20:24:31 UTC (rev 6947) @@ -2459,13 +2459,29 @@ @Override public int asInt() { - // We have to override asInt because we override __int__, but generally don't want - // implicit atoi conversions for the base types. blah + // We have to override asInt/Long/Double because we override __int/long/float__, + // but generally don't want implicit atoi conversions for the base types. blah + asNumberCheck("__int__", "an integer"); + return super.asInt(); + } + + @Override + public long asLong() { + asNumberCheck("__long__", "an integer"); + return super.asLong(); + } + + @Override + public double asDouble() { + asNumberCheck("__float__", "a float"); + return super.asDouble(); + } + + private void asNumberCheck(String methodName, String description) { PyType type = getType(); - if (type == PyString.TYPE || type == PyUnicode.TYPE || type.lookup("__int__") == null) { - throw Py.TypeError("an integer is required"); + if (type == PyString.TYPE || type == PyUnicode.TYPE || type.lookup(methodName) == null) { + throw Py.TypeError(description + " is required"); } - return super.asInt(); } public String asName(int index) throws PyObject.ConversionException { Modified: trunk/jython/src/org/python/modules/_threading/Condition.java =================================================================== --- trunk/jython/src/org/python/modules/_threading/Condition.java 2009-11-17 16:16:14 UTC (rev 6946) +++ trunk/jython/src/org/python/modules/_threading/Condition.java 2009-11-22 20:24:31 UTC (rev 6947) @@ -86,7 +86,7 @@ if (timeout == Py.None) { _condition.await(); } else { - long nanos = (long) (timeout.__float__().asDouble() * 1e9); + long nanos = (long) (timeout.asDouble() * 1e9); _condition.awaitNanos(nanos); } } Modified: trunk/jython/src/org/python/modules/random/PyRandom.java =================================================================== --- trunk/jython/src/org/python/modules/random/PyRandom.java 2009-11-17 16:16:14 UTC (rev 6946) +++ trunk/jython/src/org/python/modules/random/PyRandom.java 2009-11-22 20:24:31 UTC (rev 6947) @@ -19,7 +19,6 @@ import org.python.expose.ExposedNew; import org.python.expose.ExposedType; - @ExposedType(name = "_random.Random") public class PyRandom extends PyObject { @@ -41,46 +40,39 @@ * uses the value, else it uses the hash function of PyObject */ @ExposedMethod(defaults = "null") - final PyObject Random_seed(PyObject seed) { + final void Random_seed(PyObject seed) { + long n; if (seed == null) { seed = new PyLong(System.currentTimeMillis()); } if (seed instanceof PyLong) { PyLong max = new PyLong(Long.MAX_VALUE); - PyLong seed_modulus = (PyLong)(seed.__mod__(max)); - this.javaRandom.setSeed(seed_modulus.asLong(0)); + n = seed.__mod__(max).asLong(); } else if (seed instanceof PyInteger) { - this.javaRandom.setSeed(((PyInteger)seed).getValue()); + n = seed.asLong(); } else { - this.javaRandom.setSeed(seed.hashCode()); + n = seed.hashCode(); } - - // duplicating the _randommodule.c::init_by_array return - return Py.None; + this.javaRandom.setSeed(n); } @ExposedNew @ExposedMethod - public void Random___init__(PyObject[] args, String[] keywords) {} + final void Random___init__(PyObject[] args, String[] keywords) {} @ExposedMethod - public PyObject Random_jumpahead(PyObject arg0) { - long inc; - if (arg0 instanceof PyLong) { - inc=((PyLong)arg0).asLong(0); - } else if (arg0 instanceof PyInteger) { - inc=((PyInteger)arg0).getValue(); - } else { - throw Py.TypeError("jumpahead requires an integer"); + final void Random_jumpahead(PyObject arg0) { + if (!(arg0 instanceof PyInteger || arg0 instanceof PyLong)) { + throw Py.TypeError(String.format("jumpahead requires an integer, not '%s'", + arg0.getType().fastGetName())); } - for(int i=0;i<inc;i++) { + for (long i = arg0.asLong(); i > 0; i--) { this.javaRandom.nextInt(); } - return Py.None; } @ExposedMethod - public PyObject Random_setstate(PyObject arg0) { + final void Random_setstate(PyObject arg0) { if (!(arg0 instanceof PyTuple)) { throw Py.TypeError("state vector must be a tuple"); } @@ -104,13 +96,10 @@ } catch (ClassNotFoundException e) { throw Py.SystemError("state vector invalid: "+e.getMessage()); } - - // duplicating the _randommodule.c::random_setstate return - return Py.None; } @ExposedMethod - public PyObject Random_getstate() { + final PyObject Random_getstate() { try { ByteArrayOutputStream bout=new ByteArrayOutputStream(); ObjectOutputStream oout=new ObjectOutputStream(bout); @@ -134,7 +123,7 @@ * problems. */ @ExposedMethod - public PyObject Random_random() { + final PyObject Random_random() { long a=this.javaRandom.nextInt()>>>5; long b=this.javaRandom.nextInt()>>>6; double ret=(a*67108864.0+b)*(1.0/9007199254740992.0); @@ -142,7 +131,7 @@ } @ExposedMethod - public PyLong Random_getrandbits(int k) { + final PyLong Random_getrandbits(int k) { return new PyLong(new BigInteger(k, javaRandom)); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |