Skip to content

Commit

Permalink
Freeze the defaultdict after construction and re-enable lazy evaluati…
Browse files Browse the repository at this point in the history
…on of the search results.
  • Loading branch information
jaraco committed Mar 27, 2021
1 parent 8e3e4af commit d84930c
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import contextlib
import collections

from ._collections import freezable_defaultdict
from ._compat import (
NullFinder,
Protocol,
PyPy_repr,
install,
Protocol,
)

from ._functools import method_cache
from ._itertools import unique_everseen

Expand Down Expand Up @@ -659,8 +659,8 @@ class Lookup:
def __init__(self, path: FastPath):
base = os.path.basename(path.root).lower()
base_is_egg = base.endswith(".egg")
self.infos = collections.defaultdict(list)
self.eggs = collections.defaultdict(list)
self.infos = freezable_defaultdict(list)
self.eggs = freezable_defaultdict(list)

for child in path.children():
low = child.lower()
Expand All @@ -674,6 +674,9 @@ def __init__(self, path: FastPath):
legacy_normalized = Prepared.legacy_normalize(name)
self.eggs[legacy_normalized].append(path.joinpath(child))

self.infos.freeze()
self.eggs.freeze()

def search(self, prepared):
infos = (
self.infos[prepared.normalized]
Expand All @@ -685,7 +688,7 @@ def search(self, prepared):
if prepared
else itertools.chain.from_iterable(self.eggs.values())
)
return list(itertools.chain(infos, eggs))
return itertools.chain(infos, eggs)


class Prepared:
Expand Down

0 comments on commit d84930c

Please sign in to comment.