gh-65697: Improved error msg for configparser key validation (#135527) · python/cpython@81237fb · GitHub
Skip to content

Commit 81237fb

Browse files
gh-65697: Improved error msg for configparser key validation (#135527)
* Improved error msg for configparser key validation and added note in 3.14 whatsnew * Properly added change to configparser * 📜🤖 Added by blurb_it. --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 2bd3895 commit 81237fb

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

Doc/whatsnew/3.14.rst

Lines changed: 8 additions & 0 deletions

Lib/configparser.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,11 +1218,14 @@ def _convert_to_boolean(self, value):
12181218

12191219
def _validate_key_contents(self, key):
12201220
"""Raises an InvalidWriteError for any keys containing
1221-
delimiters or that match the section header pattern"""
1221+
delimiters or that begins with the section header pattern"""
12221222
if re.match(self.SECTCRE, key):
1223-
raise InvalidWriteError("Cannot write keys matching section pattern")
1224-
if any(delim in key for delim in self._delimiters):
1225-
raise InvalidWriteError("Cannot write key that contains delimiters")
1223+
raise InvalidWriteError(
1224+
f"Cannot write key {key}; begins with section pattern")
1225+
for delim in self._delimiters:
1226+
if delim in key:
1227+
raise InvalidWriteError(
1228+
f"Cannot write key {key}; contains delimiter {delim}")
12261229

12271230
def _validate_value_types(self, *, section="", option="", value=""):
12281231
"""Raises a TypeError for illegal non-string values.
Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)