[Python-Dev] exec/with thunk-handling proposal
holger krekel
pyth@devel.trillke.net
Tue, 4 Feb 2003 16:39:38 +0100
Jeff Epler wrote:
> On Tue, Feb 04, 2003 at 01:03:24PM +0100, Alex Martelli wrote:
> > On Tuesday 04 February 2003 12:54 pm, holger krekel wrote:
> > ...
> > > f1=open(inputfn)
> > > with autoclose(f1):
> > > f2 = open(outputfn, 'w')
> > > with autoclose(f2):
> > > for line in f1:
> > > ...
> > > f2.write(line)
> > >
> > > I think there should be a better solution for multiple ressources.
> >
> > I agree this is slight too cumbersome, and deeply wish it was
> > feasible to go with:
> >
> > with f1=autoclose(inputfn):
> > with f2=autoclose(outputfn, 'w'):
> > for line in f1:
> > ...
> > f2.write(line)
> >
> > i.e., nested with's would be just fine, IF binding the with'd expression
> > to a name was allowed as part of the with statement, by whatever sugar.
>
> This made me realize something. What happens if some sort of exception
> is raised between 'f1 = open()' and the start of the block 'with
> autoclose(f1):'?
With CPython the file object will be refcount-finalized sooner or later.
With JPython there should be a leak.
With
self.acquire()
# KeyBoardInterrupt here
try: ...
finally:
self.release()
you can get a deadlock (as you stated). It's just
so unlikely that it hardly ever causes problems. You can
mask out asynchronous signals if this is really critical.
And the unmaskable SIG9 will eventually take care anyway :-)
holger