Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37ccd6f

Browse files
committed
Fix packaging.database.Distribution.list_distinfo_files (#12785).
This method was supposed to return only the file under the dist-info directory, but it actually returned all installed files. The tests didn’t catch this because they were flawed; I updated them. Thanks to Nadeem Vawda and Jeremy Kloth for testing. As a bonus, the removal of os.path.relpath use should also fix the Windows buildbots.
1 parent 86ca04c commit 37ccd6f

2 files changed

Lines changed: 27 additions & 23 deletions

File tree

Lib/packaging/database.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ def list_distinfo_files(self, local=False):
263263
:returns: iterator of paths
264264
"""
265265
for path, checksum, size in self._get_records(local):
266-
yield path
266+
# XXX add separator or use real relpath algo
267+
if path.startswith(self.path):
268+
yield path
267269

268270
def __eq__(self, other):
269271
return isinstance(other, Distribution) and self.path == other.path

Lib/packaging/tests/test_database.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import sys
55
import shutil
66
import tempfile
7-
from os.path import relpath # separate import for backport concerns
87
from hashlib import md5
98
from textwrap import dedent
109

@@ -32,11 +31,11 @@ def get_hexdigest(filename):
3231
return checksum.hexdigest()
3332

3433

35-
def record_pieces(file):
36-
path = relpath(file, sys.prefix)
37-
digest = get_hexdigest(file)
38-
size = os.path.getsize(file)
39-
return [path, digest, size]
34+
def record_pieces(path):
35+
path = os.path.join(*path)
36+
digest = get_hexdigest(path)
37+
size = os.path.getsize(path)
38+
return path, digest, size
4039

4140

4241
class FakeDistsMixin:
@@ -141,12 +140,10 @@ def setUp(self):
141140

142141
for path, dirs, files in os.walk(dist_location):
143142
for f in files:
144-
record_writer.writerow(record_pieces(
145-
os.path.join(path, f)))
143+
record_writer.writerow(record_pieces((path, f)))
146144
for file in ('INSTALLER', 'METADATA', 'REQUESTED'):
147-
record_writer.writerow(record_pieces(
148-
os.path.join(distinfo_dir, file)))
149-
record_writer.writerow([relpath(record_file, sys.prefix)])
145+
record_writer.writerow(record_pieces((distinfo_dir, file)))
146+
record_writer.writerow([record_file])
150147

151148
with open(record_file) as file:
152149
record_reader = csv.reader(file, lineterminator='\n')
@@ -171,15 +168,17 @@ def test_uses(self):
171168
distinfo_name + '.dist-info')
172169
true_path = [self.fake_dists_path, distinfo_name,
173170
'grammar', 'utils.py']
174-
true_path = relpath(os.path.join(*true_path), sys.prefix)
171+
true_path = os.path.join(*true_path)
175172
false_path = [self.fake_dists_path, 'towel_stuff-0.1', 'towel_stuff',
176173
'__init__.py']
177-
false_path = relpath(os.path.join(*false_path), sys.prefix)
174+
false_path = os.path.join(*false_path)
178175

179176
# Test if the distribution uses the file in question
180177
dist = Distribution(distinfo_dir)
181-
self.assertTrue(dist.uses(true_path))
182-
self.assertFalse(dist.uses(false_path))
178+
self.assertTrue(dist.uses(true_path), 'dist %r is supposed to use %r' %
179+
(dist, true_path))
180+
self.assertFalse(dist.uses(false_path), 'dist %r is not supposed to '
181+
'use %r' % (dist, true_path))
183182

184183
def test_get_distinfo_file(self):
185184
# Test the retrieval of dist-info file objects.
@@ -215,20 +214,23 @@ def test_get_distinfo_file(self):
215214
'MAGICFILE')
216215

217216
def test_list_distinfo_files(self):
218-
# Test for the iteration of RECORD path entries.
219217
distinfo_name = 'towel_stuff-0.1'
220218
distinfo_dir = os.path.join(self.fake_dists_path,
221219
distinfo_name + '.dist-info')
222220
dist = Distribution(distinfo_dir)
223221
# Test for the iteration of the raw path
224-
distinfo_record_paths = self.records[distinfo_dir].keys()
222+
distinfo_files = [os.path.join(distinfo_dir, filename) for filename in
223+
os.listdir(distinfo_dir)]
225224
found = dist.list_distinfo_files()
226-
self.assertEqual(sorted(found), sorted(distinfo_record_paths))
225+
self.assertEqual(sorted(found), sorted(distinfo_files))
227226
# Test for the iteration of local absolute paths
228-
distinfo_record_paths = [os.path.join(sys.prefix, path)
229-
for path in self.records[distinfo_dir]]
230-
found = dist.list_distinfo_files(local=True)
231-
self.assertEqual(sorted(found), sorted(distinfo_record_paths))
227+
distinfo_files = [os.path.join(sys.prefix, distinfo_dir, path) for
228+
path in distinfo_files]
229+
found = sorted(dist.list_distinfo_files(local=True))
230+
if os.sep != '/':
231+
self.assertNotIn('/', found[0])
232+
self.assertIn(os.sep, found[0])
233+
self.assertEqual(found, sorted(distinfo_files))
232234

233235
def test_get_resources_path(self):
234236
distinfo_name = 'babar-0.1'

0 commit comments

Comments
 (0)