1616import org .python .core .PyLong ;
1717import org .python .core .PyObject ;
1818import org .python .core .PyString ;
19+ import org .python .core .PyStringMap ;
1920import org .python .core .PyTuple ;
2021
2122import java .math .BigInteger ;
@@ -257,7 +258,7 @@ public class struct {
257258 * Exception raised on various occasions; argument is a
258259 * string describing what is wrong.
259260 */
260- public static PyString error = new PyString ( "struct. error" );
261+ public static final PyObject error = Py . makeClass ( " error", Py . Exception , exceptionNamespace () );
261262
262263 public static String __doc__ =
263264 "Functions to convert between Python values and C structs.\n " +
@@ -330,8 +331,9 @@ int get_int(PyObject value) {
330331 long get_long (PyObject value ) {
331332 if (value instanceof PyLong ){
332333 Object v = value .__tojava__ (Long .TYPE );
333- if (v == Py .NoConversion )
334- throw Py .OverflowError ("long int too long to convert" );
334+ if (v == Py .NoConversion ) {
335+ throw StructError ("long int too long to convert" );
336+ }
335337 return ((Long ) v ).longValue ();
336338 } else
337339 return get_int (value );
@@ -341,7 +343,7 @@ BigInteger get_ulong(PyObject value) {
341343 if (value instanceof PyLong ){
342344 BigInteger v = (BigInteger )value .__tojava__ (BigInteger .class );
343345 if (v .compareTo (PyLong .maxULong ) > 0 ){
344- throw Py . OverflowError ("unsigned long int too long to convert" );
346+ throw StructError ("unsigned long int too long to convert" );
345347 }
346348 return v ;
347349 } else
@@ -1041,4 +1043,10 @@ public static PyTuple unpack(String format, String string) {
10411043 private static PyException StructError (String explanation ) {
10421044 return new PyException (error , explanation );
10431045 }
1046+
1047+ private static PyObject exceptionNamespace () {
1048+ PyObject dict = new PyStringMap ();
1049+ dict .__setitem__ ("__module__" , new PyString ("struct" ));
1050+ return dict ;
1051+ }
10441052}
0 commit comments