Skip to content

Add context manager protocol for .NET IDisposable types#2568

Merged
filmor merged 4 commits into
pythonnet:masterfrom
den-run-ai:feature/context-manager-disposable
May 11, 2026
Merged

Add context manager protocol for .NET IDisposable types#2568
filmor merged 4 commits into
pythonnet:masterfrom
den-run-ai:feature/context-manager-disposable

Conversation

@den-run-ai
Copy link
Copy Markdown
Contributor

What does this implement/fix? Explain your changes.

Implemented IDisposable based on this discussion:
#79

@microsoft-github-policy-service agree

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Ensure you have signed the .NET Foundation CLA
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

@lostmsu
Copy link
Copy Markdown
Member

lostmsu commented Mar 24, 2025

FYI: tests are not passing

@den-run-ai
Copy link
Copy Markdown
Contributor Author

This seems not related to this PR @lostmsu

i tested on mac with .net 8

@lostmsu
Copy link
Copy Markdown
Member

lostmsu commented Apr 11, 2025

@den-run-ai This is definitely related to PR, master does not seem to be failing these tests.

@michaelblyons
Copy link
Copy Markdown

For anyone else who is annoyed that the test failures do not provoke backtraces on GitHub's UI,1 here is the output:

_______________________ test_import_all_on_second_time ________________________

    def test_import_all_on_second_time():
        """Test import all attributes after a normal import without '*'.
        Due to import * only allowed at module level, the test body splitted
        to a module file."""
>       from . import importtest

tests\test_import.py:20: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    import sys
    try:
        del sys.modules["System.IO"]
    except KeyError:
        pass
    
    assert "FileStream" not in globals()
    import System.IO
    from System.IO import *
    
>   assert "FileStream" in globals()
E   AssertionError

tests\importtest.py:13: AssertionError

My (uninitiated) interpretation is that some cruft is left hanging around despite the Dispose() call.

Footnotes

  1. And also lest the test artefacts age away.

@filmor filmor force-pushed the feature/context-manager-disposable branch from 91c9670 to 4edf14c Compare May 5, 2026 16:23
@filmor
Copy link
Copy Markdown
Member

filmor commented May 7, 2026

The test-error is unrelated, it shows up because the test-suite imports FileStream which is used in the import test. I will follow up on this in #2717.

@filmor filmor force-pushed the feature/context-manager-disposable branch from 959f544 to 7ed43bd Compare May 7, 2026 17:55
@filmor filmor merged commit 25e0ccf into pythonnet:master May 11, 2026
80 of 83 checks passed
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.

4 participants