8000 gh-107625: configparser: Raise ParsingError if a key contains empty value by Agent-Hellboy · Pull Request #107651 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-107625: configparser: Raise ParsingError if a key contains empty value #107651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
29 changes: 28 additions & 1 deletion Doc/library/configparser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,13 @@ ConfigParser Objects
instance will contain an empty dataset. An application which requires
initial values to be loaded from a file should load the required file or
files using :meth:`read_file` before calling :meth:`read` for any
optional files::
optional files

Raise a :exc:`ParsingError` instead of an :exc:`AttributeError`
when the configuration contains a key without a corresponding value.
This change is intended to handle scenarios where a key lacks a value
in a configuration. Please note that it's not recommended to have extra
spaces or blank lines within sections of the configuration.

import configparser, os

Expand All @@ -1053,6 +1059,13 @@ ConfigParser Objects
.. versionadded:: 3.7
The *filenames* parameter accepts a :class:`bytes` object.

.. versionchanged:: 3.13
Parsing errors will result in a ParsingError being raised instead
of an AttributeError when encountering a key without a corresponding
value. This change is designed to handle situations where a key is
present without a value, particularly in scenarios not recommended,
such as having extra spaces or blank lines within sections.


.. method:: read_file(f, source=None)

Expand All @@ -1063,9 +1076,23 @@ ConfigParser Objects
not given and *f* has a :attr:`name` attribute, that is used for
*source*; the default is ``'<???>'``.

Raise a :exc:`ParsingError` instead of an :exc:`AttributeError`
when the configuration contains a key without a corresponding value.
This change is intended to handle scenarios where a key lacks a value
in a configuration. Please note that it's not recommended to have extra
spaces or blank lines within sections of the configuration.

.. versionadded:: 3.2
Replaces :meth:`readfp`.

.. versionchanged:: 3.13
Parsing errors will result in a ParsingError being raised instead
of an AttributeError when encountering a key without a corresponding
value. This change is designed to handle situations where a key is
present without a value, particularly in scenarios not recommended,
such as having extra spaces or blank lines within sections.


.. method:: read_string(string, source='<string>')

Parse configuration data from a string.
Expand Down
7 changes: 5 additions & 2 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,11 @@ def _read(self, fp, fpname):
first_nonspace = self.NONSPACECRE.search(line)
cur_indent_level = first_nonspace.start() if first_nonspace else 0
if (cursect is not None and optname and
cur_indent_level > indent_level):
cursect[optname].append(value)
cur_indent_level > indent_level):
if cursect[optname]:
cursect[optname].append(value)
else:
raise ParsingError(f"No-value option {optname} cannot be continued")
# a section header or option header?
else:
indent_level = cur_indent_level
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,23 @@ def test_source_as_bytes(self):
"'[badbad'"
)

def test_gh_107625(self):
#test key without value with extra whitespace within sections
lines = [
'[SECT]\n',
'KEY1\n',
' KEY2 = VAL2\n', # note the Space before the key!
]
with self.assertRaises(configparser.ParsingError):
parser = configparser.ConfigParser(
comment_prefixes ="",
allow_no_value =True,
strict =False,
delimiters =( '=', ),
interpolation =None,
)
parser.read_file(lines)


class CoverageOneHundredTestCase(unittest.TestCase):
"""Covers edge cases in the codebase."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise an :exc:`ParsingError` instead of an :exc:`AttributeError` when the configuration contains a key without a corresponding value in a scenario where extra space or a new line is present within the section.
0