*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*.
.. 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
--------------
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])
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()