diff --git a/lib/utils/text.py b/lib/utils/text.py index c51d7292c7dde2965b5df07cd630add3b87ed07f..e95c53787d7ab8564a182c486cca89c2bf9cbc8f 100644 --- a/lib/utils/text.py +++ b/lib/utils/text.py @@ -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 diff --git a/test/ganeti.utils.text_unittest.py b/test/ganeti.utils.text_unittest.py index 2bc0c23b4d9ed8dfd0b8a9b59a93e143966e8e59..de7511ec8d78fba4730f5bc8589d6b5b25b094ff 100755 --- a/test/ganeti.utils.text_unittest.py +++ b/test/ganeti.utils.text_unittest.py @@ -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):