Skip to content

Commit 654dc66

Browse files
committed
Merge branch 'bugfix/117-too-many-dists' into 'master'
Fix redundant dists from Fastpath.zip_children Closes #117 See merge request python-devs/importlib_metadata!115
2 parents 0ea5172 + c11a68d commit 654dc66

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

importlib_metadata/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
MetaPathFinder,
2929
email_message_from_string,
3030
PyPy_repr,
31+
unique_ordered,
3132
)
3233
from importlib import import_module
3334
from itertools import starmap
@@ -425,8 +426,8 @@ def zip_children(self):
425426
names = zip_path.root.namelist()
426427
self.joinpath = zip_path.joinpath
427428

428-
return (
429-
posixpath.split(child)[0]
429+
return unique_ordered(
430+
child.split(posixpath.sep, 1)[0]
430431
for child in names
431432
)
432433

importlib_metadata/_compat.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
NotADirectoryError = builtins.NotADirectoryError
1616
PermissionError = builtins.PermissionError
1717
map = builtins.map
18+
from itertools import filterfalse
1819
else: # pragma: nocover
1920
from backports.configparser import ConfigParser
2021
from itertools import imap as map # type: ignore
22+
from itertools import ifilterfalse as filterfalse
2123
import contextlib2 as contextlib
2224
FileNotFoundError = IOError, OSError
2325
IsADirectoryError = IOError, OSError
@@ -131,3 +133,18 @@ def make_param(name):
131133
if affected: # pragma: nocover
132134
__repr__ = __compat_repr__
133135
del affected
136+
137+
138+
# from itertools recipes
139+
def unique_everseen(iterable): # pragma: nocover
140+
"List unique elements, preserving order. Remember all elements ever seen."
141+
seen = set()
142+
seen_add = seen.add
143+
144+
for element in filterfalse(seen.__contains__, iterable):
145+
seen_add(element)
146+
yield element
147+
148+
149+
unique_ordered = (
150+
unique_everseen if sys.version_info < (3, 7) else dict.fromkeys)

importlib_metadata/docs/changelog.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
importlib_metadata NEWS
33
=========================
44

5+
v1.5.2
6+
======
7+
8+
* Fix redundant entries from ``FastPath.zip_children``.
9+
Closes #117.
10+
511
v1.5.1
612
======
713

importlib_metadata/tests/test_zip.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import sys
22
import unittest
33

4-
from .. import distribution, entry_points, files, PackageNotFoundError, version
4+
from .. import (
5+
distribution, entry_points, files, PackageNotFoundError,
6+
version, distributions,
7+
)
58

69
try:
710
from importlib.resources import path
@@ -52,6 +55,10 @@ def test_files(self):
5255
path = str(file.dist.locate_file(file))
5356
assert '.whl/' in path, path
5457

58+
def test_one_distribution(self):
59+
dists = list(distributions(path=sys.path[:1]))
60+
assert len(dists) == 1
61+
5562

5663
class TestEgg(TestZip):
5764
def setUp(self):

0 commit comments

Comments
 (0)