Skip to content

Commit 22e120f

Browse files
savannahostrowskimiss-islington
authored andcommitted
GH-43374: Fix urlretrieve reporthook to report actual bytes read (GH-142653)
(cherry picked from commit 68a01f9) Co-authored-by: Savannah Ostrowski <savannah@python.org>
1 parent a4a33ff commit 22e120f

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
@@ -814,7 +814,7 @@ def hooktester(block_count, block_read_size, file_size, _report=report):
814814
self.assertEqual(report[0][2], 8193)
815815
self.assertEqual(report[0][1], 8192)
816816
self.assertEqual(report[1][1], 8192)
817-
self.assertEqual(report[2][1], 8192)
817+
self.assertEqual(report[2][1], 1) # last block only reads 1 byte
818818

819819

820820
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
@@ -221,12 +221,14 @@ def recording_reporthook(blocks, block_size, total_size):
221221
self.assertEqual(records[0][2], expected_size)
222222
self.assertEqual(records[-1][2], expected_size)
223223

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

231233

232234
if __name__ == "__main__":

Lib/urllib/request.py

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

249249
if size >= 0 and read < size:
250250
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)