WinRT Windows.Storage.Streams
WinRT-Windows.Storage.Streams is a Python projection package providing access to the Windows Runtime (WinRT) APIs within the `Windows.Storage.Streams` namespace. This enables Python developers to interact with stream-related functionalities such as asynchronous read, write, and seek operations on various stream types. It is part of the modular PyWinRT project, which frequently releases updates synchronized with Windows SDK versions, with the current version being 3.2.1. [3, 7, 10]
Common errors
-
ModuleNotFoundError: No module named 'winrt.windows.storage.streams'
cause The `winrt-windows-storage-streams` package, or its core `winrt-runtime` dependency, is not installed or the Python environment is incorrect.fixInstall the necessary packages: `pip install winrt-runtime winrt-windows-storage-streams`. -
TypeError: object does not implement Python buffer protocol
cause Attempting to pass a standard Python buffer-like object (e.g., `bytes`, `bytearray`) directly to a WinRT API expecting an `IBuffer` on `pywinrt` versions prior to `3.2.0` that did not yet fully support the Python buffer protocol.fixUpgrade to `winrt-windows-storage-streams==3.2.0` or newer, which added support for passing Python buffer protocol objects directly to `IBuffer` arguments. Alternatively, manually construct a `winrt.windows.storage.streams.Buffer` if on an older version and the API accepts it. -
AttributeError: 'IAsyncOperation' object has no attribute 'wait'
cause Attempting to call `.wait()` or `.get()` on a WinRT asynchronous operation (`IAsyncAction` or `IAsyncOperation`) in versions prior to `3.2.0`, where these synchronous helper methods were not available.fixUpgrade to `winrt-runtime==3.2.0` or newer, which added `get()` and `wait()` methods for synchronous execution of async operations. Alternatively, use `await` with `asyncio` for asynchronous execution: `await async_operation_instance`.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes, including a new `winrt.runtime` module, `winrt.system.Object.as_()` method, and `box_...`/`unbox_...` functions in `winrt.system`. Direct migration from older monolithic `winrt` or `winsdk` packages may require substantial code changes.
- deprecated The previous monolithic `winrt` (Microsoft) and `winsdk` (community) packages are deprecated. The PyWinRT project now uses a modular approach, where each Windows SDK namespace (e.g., `Windows.Storage.Streams`) is a separate `winrt-*` PyPI package. Using older monolithic packages can lead to compatibility issues, especially with newer Python versions (>=3.10).
- gotcha The `v2.0.0` release was not published to PyPI due to compilation issues. If targeting versions around `2.0.0`, ensure you use `v2.0.1` or newer.
- breaking `asyncio` cancellation is now propagated to WinRT asynchronous actions and operations being awaited. This changes how cancellation behaves for long-running WinRT tasks.
- gotcha Prior to `v3.1.0`, there were issues with `IIterator.__iter__()` returning invalid objects and potentially causing crashes. This primarily affected iteration over WinRT collections.
Install
-
pip install winrt-windows-storage-streams -
pip install winrt-runtime winrt-windows-storage-streams
Imports
- DataWriter
from winrt.windows.storage.streams import DataWriter
- DataReader
from winrt.windows.storage.streams import DataReader
- InMemoryRandomAccessStream
from winrt.windows.storage.streams import InMemoryRandomAccessStream
- Buffer
from winrt.system import Buffer
from winrt.windows.storage.streams import Buffer
Quickstart
import asyncio
from winrt.windows.storage.streams import DataWriter, DataReader, InMemoryRandomAccessStream, Buffer
async def write_and_read_stream():
stream = InMemoryRandomAccessStream()
writer = DataWriter(stream)
# Write some bytes
data_to_write = b"Hello, WinRT Streams!"
writer.write_bytes(data_to_write)
await writer.store_async()
await writer.flush_async()
# Seek to the beginning of the stream and read
stream.seek(0)
reader = DataReader(stream)
await reader.load_async(stream.size)
read_buffer = reader.read_buffer(stream.size)
# Convert IBuffer to Python bytes using buffer protocol
read_bytes = bytes(read_buffer)
print(f"Original: {data_to_write.decode('utf-8')}")
print(f"Read: {read_bytes.decode('utf-8')}")
writer.close()
reader.close()
stream.close()
if __name__ == "__main__":
asyncio.run(write_and_read_stream())