Commit e4a48c7b authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

utils: Fix UnescapeAndSplit parsing bug



If a value passed to UnescapeAndSplit ended with a backslash an
exception would be raised:

$ gnt-instance modify -H mem=x\\ inst1.example.com
[…]
    e2 = slist.pop(0)
IndexError: pop from empty list
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent c71d2178
......@@ -343,7 +343,7 @@ def UnescapeAndSplit(text, sep=","):
e1 = slist.pop(0)
if e1.endswith("\\"):
num_b = len(e1) - len(e1.rstrip("\\"))
if num_b % 2 == 1:
if num_b % 2 == 1 and slist:
e2 = slist.pop(0)
# here the backslashes remain (all), and will be reduced in
# the next step
......
......@@ -298,7 +298,7 @@ class TestUnescapeAndSplit(unittest.TestCase):
def setUp(self):
# testing more that one separator for regexp safety
self._seps = [",", "+", "."]
self._seps = [",", "+", ".", ":"]
def testSimple(self):
a = ["a", "b", "c", "d"]
......@@ -323,6 +323,18 @@ class TestUnescapeAndSplit(unittest.TestCase):
b = ["a", "b\\" + sep + "c", "d"]
self.failUnlessEqual(utils.UnescapeAndSplit(sep.join(a), sep=sep), b)
def testEscapeAtEnd(self):
for sep in self._seps:
self.assertEqual(utils.UnescapeAndSplit("\\", sep=sep), ["\\"])
a = ["a", "b\\", "c"]
b = ["a", "b" + sep + "c\\"]
self.assertEqual(utils.UnescapeAndSplit("%s\\" % sep.join(a), sep=sep), b)
a = ["\\" + sep, "\\" + sep, "c", "d\\.moo"]
b = [sep, sep, "c", "d.moo\\"]
self.assertEqual(utils.UnescapeAndSplit("%s\\" % sep.join(a), sep=sep), b)
class TestCommaJoin(unittest.TestCase):
def test(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment