Skip to content

Commit b30f6f7

Browse files
committed
o fix passing chars > 256 to Py.makeCharacter
o make struct.error a class and raise struct.errors instead of OverflowErrors
1 parent 476aa76 commit b30f6f7

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/org/python/core/Py.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,11 @@ public static final PyString makeCharacter(char c) {
14441444
static final PyString makeCharacter(int codepoint, boolean toUnicode) {
14451445
if (toUnicode) {
14461446
return new PyUnicode(codepoint);
1447+
} else if (codepoint > 65536) {
1448+
throw new IllegalArgumentException(String.format("Codepoint > 65536 (%d) requires "
1449+
+ "toUnicode argument", codepoint));
1450+
} else if (codepoint > 256) {
1451+
return new PyString((char)codepoint);
14471452
}
14481453

14491454
if (letters == null) {

src/org/python/modules/struct.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.python.core.PyLong;
1717
import org.python.core.PyObject;
1818
import org.python.core.PyString;
19+
import org.python.core.PyStringMap;
1920
import org.python.core.PyTuple;
2021

2122
import 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

Comments
 (0)