Skip to content

Commit 1b3f3b6

Browse files
committed
Allowed parsing of an invalid file, for example a one containing blocks of other format
1 parent 3e2f4cc commit 1b3f3b6

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/configobj/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def match_utf8(encoding):
131131
'default_encoding': None,
132132
'unrepr': False,
133133
'write_empty_values': False,
134+
'invalid': False
134135
}
135136

136137
# this could be replaced if six is used for compatibility, or there are no
@@ -1151,7 +1152,7 @@ def __init__(self, infile=None, options=None, configspec=None, encoding=None,
11511152
interpolation=True, raise_errors=False, list_values=True,
11521153
create_empty=False, file_error=False, stringify=True,
11531154
indent_type=None, default_encoding=None, unrepr=False,
1154-
write_empty_values=False, _inspec=False):
1155+
write_empty_values=False, invalid=False, _inspec=False):
11551156
"""
11561157
Parse a config file or create a config file object.
11571158
@@ -1173,7 +1174,7 @@ def __init__(self, infile=None, options=None, configspec=None, encoding=None,
11731174
'create_empty': create_empty, 'file_error': file_error,
11741175
'stringify': stringify, 'indent_type': indent_type,
11751176
'default_encoding': default_encoding, 'unrepr': unrepr,
1176-
'write_empty_values': write_empty_values}
1177+
'write_empty_values': write_empty_values, 'invalid':invalid}
11771178

11781179
if options is None:
11791180
options = _options
@@ -1328,6 +1329,7 @@ def _initialise(self, options=None):
13281329
self.BOM = False
13291330
self.newlines = None
13301331
self.write_empty_values = options['write_empty_values']
1332+
self.invalid = options['invalid']
13311333
self.unrepr = options['unrepr']
13321334

13331335
self.initial_comment = []
@@ -1603,9 +1605,10 @@ def _parse(self, infile):
16031605
# so it should be a valid ``key = value`` line
16041606
mat = self._keyword.match(line)
16051607
if mat is None:
1606-
self._handle_error(
1607-
'Invalid line ({!r}) (matched as neither section nor keyword)'.format(line),
1608-
ParseError, infile, cur_index)
1608+
if not self.invalid:
1609+
self._handle_error(
1610+
'Invalid line ({!r}) (matched as neither section nor keyword)'.format(line),
1611+
ParseError, infile, cur_index)
16091612
else:
16101613
# is a keyword value
16111614
# value will include any inline comment

0 commit comments

Comments
 (0)