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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
454f026
Added check for delimiters in cfgparser keys to _validate_value_types
lincolnj1 Jan 16, 2025
20b24b1
Deleted some trailing whitespace
lincolnj1 Jan 22, 2025
5b730b6
Merge branch 'python:main' into fix-issue-128843
lincolnj1 Jan 22, 2025
9cc3506
Added check for section pattern in key and moved
lincolnj1 Jan 24, 2025
3638e67
Clarified _validate_key_contents() doc comment
lincolnj1 Jan 24, 2025
b161cea
Merge branch 'python:main' into fix-issue-128843
lincolnj1 Jan 24, 2025
f73d17a
Merge branch 'python:main' into fix-issue-128843
lincolnj1 Jan 24, 2025
5a47f44
Clarified new error name/doc comment and improved
lincolnj1 Jan 24, 2025
e12b696
Clarified InvalidWriteError doc comment
lincolnj1 Jan 24, 2025
f5f1cbb
Merge branch 'fix-issue-128843' of https://github.com/lincolnj1/cpyth…
lincolnj1 Jan 24, 2025
3d84347
Merge branch 'main' into validate-config-writes
lincolnj1 Jan 24, 2025
97c8e93
Remove trailing whitespace
lincolnj1 Jan 27, 2025
ea137d0
Adding documentation for InvalidWriteError
lincolnj1 Feb 15, 2025
7ca33c7
Remove trailing whitespace
lincolnj1 Feb 15, 2025
353eaf1
Cleaned up documentation on InvalidWriteError
lincolnj1 Feb 21, 2025
ccf715e
Merge branch 'main' into validate-config-writes
lincolnj1 Feb 21, 2025
a3e8848
Delint of test-configparser.py
lincolnj1 Feb 21, 2025
0e278fd
Removing trailing whitespace on line 2184 of test_configparser
lincolnj1 Feb 21, 2025
3da65ae
📜🤖 Added by blurb_it.
blurb-it[bot] Feb 21, 2025
aa9c92a
Changed formatting of configparser.py __all__
lincolnj1 Feb 21, 2025
b001df6
Merge branch 'validate-config-writes' of github.com:lincolnj1/cpython…
lincolnj1 Feb 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added check for section pattern in key and moved
invalid key checks to seperate function
  • Loading branch information
lincolnj1 committed Jan 24, 2025
commit 9cc3506b9ef43a8f21300f5ef82e572312ea70ad
22 changes: 15 additions & 7 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
"MultilineContinuationError", "UnnamedSectionDisabledError",
"ConfigParser", "RawConfigParser",
"Interpolation", "BasicInterpolation", "ExtendedInterpolation",
"SectionProxy", "ConverterMapping", "InvalidKeyError",
"SectionProxy", "ConverterMapping", "InvalidInputError",
"DEFAULTSECT", "MAX_INTERPOLATION_DEPTH", "UNNAMED_SECTION")

_default_dict = dict
Expand Down Expand Up @@ -375,10 +375,11 @@ class _UnnamedSection:
def __repr__(self):
return "<UNNAMED_SECTION>"

class InvalidKeyError(Error):
class InvalidInputError(Error):
"""Raised when attempting to write a key which contains any delimiters"""
def __init__(self):
Error.__init__(self, "Cannot write key that contains a delimiter")

def __init__(self, msg=''):
Error.__init__(self, msg)


UNNAMED_SECTION = _UnnamedSection()
Expand Down Expand Up @@ -978,6 +979,7 @@ def _write_section(self, fp, section_name, section_items, delimiter, unnamed=Fal
if not unnamed:
fp.write("[{}]\n".format(section_name))
for key, value in section_items:
self._validate_key_contents(key)
value = self._interpolation.before_write(self, section_name, key,
value)
if value is not None or not self._allow_no_value:
Expand Down Expand Up @@ -1218,6 +1220,15 @@ def _convert_to_boolean(self, value):
if value.lower() not in self.BOOLEAN_STATES:
raise ValueError('Not a boolean: %s' % value)
return self.BOOLEAN_STATES[value.lower()]

def _validate_key_contents(self, key):
"""Raises an InvalidInputError for any keys containing
delimiters or a leading '['"""
if re.match(self.SECTCRE, key):
raise InvalidInputError("Cannot write keys matching section pattern")
for delim in self._delimiters:
if delim in key:
raise InvalidInputError("Cannot write key that contains delimiters")

def _validate_value_types(self, *, section="", option="", value=""):
"""Raises a TypeError for illegal non-string values.
Expand All @@ -1239,9 +1250,6 @@ def _validate_value_types(self, *, section="", option="", value=""):
if not self._allow_no_value or value:
if not isinstance(value, str):
raise TypeError("option values must be strings")
for delim in self._delimiters:
if delim in option:
raise InvalidKeyError()

@property
def converters(self):
Expand Down
31 changes: 22 additions & 9 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2173,16 +2173,29 @@ def test_disabled_error(self):

with self.assertRaises(configparser.UnnamedSectionDisabledError):
configparser.ConfigParser().add_section(configparser.UNNAMED_SECTION)

class InvalidInputTestCase(unittest.TestCase):
"""Tests for issue #65697, where configparser will write configs
it parses back differently. Ex: keys containing delimiters or
matching the section pattern"""

def test_delimiter_in_key(self):
cfg = configparser.ConfigParser(delimiters=('='))
cfg.add_section('section1')
cfg.set('section1', 'a=b', 'c')
output = io.StringIO()
with self.assertRaises(configparser.InvalidInputError):
cfg.write(output)
output.close()

def test_invalid_key(self):
cfg2file = configparser.ConfigParser()
cfg2file.add_section("section1")
with self.assertRaises(configparser.InvalidKeyError):
cfg2file.set("section1", "one=two", "three")

with self.assertRaises(configparser.InvalidKeyError):
cfg2file.set("section1", "one:two", "three")

def test_section_bracket_in_key(self):
cfg = configparser.ConfigParser()
cfg.add_section('section1')
cfg.set('section1', '[this parses back as a section]', 'foo')
output = io.StringIO()
with self.assertRaises(configparser.InvalidInputError):
cfg.write(output)
output.close()

class MiscTestCase(unittest.TestCase):
def test__all__(self):
Expand Down