Skip to content

Commit 4e28868

Browse files
committed
Use a hand-written parser for entry points.
This speeds up the `entry_points()` tox perf check by ~30%, while being both shorter and easier to follow.
1 parent 61a265c commit 4e28868

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

importlib_metadata/__init__.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
from ._itertools import unique_everseen
2323

24-
from configparser import ConfigParser
2524
from contextlib import suppress
2625
from importlib import import_module
2726
from importlib.abc import MetaPathFinder
@@ -114,21 +113,18 @@ def extras(self):
114113
match = self.pattern.match(self.value)
115114
return list(re.finditer(r'\w+', match.group('extras') or ''))
116115

117-
@classmethod
118-
def _from_config(cls, config):
119-
return (
120-
cls(name, value, group)
121-
for group in config.sections()
122-
for name, value in config.items(group)
123-
)
124-
125116
@classmethod
126117
def _from_text(cls, text):
127-
config = ConfigParser(delimiters='=')
128-
# case sensitive: https://stackoverflow.com/q/1611799/812183
129-
config.optionxform = str
130-
config.read_string(text)
131-
return cls._from_config(config)
118+
# A hand-rolled parser is much faster than ConfigParser.
119+
if not text:
120+
return
121+
group = None
122+
for line in filter(None, map(str.strip, text.splitlines())):
123+
if line.startswith("["):
124+
group = line[1:-1]
125+
else:
126+
name, value = map(str.strip, line.split("=", 1))
127+
yield cls(name, value, group)
132128

133129
@classmethod
134130
def _from_text_for(cls, text, dist):

0 commit comments

Comments
 (0)