Skip to content

Commit fb65a6c

Browse files
jaracodiegorusso
authored andcommitted
pythongh-117348: restore import time performance of configparser (python#117703)
Reduces import time by over 50% (10431µs vs 4350µs on Apple M3 Pro).
1 parent 0e56de7 commit fb65a6c

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

Lib/configparser.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,18 @@
143143
between keys and values are surrounded by spaces.
144144
"""
145145

146-
from collections.abc import MutableMapping
146+
# Do not import dataclasses; overhead is unacceptable (gh-117703)
147+
148+
from collections.abc import Iterable, MutableMapping
147149
from collections import ChainMap as _ChainMap
148150
import contextlib
149-
from dataclasses import dataclass, field
150151
import functools
151152
import io
152153
import itertools
153154
import os
154155
import re
155156
import sys
156-
from typing import Iterable
157+
import types
157158

158159
__all__ = ("NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
159160
"NoOptionError", "InterpolationError", "InterpolationDepthError",
@@ -538,29 +539,26 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
538539
"found: %r" % (rest,))
539540

540541

541-
@dataclass
542542
class _ReadState:
543-
elements_added : set[str] = field(default_factory=set)
543+
elements_added : set[str]
544544
cursect : dict[str, str] | None = None
545545
sectname : str | None = None
546546
optname : str | None = None
547547
lineno : int = 0
548548
indent_level : int = 0
549-
errors : list[ParsingError] = field(default_factory=list)
550-
549+
errors : list[ParsingError]
551550

552-
@dataclass
553-
class _Prefixes:
554-
full : Iterable[str]
555-
inline : Iterable[str]
551+
def __init__(self):
552+
self.elements_added = set()
553+
self.errors = list()
556554

557555

558556
class _Line(str):
559557

560558
def __new__(cls, val, *args, **kwargs):
561559
return super().__new__(cls, val)
562560

563-
def __init__(self, val, prefixes: _Prefixes):
561+
def __init__(self, val, prefixes):
564562
self.prefixes = prefixes
565563

566564
@functools.cached_property
@@ -653,7 +651,7 @@ def __init__(self, defaults=None, dict_type=_default_dict,
653651
else:
654652
self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
655653
re.VERBOSE)
656-
self._prefixes = _Prefixes(
654+
self._prefixes = types.SimpleNamespace(
657655
full=tuple(comment_prefixes or ()),
658656
inline=tuple(inline_comment_prefixes or ()),
659657
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Largely restored import time performance of configparser by avoiding
2+
dataclasses.

0 commit comments

Comments
 (0)