Maybe I don't understand the problem you're having. Can you describe it with a little more detail? When exactly do you want the file to be closed?
2009/3/29 Adam Brand <[email protected]> > We are using 2.0 beta I think as part of ipy for asp.net. I don’t think > close() is implemented in that version… Am I wrong? > > > > Harry (who made it) suggested this: > > def _process(xr): > while xr.Read(): > xr.MoveToContent() > node = XmlNode(xr) > yield node > if xr.IsEmptyElement: > node = XmlNode(xr, endElement=True) > yield node > > > > def load(xml): > """generates an iterator over the XmlNodes in the stream of XML > represented by the xml argument""" > if isinstance(xml, XmlReader): > for n in _process(xml): yield n > else: > with XmlReader.Create(xml) as xr: > for n in _process(xr): yield n > > > > > > Adam Brand > > SilverKey Technologies > > > > *From:* [email protected] [mailto: > [email protected]] *On Behalf Of *Curt Hagenlocher > *Sent:* Saturday, March 28, 2009 7:39 PM > *To:* Discussion of IronPython > *Subject:* Re: [IronPython] try...finally in yield > > > > So I assume you'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 "from __future__ > import with_statement" at the top of your file and then say something like > this: > > def parse(xml): > with XmlReader.Create(xml) as xr > while xr.Read(): > [...] > > We automatically do the right thing when using "with" and IDisposable, so > "with" effectively becomes like a C# "using" block. > > 2009/3/28 Adam Brand <[email protected]> > > I'm using IronPython for ASP.Net...have some code (not mine, > http://devhawk.net/2008/05/07/Deserializing+XML+With+IronPython.aspx - > Harry Pierson's) that converts an xml file into an object. It has the below > function: > > def parse(xml): > xr = XmlReader.Create(xml) > while xr.Read(): > xr.MoveToContent() > node = XmlNode(xr) > yield node > if (xr.IsEmptyElement): > node.nodeType = XmlNodeType.EndElement > del node.attributes > yield node > > 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't > work. > > In reading up, I found this: > http://docs.python.org/whatsnew/2.5.html#pep-342 > > "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. " > > I'm guessing that this isn't implemented in the version of IronPython in IP > for ASP.Net. > > Does anyone have any ideas on a workaround for the generator for this > version? > > Thanks, > Adam > > -- > Adam Brand > > > _______________________________________________ > Users mailing list > [email protected] > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > > > _______________________________________________ > Users mailing list > [email protected] > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > >
_______________________________________________ Users mailing list [email protected] http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
