Skip to content

Add context manager protocol for .NET IDisposable types#119

Closed
Martin-Molinero wants to merge 1 commit into
masterfrom
cherry/context-manager-idisposable
Closed

Add context manager protocol for .NET IDisposable types#119
Martin-Molinero wants to merge 1 commit into
masterfrom
cherry/context-manager-idisposable

Conversation

@Martin-Molinero

Copy link
Copy Markdown
Member

Cherry-pick from upstream pythonnet/pythonnet: lets .NET IDisposable objects be used as Python context managers (with statement), calling Dispose() on exit.

Conflict note: only docs conflicted (CHANGELOG.md, doc/source/python.rst) — resolved to keep the upstream additions; no code conflicts. Builds clean; adds tests/test_disposable.py. Fits this fork's pattern of adding Pythonic protocols (__bool__, container methods, len).

🤖 Generated with Claude Code

* Add context manager protocol for .NET IDisposable types

---------

Co-authored-by: den-run-ai <macmone@Deniss-Air.hsd1.ca.comcast.net>
Co-authored-by: Benedikt Reinartz <filmor@gmail.com>
(cherry picked from commit 25e0ccf)
@Martin-Molinero

Copy link
Copy Markdown
Member Author

Closing — this can't pass on this fork as-is.

The upstream implementation (pythonnet#2568) adds __enter__/__exit__ via the CollectionMixinsProvider (ContextManagerMixin in Mixins/collections.py). This fork deliberately disabled CollectionMixinsProvider (see the // see https://github.com/pythonnet/pythonnet/issues/1785 comment in InteropConfiguration.cs), so the mixin is never injected and the new tests fail with AttributeError: __enter__.

Making this work would require re-enabling the mixins provider, which is a deliberate fork decision — out of scope for a cherry-pick. If you want IDisposable-as-context-manager, it'd need either re-enabling mixins (revisiting pythonnet#1785) or a C#-side implementation that doesn't depend on the mixin mechanism. Closing; happy to revisit if you decide to re-enable mixins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants