Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20438 > unrolled thread
| Started by | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| First post | 2012-02-15 13:12 +0000 |
| Last post | 2012-02-15 10:40 -0800 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
atexit.register in case of errors Andrea Crotti <andrea.crotti.0@gmail.com> - 2012-02-15 13:12 +0000
Re: atexit.register in case of errors Mel Wilson <mwilson@the-wire.com> - 2012-02-15 08:33 -0500
Re: atexit.register in case of errors Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-02-15 08:52 -0500
Re: atexit.register in case of errors Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-02-15 16:18 +0100
Re: atexit.register in case of errors Andrea Crotti <andrea.crotti.0@gmail.com> - 2012-02-15 15:41 +0000
Re: atexit.register in case of errors Andrea Crotti <andrea.crotti.0@gmail.com> - 2012-02-15 14:35 +0000
Re: atexit.register in case of errors Miki Tebeka <miki.tebeka@gmail.com> - 2012-02-15 10:40 -0800
Re: atexit.register in case of errors Miki Tebeka <miki.tebeka@gmail.com> - 2012-02-15 10:40 -0800
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2012-02-15 13:12 +0000 |
| Subject | atexit.register in case of errors |
| Message-ID | <mailman.5827.1329311531.27778.python-list@python.org> |
I have the following very simplified situation
from atexit import register
def goodbye():
print("saying goodbye")
def main():
while True:
var = raw_input("read something")
if __name__ == '__main__':
register(goodbye)
main()
But in my case the "goodbye" function is deleting the logging file which
was created
during the application execution.
Now the problem is that it *always* executes, even when the applications
quits for
some bad errors.
Is there a way to have an exit hook, which doesn't execute in case of
errors?
I've seen the code of atexit and it apparently doesn't know anything
about the current
status and why the application is actually quitting, is that correct?
[toc] | [next] | [standalone]
| From | Mel Wilson <mwilson@the-wire.com> |
|---|---|
| Date | 2012-02-15 08:33 -0500 |
| Message-ID | <jhgc7t$b2m$1@speranza.aioe.org> |
| In reply to | #20438 |
Andrea Crotti wrote:
> I have the following very simplified situation
>
> from atexit import register
>
>
> def goodbye():
> print("saying goodbye")
>
>
> def main():
> while True:
> var = raw_input("read something")
>
>
> if __name__ == '__main__':
> register(goodbye)
> main()
>
>
> But in my case the "goodbye" function is deleting the logging file which
> was created
> during the application execution.
> Now the problem is that it *always* executes, even when the applications
> quits for
> some bad errors.
>
> Is there a way to have an exit hook, which doesn't execute in case of
> errors?
> I've seen the code of atexit and it apparently doesn't know anything
> about the current
> status and why the application is actually quitting, is that correct?
That's sort of the point: to do things that simply *have* to happen, even if
you've lost control of the program.
The usual way to do what you're asking is
if __name__ == '__main__':
main()
goodbye()
and write main so that it returns after it's done all the things it's
supposed to do. If you've sprinkled `sys.exit()` all over your code, then
don't do that. If you're forced to deal with a library that hides
`sys.exit()` calls in the functions, then you have my sympathy. Library
authors should not do that, and there have been threads on c.l.p explaining
why they shouldn't.
Mel.
[toc] | [prev] | [next] | [standalone]
| From | Devin Jeanpierre <jeanpierreda@gmail.com> |
|---|---|
| Date | 2012-02-15 08:52 -0500 |
| Message-ID | <mailman.5829.1329313984.27778.python-list@python.org> |
| In reply to | #20439 |
On Wed, Feb 15, 2012 at 8:33 AM, Mel Wilson <mwilson@the-wire.com> wrote:
> The usual way to do what you're asking is
>
> if __name__ == '__main__':
> main()
> goodbye()
>
> and write main so that it returns after it's done all the things it's
> supposed to do. If you've sprinkled `sys.exit()` all over your code, then
> don't do that. If you're forced to deal with a library that hides
> `sys.exit()` calls in the functions, then you have my sympathy. Library
> authors should not do that, and there have been threads on c.l.p explaining
> why they shouldn't.
In such a case. one can do::
if __name__ == '__main__':
try:
main()
except SystemExit:
pass
goodbye()
-- Devin
[toc] | [prev] | [next] | [standalone]
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Date | 2012-02-15 16:18 +0100 |
| Message-ID | <jhgicb$v3k$1@r03.glglgl.gl> |
| In reply to | #20441 |
Am 15.02.2012 14:52 schrieb Devin Jeanpierre:
> On Wed, Feb 15, 2012 at 8:33 AM, Mel Wilson<mwilson@the-wire.com> wrote:
>> The usual way to do what you're asking is
>>
>> if __name__ == '__main__':
>> main()
>> goodbye()
>>
>> and write main so that it returns after it's done all the things it's
>> supposed to do. If you've sprinkled `sys.exit()` all over your code, then
>> don't do that. If you're forced to deal with a library that hides
>> `sys.exit()` calls in the functions, then you have my sympathy. Library
>> authors should not do that, and there have been threads on c.l.p explaining
>> why they shouldn't.
>
> In such a case. one can do::
>
> if __name__ == '__main__':
> try:
> main()
> except SystemExit:
> pass
> goodbye()
>
> -- Devin
Wouldn't
if __name__ == '__main__':
try:
main()
finally:
goodbye()
be even better? Or doesn't it work well together with SystemExit?
Thomas
[toc] | [prev] | [next] | [standalone]
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2012-02-15 15:41 +0000 |
| Message-ID | <mailman.5833.1329320476.27778.python-list@python.org> |
| In reply to | #20446 |
On 02/15/2012 03:18 PM, Thomas Rachel wrote: > > Wouldn't > > if __name__ == '__main__': > try: > main() > finally: > goodbye() > > be even better? Or doesn't it work well together with SystemExit? > > > Thomas Well in that case goodbye is always called, even if I have some other nasty exception, which is not what I want.. (and is exactly what I had with atexit.register). Isn't it?
[toc] | [prev] | [next] | [standalone]
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2012-02-15 14:35 +0000 |
| Message-ID | <mailman.5831.1329316538.27778.python-list@python.org> |
| In reply to | #20439 |
On 02/15/2012 01:52 PM, Devin Jeanpierre wrote: > On Wed, Feb 15, 2012 at 8:33 AM, Mel Wilson<mwilson@the-wire.com> wrote: >> The usual way to do what you're asking is >> >> if __name__ == '__main__': >> main() >> goodbye() >> >> and write main so that it returns after it's done all the things it's >> supposed to do. If you've sprinkled `sys.exit()` all over your code, then >> don't do that. If you're forced to deal with a library that hides >> `sys.exit()` calls in the functions, then you have my sympathy. Library >> authors should not do that, and there have been threads on c.l.p explaining >> why they shouldn't. > In such a case. one can do:: > > if __name__ == '__main__': > try: > main() > except SystemExit: > pass > goodbye() > > -- Devin Makes perfect sense, I solved like this then, thanks
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2012-02-15 10:40 -0800 |
| Message-ID | <mailman.5842.1329331202.27778.python-list@python.org> |
| In reply to | #20438 |
Another option is to use a global error flag and set it in sys.excepthook (see http://docs.python.org/library/sys.html#sys.excepthook). goodbye will check the error flag and skip execution if error flag is set.
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2012-02-15 10:40 -0800 |
| Message-ID | <16963932.42.1329331200261.JavaMail.geo-discussion-forums@ynje6> |
| In reply to | #20438 |
Another option is to use a global error flag and set it in sys.excepthook (see http://docs.python.org/library/sys.html#sys.excepthook). goodbye will check the error flag and skip execution if error flag is set.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web