diff --git a/manila/tests/test_utils.py b/manila/tests/test_utils.py index 9fdf051084..c8bc59146c 100644 --- a/manila/tests/test_utils.py +++ b/manila/tests/test_utils.py @@ -114,19 +114,25 @@ class GenericUtilsTestCase(test.TestCase): @ddt.data( (("3G", "G"), 3.0), (("4.1G", "G"), 4.1), + (("4,1G", "G"), 4.1), (("5.23G", "G"), 5.23), + (("5,23G", "G"), 5.23), (("9728M", "G"), 9.5), (("8192K", "G"), 0.0078125), (("2T", "G"), 2048.0), (("2.1T", "G"), 2150.4), + (("2,1T", "G"), 2150.4), (("3P", "G"), 3145728.0), (("3.4P", "G"), 3565158.4), + (("3,4P", "G"), 3565158.4), (("9728M", "M"), 9728.0), (("9728.2381T", "T"), 9728.2381), + (("9728,2381T", "T"), 9728.2381), (("0", "G"), 0.0), (("512", "M"), 0.00048828125), (("2097152.", "M"), 2.0), ((".1024", "K"), 0.0001), + ((",1024", "K"), 0.0001), (("2048G", "T"), 2.0), (("65536G", "P"), 0.0625), ) @@ -143,6 +149,14 @@ class GenericUtilsTestCase(test.TestCase): ("1KM", "G"), ("K1M", "G"), ("M1K", "G"), + ("1.2fake", "G"), + ("1,2fake", "G"), + ("2.2GG", "G"), + ("1.1KM", "G"), + ("K2.2M", "G"), + ("K2,2M", "G"), + ("M2.2K", "G"), + ("M2,2K", "G"), ("", "G"), (23, "G"), (23.0, "G"), diff --git a/manila/utils.py b/manila/utils.py index 16720502cd..7325d3bf10 100644 --- a/manila/utils.py +++ b/manila/utils.py @@ -550,14 +550,15 @@ def translate_string_size_to_float(string, multiplier='G'): } ) try: - value = float(string) / 1024.0 + value = float(string.replace(",", ".")) / 1024.0 value = value / mapping[multiplier] return value except (ValueError, TypeError): matched = re.match( - r"^(\d+\.*\d*)([%s])$" % ','.join(multipliers), string) + r"^(\d*[.,]*\d*)([%s])$" % ''.join(multipliers), string) if matched: - value = float(matched.groups()[0]) + # The replace() is needed in case decimal separator is a comma + value = float(matched.groups()[0].replace(",", ".")) multiplier = mapping[matched.groups()[1]] / mapping[multiplier] return value * multiplier diff --git a/releasenotes/notes/bug-1714691-decimal-separators-in-locales-392c0c794c49c1c2.yaml b/releasenotes/notes/bug-1714691-decimal-separators-in-locales-392c0c794c49c1c2.yaml new file mode 100644 index 0000000000..13bc8fd4b5 --- /dev/null +++ b/releasenotes/notes/bug-1714691-decimal-separators-in-locales-392c0c794c49c1c2.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - Fixed issue where locales other than POSIX and en_US.UTF-8 might + cause the translate_string_size_to_float method to fail on a + comma decimal separator instead of a period decimal separator.