Skip to content

Commit b45f308

Browse files
committed
Ensure that pkg_resources.working_set (.entries) load lazily
Fixes pypa#510 Fixes pypa#711 Fixes pypa#926
1 parent df51e62 commit b45f308

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

pkg_resources/__init__.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -555,16 +555,15 @@ class WorkingSet:
555555

556556
def __init__(self, entries=None):
557557
"""Create working set from list of path entries (default=sys.path)"""
558-
self.entries = []
558+
self._entries = None
559559
self.entry_keys = {}
560560
self.by_key = {}
561561
self.callbacks = []
562562

563563
if entries is None:
564564
entries = sys.path
565565

566-
for entry in entries:
567-
self.add_entry(entry)
566+
self.paths = entries
568567

569568
@classmethod
570569
def _build_master(cls):
@@ -619,10 +618,20 @@ def add_entry(self, entry):
619618
equal ``sys.path``.)
620619
"""
621620
self.entry_keys.setdefault(entry, [])
622-
self.entries.append(entry)
621+
self._entries.append(entry)
623622
for dist in find_distributions(entry, True):
624623
self.add(dist, entry, False)
625624

625+
@property
626+
def entries(self):
627+
"""Lazy access to path entries."""
628+
if self._entries is None:
629+
self._entries = []
630+
631+
for entry in self.paths:
632+
self.add_entry(entry)
633+
return self._entries
634+
626635
def __contains__(self, dist):
627636
"""True if `dist` is the active distribution for its project"""
628637
return self.by_key.get(dist.key) == dist
@@ -3270,21 +3279,11 @@ def _initialize_master_working_set():
32703279
run_script = working_set.run_script
32713280
# backward compatibility
32723281
run_main = run_script
3273-
# Activate all distributions already on sys.path with replace=False and
3274-
# ensure that all distributions added to the working set in the future
3275-
# (e.g. by calling ``require()``) will get activated as well,
3276-
# with higher priority (replace=True).
3277-
tuple(
3278-
dist.activate(replace=False)
3279-
for dist in working_set
3280-
)
3282+
32813283
add_activation_listener(
32823284
lambda dist: dist.activate(replace=True),
32833285
existing=False,
32843286
)
3285-
working_set.entries = []
3286-
# match order
3287-
list(map(working_set.add_entry, sys.path))
32883287
globals().update(locals())
32893288

32903289
class PkgResourcesDeprecationWarning(Warning):

0 commit comments

Comments
 (0)