Skip to content

Commit 1f90f04

Browse files
committed
Support inf, -inf, nan literals (and any
uppercasing). struct.pack("d") ducktypes like CPython and calls __float__. This fixes a number of issues for PyAMF.
1 parent 9fc91c7 commit 1f90f04

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

src/org/python/core/PyFloat.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ final PyString float___repr__() {
105105
}
106106

107107
private String formatDouble(int precision) {
108+
if (Double.isNaN(value)) {
109+
return "nan";
110+
}
111+
108112
String result = String.format("%%.%dg", precision);
109113
result = Py.newString(result).__mod__(this).toString();
110114

src/org/python/core/PyString.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,10 @@ public double atof() {
15101510
try {
15111511
// Double.valueOf allows format specifier ("d" or "f") at the end
15121512
String lowSval = sval.toLowerCase();
1513+
if (lowSval.equals("nan")) return Double.NaN;
1514+
else if (lowSval.equals("inf")) return Double.POSITIVE_INFINITY;
1515+
else if (lowSval.equals("-inf")) return Double.NEGATIVE_INFINITY;
1516+
15131517
if (lowSval.endsWith("d") || lowSval.endsWith("f")) {
15141518
throw new NumberFormatException("format specifiers not allowed");
15151519
}

src/org/python/modules/struct.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,6 @@ BigInteger get_ulong(PyObject value) {
351351
}
352352

353353
double get_float(PyObject value) {
354-
if (!(value instanceof PyFloat))
355-
throw StructError("required argument is not an float");
356354
return value.__float__().getValue();
357355
}
358356

0 commit comments

Comments
 (0)