]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-43374: Fix urlretrieve reporthook to report actual bytes read (#142653)
authorSavannah Ostrowski <savannah@python.org>
Fri, 9 Jan 2026 00:32:05 +0000 (16:32 -0800)
committerGitHub <noreply@github.com>
Fri, 9 Jan 2026 00:32:05 +0000 (00:32 +0000)
Lib/test/test_urllib.py
Lib/test/test_urllibnet.py
Lib/urllib/request.py
Misc/NEWS.d/next/Library/2025-12-12-23-17-10.gh-issue-43374.M6jGC5.rst [new file with mode: 0644]

index ae524c5ffba6b1f6cafb7fa20bdebf3712acfe4b..a468b115752819aea2d17ca1a3e9625a91bd1756 100644 (file)
@@ -727,7 +727,7 @@ class urlretrieve_FileTests(unittest.TestCase):
         self.assertEqual(report[0][2], 8193)
         self.assertEqual(report[0][1], 8192)
         self.assertEqual(report[1][1], 8192)
-        self.assertEqual(report[2][1], 8192)
+        self.assertEqual(report[2][1], 1)  # last block only reads 1 byte
 
 
 class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin):
index 1a42c35dc49b9e734fae7bc554b2c2ce8f8ac7ef..da094752b84c6254b2f4596b4d476ead6ac811c5 100644 (file)
@@ -219,12 +219,14 @@ class urlretrieveNetworkTests(unittest.TestCase):
         self.assertEqual(records[0][2], expected_size)
         self.assertEqual(records[-1][2], expected_size)
 
-        block_sizes = {block_size for _, block_size, _ in records}
-        self.assertEqual({records[0][1]}, block_sizes,
-                         msg="block sizes in %s must be equal" % records_repr)
-        self.assertGreaterEqual(records[-1][0]*records[0][1], expected_size,
-                                msg="number of blocks * block size must be"
-                                " >= total size in %s" % records_repr)
+        self.assertEqual(records[0][1], 8192,
+                         msg="first block size should be 8192 in %s" % records_repr)
+        for block_num, block_size, total_size in records:
+            self.assertLessEqual(block_size, 8192,
+                                 msg="block size should be <= 8192 in %s" % records_repr)
+        total_read = sum(block_size for _, block_size, _ in records[1:])
+        self.assertEqual(total_read, expected_size,
+                         msg="sum of bytes read must equal total size in %s" % records_repr)
 
 
 if __name__ == "__main__":
index f32de189b1353a5b5939ba32c309c79a9774f5d8..60607c48145cdab0ded0b5a558695563e97d6584 100644 (file)
@@ -242,7 +242,7 @@ def urlretrieve(url, filename=None, reporthook=None, data=None):
                 tfp.write(block)
                 blocknum += 1
                 if reporthook:
-                    reporthook(blocknum, bs, size)
+                    reporthook(blocknum, len(block), size)
 
     if size >= 0 and read < size:
         raise ContentTooShortError(
diff --git a/Misc/NEWS.d/next/Library/2025-12-12-23-17-10.gh-issue-43374.M6jGC5.rst b/Misc/NEWS.d/next/Library/2025-12-12-23-17-10.gh-issue-43374.M6jGC5.rst
new file mode 100644 (file)
index 0000000..0fe3c35
--- /dev/null
@@ -0,0 +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.