Maybe I don&#39;t understand the problem you&#39;re having.  Can you describe it with a little more detail?  When exactly do you want the file to be closed?<br><br><div class="gmail_quote">2009/3/29 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;">








<div link="blue" vlink="purple" lang="EN-US">

<div>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">We are using 2.0 beta I think as part of ipy for <a href="http://asp.net" target="_blank">asp.net</a>. I don’t
think close() is implemented in that version… Am I wrong?</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">Harry (who made it) suggested this:</span></p>

<p><span style="font-family: Courier;">def _process(xr):<div class="im"><br>
  while xr.Read():<br>
    xr.MoveToContent()<br>
    node = XmlNode(xr)<br>
    yield node<br>
    if xr.IsEmptyElement:<br></div>
      node = XmlNode(xr, endElement=True)<br>
      yield node</span></p>

<p> </p>

<p><span style="font-family: Courier;">def load(xml):<br>
  &quot;&quot;&quot;generates an iterator over the XmlNodes in the stream
of XML represented by the xml argument&quot;&quot;&quot;<br>
  if isinstance(xml, XmlReader):<br>
    for n in _process(xml): yield n<br>
  else:<br>
    with XmlReader.Create(xml) as xr:<br>
      for n in _process(xr): yield n</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">Adam Brand</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">SilverKey Technologies</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p>

<div style="border-style: solid none none; border-color: rgb(181, 196, 223) -moz-use-text-color -moz-use-text-color; border-width: 1pt medium medium; padding: 3pt 0in 0in;">

<p><b><span style="font-size: 10pt;">From:</span></b><span style="font-size: 10pt;">
<a href="mailto:users-bounces@lists.ironpython.com" target="_blank">users-bounces@lists.ironpython.com</a> [mailto:<a href="mailto:users-bounces@lists.ironpython.com" target="_blank">users-bounces@lists.ironpython.com</a>] <b>On
Behalf Of </b>Curt Hagenlocher<br>
<b>Sent:</b> Saturday, March 28, 2009 7:39 PM<br>
<b>To:</b> Discussion of IronPython<br>
<b>Subject:</b> Re: [IronPython] try...finally in yield</span></p>

</div><div><div></div><div class="h5">

<p> </p>

<p style="margin-bottom: 12pt;">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.</p>

<div>

<p>2009/3/28 Adam Brand &lt;<a href="mailto:adamb@silverkeytech.com" target="_blank">adamb@silverkeytech.com</a>&gt;</p>

<p style="margin-bottom: 12pt;">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>
<span style="color: rgb(136, 136, 136);">Adam Brand<br>
<br>
</span><br>
_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com" target="_blank">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></p>

</div>

<p> </p>

</div></div></div>

</div>


<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>