From 8800e54f770c4c4a6f813a5ea72bb12316482d87 Mon Sep 17 00:00:00 2001 From: Dustin Schoenbrun Date: Wed, 25 Oct 2017 18:00:50 -0400 Subject: [PATCH] Fix issue with different decimal separators An issue was discovered in the ZFSonLinux driver wherein on a system set to have a locale where the decimal separator is not a period but rather a comma the method translate_string_size_to_float() would fail because it's regex to find sizes in strings did not account for any other decimal separators other than periods. This fix updates the regular expression to accept either a period or a comma as a decimal separator in a size string. Many thanks to Dr. Clemens Hardewig for reporting the original bug and providing an updated regular expression. Change-Id: I15da4aaff90814eed03816db013d6acc89ba2ee8 Closes-Bug: #1714691 --- manila/tests/test_utils.py | 14 ++++++++++++++ manila/utils.py | 7 ++++--- ...mal-separators-in-locales-392c0c794c49c1c2.yaml | 5 +++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/bug-1714691-decimal-separators-in-locales-392c0c794c49c1c2.yaml diff --git a/manila/tests/test_utils.py b/manila/tests/test_utils.py index 7b0e5e22e5..99fc6cb4e3 100644 --- a/manila/tests/test_utils.py +++ b/manila/tests/test_utils.py @@ -116,19 +116,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), ) @@ -145,6 +151,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 731d2ad144..2cf94fc22f 100644 --- a/manila/utils.py +++ b/manila/utils.py @@ -609,14 +609,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.