Skip to content

Commit 220c5f9

Browse files
authored
Fix some localization issues (#1622)
* Fix some localization issues * Fix some formatting issues * Re-enable test
1 parent ba425c8 commit 220c5f9

9 files changed

Lines changed: 46 additions & 60 deletions

File tree

Src/IronPython.Modules/_csv.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
using System;
66
using System.Collections;
77
using System.Collections.Generic;
8+
using System.Globalization;
89
using System.Runtime.CompilerServices;
910
using System.Text;
11+
1012
using IronPython.Runtime;
1113
using IronPython.Runtime.Exceptions;
1214
using IronPython.Runtime.Operations;
1315
using IronPython.Runtime.Types;
16+
1417
using Microsoft.Scripting;
1518
using Microsoft.Scripting.Runtime;
1619

@@ -833,8 +836,7 @@ private void ParseSaveField() {
833836
if (_is_numeric_field) {
834837
_is_numeric_field = false;
835838

836-
double tmp;
837-
if (double.TryParse(field, out tmp)) {
839+
if (double.TryParse(field, NumberStyles.Float, CultureInfo.InvariantCulture, out double tmp)) {
838840
if (field.Contains("."))
839841
_fields.Add(tmp);
840842
else

Src/IronPython/Runtime/Operations/DecimalOps.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace IronPython.Runtime.Operations {
1313
public static class DecimalOps {
1414
public static bool __bool__(decimal x) => x != 0;
1515

16+
public static string __str__(decimal x) => x.ToString(CultureInfo.InvariantCulture);
17+
1618
public static string __repr__(decimal x) => x.ToString(CultureInfo.InvariantCulture);
1719

1820
[SpecialName]
@@ -61,7 +63,7 @@ public static string __format__(CodeContext/*!*/ context, decimal self, [NotNone
6163
StringFormatSpec spec = StringFormatSpec.FromString(formatSpec);
6264
// default to the normal
6365
if (spec.IsEmpty) {
64-
return self.ToString();
66+
return __str__(self);
6567
}
6668

6769
string digits = DecimalToFormatString(context, self, spec);

Src/IronPython/Runtime/Operations/FloatOps.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ internal static string DoubleToFormatString(CodeContext/*!*/ context, double sel
885885
int decimalPoints = Math.Max(spec.Precision.Value - digitCnt, 0);
886886

887887
self = MathUtils.Round(self, decimalPoints, MidpointRounding.ToEven);
888-
digits = self.ToString("0.0" + new string('#', decimalPoints));
888+
digits = self.ToString("0.0" + new string('#', decimalPoints), CultureInfo.InvariantCulture);
889889
}
890890
} else {
891891
// just the default formatting
@@ -951,17 +951,17 @@ internal static string DoubleToFormatString(CodeContext/*!*/ context, double sel
951951

952952
if (spec.Type == 'n' && context.LanguageContext.NumericCulture != PythonContext.CCulture) {
953953
if (digitCnt != precision && (self % 1) != 0) {
954-
digits = self.ToString("#,0.0" + new string('#', decimalPoints));
954+
digits = self.ToString("#,0.0" + new string('#', decimalPoints), context.LanguageContext.NumericCulture);
955955
} else {
956956
// leave out the decimal if the precision == # of digits or we have a whole number
957-
digits = self.ToString("#,0");
957+
digits = self.ToString("#,0", context.LanguageContext.NumericCulture);
958958
}
959959
} else {
960960
if (digitCnt != precision && (self % 1) != 0) {
961-
digits = self.ToString("0.0" + new string('#', decimalPoints));
961+
digits = self.ToString("0.0" + new string('#', decimalPoints), CultureInfo.InvariantCulture);
962962
} else {
963963
// leave out the decimal if the precision == # of digits or we have a whole number
964-
digits = self.ToString("0");
964+
digits = self.ToString("0", CultureInfo.InvariantCulture);
965965
}
966966
}
967967
}

Src/IronPython/Runtime/Operations/MarshalOps.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Numerics;
45
using System.Text;
56

6-
using Microsoft.Scripting.Runtime;
7-
using Microsoft.Scripting.Utils;
8-
97
using IronPython.Runtime.Exceptions;
10-
using IronPython.Runtime.Types;
8+
9+
using Microsoft.Scripting.Runtime;
1110

1211
namespace IronPython.Runtime.Operations {
1312
public class MarshalOps {
@@ -487,8 +486,7 @@ private double ReadFloatStr () {
487486

488487
string str = DecodeString (Encoding.ASCII, ReadBytes (_myBytes.Current));
489488

490-
double res = 0;
491-
if (double.TryParse (str, out res)) {
489+
if (double.TryParse(str, NumberStyles.Float, CultureInfo.InvariantCulture, out double res)) {
492490
return res;
493491
}
494492
return 0;

Tests/test_decimal.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,28 @@
77

88
from iptest import run_test, skipUnlessIronPython
99

10+
def SystemDecimalParse(s):
11+
import System
12+
import System.Globalization
13+
return System.Decimal.Parse(s, System.Globalization.CultureInfo.InvariantCulture)
14+
1015
@skipUnlessIronPython()
1116
class DecimalTest(unittest.TestCase):
12-
def test_explicit_from_System_Decimal(self):
13-
import System
1417

18+
def test_explicit_from_System_Decimal(self):
1519
#int
16-
self.assertEqual(str(Decimal(System.Decimal.Parse('45'))), '45')
20+
self.assertEqual(str(Decimal(SystemDecimalParse('45'))), '45')
1721

1822
#float
19-
self.assertEqual(str(Decimal(System.Decimal.Parse('45.34'))), '45.34')
23+
self.assertEqual(str(Decimal(SystemDecimalParse('45.34'))), '45.34')
2024

2125
def test_formatting(self):
22-
import System
23-
d = System.Decimal.Parse('1.4274243253253245432543254545')
26+
d = SystemDecimalParse('1.4274243253253245432543254545')
2427
self.assertEqual('{}'.format(d), '1.4274243253253245432543254545')
2528
self.assertEqual('{:,.2f}'.format(d), '1.43')
2629
self.assertEqual('{:e}'.format(d), '1.427424325325e+00')
2730

28-
d = System.Decimal.Parse('4000000000.40000000')
31+
d = SystemDecimalParse('4000000000.40000000')
2932
self.assertEqual('{}'.format(d), '4000000000.40000000')
3033
self.assertEqual('{:e}'.format(d), '4.000000000400e+09')
3134

Tests/test_methoddispatch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def Check(flagValue, func, *args):
407407
#======================================================================
408408

409409
#!!! easy way to get M5(float) invoked
410-
Check(105, d.M5, System.Single.Parse("3.14"))
410+
Check(105, d.M5, System.Single(3.14))
411411
Check(205, d.M5, 3.14)
412412
self.assertRaises(TypeError, d.M5, None)
413413

@@ -1231,7 +1231,7 @@ def __repr__(self):
12311231

12321232
one.extend([True, False, big(5), DispatchHelpers.Color.Red ])
12331233

1234-
two = [t.Parse("5.5") for t in [ System.Decimal, System.Single, System.Double] ]
1234+
two = [t(5.5) for t in [ System.Decimal, System.Single, System.Double] ]
12351235

12361236
two.extend([None, "5", "5.5", maxint * 2, ])
12371237

Tests/test_number.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def __pos__(self):
246246
if a: self.assertEqual(False, True)
247247

248248

249-
self.assertEqual(int(Single.Parse("3.14159")), 3)
249+
self.assertEqual(int(Single(3.14159)), 3)
250250

251251
#TODO: @skip("interpreted") #Too slow
252252
def test_operators(self):

Tests/test_strformat.py

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -302,75 +302,45 @@ def test_float___format__(self):
302302
tests+= [ (2.0, '6.1', ' 2e+00'),
303303
(2.5, '6.1', ' 2e+00'),
304304
(2.25, '6.1', ' 2e+00'),
305-
(2.25, '6.2', ' 2.2'),
306305
(23.0, '.1', '2e+01'),
307306
(23.0, '.2', '2.3e+01'),
308307
(230.5, '.3', '2.3e+02'),
309308
(11230.54, '.5', '1.1231e+04'),
310309
(111230.54, '.1', '1e+05'),
311310
(100000.0, '.5', '1e+05'),
312-
(230.5, '.3g', '230'),
313-
(230.5, '.3n', '230'),
314311
(0.0, '1.1', '0e+00'),
315312
(0.0, '1.0', '0e+00'),
316313
(1.0, '.0', '1e+00'),
317314
(1.1, '.0', '1e+00'),
318315
(1.1, '.1', '1e+00'),
319316
(10.0, '.1', '1e+01'),
320317
(10.0, '.0', '1e+01'),
321-
(100000000000.0, '', '100000000000.0'),
322318
(1000000000.12, '1.10', '1e+09'),
323319
(1000000000.12, '1.3', '1e+09'),
324320
(999999999999.9, '1.0', '1e+12'),
325321
(999999999999.9, '1.2', '1e+12'),
326-
(999999999999.0, '', '999999999999.0'),
327-
(-999999999999.0, '', '-999999999999.0'),
328-
(10e667, '+', '+inf'),
329-
(-10e667, '+', '-inf'),
330-
(10e667/10e667, '+', '+nan'),
331-
(10e667, '-', 'inf'),
332-
(-10e667, '-', '-inf'),
333-
(10e667/10e667, '-', 'nan'),
334-
(10e667, ' ', ' inf'),
335-
(-10e667, ' ', '-inf'),
336-
(10e667/10e667, ' ', ' nan'),
337322
]
338323
else:
339324
tests+= [ (2.0, '6.1', ' 2.0'),
340325
(2.5, '6.1', ' 2.0'),
341326
(2.25, '6.1', ' 2.0'),
342-
(2.25, '6.2', ' 2.2'),
343327
(23.0, '.1', '2.0e+01'),
344328
(23.0, '.2', '23.0'),
345329
(230.5, '.3', '230.0'),
346330
(11230.54, '.5', '11231.0'),
347331
(111230.54, '.1', '1.0e+05'),
348332
(100000.0, '.5', '1.0e+05'),
349-
(230.5, '.3g', '230'),
350-
(230.5, '.3n', '230'),
351333
(0.0, '1.1', '0.0'),
352334
(0.0, '1.0', '0.0'),
353335
(1.0, '.0', '1.0'),
354336
(1.1, '.0', '1.0'),
355337
(1.1, '.1', '1.0'),
356338
(10.0, '.1', '1.0e+01'),
357339
(10.0, '.0', '1.0e+01'),
358-
(100000000000.0, '', '100000000000.0'),
359340
(1000000000.12, '1.10', '1000000000.0'),
360341
(1000000000.12, '1.3', '1.0e+09'),
361342
(999999999999.9, '1.0', '1.0e+12'),
362343
(999999999999.9, '1.2', '1.0e+12'),
363-
(999999999999.0, '', '999999999999.0'),
364-
(-999999999999.0, '', '-999999999999.0'),
365-
(10e667, '+', '+inf'),
366-
(-10e667, '+', '-inf'),
367-
(10e667/10e667, '+', '+nan'),
368-
(10e667, '-', 'inf'),
369-
(-10e667, '-', '-inf'),
370-
(10e667/10e667, '-', 'nan'),
371-
(10e667, ' ', ' inf'),
372-
(-10e667, ' ', '-inf'),
373-
(10e667/10e667, ' ', ' nan'),
374344
]
375345

376346
tests+= [ (2.0, '', '2.0'),
@@ -394,6 +364,7 @@ def test_float___format__(self):
394364
(2.0, 'x^ 9.10', 'xx 2.0xxx'),
395365
(2.0, '\0^ 9.10', '\0\0 2.0\0\0\0'),
396366
(2.23, '6.2', ' 2.2'),
367+
(2.25, '6.2', ' 2.2'),
397368
(2.25, '6.3', ' 2.25'),
398369
(2.123456789, '2.10', '2.123456789'),
399370

@@ -509,7 +480,7 @@ def test_float___format__(self):
509480
(23.45, '.2g', '23'),
510481
(230.0, '.2g', '2.3e+02'),
511482
(230.1, '.2g', '2.3e+02'),
512-
483+
(230.5, '.3g', '230'),
513484
(230.5, '.4g', '230.5'),
514485
(230.54, '.4g', '230.5'),
515486
(230.54, '.5g', '230.54'),
@@ -525,7 +496,7 @@ def test_float___format__(self):
525496
(23.0, '.2n', '23'),
526497
(230.0, '.2n', '2.3e+02'),
527498
(230.1, '.2n', '2.3e+02'),
528-
499+
(230.5, '.3n', '230'),
529500
(230.5, '.4n', '230.5'),
530501
(230.54, '.4n', '230.5'),
531502
(230.54, '.5n', '230.54'),
@@ -539,6 +510,7 @@ def test_float___format__(self):
539510
(11231.54, 'n', '11231.5'),
540511
(111230.54, 'n', '111231'),
541512
(111230.54, 'g', '111231'),
513+
542514
(0.0, '', '0.0'),
543515
(0.0, '1', '0.0'),
544516
(1.1, '.2', '1.1'),
@@ -547,13 +519,25 @@ def test_float___format__(self):
547519
(100000000.0, '', '100000000.0'),
548520
(1000000000.0, '', '1000000000.0'),
549521
(10000000000.0, '', '10000000000.0'),
522+
(100000000000.0, '', '100000000000.0'),
550523
(1000000000000.0, '', '1000000000000.0'),
551524
(1000000000000.0, 'g', '1e+12'),
552525
(-1000000000000.0, '', '-1000000000000.0'),
553526
(-1000000000000.0, 'g', '-1e+12'),
554527
(-1000000000000.0, 'G', '-1E+12'),
555528
(-1000000000000.0, '.1g', '-1e+12'),
556529
(-1000000000000.0, '.1G', '-1E+12'),
530+
(999999999999.0, '', '999999999999.0'),
531+
(-999999999999.0, '', '-999999999999.0'),
532+
(10e667, '+', '+inf'),
533+
(-10e667, '+', '-inf'),
534+
(10e667/10e667, '+', '+nan'),
535+
(10e667, '-', 'inf'),
536+
(-10e667, '-', '-inf'),
537+
(10e667/10e667, '-', 'nan'),
538+
(10e667, ' ', ' inf'),
539+
(-10e667, ' ', '-inf'),
540+
(10e667/10e667, ' ', ' nan'),
557541
(10e667, '', 'inf'),
558542
(-10e667, '', '-inf'),
559543
(10e667/10e667, '', 'nan'),

Tests/test_types_stdlib.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ def load_tests(loader, standard_tests, pattern):
5656
suite.addTest(test.test_types.TypesTests('test_boolean_ops'))
5757
suite.addTest(test.test_types.TypesTests('test_comparisons'))
5858
suite.addTest(unittest.expectedFailure(test.test_types.TypesTests('test_float__format__'))) # AssertionError: '1.12339e+200' != '1.1234e+200'
59-
if is_netcoreapp21 and is_linux:
60-
suite.addTest(unittest.expectedFailure(test.test_types.TypesTests('test_float__format__locale'))) # https://github.com/IronLanguages/ironpython3/issues/751
61-
else:
62-
suite.addTest(test.test_types.TypesTests('test_float__format__locale'))
59+
suite.addTest(test.test_types.TypesTests('test_float__format__locale'))
6360
suite.addTest(test.test_types.TypesTests('test_float_constructor'))
6461
suite.addTest(test.test_types.TypesTests('test_float_to_string'))
6562
suite.addTest(test.test_types.TypesTests('test_floats'))

0 commit comments

Comments
 (0)