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

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a25ac00
Extract method for _read_inner, reducing complexity and indentation b…
jaraco Mar 27, 2024
1e69aae
Extract method for _raise_all and yield ParseErrors from _read_inner.
jaraco Mar 27, 2024
8c47781
Prefer iterators to splat expansion and literal indexing.
jaraco Mar 27, 2024
7479814
Extract method for _strip_comments. Reduces complexity by 7.
jaraco Mar 27, 2024
a2fffee
Model the file lines in a class to encapsulate the comment status and…
jaraco Mar 28, 2024
23468cb
Encapsulate the read state as a dataclass
jaraco Mar 28, 2024
3d1ef0a
Extract _handle_continuation_line and _handle_rest methods. Reduces c…
jaraco Mar 28, 2024
071baeb
Reindent
jaraco Mar 28, 2024
81f4ce2
At least for now, collect errors in the ReadState
jaraco Mar 28, 2024
8942cc1
Check for missing section header separately.
jaraco Mar 28, 2024
1e72168
Extract methods for _handle_header and _handle_option. Reduces comple…
jaraco Mar 28, 2024
0dfd797
Remove unreachable code. Reduces complexity by 4.
jaraco Mar 28, 2024
77ed897
Remove unreachable branch
jaraco Mar 28, 2024
76f42d3
Handle error condition early. Reduces complexity by 1.
jaraco Mar 28, 2024
c18a2bb
Add blurb
jaraco Mar 29, 2024
97aa785
Move _raise_all to ParsingError, as its behavior is most closely rela…
jaraco Mar 29, 2024
d310cb4
Split _strip* into separate methods.
jaraco Mar 29, 2024
f2a355c
Refactor _strip_full to compute the strip just once and use 'not any'…
jaraco Mar 29, 2024
2492614
Replace use of 'sys.maxsize' with direct computation of the stripped …
jaraco Mar 29, 2024
4968591
Extract has_comments as a dynamic property.
jaraco Mar 29, 2024
7d807bb
Implement clean as a cached property.
jaraco Mar 29, 2024
29cb20f
Model comment prefixes in the RawConfigParser within a prefixes names…
jaraco Mar 29, 2024
c834c35
Use a regular expression to search for the first match.
jaraco Mar 29, 2024
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
Extract method for _raise_all and yield ParseErrors from _read_inner.
Reduces complexity by 1 and reduces touch points for handling errors in _read_inner.
  • Loading branch information
jaraco committed Mar 29, 2024
commit 1e69aaecf0ae53540b798b698ee2ca7a7a243aab
37 changes: 21 additions & 16 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,23 @@ def __init__(self, option, section, rawval):
class ParsingError(Error):
"""Raised when a configuration file does not follow legal syntax."""

def __init__(self, source):
def __init__(self, source, *args):
super().__init__(f'Source contains parsing errors: {source!r}')
self.source = source
self.errors = []
self.args = (source, )
if args:
self.append(*args)

def append(self, lineno, line):
self.errors.append((lineno, line))
self.message += '\n\t[line %2d]: %s' % (lineno, line)
self.message += '\n\t[line %2d]: %s' % (lineno, repr(line))

def combine(self, others):
for other in others:
for error in other.errors:
self.append(*error)
return self


class MissingSectionHeaderError(ParsingError):
Expand Down Expand Up @@ -977,18 +985,25 @@ def _read(self, fp, fpname):
"""

try:
self._read_inner(fp, fpname)
self._raise_all(*self._read_inner(fp, fpname))
finally:
self._join_multiline_values()

def _raise_all(self, *exceptions: ParsingError):
"""
Combine any number of ParsingErrors into one and raise it.
"""
if not exceptions:
return
raise exceptions[0].combine(exceptions[1:])

def _read_inner(self, fp, fpname):
elements_added = set()
cursect = None # None, or a dictionary
sectname = None
optname = None
lineno = 0
indent_level = 0
e = None # None, or an exception

for lineno, line in enumerate(fp, start=1):
comment_start = sys.maxsize
Expand Down Expand Up @@ -1096,7 +1111,7 @@ def _read_inner(self, fp, fpname):
if mo:
optname, vi, optval = mo.group('option', 'vi', 'value')
if not optname:
e = self._handle_error(e, fpname, lineno, line)
yield ParsingError(fpname, lineno, line)
optname = self.optionxform(optname.rstrip())
if (self._strict and
(sectname, optname) in elements_added):
Expand All @@ -1116,11 +1131,7 @@ def _read_inner(self, fp, fpname):
# exception but keep going. the exception will be
# raised at the end of the file and will contain a
# list of all bogus lines
e = self._handle_error(e, fpname, lineno, line)

# if any parsing errors occurred, raise an exception
if e:
raise e
yield ParsingError(fpname, lineno, line)


def _join_multiline_values(self):
Expand All @@ -1141,12 +1152,6 @@ def _read_defaults(self, defaults):
for key, value in defaults.items():
self._defaults[self.optionxform(key)] = value

def _handle_error(self, exc, fpname, lineno, line):
if not exc:
exc = ParsingError(fpname)
exc.append(lineno, repr(line))
return exc

def _unify_values(self, section, vars):
"""Create a sequence of lookups with 'vars' taking priority over
the 'section' which takes priority over the DEFAULTSECT.
Expand Down