https://github.com/python/cpython/commit/4766237d19c81b73296524ee51f834c381d3c16e
commit: 4766237d19c81b73296524ee51f834c381d3c16e
branch: main
author: Savannah Ostrowski <[email protected]>
committer: savannahostrowski <[email protected]>
date: 2026-01-12T19:03:42Z
summary:

GH-43374: Revert "GH-43374: Fix urlretrieve reporthook to report actual bytes 
r… (#143711)

Revert "GH-43374: Fix urlretrieve reporthook to report actual bytes read 
(#142653)"

This reverts commit 68a01f901f446f71aac88431e01bb18b9fa35bd0.

files:
D Misc/NEWS.d/next/Library/2025-12-12-23-17-10.gh-issue-43374.M6jGC5.rst
M Lib/test/test_urllib.py
M Lib/test/test_urllibnet.py
M Lib/urllib/request.py

diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index a468b115752819..ae524c5ffba6b1 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -727,7 +727,7 @@ def hooktester(block_count, block_read_size, file_size, 
_report=report):
         self.assertEqual(report[0][2], 8193)
         self.assertEqual(report[0][1], 8192)
         self.assertEqual(report[1][1], 8192)
-        self.assertEqual(report[2][1], 1)  # last block only reads 1 byte
+        self.assertEqual(report[2][1], 8192)
 
 
 class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin):
diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py
index da094752b84c62..1a42c35dc49b9e 100644
--- a/Lib/test/test_urllibnet.py
+++ b/Lib/test/test_urllibnet.py
@@ -219,14 +219,12 @@ def recording_reporthook(blocks, block_size, total_size):
         self.assertEqual(records[0][2], expected_size)
         self.assertEqual(records[-1][2], expected_size)
 
-        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)
+        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)
 
 
 if __name__ == "__main__":
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index 60607c48145cda..f32de189b1353a 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -242,7 +242,7 @@ def urlretrieve(url, filename=None, reporthook=None, 
data=None):
                 tfp.write(block)
                 blocknum += 1
                 if reporthook:
-                    reporthook(blocknum, len(block), size)
+                    reporthook(blocknum, bs, 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
deleted file mode 100644
index 0fe3c35ab3fc1d..00000000000000
--- a/Misc/NEWS.d/next/Library/2025-12-12-23-17-10.gh-issue-43374.M6jGC5.rst
+++ /dev/null
@@ -1 +0,0 @@
-Fix :func:`urllib.request.urlretrieve` to pass the actual number of bytes read 
to the *reporthook* callback, instead of always passing the block size.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to