summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjhermann <jh@web.de>2017-02-28 18:08:34 +0100
committerjhermann <jh@web.de>2017-02-28 18:08:34 +0100
commit55c8d649e37b47c43ad296e9194085001f54550c (patch)
tree698cad8adab5c6e7a5e7db3095dea7df81234ed9
parent86f03beaf5adc6a49928c6c6cf45f9c420452890 (diff)
downloadconfigobj-git-55c8d649e37b47c43ad296e9194085001f54550c.tar.gz
quoting: make helper an actual function, and use repr in exceptions
-rw-r--r--src/configobj/__init__.py25
-rw-r--r--src/tests/test_configobj.py3
2 files changed, 12 insertions, 16 deletions
diff --git a/src/configobj/__init__.py b/src/configobj/__init__.py
index e5d9ff1..3f6eac0 100644
--- a/src/configobj/__init__.py
+++ b/src/configobj/__init__.py
@@ -1023,6 +1023,14 @@ class Section(dict):
self[section].restore_defaults()
+def _get_triple_quote(value):
+ """Helper for triple-quoting round-trips."""
+ if ('"""' in value) and ("'''" in value):
+ raise ConfigObjError('Value cannot be safely quoted: {0!r}'.format(value))
+
+ return tsquot if "'''" in value else tdquot
+
+
class ConfigObj(Section):
"""An object to read, create, and write config files."""
@@ -1773,7 +1781,7 @@ class ConfigObj(Section):
# for normal values either single or double quotes will do
elif '\n' in value:
# will only happen if multiline is off - e.g. '\n' in key
- raise ConfigObjError('Value "%s" cannot be safely quoted.' % value)
+ raise ConfigObjError('Value cannot be safely quoted: {0!r}'.format(value))
elif ((value[0] not in wspace_plus) and
(value[-1] not in wspace_plus) and
(',' not in value)):
@@ -1782,7 +1790,7 @@ class ConfigObj(Section):
quot = self._get_single_quote(value)
else:
# if value has '\n' or "'" *and* '"', it will need triple quotes
- quot = self._get_triple_quote(value)
+ quot = _get_triple_quote(value)
if quot == noquot and '#' in value and self.list_values:
quot = self._get_single_quote(value)
@@ -1792,7 +1800,7 @@ class ConfigObj(Section):
def _get_single_quote(self, value):
if ("'" in value) and ('"' in value):
- raise ConfigObjError('Value "%s" cannot be safely quoted.' % value)
+ raise ConfigObjError('Value cannot be safely quoted: {0!r}'.format(value))
elif '"' in value:
quot = squot
else:
@@ -1800,17 +1808,6 @@ class ConfigObj(Section):
return quot
- @staticmethod
- def _get_triple_quote(value):
- if (value.find('"""') != -1) and (value.find("'''") != -1):
- raise ConfigObjError('Value "%s" cannot be safely quoted.' % value)
- if value.find("'''") == -1:
- quot = tdquot
- else:
- quot = tsquot
- return quot
-
-
def _handle_value(self, value):
"""
Given a value string, unquote, remove comment,
diff --git a/src/tests/test_configobj.py b/src/tests/test_configobj.py
index 9f49fb6..6bc5813 100644
--- a/src/tests/test_configobj.py
+++ b/src/tests/test_configobj.py
@@ -1007,8 +1007,7 @@ class TestQuotes(object):
tests what happens when dealing with quotes
"""
def assert_bad_quote_message(self, empty_cfg, to_quote, **kwargs):
- #TODO: this should be use repr instead of str
- message = 'Value "{0}" cannot be safely quoted.'
+ message = 'Value cannot be safely quoted: {0!r}'
with pytest.raises(ConfigObjError) as excinfo:
empty_cfg._quote(to_quote, **kwargs)
assert str(excinfo.value) == message.format(to_quote)