Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19661
| References | <10841855.1646.1326296715536.JavaMail.geo-discussion-forums@yqiz15> <CALwzidntt7n1PskCuoHaehCR1+K_F13YxdGapW2fWQtykNYm+A@mail.gmail.com> <5B80DD153D7D744689F57F4FB69AF474103948@SCACMX008.exchad.jpmchase.net> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2012-01-31 15:09 -0700 |
| Subject | Re: contextlib.contextmanager and try/finally |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5261.1328047804.27778.python-list@python.org> (permalink) |
On Tue, Jan 31, 2012 at 2:07 PM, Prasad, Ramit <ramit.prasad@jpmorgan.com> 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 >>finally block will execute in a timely fashion. > > Is that true even in the face of something like sys.exit()? Yes. > What happens if 1) sys.exit is called while in the same thread Why don't you try it and find out? To answer the question, though, sys.exit() raises a SystemExit exception, which propagates out of the with block and calls the __exit__ method, which then throws the exception to the generator, which executes its finally clause and exits. The __exit__ method returns false, so the SystemExit exception continues to propagate, and if it is not caught, then the process exits. > 2) sys.exit is called from another thread but while this thread > is in context manager? Then the other thread raises SystemExit, and the current thread is unaffected. sys.exit only affects the thread it is called in. You can certainly come up with scenarios in which the finally clause does not execute, e.g. killing the interpreter with "kill -9" or yanking out the power cord. Within the confines of the Python interpreter, though, it is guaranteed that the finally block will execute.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
contextlib.contextmanager and try/finally johannh@gmail.com - 2012-01-11 07:45 -0800
Re: contextlib.contextmanager and try/finally Neil Cerutti <neilc@norwich.edu> - 2012-01-11 16:46 +0000
Re: contextlib.contextmanager and try/finally Robert Kern <robert.kern@gmail.com> - 2012-01-11 17:14 +0000
Re: contextlib.contextmanager and try/finally Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-11 10:20 -0700
Re: contextlib.contextmanager and try/finally johannh@gmail.com - 2012-01-11 09:30 -0800
Re: contextlib.contextmanager and try/finally johannh@gmail.com - 2012-01-11 09:30 -0800
RE: contextlib.contextmanager and try/finally "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-01-31 21:07 +0000
RE: contextlib.contextmanager and try/finally Peter Otten <__peter__@web.de> - 2012-01-31 23:02 +0100
Re: contextlib.contextmanager and try/finally Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-31 15:09 -0700
Re: contextlib.contextmanager and try/finally Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-01 02:15 +0000
csiph-web