You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IDisposable should implement the context manager protocol #79
Sounds like a good idea, and should be pretty easy to implement.
I think it would just be a case of checking if a type implements IDisposable in ClassManager.GetClassInfo (classmanager.cs) and adding the enter and exit methods. Might need a new subclass of MethodInfo too, since exit and Dispose have different signatures.
Iterators have a tp_iter pointer in the PyTypeObject struct, which is what is implemented in ClassBase for IEnumerable. There's no equivalent for context managers (unless I've missed something), so I think the enter and exit methods have to be in the class' or object's dict. That's why I was thinking constructing these methods in GetClassInfo would make sense.
It is, yes. I currently use a simple wrapper function for this:
"""Wrapper function to help dealing with IDisposable classes."""
from inspect import isclass, isfunction
from contextlib import contextmanager
__all__ = ['disposable']
@contextmanager
def disposable(obj_or_class, *args, **kwargs):
"""Contextmanager wrapper around IDisposables."""
if isclass(obj_or_class) or isfunction(obj_or_class):
obj = obj_or_class(*args, **kwargs)
else:
obj = obj_or_class
if hasattr(obj, '__enter__') and hasattr(obj, '__exit__'):
# Already a contextmanager, skip
return obj
if not hasattr(obj, 'Dispose') or not callable(obj.Dispose):
# Likely not IDisposable, currently isinstance doesn't work for
# interfaces so we have to check that in this explicit way
return obj
try:
yield obj
finally:
obj.Dispose()
This allows me to use something like
with disposable(dbConnection):
bla
Since IDisposable is so tied into .NET itself by the using keyword it makes sense to forward this functionality directly.
Activity
tonyroberts commentedon Jun 26, 2015
Sounds like a good idea, and should be pretty easy to implement.
I think it would just be a case of checking if a type implements IDisposable in ClassManager.GetClassInfo (classmanager.cs) and adding the enter and exit methods. Might need a new subclass of MethodInfo too, since exit and Dispose have different signatures.
filmor commentedon Jun 26, 2015
Oh, nice hint, I'll have a look.
filmor commentedon Jun 26, 2015
Doesn't it make more sense to implement this like
IEnumerableis implemented, inclassbase.cs?tonyroberts commentedon Jun 27, 2015
Iterators have a tp_iter pointer in the PyTypeObject struct, which is what is implemented in ClassBase for IEnumerable. There's no equivalent for context managers (unless I've missed something), so I think the enter and exit methods have to be in the class' or object's dict. That's why I was thinking constructing these methods in GetClassInfo would make sense.
den-run-ai commentedon Jan 30, 2016
@filmor @tonyroberts is this to support
withstatements like here?http://stackoverflow.com/questions/1757296/what-is-the-equivalent-of-the-c-sharp-using-block-in-ironpython
filmor commentedon Feb 22, 2016
It is, yes. I currently use a simple wrapper function for this:
This allows me to use something like
Since
IDisposableis so tied into .NET itself by theusingkeyword it makes sense to forward this functionality directly.den-run-ai commentedon Feb 23, 2016
@filmor @tonyroberts this should definitely go into advanced examples!
filmor commentedon Feb 24, 2016
@tonyroberts How exactly would I add a method from C#? Hooking into the attribute resolution feels wrong ...
tonyroberts commentedon Feb 24, 2016
You could add it to the class dict. That's probably the best way, and I
that's how the normal methods are added I seem to remember.
On Wed, Feb 24, 2016 at 10:29 AM Benedikt Reinartz notifications@github.com
wrote:
awa5114 commentedon Mar 23, 2025
It's been about a decade. Has this been addressed yet?
den-run-ai commentedon Mar 23, 2025
@awa5114 i took a stab at this with claude code, looks reasonable:
#2568