Skip to content

Commit dc751b8

Browse files
committed
refactor: apply requested changes
1 parent fa11e55 commit dc751b8

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

Lib/configparser.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,11 @@ def _strip_full(self, string):
581581
return '' if any(map(string.startswith, self.comments.full)) else True
582582

583583
def _strip_inline(self, string):
584+
match = None
584585
if self.comments.inline:
585586
match = self.comments.inline.search(string)
586-
if match:
587-
return string[:match.start()].rstrip()
587+
if match:
588+
return string[:match.start()].rstrip()
588589
return string
589590

590591

@@ -654,17 +655,14 @@ def __init__(self, defaults=None, dict_type=_default_dict,
654655
else:
655656
self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
656657
re.VERBOSE)
657-
comment_prefixes = tuple(comment_prefixes or ())
658-
if inline_comment_prefixes:
659-
# prefix at the beginning of the line or following a space
660-
inline_comment_cre = re.compile(
661-
'|'.join(fr'(^|\s)({re.escape(prefix)})'
662-
for prefix in inline_comment_prefixes))
663-
else:
664-
inline_comment_cre = None
658+
# prefix at the beginning of the line or following a space
659+
inline_tmpl = lambda prefix: fr'(^|\s)({re.escape(prefix)})'
660+
inline_comm = '|'.join(map(inline_tmpl, inline_comment_prefixes or ()))
661+
# optional cre used with _LineParser for best performance (gh-128641)
662+
inline_comment_cre = re.compile(inline_comm) if inline_comm else None
665663
self._comments = types.SimpleNamespace(
666-
full=comment_prefixes,
667664
inline=inline_comment_cre,
665+
full=tuple(comment_prefixes or ())
668666
)
669667
self._strict = strict
670668
self._allow_no_value = allow_no_value
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Improve performance of :meth:`configparser.ConfigParser.read` by up to 45%.
1+
Restore :meth:`configparser.ConfigParser.read` performance.

0 commit comments

Comments
 (0)