Skip to content

Commit 2c31003

Browse files
committed
Fixed handling of illegal characters when converting string to float
1 parent 272c3ff commit 2c31003

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,8 +1029,8 @@ private double convertStringToDoubleOrThrow(String str) throws NumberFormatExcep
10291029

10301030
for (int i = 0; i < n; i++) {
10311031
char ch = str.charAt(i);
1032-
if (ch == '\u0000') {
1033-
throw raise(ValueError, ErrorMessages.EMPTY_STR_FOR_COMPLEX);
1032+
if (ch == '\u0000' || ch == 'x' || ch == 'X') {
1033+
throw new NumberFormatException();
10341034
}
10351035
if (Character.isDigit(ch)) {
10361036
if (s == null) {
@@ -1039,11 +1039,14 @@ private double convertStringToDoubleOrThrow(String str) throws NumberFormatExcep
10391039
int val = Character.digit(ch, 10);
10401040
s.setCharAt(i, Character.forDigit(val, 10));
10411041
}
1042+
if (Character.isWhitespace(ch)) {
1043+
if (s == null) {
1044+
s = new StringBuilder(str);
1045+
}
1046+
s.setCharAt(i, ' ');
1047+
}
10421048
}
1043-
String sval = str.trim();
1044-
if (s != null) {
1045-
sval = s.toString();
1046-
}
1049+
String sval = s != null ? s.toString() : str.trim();
10471050
String lowSval = sval.toLowerCase(Locale.ENGLISH);
10481051
if (lowSval.equals("nan") || lowSval.equals("+nan")) {
10491052
return Double.NaN;

0 commit comments

Comments
 (0)