summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfuzzyman <devnull@localhost>2009-10-26 20:54:09 +0000
committerfuzzyman <devnull@localhost>2009-10-26 20:54:09 +0000
commit949fdba09a524bc873ed0d328d416cc4935e092e (patch)
tree5500a53ce71d47dbe22b934468581595df2ec7b3
parent08faa31ffdfd1a009f8392186e1fa9c872af0320 (diff)
downloadconfigobj-949fdba09a524bc873ed0d328d416cc4935e092e.tar.gz
Empty values in lists are now a syntax error.
-rw-r--r--configobj.py28
-rw-r--r--docs/configobj.txt2
-rw-r--r--test_configobj.py40
3 files changed, 55 insertions, 15 deletions
diff --git a/configobj.py b/configobj.py
index 7771431..cc3e045 100644
--- a/configobj.py
+++ b/configobj.py
@@ -19,19 +19,15 @@
from __future__ import generators
-import sys
import os
import re
+import sys
-compiler = None
-try:
- import compiler
-except ImportError:
- # for IronPython
- pass
+from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF16_BE, BOM_UTF16_LE
-from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF16_BE, BOM_UTF16_LE
+# imported lazily to avoid startup performance hit if it isn't used
+compiler = None
# A dictionary mapping BOM to
# the encoding to decode with, and what to set the
@@ -135,9 +131,10 @@ OPTION_DEFAULTS = {
def getObj(s):
- s = "a=" + s
+ global compiler
if compiler is None:
- raise ImportError('compiler module not available')
+ import compiler
+ s = "a=" + s
p = compiler.parse(s)
return p.getChildren()[1].getChildren()[0].getChildren()[1]
@@ -280,11 +277,9 @@ class RepeatSectionError(ConfigObjError):
class MissingInterpolationOption(InterpolationError):
"""A value specified for interpolation was missing."""
-
def __init__(self, option):
- InterpolationError.__init__(
- self,
- 'missing option "%s" in interpolation.' % option)
+ msg = 'missing option "%s" in interpolation.' % option
+ InterpolationError.__init__(self, msg)
class UnreprError(ConfigObjError):
@@ -1136,7 +1131,7 @@ class ConfigObj(Section):
(
(?:".*?")| # double quotes
(?:'.*?')| # single quotes
- (?:[^'",\#].*?) # unquoted
+ (?:[^'",\#]?.*?) # unquoted
)
\s*,\s* # comma
''',
@@ -1691,6 +1686,9 @@ class ConfigObj(Section):
def _unquote(self, value):
"""Return an unquoted version of a value"""
+ if not value:
+ # should only happen during parsing of lists
+ raise SyntaxError
if (value[0] == value[-1]) and (value[0] in ('"', "'")):
value = value[1:-1]
return value
diff --git a/docs/configobj.txt b/docs/configobj.txt
index 7d56ddb..6c4e09d 100644
--- a/docs/configobj.txt
+++ b/docs/configobj.txt
@@ -2296,6 +2296,8 @@ From version 4 it lists all releases and changes.
* After validation any additional entries not in the configspec are listed in
the 'extra_values' section member
* BUGFIX: clear() clears 'defaults'
+* BUGFIX: empty values in list values were accidentally valid syntax. They now
+ raise a ParseError. e.g. "value = 1, , 2"
2009/04/13 - Version 4.6.0
diff --git a/test_configobj.py b/test_configobj.py
index b68a505..43d1009 100644
--- a/test_configobj.py
+++ b/test_configobj.py
@@ -2063,6 +2063,46 @@ def _test_reset_and_clear_more():
{}
"""
+def _test_invalid_lists():
+ """
+ >>> v = ['string = val, val2, , val3']
+ >>> c = ConfigObj(v)
+ Traceback (most recent call last):
+ ParseError: Parse error in value at line 1.
+ >>> v = ['string = val, val2,, val3']
+ >>> c = ConfigObj(v)
+ Traceback (most recent call last):
+ ParseError: Parse error in value at line 1.
+ >>> v = ['string = val, val2,,']
+ >>> c = ConfigObj(v)
+ Traceback (most recent call last):
+ ParseError: Parse error in value at line 1.
+ >>> v = ['string = val, ,']
+ >>> c = ConfigObj(v)
+ Traceback (most recent call last):
+ ParseError: Parse error in value at line 1.
+ >>> v = ['string = val, , ']
+ >>> c = ConfigObj(v)
+ Traceback (most recent call last):
+ ParseError: Parse error in value at line 1.
+ >>> v = ['string = ,,']
+ >>> c = ConfigObj(v)
+ Traceback (most recent call last):
+ ParseError: Parse error in value at line 1.
+ >>> v = ['string = ,, ']
+ >>> c = ConfigObj(v)
+ Traceback (most recent call last):
+ ParseError: Parse error in value at line 1.
+ >>> v = ['string = ,foo']
+ >>> c = ConfigObj(v)
+ Traceback (most recent call last):
+ ParseError: Parse error in value at line 1.
+ >>> v = ['string = foo, ']
+ >>> c = ConfigObj(v)
+ >>> c['string']
+ ['foo']
+ """
+
# TODO: Test BOM handling
# TODO: Test error code for badly built multiline values
# TODO: Test handling of StringIO