Repository navigation
Add Python close and context managers for closable WinRT interfaces - #233
Merged
leileizhang (lei9444) merged 2 commits intoOct 30, 2026
Merged
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Mixed-language test coverageWorkflow status: ✅ Passed
|
shisan (qiutongMS)
approved these changes
Oct 30, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
shisan (qiutongMS)
approved these changes
Oct 30, 2026
leileizhang (lei9444)
deleted the
lei9444-python-stream-context-managers
branch
October 30, 2026 05:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Python stream results such as
IRandomAccessStreamandIInputStreamlackclose()and context-manager methods even though their WinMD contracts requireIClosable. Add helpers only where that explicit, transitive requirement exists, reusing the existing concrete-class implementation to QueryInterface toIClosableand call nativeClose. Successful helper calls are idempotent, failed closes remain retryable, and context exit does not suppress body exceptions.IClosableitself gains context-manager methods while retaining its metadata-definedclose()behavior.Runtime and
.pyigeneration share the same member plan, includingSelfandLiteral[False]context-manager declarations. Existing metadata methods, properties, CLR names, and compatibility aliases retain their meanings; helpers yield when their names are occupied. Existing concrete-class closing behavior is preserved. Native and typing fixtures use the generated project'ssrclayout, including the constructor-diagnostics regression.Reproduction
Prerequisites: Windows 10/11 with Windows SDK metadata, a supported CPython version, and
dynwinrt-codegenwith its matchingdynwinrtruntime installed.Steps:
probe.pyand runpython probe.py:Actual behavior: The
withstatement raisesTypeErrorbecause the returnedIRandomAccessStreamprojection has no context-manager methods. Explicitstream.close()also raisesAttributeError.After the fix: The script prints
8followed byRO_E_CLOSED: context exit closes the actual native stream rather than merely releasing a Python reference.Validation
Fresh generation with and without
.pyifiles exercises actual file, input, output, and content-type streams, exception propagation, failed-close retry, native aliases, and existing concrete classes. An installed generated project also closes a real file stream under isolated Python imports. Strict mypy checks preserveSelfand non-suppressing exits while rejecting closing methods onIBuffer; metadata cases cover transitive and closed-generic requirements, native-name collisions, and compatibility aliases. Constructor diagnostics and concrete-class snapshots remain intact.Compatibility and risks
Regenerate and reinstall bindings to obtain the interface helpers. Closing a resource does not release its Python projection or dispose an interface-implementation owner. Interfaces without an explicit
IClosablerequirement and arbitraryObjectvalues are unchanged. When a native member occupies a helper name, usewith stream.as_interface(IClosable):instead. DirectIClosable.close()retains native repeat-call behavior.