Index: PyLong.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyLong.java,v retrieving revision 2.18 diff -u -r2.18 PyLong.java --- PyLong.java 5 Aug 2003 11:21:52 -0000 2.18 +++ PyLong.java 8 Mar 2004 16:32:34 -0000 @@ -15,6 +15,8 @@ BigInteger.valueOf(Long.MIN_VALUE); private static final BigInteger maxLong = BigInteger.valueOf(Long.MAX_VALUE); + public static final BigInteger maxULong = + BigInteger.valueOf(1).shiftLeft(64).subtract(BigInteger.valueOf(1)); private static final BigInteger minDouble = new java.math.BigDecimal(Double.MIN_VALUE).toBigInteger(); Index: struct.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/modules/struct.java,v retrieving revision 2.7 diff -u -r2.7 struct.java --- struct.java 29 Oct 2002 10:54:37 -0000 2.7 +++ struct.java 8 Mar 2004 16:33:44 -0000 @@ -8,7 +8,8 @@ package org.python.modules; -import java.io.*; +import java.math.BigInteger; + import org.python.core.*; /** @@ -318,15 +319,26 @@ } } - long get_ulong(PyObject value) { - if (value instanceof PyLong){ - Object v = value.__tojava__(Long.TYPE); - if (v == Py.NoConversion) - throw Py.OverflowError("long int too long to convert"); - return ((Long) v).longValue(); - } else - return get_int(value); - } + long get_long(PyObject value) { + if (value instanceof PyLong){ + Object v = value.__tojava__(Long.TYPE); + if (v == Py.NoConversion) + throw Py.OverflowError("long int too long to convert"); + return ((Long) v).longValue(); + } else + return get_int(value); + } + + BigInteger get_ulong(PyObject value) { + if (value instanceof PyLong){ + BigInteger v = (BigInteger)value.__tojava__(BigInteger.class); + if (v.compareTo(PyLong.maxULong) > 0){ + throw Py.OverflowError("unsigned long int too long to convert"); + } + return v; + } else + return BigInteger.valueOf(get_int(value)); + } double get_float(PyObject value) { if (!(value instanceof PyFloat)) @@ -491,7 +503,7 @@ if (!(value instanceof PyString)) throw StructError("argument for 'p' must be a string"); - buf.writeByte(Math.min(value.toString().length(), count-1)); + buf.writeByte(Math.min(0xFF, Math.min(value.toString().length(), count-1))); return super.doPack(buf, count-1, pos, args); } @@ -602,7 +614,7 @@ static class LEUnsignedIntFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { - LEwriteInt(buf, (int)(get_ulong(value) & 0xFFFFFFFF)); + LEwriteInt(buf, (int)(get_long(value) & 0xFFFFFFFF)); } Object unpack(ByteStream buf) { @@ -627,7 +639,7 @@ static class BEUnsignedIntFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { - BEwriteInt(buf, (int)(get_ulong(value) & 0xFFFFFFFF)); + BEwriteInt(buf, (int)(get_long(value) & 0xFFFFFFFF)); } Object unpack(ByteStream buf) { long v = BEreadInt(buf); @@ -639,10 +651,11 @@ static class LEUnsignedLongFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { - long lvalue = get_ulong( value ); - if (lvalue < 0) { - throw StructError("can't convert negative long to unsigned"); - } + BigInteger bi = get_ulong(value); + if (bi.compareTo(BigInteger.valueOf(0)) < 0) { + throw StructError("can't convert negative long to unsigned"); + } + long lvalue = bi.longValue(); // underflow is OK -- the bits are correct int high = (int) ( (lvalue & 0xFFFFFFFF00000000L)>>32 ); int low = (int) ( lvalue & 0x00000000FFFFFFFFL ); LEwriteInt( buf, low ); @@ -652,7 +665,7 @@ Object unpack(ByteStream buf) { long low = ( LEreadInt( buf ) & 0X00000000FFFFFFFFL ); long high = ( LEreadInt( buf ) & 0X00000000FFFFFFFFL ); - java.math.BigInteger result=java.math.BigInteger.valueOf(high); + java.math.BigInteger result=java.math.BigInteger.valueOf(high); result=result.multiply(java.math.BigInteger.valueOf(0x100000000L)); result=result.add(java.math.BigInteger.valueOf(low)); return new PyLong(result); @@ -662,10 +675,11 @@ static class BEUnsignedLongFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { - long lvalue = get_ulong( value ); - if (lvalue < 0) { - throw StructError("can't convert negative long to unsigned"); - } + BigInteger bi = get_ulong(value); + if (bi.compareTo(BigInteger.valueOf(0)) < 0) { + throw StructError("can't convert negative long to unsigned"); + } + long lvalue = bi.longValue(); // underflow is OK -- the bits are correct int high = (int) ( (lvalue & 0xFFFFFFFF00000000L)>>32 ); int low = (int) ( lvalue & 0x00000000FFFFFFFFL ); BEwriteInt( buf, high ); @@ -685,7 +699,7 @@ static class LELongFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { - long lvalue = get_ulong( value ); + long lvalue = get_long( value ); int high = (int) ( (lvalue & 0xFFFFFFFF00000000L)>>32 ); int low = (int) ( lvalue & 0x00000000FFFFFFFFL ); LEwriteInt( buf, low ); @@ -693,8 +707,8 @@ } Object unpack(ByteStream buf) { - long low= ( LEreadInt( buf )&(0x00000000FFFFFFFFL) ); - long high= ( (LEreadInt( buf )<<32)&(0xFFFFFFFF00000000L) ); + long low = LEreadInt(buf) & 0x00000000FFFFFFFFL; + long high = ((long)(LEreadInt(buf))<<32) & 0xFFFFFFFF00000000L; long result=(high|low); return new PyLong(result); } @@ -703,7 +717,7 @@ static class BELongFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { - long lvalue = get_ulong( value ); + long lvalue = get_long( value ); int high = (int) ( (lvalue & 0xFFFFFFFF00000000L)>>32 ); int low = (int) ( lvalue & 0x00000000FFFFFFFFL ); BEwriteInt( buf, high ); @@ -711,8 +725,8 @@ } Object unpack(ByteStream buf) { - long high= ( (BEreadInt( buf )<<32)&(0xFFFFFFFF00000000L) ); - long low= ( BEreadInt( buf )&(0x00000000FFFFFFFFL) ); + long high = ((long)(BEreadInt(buf))<<32) & 0xFFFFFFFF00000000L; + long low = BEreadInt(buf) & 0x00000000FFFFFFFFL; long result=(high|low); return new PyLong(result); }