Merge pull request #29192 from bastiaanb/fix/issue-29191-subdict_match-regression

fix issue 29191: only try partial matches when a wildcard has been sp…
This commit is contained in:
Mike Place 2015-11-25 13:06:39 -07:00
commit 7bd15621c8
2 changed files with 39 additions and 18 deletions

View File

@ -1370,7 +1370,8 @@ def subdict_match(data,
return fnmatch.fnmatch(str(target).lower(), pattern.lower()) return fnmatch.fnmatch(str(target).lower(), pattern.lower())
def _dict_match(target, pattern, regex_match=False, exact_match=False): def _dict_match(target, pattern, regex_match=False, exact_match=False):
if pattern.startswith('*:'): wildcard = pattern.startswith('*:')
if wildcard:
pattern = pattern[2:] pattern = pattern[2:]
if pattern == '*': if pattern == '*':
@ -1384,6 +1385,7 @@ def subdict_match(data,
regex_match=regex_match, regex_match=regex_match,
exact_match=exact_match): exact_match=exact_match):
return True return True
if wildcard:
for key in target.keys(): for key in target.keys():
if _match(key, if _match(key,
pattern, pattern,

View File

@ -216,6 +216,7 @@ class UtilsTestCase(TestCase):
test_two_level_dict_and_list = { test_two_level_dict_and_list = {
'abc': ['def', 'ghi', {'lorem': {'ipsum': [{'dolor': 'sit'}]}}], 'abc': ['def', 'ghi', {'lorem': {'ipsum': [{'dolor': 'sit'}]}}],
} }
test_three_level_dict = {'a': {'b': {'c': 'v'}}}
self.assertTrue( self.assertTrue(
utils.subdict_match( utils.subdict_match(
@ -264,6 +265,24 @@ class UtilsTestCase(TestCase):
test_two_level_dict_and_list, 'abc:lorem:ipsum:dolor:sit' test_two_level_dict_and_list, 'abc:lorem:ipsum:dolor:sit'
) )
) )
# Test four level dict match for reference
self.assertTrue(
utils.subdict_match(
test_three_level_dict, 'a:b:c:v'
)
)
self.assertFalse(
# Test regression in 2015.8 where 'a:c:v' would match 'a:b:c:v'
utils.subdict_match(
test_three_level_dict, 'a:c:v'
)
)
# Test wildcard match
self.assertTrue(
utils.subdict_match(
test_three_level_dict, 'a:*:c:v'
)
)
def test_traverse_dict(self): def test_traverse_dict(self):
test_two_level_dict = {'foo': {'bar': 'baz'}} test_two_level_dict = {'foo': {'bar': 'baz'}}