jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1354021?usp=email )

Change subject: upload: Verify zero-byte stashes against the empty hash
......................................................................

upload: Verify zero-byte stashes against the empty hash

Treat bytes_to_read=0 as an empty prefix in compute_file_hash; only None
means the whole file. Pass None when the upload offset is False so the
completed-stash sentinel continues to verify the full local file.

Cover empty-prefix hashing and extend the existing completed-stash test
to cover the False sentinel.

Change-Id: I1b057a456489f44d938bd774a424ba3e3ada4b3c
---
M pywikibot/site/_upload.py
M pywikibot/tools/__init__.py
M tests/tools_tests.py
M tests/upload_tests.py
4 files changed, 15 insertions(+), 4 deletions(-)

Approvals:
  jenkins-bot: Verified
  Xqt: Looks good to me, approved




diff --git a/pywikibot/site/_upload.py b/pywikibot/site/_upload.py
index 8e15d21..0e605d3 100644
--- a/pywikibot/site/_upload.py
+++ b/pywikibot/site/_upload.py
@@ -237,7 +237,9 @@
                 # The SHA1 was also requested so calculate and compare it
                 assert 'sha1' in stash_info, \
                     f'sha1 not in stash info: {stash_info}'
-                sha1 = compute_file_hash(self.filename, bytes_to_read=offset)
+                sha1 = compute_file_hash(
+                    self.filename,
+                    bytes_to_read=None if offset is False else offset)
                 if sha1 != stash_info['sha1']:
                     raise ValueError(
                         f'The SHA1 of {offset} bytes of the stashed '
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index 1cb30d6..d49481a 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -919,6 +919,8 @@
     .. version-changed:: 8.2
        The *sha* parameter may also be a hash constructor, or a callable
        that returns a hash object.
+    .. version-changed:: 11.9
+       A zero *bytes_to_read* hashes no file contents.


     :param filename: Filename path
@@ -933,13 +935,14 @@
         ``lambda: hashlib.sha1()``.
     :param bytes_to_read: Only the first bytes_to_read will be
         considered; if file size is smaller, the whole file will be
-        considered.
+        considered. If None, the whole file is considered.
     """
     with open(filename, 'rb') as f:
         if PYTHON_VERSION < (3, 11) or bytes_to_read is not None:
             digest = sha() if callable(sha) else hashlib.new(sha)
             size = os.path.getsize(filename)
-            bytes_to_read = min(bytes_to_read or size, size)
+            bytes_to_read = (size if bytes_to_read is None
+                             else min(bytes_to_read, size))
             step = 1 << 20
             while bytes_to_read > 0:
                 read_bytes = f.read(min(bytes_to_read, step))
diff --git a/tests/tools_tests.py b/tests/tools_tests.py
index 3827758..4d2db4e 100755
--- a/tests/tools_tests.py
+++ b/tests/tools_tests.py
@@ -789,6 +789,12 @@
             '617ce7d539848885b52355ed597a042dae1e726f',
         ))

+    def test_zero_byte_calculation(self) -> None:
+        """Test that a zero-byte limit hashes no file contents."""
+        self.assertEqual(
+            tools.compute_file_hash(self.filename, bytes_to_read=0),
+            hashlib.sha1(b'').hexdigest())
+
     def test_sha224_complete_calculation(self) -> None:
         """Test SHA224 of complete file."""
         res = tools.compute_file_hash(self.filename, sha='sha224')
diff --git a/tests/upload_tests.py b/tests/upload_tests.py
index 077a141..6e8ff5b 100755
--- a/tests/upload_tests.py
+++ b/tests/upload_tests.py
@@ -187,7 +187,7 @@
     def test_final_warning_recovers_stash(self) -> None:
         """Test accepting a final warning validates and reuses its stash."""
         sha1 = compute_file_hash(self.source)
-        for response_offset in (None, 1276):
+        for response_offset in (None, False, 1276):
             warning = {
                 'result': 'Warning',
                 'warnings': {'exists': 'Test.png'},

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1354021?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I1b057a456489f44d938bd774a424ba3e3ada4b3c
Gerrit-Change-Number: 1354021
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to