From ed653bffa899dd6cabab20e4ab0283dc742e7da9 Mon Sep 17 00:00:00 2001 From: John Warren Date: Fri, 2 Aug 2013 16:15:26 +0000 Subject: Fix Message format-string parsing The parsing in openstack/common/gettextutils.Message._save_dictionary_parameter does not account for two or more format directives in direct succession, e.g.: %(key1)s%(key2)s causing key/value pairs to not be copied during a mod operation. Fixes bug 1207789 Change-Id: I56940003de3e6e2fa7dd08604d04f4c57586b0a3 --- openstack/common/gettextutils.py | 2 +- tests/unit/test_gettext.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/openstack/common/gettextutils.py b/openstack/common/gettextutils.py index a90b241..bbf8fe9 100644 --- a/openstack/common/gettextutils.py +++ b/openstack/common/gettextutils.py @@ -137,7 +137,7 @@ class Message(UserString.UserString, object): # look for %(blah) fields in string; # ignore %% and deal with the # case where % is first character on the line - keys = re.findall('(?:[^%]|^)%\((\w*)\)[a-z]', full_msg) + keys = re.findall('(?:[^%]|^)?%\((\w*)\)[a-z]', full_msg) # if we don't find any %(blah) blocks but have a %s if not keys and re.findall('(?:[^%]|^)%[a-z]', full_msg): diff --git a/tests/unit/test_gettext.py b/tests/unit/test_gettext.py index 93c0189..d9cb9b8 100644 --- a/tests/unit/test_gettext.py +++ b/tests/unit/test_gettext.py @@ -206,6 +206,19 @@ class MessageTestCase(utils.BaseTestCase): self.assertEqual(result, msgid % params) + def test_regex_find_named_parameters_no_space(self): + msgid = ("Request: %(method)s http://%(server)s:" + "%(port)s%(url)s with headers %(headers)s") + params = {'method': 'POST', + 'server': 'test1', + 'port': 1234, + 'url': 'test2', + 'headers': {'h1': 'val1'}} + + result = self._lazy_gettext(msgid) % params + + self.assertEqual(result, msgid % params) + def test_regex_dict_is_parameter(self): msgid = ("Test that we can inject a dictionary %s") params = {'description': 'test1', -- cgit