tags:

views:

257

answers:

6

What's the equivalent of this in IronPython? Is it just a try-finally block?

using (var something = new ClassThatImplementsIDisposable())
{
  // stuff happens here
}
+4  A: 

IronPython (as of the 2.6 release candidates) supports the with statement, which wraps an IDisposable object in a manner similar to using.

Reed Copsey
Just curious - why the downvotes? As far as I know, this is true- python, as a language, doesn't include this in the language.
Reed Copsey
-1 Python does include this, it's the 'with' statement.
Javier Badia
@Javier Badi: Say that again after you've tried using it.
Teddy
Actually, with is different. It does not handle IDisposable out of the box with .NET - you would need to wrap this into your own type, and specify an __exit__ routine to do the disposal. It is not the same as using in C#.
Reed Copsey
Downvoted because this is the same as using, and does handle IDisposable.
Dino Viehland
@Dino: That is not true in the current, stable release. True in 2.6 RCs, though. Updated my answer for that.
Reed Copsey
A: 

the using block is in fact the following under the hood:

try {
  (do something unmanaged here)
}
finally {
  unmanagedObject.Dispose();
}

Hope this helps you understand the logic behind the using statement.

Webleeuw
A: 

There is the with statement: http://www.ironpythoninaction.com/magic-methods.html#context-managers-and-the-with-statement

with open(filename) as handle:
    data = handle.read()
    ...
Bastien Léonard
No. The "with" statement only works with lockable objects, to which category file objects just happens to belong. It does *not* work with arbitrary types, say, integers.
Teddy
A: 

Having used C#, Python but not IronPython:

This usage of "using" is syntactic sugar, nothing more.

Broam
Not true. A "using" block will automatically dispose all objects that implement IDisposable in C# once the code has been completed (with or without exceptions).
Sonny Boy
Right, but there's nothing special about it compared to try{}finally{}
Broam
It's technically correct, but it's also completely irrelevant. A loop is syntactic sugar for if/goto; a class is syntactic sugar for a struct with a bunch of function pointers; and a struct is syntactic sugar for some pointer arithmetic and casts over a `char` array. So what?
Pavel Minaev
+2  A: 

With statement. For example:

with open("/temp/abc") as f:
    lines = f.readlines()
Javier Badia
This does not support IDisposable out of the box. It works with python types with an __exit__ routine specified.
Reed Copsey
+8  A: 

IronPython supports using IDisposable with with statement, so you can write something like this:

with ClassThatImplementsIDisposable() as something:
    pass
Bojan Resnik
Good option for your own types, but doesn't work for framework types.
Reed Copsey
Any particular reason why it doesn't work with framework types?
Josh Kodroff
Are you sure? I just tried with `StreamWriter` and it seems to work as expected.
Bojan Resnik
IronPython maps IDisposable onto a set of __enter__ and __exit__ methdos for the context manager so this indeed does work w/ any type which implements IDisposable.
Dino Viehland