Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3379.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Document how :func:`trio.wrap_file` shares ownership, close behavior, and state with the synchronous file object it wraps.
10 changes: 10 additions & 0 deletions src/trio/_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,16 @@ def wrap_file(file: FileT) -> AsyncIOWrapper[FileT]:
"""This wraps any file object in a wrapper that provides an asynchronous
file object interface.

The wrapper and ``file`` refer to the same underlying object. Calling
:meth:`~trio.abc.AsyncResource.aclose` on the wrapper closes ``file`` as
well. Discarding the wrapper does not explicitly close ``file``; use
``aclose`` or an async context manager when the wrapper owns its lifetime.

You may continue to use ``file`` directly until it is closed, but both
references share the same file position and buffers. Since the wrapper
runs blocking operations in worker threads, simultaneous access through
both references is safe only when the underlying object is thread-safe.

Args:
file: a :term:`file object`

Expand Down