configtypes: Handle {1} correctly.

This commit is contained in:
Florian Bruhin 2015-07-26 13:56:10 +02:00
parent 94b0f92b75
commit 4bdf00b148
2 changed files with 4 additions and 2 deletions

View File

@ -874,7 +874,7 @@ class FormatString(BaseType):
s = self.transform(value) s = self.transform(value)
try: try:
return s.format(**{k: '' for k in self.fields}) return s.format(**{k: '' for k in self.fields})
except KeyError as e: except (KeyError, IndexError) as e:
raise configexc.ValidationError(value, "Invalid placeholder " raise configexc.ValidationError(value, "Invalid placeholder "
"{}".format(e)) "{}".format(e))
except ValueError as e: except ValueError as e:
@ -1089,7 +1089,7 @@ class SearchEngineUrl(BaseType):
raise configexc.ValidationError(value, "must contain \"{}\"") raise configexc.ValidationError(value, "must contain \"{}\"")
try: try:
value.format("") value.format("")
except KeyError: except (KeyError, IndexError) as e:
raise configexc.ValidationError( raise configexc.ValidationError(
value, "may not contain {...} (use {{ and }} for literal {/})") value, "may not contain {...} (use {{ and }} for literal {/})")

View File

@ -1501,6 +1501,7 @@ class TestSearchEngineUrl:
'foo', # no placeholder 'foo', # no placeholder
':{}', # invalid URL ':{}', # invalid URL
'foo{bar}baz{}', # {bar} format string variable 'foo{bar}baz{}', # {bar} format string variable
'{1}{}', # numbered format string variable
]) ])
def test_validate_invalid(self, klass, val): def test_validate_invalid(self, klass, val):
with pytest.raises(configexc.ValidationError): with pytest.raises(configexc.ValidationError):
@ -1761,6 +1762,7 @@ class TestFormatString:
@pytest.mark.parametrize('val', [ @pytest.mark.parametrize('val', [
'{foo} {bar} {baz}', '{foo} {bar} {baz}',
'{foo} {bar', '{foo} {bar',
'{1}',
'', '',
]) ])
def test_validate_invalid(self, typ, val): def test_validate_invalid(self, typ, val):