From: <zy...@us...> - 2008-07-30 19:57:26
|
Revision: 5023 http://jython.svn.sourceforge.net/jython/?rev=5023&view=rev Author: zyasoft Date: 2008-07-30 19:57:16 +0000 (Wed, 30 Jul 2008) Log Message: ----------- Changed default format for __getformat__/__setformat__ to "IEEE, big-endian", and supported switching back and forth with "unknown". Modified Paths: -------------- branches/asm/Lib/test/test_float_jy.py branches/asm/src/org/python/core/PyFloat.java branches/asm/src/org/python/modules/struct.java Modified: branches/asm/Lib/test/test_float_jy.py =================================================================== --- branches/asm/Lib/test/test_float_jy.py 2008-07-30 15:46:31 UTC (rev 5022) +++ branches/asm/Lib/test/test_float_jy.py 2008-07-30 19:57:16 UTC (rev 5023) @@ -7,7 +7,7 @@ import unittest from test import test_support -jython = sys.platform.startswith('java') +jython = test_support.is_jython class FloatTestCase(unittest.TestCase): @@ -67,14 +67,12 @@ def test_nan(self): self.assert_(type(float('NaN')), float) - # XXX: FIXME - #self.assert_(type(float('nan')), float) + self.assert_(type(float('nan')), float) self.assertEqual(long(float('NaN')), 0) def test_infinity(self): self.assert_(type(float('Infinity')), float) - # XXX: FIXME - #self.assert_(type(float('inf')), float) + self.assert_(type(float('inf')), float) self.assertRaises(OverflowError, long, float('Infinity')) def test_float_none(self): Modified: branches/asm/src/org/python/core/PyFloat.java =================================================================== --- branches/asm/src/org/python/core/PyFloat.java 2008-07-30 15:46:31 UTC (rev 5022) +++ branches/asm/src/org/python/core/PyFloat.java 2008-07-30 19:57:16 UTC (rev 5023) @@ -584,26 +584,58 @@ return float___getnewargs__(); } + // standard singleton issues apply here to __getformat__/__setformat__, + // but this is what Python demands + + public enum Format { + UNKNOWN ("unknown"), + BE ("IEEE, big-endian"), + LE ("IEEE, little-endian"); + + private final String format; + Format(String format) { + this.format = format; + } + public String format() { return format; } + } + + // subset of IEEE-754, the JVM is big-endian + public static volatile Format double_format = Format.BE; + public static volatile Format float_format = Format.BE; + @ExposedClassMethod public static String float___getformat__(PyType type, String typestr) { - if (!"double".equals(typestr) && !"float".equals(typestr)) { + if ("double".equals(typestr)) { + return double_format.format(); + } else if ("float".equals(typestr)) { + return float_format.format(); + } else { throw Py.ValueError("__getformat__() argument 1 must be 'double' or 'float'"); } - return "unknown"; } - + @ExposedClassMethod public static void float___setformat__(PyType type, String typestr, String format) { + Format new_format = null; if (!"double".equals(typestr) && !"float".equals(typestr)) { throw Py.ValueError("__setformat__() argument 1 must be 'double' or 'float'"); } - if ("IEEE, little-endian".equals(format) || "IEEE, big-endian".equals(format)) { - throw Py.ValueError(String.format("can only set %s format to 'unknown' or the " - + "detected platform value", typestr)); - } else if (!"unknown".equals(format)) { - throw Py.ValueError("__setformat__() argument 2 must be 'unknown', " - + "'IEEE, little-endian' or 'IEEE, big-endian'"); - } + if (Format.LE.format().equals(format)) { + throw Py.ValueError(String.format("can only set %s format to 'unknown' or the " + "detected platform value", typestr)); + } else if (Format.BE.format().equals(format)) { + new_format = Format.BE; + } else if (Format.UNKNOWN.format().equals(format)) { + new_format = Format.UNKNOWN; + } else { + throw Py.ValueError("__setformat__() argument 2 must be 'unknown', " + "'IEEE, little-endian' or 'IEEE, big-endian'"); + } + if (new_format != null) { + if ("double".equals(typestr)) { + double_format = new_format; + } else { + float_format = new_format; + } + } } public boolean isMappingType() { return false; } Modified: branches/asm/src/org/python/modules/struct.java =================================================================== --- branches/asm/src/org/python/modules/struct.java 2008-07-30 15:46:31 UTC (rev 5022) +++ branches/asm/src/org/python/modules/struct.java 2008-07-30 19:57:16 UTC (rev 5023) @@ -748,8 +748,13 @@ } Object unpack(ByteStream buf) { - int v = LEreadInt(buf); - return Py.newFloat(Float.intBitsToFloat(v)); + int bits = LEreadInt(buf); + float v = Float.intBitsToFloat(bits); + if (PyFloat.float_format == PyFloat.Format.UNKNOWN && ( + Float.isInfinite(v) || Float.isNaN(v))) { + throw Py.ValueError("can't unpack IEEE 754 special value on non-IEEE platform"); + } + return Py.newFloat(v); } } @@ -763,7 +768,12 @@ Object unpack(ByteStream buf) { long bits = (LEreadInt(buf) & 0xFFFFFFFFL) + (((long)LEreadInt(buf)) << 32); - return Py.newFloat(Double.longBitsToDouble(bits)); + double v = Double.longBitsToDouble(bits); + if (PyFloat.double_format == PyFloat.Format.UNKNOWN && + (Double.isInfinite(v) || Double.isNaN(v))) { + throw Py.ValueError("can't unpack IEEE 754 special value on non-IEEE platform"); + } + return Py.newFloat(v); } } @@ -775,8 +785,13 @@ } Object unpack(ByteStream buf) { - int v = BEreadInt(buf); - return Py.newFloat(Float.intBitsToFloat(v)); + int bits = BEreadInt(buf); + float v = Float.intBitsToFloat(bits); + if (PyFloat.float_format == PyFloat.Format.UNKNOWN && ( + Float.isInfinite(v) || Float.isNaN(v))) { + throw Py.ValueError("can't unpack IEEE 754 special value on non-IEEE platform"); + } + return Py.newFloat(v); } } @@ -788,9 +803,14 @@ } Object unpack(ByteStream buf) { - long bits = (((long)BEreadInt(buf)) << 32) + - (BEreadInt(buf) & 0xFFFFFFFFL); - return Py.newFloat(Double.longBitsToDouble(bits)); + long bits = (((long) BEreadInt(buf)) << 32) + + (BEreadInt(buf) & 0xFFFFFFFFL); + double v = Double.longBitsToDouble(bits); + if (PyFloat.double_format == PyFloat.Format.UNKNOWN && + (Double.isInfinite(v) || Double.isNaN(v))) { + throw Py.ValueError("can't unpack IEEE 754 special value on non-IEEE platform"); + } + return Py.newFloat(v); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |