Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a47f44

Browse files
committed
Clarified new error name/doc comment and improved
validating check readability
1 parent 3638e67 commit 5a47f44

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

Lib/configparser.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
"MultilineContinuationError", "UnnamedSectionDisabledError",
164164
"ConfigParser", "RawConfigParser",
165165
"Interpolation", "BasicInterpolation", "ExtendedInterpolation",
166-
"SectionProxy", "ConverterMapping", "InvalidInputError",
166+
"SectionProxy", "ConverterMapping", "InvalidWriteError",
167167
"DEFAULTSECT", "MAX_INTERPOLATION_DEPTH", "UNNAMED_SECTION")
168168

169169
_default_dict = dict
@@ -375,8 +375,10 @@ class _UnnamedSection:
375375
def __repr__(self):
376376
return "<UNNAMED_SECTION>"
377377

378-
class InvalidInputError(Error):
379-
"""Raised when attempting to write a key which contains any delimiters"""
378+
class InvalidWriteError(Error):
379+
"""Raised when attempting to write data that would cause file .ini corruption.
380+
ex: writing a key which begins with the section header pattern would
381+
read back as a new section """
380382

381383
def __init__(self, msg=''):
382384
Error.__init__(self, msg)
@@ -1222,13 +1224,12 @@ def _convert_to_boolean(self, value):
12221224
return self.BOOLEAN_STATES[value.lower()]
12231225

12241226
def _validate_key_contents(self, key):
1225-
"""Raises an InvalidInputError for any keys containing
1227+
"""Raises an InvalidWriteError for any keys containing
12261228
delimiters or that match the section header pattern"""
12271229
if re.match(self.SECTCRE, key):
1228-
raise InvalidInputError("Cannot write keys matching section pattern")
1229-
for delim in self._delimiters:
1230-
if delim in key:
1231-
raise InvalidInputError("Cannot write key that contains delimiters")
1230+
raise InvalidWriteError("Cannot write keys matching section pattern")
1231+
if any(delim in key for delim in self._delimiters):
1232+
raise InvalidWriteError("Cannot write key that contains delimiters")
12321233

12331234
def _validate_value_types(self, *, section="", option="", value=""):
12341235
"""Raises a TypeError for illegal non-string values.

Lib/test/test_configparser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,7 +2184,7 @@ def test_delimiter_in_key(self):
21842184
cfg.add_section('section1')
21852185
cfg.set('section1', 'a=b', 'c')
21862186
output = io.StringIO()
2187-
with self.assertRaises(configparser.InvalidInputError):
2187+
with self.assertRaises(configparser.InvalidWriteError):
21882188
cfg.write(output)
21892189
output.close()
21902190

@@ -2193,7 +2193,7 @@ def test_section_bracket_in_key(self):
21932193
cfg.add_section('section1')
21942194
cfg.set('section1', '[this parses back as a section]', 'foo')
21952195
output = io.StringIO()
2196-
with self.assertRaises(configparser.InvalidInputError):
2196+
with self.assertRaises(configparser.InvalidWriteError):
21972197
cfg.write(output)
21982198
output.close()
21992199

0 commit comments

Comments
 (0)