Browse Source

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
(cherry picked from commit 8800e54f77)
(cherry picked from commit 776015af40)
Dustin Schoenbrun 1 year ago
parent
commit
d30aff5f48

+ 14
- 0
manila/tests/test_utils.py View File

@@ -114,19 +114,25 @@ class GenericUtilsTestCase(test.TestCase):
114 114
     @ddt.data(
115 115
         (("3G", "G"), 3.0),
116 116
         (("4.1G", "G"), 4.1),
117
+        (("4,1G", "G"), 4.1),
117 118
         (("5.23G", "G"), 5.23),
119
+        (("5,23G", "G"), 5.23),
118 120
         (("9728M", "G"), 9.5),
119 121
         (("8192K", "G"), 0.0078125),
120 122
         (("2T", "G"), 2048.0),
121 123
         (("2.1T", "G"), 2150.4),
124
+        (("2,1T", "G"), 2150.4),
122 125
         (("3P", "G"), 3145728.0),
123 126
         (("3.4P", "G"), 3565158.4),
127
+        (("3,4P", "G"), 3565158.4),
124 128
         (("9728M", "M"), 9728.0),
125 129
         (("9728.2381T", "T"), 9728.2381),
130
+        (("9728,2381T", "T"), 9728.2381),
126 131
         (("0", "G"), 0.0),
127 132
         (("512", "M"), 0.00048828125),
128 133
         (("2097152.", "M"), 2.0),
129 134
         ((".1024", "K"), 0.0001),
135
+        ((",1024", "K"), 0.0001),
130 136
         (("2048G", "T"), 2.0),
131 137
         (("65536G", "P"), 0.0625),
132 138
     )
@@ -143,6 +149,14 @@ class GenericUtilsTestCase(test.TestCase):
143 149
         ("1KM", "G"),
144 150
         ("K1M", "G"),
145 151
         ("M1K", "G"),
152
+        ("1.2fake", "G"),
153
+        ("1,2fake", "G"),
154
+        ("2.2GG", "G"),
155
+        ("1.1KM", "G"),
156
+        ("K2.2M", "G"),
157
+        ("K2,2M", "G"),
158
+        ("M2.2K", "G"),
159
+        ("M2,2K", "G"),
146 160
         ("", "G"),
147 161
         (23, "G"),
148 162
         (23.0, "G"),

+ 4
- 3
manila/utils.py View File

@@ -550,14 +550,15 @@ def translate_string_size_to_float(string, multiplier='G'):
550 550
             }
551 551
         )
552 552
     try:
553
-        value = float(string) / 1024.0
553
+        value = float(string.replace(",", ".")) / 1024.0
554 554
         value = value / mapping[multiplier]
555 555
         return value
556 556
     except (ValueError, TypeError):
557 557
         matched = re.match(
558
-            r"^(\d+\.*\d*)([%s])$" % ','.join(multipliers), string)
558
+            r"^(\d*[.,]*\d*)([%s])$" % ''.join(multipliers), string)
559 559
         if matched:
560
-            value = float(matched.groups()[0])
560
+            # The replace() is needed in case decimal separator is a comma
561
+            value = float(matched.groups()[0].replace(",", "."))
561 562
             multiplier = mapping[matched.groups()[1]] / mapping[multiplier]
562 563
             return value * multiplier
563 564
 

+ 5
- 0
releasenotes/notes/bug-1714691-decimal-separators-in-locales-392c0c794c49c1c2.yaml View File

@@ -0,0 +1,5 @@
1
+---
2
+fixes:
3
+  - Fixed issue where locales other than POSIX and en_US.UTF-8 might
4
+    cause the translate_string_size_to_float method to fail on a
5
+    comma decimal separator instead of a period decimal separator.

Loading…
Cancel
Save