Skip to content

Commit ac7e69c

Browse files
den-run-aiden-run-ai
den-run-ai
authored and
den-run-ai
committed
update docs
1 parent 9c73c35 commit ac7e69c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
88
## Unreleased
99

1010
### Added
11+
12+
- Add context manager protocol for .NET IDisposable types, allowing use of `with` statements for IDisposable objects (#9c73c35)
13+
1114
### Changed
1215
### Fixed
1316

doc/source/python.rst

+28
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,34 @@ Python idioms:
479479
for item in domain.GetAssemblies():
480480
name = item.GetName()
481481
482+
Using Context Managers (IDisposable)
483+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
484+
485+
.NET types that implement ``IDisposable`` can be used with Python's context manager
486+
protocol using the standard ``with`` statement. This automatically calls the object's
487+
``Dispose()`` method when exiting the ``with`` block:
488+
489+
.. code:: python
490+
491+
from System.IO import MemoryStream, StreamWriter
492+
493+
# Use a MemoryStream as a context manager
494+
with MemoryStream() as stream:
495+
# The stream is automatically disposed when exiting the with block
496+
writer = StreamWriter(stream)
497+
writer.Write("Hello, context manager!")
498+
writer.Flush()
499+
500+
# Do something with the stream
501+
stream.Position = 0
502+
# ...
503+
504+
# After exiting the with block, the stream is disposed
505+
# Attempting to use it here would raise an exception
506+
507+
This works for any .NET type that implements ``IDisposable``, making resource
508+
management much cleaner and safer in Python code.
509+
482510
Type Conversion
483511
---------------
484512

0 commit comments

Comments
 (0)