Skip to content

Commit 68a01f9

Browse files
GH-43374: Fix urlretrieve reporthook to report actual bytes read (#142653)
1 parent dfeefbe commit 68a01f9

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

Lib/test/test_urllib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ def hooktester(block_count, block_read_size, file_size, _report=report):
727727
self.assertEqual(report[0][2], 8193)
728728
self.assertEqual(report[0][1], 8192)
729729
self.assertEqual(report[1][1], 8192)
730-
self.assertEqual(report[2][1], 8192)
730+
self.assertEqual(report[2][1], 1) # last block only reads 1 byte
731731

732732

733733
class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin):

Lib/test/test_urllibnet.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,14 @@ def recording_reporthook(blocks, block_size, total_size):
219219
self.assertEqual(records[0][2], expected_size)
220220
self.assertEqual(records[-1][2], expected_size)
221221

222-
block_sizes = {block_size for _, block_size, _ in records}
223-
self.assertEqual({records[0][1]}, block_sizes,
224-
msg="block sizes in %s must be equal" % records_repr)
225-
self.assertGreaterEqual(records[-1][0]*records[0][1], expected_size,
226-
msg="number of blocks * block size must be"
227-
" >= total size in %s" % records_repr)
222+
self.assertEqual(records[0][1], 8192,
223+
msg="first block size should be 8192 in %s" % records_repr)
224+
for block_num, block_size, total_size in records:
225+
self.assertLessEqual(block_size, 8192,
226+
msg="block size should be <= 8192 in %s" % records_repr)
227+
total_read = sum(block_size for _, block_size, _ in records[1:])
228+
self.assertEqual(total_read, expected_size,
229+
msg="sum of bytes read must equal total size in %s" % records_repr)
228230

229231

230232
if __name__ == "__main__":

Lib/urllib/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def urlretrieve(url, filename=None, reporthook=None, data=None):
242242
tfp.write(block)
243243
blocknum += 1
244244
if reporthook:
245-
reporthook(blocknum, bs, size)
245+
reporthook(blocknum, len(block), size)
246246

247247
if size >= 0 and read < size:
248248
raise ContentTooShortError(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :func:`urllib.request.urlretrieve` to pass the actual number of bytes read to the *reporthook* callback, instead of always passing the block size.

0 commit comments

Comments
 (0)