Skip to content

Commit 229ac53

Browse files
authored
Enable some float construction tests (#971)
* Enable some float construction tests * Update after review
1 parent 38f0c54 commit 229ac53

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

Src/IronPython/Runtime/LiteralParser.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ public static double ParseFloat(string text) {
617617
}
618618

619619
private static double ParseFloatNoCatch(string text) {
620-
string s = ReplaceUnicodeDigits(text);
620+
string s = ReplaceUnicodeCharacters(text);
621621
switch (s.ToLowerAsciiTriggered().lstrip()) {
622622
case "nan":
623623
case "+nan":
@@ -635,12 +635,18 @@ private static double ParseFloatNoCatch(string text) {
635635
}
636636
}
637637

638-
private static string ReplaceUnicodeDigits(string text) {
638+
private static string ReplaceUnicodeCharacters(string text) {
639639
StringBuilder replacement = null;
640640
for (int i = 0; i < text.Length; i++) {
641-
if (text[i] >= '\x660' && text[i] <= '\x669') {
641+
char ch = text[i];
642+
if (ch >= '\x660' && ch <= '\x669') {
643+
// replace unicode digits
642644
if (replacement == null) replacement = new StringBuilder(text);
643-
replacement[i] = (char)(text[i] - '\x660' + '0');
645+
replacement[i] = (char)(ch - '\x660' + '0');
646+
} else if (ch >= '\x80' && char.IsWhiteSpace(ch)) {
647+
// replace unicode whitespace
648+
if (replacement == null) replacement = new StringBuilder(text);
649+
replacement[i] = ' ';
644650
}
645651
}
646652
if (replacement != null) {

Src/IronPython/Runtime/Operations/FloatOps.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private static double ParseFloat(string x) {
354354
}
355355
return LiteralParser.ParseFloat(x);
356356
} catch (FormatException) {
357-
throw PythonOps.ValueError("invalid literal for float(): {0}", x);
357+
throw PythonOps.ValueError("could not convert string to float: '{0}'", x);
358358
}
359359
}
360360

Src/StdLib/Lib/test/test_float.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ def test_float(self):
5151
self.assertRaises(TypeError, float, {})
5252
self.assertRaisesRegex(TypeError, "not 'dict'", float, {})
5353
# Lone surrogate
54-
self.assertRaises(UnicodeEncodeError, float, '\uD8F0')
54+
if sys.version_info >= (3,7) or sys.implementation.name == 'ironpython':
55+
self.assertRaises(ValueError, float, '\uD8F0')
56+
else:
57+
self.assertRaises(UnicodeEncodeError, float, '\uD8F0')
5558
# check that we don't accept alternate exponent markers
5659
self.assertRaises(ValueError, float, "-1.7d29")
5760
self.assertRaises(ValueError, float, "3D-14")

Tests/test_float_stdlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def load_tests(loader, standard_tests, pattern):
2222
#suite.addTest(test.test_float.FormatTestCase('test_format_testfile'))
2323
#suite.addTest(test.test_float.FormatTestCase('test_issue5864'))
2424
suite.addTest(test.test_float.GeneralFloatCases('test_error_message'))
25-
#suite.addTest(test.test_float.GeneralFloatCases('test_float'))
25+
suite.addTest(test.test_float.GeneralFloatCases('test_float'))
2626
suite.addTest(test.test_float.GeneralFloatCases('test_float_containment'))
2727
suite.addTest(test.test_float.GeneralFloatCases('test_float_memoryview'))
2828
suite.addTest(test.test_float.GeneralFloatCases('test_float_mod'))
@@ -31,7 +31,7 @@ def load_tests(loader, standard_tests, pattern):
3131
suite.addTest(test.test_float.GeneralFloatCases('test_floatasratio'))
3232
suite.addTest(test.test_float.GeneralFloatCases('test_floatconversion'))
3333
suite.addTest(test.test_float.GeneralFloatCases('test_is_integer'))
34-
#suite.addTest(test.test_float.GeneralFloatCases('test_non_numeric_input_types'))
34+
suite.addTest(test.test_float.GeneralFloatCases('test_non_numeric_input_types'))
3535
suite.addTest(test.test_float.HexFloatTestCase('test_ends'))
3636
suite.addTest(test.test_float.HexFloatTestCase('test_from_hex'))
3737
suite.addTest(test.test_float.HexFloatTestCase('test_invalid_inputs'))

0 commit comments

Comments
 (0)