]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-122179: Fix hashlib.file_digest and non-blocking I/O (GH-122183)
authorSebastian Rittau <srittau@rittau.biz>
Mon, 21 Apr 2025 21:15:05 +0000 (23:15 +0200)
committerGitHub <noreply@github.com>
Mon, 21 Apr 2025 21:15:05 +0000 (14:15 -0700)
* Fix hashlib.file_digest and non-blocking I/O
* Add documentation around this behavior
* Add versionchanged

Doc/library/hashlib.rst
Lib/hashlib.py
Lib/test/test_hashlib.py
Misc/NEWS.d/next/Library/2024-07-23-17-08-41.gh-issue-122179.0jZm9h.rst [new file with mode: 0644]

index 7bf6152311f058b3573c3504d6bfd87ac1529d7d..ff15a08a792ed27df2039ff425e57b30b2a6a782 100644 (file)
@@ -270,7 +270,10 @@ a file or file-like object.
    *fileobj* must be a file-like object opened for reading in binary mode.
    It accepts file objects from  builtin :func:`open`, :class:`~io.BytesIO`
    instances, SocketIO objects from :meth:`socket.socket.makefile`, and
-   similar. The function may bypass Python's I/O and use the file descriptor
+   similar. *fileobj* must be opened in blocking mode, otherwise a
+   :exc:`BlockingIOError` may be raised.
+
+   The function may bypass Python's I/O and use the file descriptor
    from :meth:`~io.IOBase.fileno` directly. *fileobj* must be assumed to be
    in an unknown state after this function returns or raises. It is up to
    the caller to close *fileobj*.
@@ -299,6 +302,10 @@ a file or file-like object.
 
    .. versionadded:: 3.11
 
+   .. versionchanged:: next
+      Now raises a :exc:`BlockingIOError` if the file is opened in blocking
+      mode. Previously, spurious null bytes were added to the digest.
+
 
 Key derivation
 --------------
index 1b2c30cc32f56473b2467a38d36e9d977e86be6a..abacac22ea0106b31fa436c66199c987a7aae49e 100644 (file)
@@ -231,6 +231,8 @@ def file_digest(fileobj, digest, /, *, _bufsize=2**18):
     view = memoryview(buf)
     while True:
         size = fileobj.readinto(buf)
+        if size is None:
+            raise BlockingIOError("I/O operation would block.")
         if size == 0:
             break  # EOF
         digestobj.update(view[:size])
index 3840dd54dcf5990b778d7c85bf69bcdaeca28e74..53afb2a8c631d788b939e6cede66150a80abcfd6 100644 (file)
@@ -1199,6 +1199,15 @@ class KDFTests(unittest.TestCase):
         with self.assertRaises(ValueError):
             hashlib.file_digest(None, "sha256")
 
+        class NonBlocking:
+            def readinto(self, buf):
+                return None
+            def readable(self):
+                return True
+
+        with self.assertRaises(BlockingIOError):
+            hashlib.file_digest(NonBlocking(), hashlib.sha256)
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2024-07-23-17-08-41.gh-issue-122179.0jZm9h.rst b/Misc/NEWS.d/next/Library/2024-07-23-17-08-41.gh-issue-122179.0jZm9h.rst
new file mode 100644 (file)
index 0000000..2b0678f
--- /dev/null
@@ -0,0 +1,3 @@
+:func:`hashlib.file_digest` now raises :exc:`BlockingIOError` when no data
+is available during non-blocking I/O. Before, it added spurious null bytes
+to the digest.