{"id":9410,"library":"winrt-windows-storage-streams","title":"WinRT Windows.Storage.Streams","description":"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]","status":"active","version":"3.2.1","language":"en","source_language":"en","source_url":"https://github.com/pywinrt/pywinrt","tags":["windows","winrt","async","interop","streams","io"],"install":[{"cmd":"pip install winrt-windows-storage-streams","lang":"bash","label":"Install only this package"},{"cmd":"pip install winrt-runtime winrt-windows-storage-streams","lang":"bash","label":"Install with core runtime dependency"}],"dependencies":[{"reason":"Provides the core runtime support for all PyWinRT projections.","package":"winrt-runtime","optional":false},{"reason":"Often used for core types like IAsyncOperation and IClosable.","package":"winrt-windows.foundation","optional":true},{"reason":"Provides collection interfaces like IIterable, IVector, and IMap.","package":"winrt-windows.foundation.collections","optional":true},{"reason":"Provides access to file system and storage APIs, often used in conjunction with streams.","package":"winrt-windows.storage","optional":true},{"reason":"Provides access to system-level APIs.","package":"winrt-windows.system","optional":true}],"imports":[{"symbol":"DataWriter","correct":"from winrt.windows.storage.streams import DataWriter"},{"symbol":"DataReader","correct":"from winrt.windows.storage.streams import DataReader"},{"symbol":"InMemoryRandomAccessStream","correct":"from winrt.windows.storage.streams import InMemoryRandomAccessStream"},{"note":"WinRT `Buffer` is in `windows.storage.streams`, not `system`.","wrong":"from winrt.system import Buffer","symbol":"Buffer","correct":"from winrt.windows.storage.streams import Buffer"}],"quickstart":{"code":"import asyncio\nfrom winrt.windows.storage.streams import DataWriter, DataReader, InMemoryRandomAccessStream, Buffer\n\nasync def write_and_read_stream():\n    stream = InMemoryRandomAccessStream()\n    writer = DataWriter(stream)\n\n    # Write some bytes\n    data_to_write = b\"Hello, WinRT Streams!\"\n    writer.write_bytes(data_to_write)\n    await writer.store_async()\n    await writer.flush_async()\n\n    # Seek to the beginning of the stream and read\n    stream.seek(0)\n    reader = DataReader(stream)\n    await reader.load_async(stream.size)\n    read_buffer = reader.read_buffer(stream.size)\n    \n    # Convert IBuffer to Python bytes using buffer protocol\n    read_bytes = bytes(read_buffer) \n    \n    print(f\"Original: {data_to_write.decode('utf-8')}\")\n    print(f\"Read:     {read_bytes.decode('utf-8')}\")\n\n    writer.close()\n    reader.close()\n    stream.close()\n\nif __name__ == \"__main__\":\n    asyncio.run(write_and_read_stream())","lang":"python","description":"This example demonstrates writing bytes to an `InMemoryRandomAccessStream` using a `DataWriter`, then seeking to the beginning, reading the data back with a `DataReader`, and converting the `IBuffer` result into Python bytes. It showcases fundamental stream operations and the use of async methods."},"warnings":[{"fix":"Refer to the PyWinRT migration guide for specific details on adapting code to the new API surface. Update imports and API calls as necessary. [GitHub Release Notes (v3.0.0)]","message":"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.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Migrate to the modular `winrt-*` package structure. Install `winrt-runtime` and specific namespace packages like `winrt-windows-storage-streams`. Update imports from `winsdk.windows...` or `winrt.windows...` to `from winrt.windows...` after installing the new packages.","message":"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).","severity":"deprecated","affected_versions":"<3.0.0 and any usage of `winsdk` or old `winrt`"},{"fix":"Avoid `v2.0.0`. If you need features from this release cycle, install `winrt-windows-storage-streams==2.0.1` or later. [GitHub Release Notes (v2.0.0)]","message":"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.","severity":"gotcha","affected_versions":"2.0.0"},{"fix":"Review code that interacts with `asyncio` tasks and WinRT async operations to ensure proper handling of cancellation signals. Ensure your application logic correctly responds to `asyncio.CancelledError`.","message":"`asyncio` cancellation is now propagated to WinRT asynchronous actions and operations being awaited. This changes how cancellation behaves for long-running WinRT tasks.","severity":"breaking","affected_versions":">=3.2.0"},{"fix":"Upgrade to `winrt-windows-storage-streams==3.1.0` or a newer version to benefit from stability fixes for collection iteration.","message":"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.","severity":"gotcha","affected_versions":"<3.1.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install the necessary packages: `pip install winrt-runtime 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.","error":"ModuleNotFoundError: No module named 'winrt.windows.storage.streams'"},{"fix":"Upgrade 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.","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.","error":"TypeError: object does not implement Python buffer protocol"},{"fix":"Upgrade 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`.","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.","error":"AttributeError: 'IAsyncOperation' object has no attribute 'wait'"}]}