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())
def _dict_match(target, pattern, regex_match=False, exact_match=False):
if pattern.startswith('*:'):
wildcard = pattern.startswith('*:')
if wildcard:
pattern = pattern[2:]
if pattern == '*':
@ -1384,25 +1385,26 @@ def subdict_match(data,
regex_match=regex_match,
exact_match=exact_match):
return True
for key in target.keys():
if _match(key,
pattern,
regex_match=regex_match,
exact_match=exact_match):
return True
if isinstance(target[key], dict):
if _dict_match(target[key],
pattern,
regex_match=regex_match,
exact_match=exact_match):
if wildcard:
for key in target.keys():
if _match(key,
pattern,
regex_match=regex_match,
exact_match=exact_match):
return True
elif isinstance(target[key], list):
for item in target[key]:
if _match(item,
pattern,
regex_match=regex_match,
exact_match=exact_match):
if isinstance(target[key], dict):
if _dict_match(target[key],
pattern,
regex_match=regex_match,
exact_match=exact_match):
return True
elif isinstance(target[key], list):
for item in target[key]:
if _match(item,
pattern,
regex_match=regex_match,
exact_match=exact_match):
return True
return False
for idx in range(1, expr.count(delimiter) + 1):

View File

@ -216,6 +216,7 @@ class UtilsTestCase(TestCase):
test_two_level_dict_and_list = {
'abc': ['def', 'ghi', {'lorem': {'ipsum': [{'dolor': 'sit'}]}}],
}
test_three_level_dict = {'a': {'b': {'c': 'v'}}}
self.assertTrue(
utils.subdict_match(
@ -264,6 +265,24 @@ class UtilsTestCase(TestCase):
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):
test_two_level_dict = {'foo': {'bar': 'baz'}}