Skip to content

Commit

Permalink
Extract method for _strip_comments. Reduces complexity by 7.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Mar 27, 2024
1 parent ad328e2 commit a4b5891
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,27 +1006,8 @@ def _read_inner(self, fp, fpname):
lineno = 0
indent_level = 0

for lineno, line in enumerate(fp, start=1):
comment_start = sys.maxsize
# strip inline comments
inline_prefixes = {p: -1 for p in self._inline_comment_prefixes}
while comment_start == sys.maxsize and inline_prefixes:
next_prefixes = {}
for prefix, index in inline_prefixes.items():
index = line.find(prefix, index+1)
if index == -1:
continue
next_prefixes[prefix] = index
if index == 0 or (index > 0 and line[index-1].isspace()):
comment_start = min(comment_start, index)
inline_prefixes = next_prefixes
# strip full line comments
for prefix in self._comment_prefixes:
if line.strip().startswith(prefix):
comment_start = 0
break
if comment_start == sys.maxsize:
comment_start = None
lines = map(self._strip_comments, fp)
for lineno, (line, comment_start) in enumerate(lines, start=1):
value = line[:comment_start].strip()
if not value:
if self._empty_lines_in_values:
Expand Down Expand Up @@ -1134,6 +1115,28 @@ def _read_inner(self, fp, fpname):
# list of all bogus lines
yield ParsingError(fpname, lineno, line)

def _strip_comments(self, line):
comment_start = sys.maxsize
# strip inline comments
inline_prefixes = {p: -1 for p in self._inline_comment_prefixes}
while comment_start == sys.maxsize and inline_prefixes:
next_prefixes = {}
for prefix, index in inline_prefixes.items():
index = line.find(prefix, index+1)
if index == -1:
continue
next_prefixes[prefix] = index
if index == 0 or (index > 0 and line[index-1].isspace()):
comment_start = min(comment_start, index)
inline_prefixes = next_prefixes
# strip full line comments
for prefix in self._comment_prefixes:
if line.strip().startswith(prefix):
comment_start = 0
break
if comment_start == sys.maxsize:
comment_start = None
return line, comment_start

def _join_multiline_values(self):
defaults = self.default_section, self._defaults
Expand Down

0 comments on commit a4b5891

Please sign in to comment.