Skip to content

Commit 55c8d64

Browse files
committed
quoting: make helper an actual function, and use repr in exceptions
1 parent 86f03be commit 55c8d64

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/configobj/__init__.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,14 @@ def restore_defaults(self):
10231023
self[section].restore_defaults()
10241024

10251025

1026+
def _get_triple_quote(value):
1027+
"""Helper for triple-quoting round-trips."""
1028+
if ('"""' in value) and ("'''" in value):
1029+
raise ConfigObjError('Value cannot be safely quoted: {0!r}'.format(value))
1030+
1031+
return tsquot if "'''" in value else tdquot
1032+
1033+
10261034
class ConfigObj(Section):
10271035
"""An object to read, create, and write config files."""
10281036

@@ -1773,7 +1781,7 @@ def _quote(self, value, multiline=True):
17731781
# for normal values either single or double quotes will do
17741782
elif '\n' in value:
17751783
# will only happen if multiline is off - e.g. '\n' in key
1776-
raise ConfigObjError('Value "%s" cannot be safely quoted.' % value)
1784+
raise ConfigObjError('Value cannot be safely quoted: {0!r}'.format(value))
17771785
elif ((value[0] not in wspace_plus) and
17781786
(value[-1] not in wspace_plus) and
17791787
(',' not in value)):
@@ -1782,7 +1790,7 @@ def _quote(self, value, multiline=True):
17821790
quot = self._get_single_quote(value)
17831791
else:
17841792
# if value has '\n' or "'" *and* '"', it will need triple quotes
1785-
quot = self._get_triple_quote(value)
1793+
quot = _get_triple_quote(value)
17861794

17871795
if quot == noquot and '#' in value and self.list_values:
17881796
quot = self._get_single_quote(value)
@@ -1792,25 +1800,14 @@ def _quote(self, value, multiline=True):
17921800

17931801
def _get_single_quote(self, value):
17941802
if ("'" in value) and ('"' in value):
1795-
raise ConfigObjError('Value "%s" cannot be safely quoted.' % value)
1803+
raise ConfigObjError('Value cannot be safely quoted: {0!r}'.format(value))
17961804
elif '"' in value:
17971805
quot = squot
17981806
else:
17991807
quot = dquot
18001808
return quot
18011809

18021810

1803-
@staticmethod
1804-
def _get_triple_quote(value):
1805-
if (value.find('"""') != -1) and (value.find("'''") != -1):
1806-
raise ConfigObjError('Value "%s" cannot be safely quoted.' % value)
1807-
if value.find("'''") == -1:
1808-
quot = tdquot
1809-
else:
1810-
quot = tsquot
1811-
return quot
1812-
1813-
18141811
def _handle_value(self, value):
18151812
"""
18161813
Given a value string, unquote, remove comment,

src/tests/test_configobj.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,8 +1007,7 @@ class TestQuotes(object):
10071007
tests what happens when dealing with quotes
10081008
"""
10091009
def assert_bad_quote_message(self, empty_cfg, to_quote, **kwargs):
1010-
#TODO: this should be use repr instead of str
1011-
message = 'Value "{0}" cannot be safely quoted.'
1010+
message = 'Value cannot be safely quoted: {0!r}'
10121011
with pytest.raises(ConfigObjError) as excinfo:
10131012
empty_cfg._quote(to_quote, **kwargs)
10141013
assert str(excinfo.value) == message.format(to_quote)

0 commit comments

Comments
 (0)