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 1 commit
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
Prev Previous commit
Next Next commit
Fix whitespace issues
  • Loading branch information
Agent-Hellboy committed Aug 5, 2023
commit dc1faeb7323834425c33a805eca8b877cc966e37
8 changes: 4 additions & 4 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,10 +987,10 @@ def _read(self, fp, fpname):
cur_indent_level = first_nonspace.start() if first_nonspace else 0
if (cursect is not None and optname and
cur_indent_level > indent_level):
if cursect[optname]:
cursect[optname].append(value)
else:
raise ParsingError(f"No-value option {optname} cannot be continued")
if cursect[optname]:
cursect[optname].append(value)
else:
raise ParsingError(f"No-value option {optname} cannot be continued")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an invalid invocation of ParsingError. The sole argument of ParsingError is the source filename. See how _handle_error() constructs ParsingError instances.

More importantly, instead of instantiating ParsingError, make a subclass like MultilineContinuationError. That way it's obvious what went wrong without having to parse the message string.

Also, unrelatedly, I would make the condition here in line 990 explicitly check for is None.

Copy link
Contributor Author
@Agent-Hellboy Agent-Hellboy Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sure.
what do you think of this

configparser.MultilineContinuationError: The file contains a key without value in the unintended 
scenario, please don't add any extra space within the section.
file: 'test', line: 3
' KEY2 = VAL2\n'

# a section header or option header?
else:
indent_level = cur_indent_level
Expand Down
30 changes: 15 additions & 15 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1556,21 +1556,21 @@ def test_source_as_bytes(self):
)

def test_gh_107625(self):
#test key without value with extra whitespace within sections
lines = [
'[SECT]\n',
'KEY1\n', 8000
' 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)
#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):
Expand Down
0