Re: contextlib.contextmanager and try/finally

2012-01-31 Thread Steven D'Aprano
On Tue, 31 Jan 2012 15:09:34 -0700, Ian Kelly wrote: > On Tue, Jan 31, 2012 at 2:07 PM, Prasad, Ramit > wrote: >>>Like Neil mentioned, a contextmanager generator is wrapped with an >>>__exit__ method that is guaranteed to be called and that explicitly >>>resumes or closes the generator.  So as lo

Re: contextlib.contextmanager and try/finally

2012-01-31 Thread Tim Delaney
On 1 February 2012 09:02, Peter Otten <__pete...@web.de> wrote: > Prasad, Ramit wrote: > > Is that true even in the face of something like sys.exit()? > > What happens if 1) sys.exit is called while in the same thread > > 2) sys.exit is called from another thread but while this thread > > is in co

Re: contextlib.contextmanager and try/finally

2012-01-31 Thread Ian Kelly
On Tue, Jan 31, 2012 at 2:07 PM, Prasad, Ramit wrote: >>Like Neil mentioned, a contextmanager generator is wrapped with an >>__exit__ method that is guaranteed to be called and that explicitly >>resumes or closes the generator.  So as long as your contextmanager >>generator is properly written (i.

RE: contextlib.contextmanager and try/finally

2012-01-31 Thread Peter Otten
Prasad, Ramit wrote: >>Like Neil mentioned, a contextmanager generator is wrapped with an >>__exit__ method that is guaranteed to be called and that explicitly >>resumes or closes the generator. So as long as your contextmanager >>generator is properly written (i.e. it yields exactly once), the >

RE: contextlib.contextmanager and try/finally

2012-01-31 Thread Prasad, Ramit
>Like Neil mentioned, a contextmanager generator is wrapped with an >__exit__ method that is guaranteed to be called and that explicitly >resumes or closes the generator. So as long as your contextmanager >generator is properly written (i.e. it yields exactly once), the >finally block will execute

Re: contextlib.contextmanager and try/finally

2012-01-11 Thread johannh
On Wednesday, January 11, 2012 11:20:19 AM UTC-6, Ian wrote: > > Second, I believe that passage is not referring to the contextmanager > decorator specifically, but more generally to the changes that were > made to allow generators to yield from within a try-finally construct > (previously this wo

Re: contextlib.contextmanager and try/finally

2012-01-11 Thread Ian Kelly
On Wed, Jan 11, 2012 at 8:45 AM, wrote: > However, then I read the following paragraph from PEP-343: > >    Note that we're not guaranteeing that the finally-clause is >    executed immediately after the generator object becomes unused, >    even though this is how it will work in CPython.  This

Re: contextlib.contextmanager and try/finally

2012-01-11 Thread Robert Kern
On 1/11/12 3:45 PM, joha...@gmail.com wrote: I'm trying to write a context manager to handle database connections, under the principle that I should not rely on CPython's reference-counting semantics to clean up scarce resources, like connections. I wrote: @contexlib.contextmanager def ensure

Re: contextlib.contextmanager and try/finally

2012-01-11 Thread Neil Cerutti
On 2012-01-11, joha...@gmail.com wrote: > That suggests that I cannot rely on the > contextlib.contextmanager decorator to ensure that the > connection is closed and would have to write my own object with > __enter__ and __exit__ methods to guarantee this. contextmanager wraps your generator in a

contextlib.contextmanager and try/finally

2012-01-11 Thread johannh
I'm trying to write a context manager to handle database connections, under the principle that I should not rely on CPython's reference-counting semantics to clean up scarce resources, like connections. I wrote: @contexlib.contextmanager def ensure_connection(con=None): con_created = False