So I assume you&#39;re calling close() on the generator?  A try/finally around the code in the generator can be used to catch the StopIteration exception and force the dispose.  But even better, you could say &quot;from __future__ import with_statement&quot; at the top of your file and then say something like this:<br>
<br>def parse(xml):<br>    with XmlReader.Create(xml) as xr<br>        while xr.Read():<br>            [...]<br><br>We automatically do the right thing when using &quot;with&quot; and IDisposable, so &quot;with&quot; effectively becomes like a C# &quot;using&quot; block.<br>
<br><div class="gmail_quote">2009/3/28 Adam Brand <span dir="ltr">&lt;<a href="mailto:adamb@silverkeytech.com">adamb@silverkeytech.com</a>&gt;</span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I&#39;m using IronPython for ASP.Net...have some code (not mine, <a href="http://devhawk.net/2008/05/07/Deserializing+XML+With+IronPython.aspx" target="_blank">http://devhawk.net/2008/05/07/Deserializing+XML+With+IronPython.aspx</a> - Harry Pierson&#39;s) that converts an xml file into an object. It has the below function:<br>

<br>def parse(xml):<br>    xr = XmlReader.Create(xml)<br>    while xr.Read():<br>        xr.MoveToContent()<br>        node = XmlNode(xr)<br>        yield node<br>        if (xr.IsEmptyElement):<br>            node.nodeType = XmlNodeType.EndElement<br>

            del node.attributes<br>            yield node<br><br>This code is problematic as it locks the xml file it is reading. I tried a try...finally to do a .Close() and .Dispose(), but the compiler was not happy with that. Just putting .Close() and .Dispose() at the end doesn&#39;t work.<br>

<br>In reading up, I found this:<br><a href="http://docs.python.org/whatsnew/2.5.html#pep-342" target="_blank">http://docs.python.org/whatsnew/2.5.html#pep-342</a><br><br>&quot;The addition of the close() method has one side effect that isn’t obvious. close() is called when a generator is garbage-collected, so this means the generator’s code gets one last chance to run before the generator is destroyed. This last chance means that try...finally statements in generators can now be guaranteed to work; the finally clause will now always get a chance to run. The syntactic restriction that you couldn’t mix yield statements with a try...finally suite has therefore been removed. &quot;<br>

<br>I&#39;m guessing that this isn&#39;t implemented in the version of IronPython in IP for ASP.Net.<br><br>Does anyone have any ideas on a workaround for the generator for this version?<br><br>Thanks,<br>Adam<br clear="all">

<br>-- <br><font color="#888888">Adam Brand<br><br>
</font><br>_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
<a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
<br></blockquote></div><br>