f



atexit, sys.exit, sys.exitfunc, reaching end of source code

Hi,

I am playing with the atexit module but I don't find a way to see the
difference
between a script calling sys.exit(<returncode>) and the interpreting
arriving at the end
of the source code file. This has a semantic difference for my
applications.
Is there a way to determine in an exithandler (that is registered
using atexit.register)
how I exited?

Second question: is there a way to determine in my exithandler what
the return code was.
atexit doesn't seem to support that

Third question: I can solve part of my problem by reassigning sys.exit
= myfunction
Is that a wise idea? It seems that an interpreter (python 2.4)
reaching end of source code, calls sys.exitfunc instead of sys.exit ?


Thanks

Carl D'Halluin

www.qlayer.com

0
7/11/2007 10:21:50 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

3 Replies
561 Views

Similar Articles

[PageSpeed] 43

carl.dhalluin@gmail.com wrote:
> I am playing with the atexit module but I don't find a way to see the
> difference
> between a script calling sys.exit(<returncode>) and the interpreting
> arriving at the end
> of the source code file. This has a semantic difference for my
> applications.
> Is there a way to determine in an exithandler (that is registered
> using atexit.register)
> how I exited?

Actually sys.exit raises exception SystemExit, but if interpreter
reaches end of script exception is not raised.  Try something
like this:

if __name__ == '__main__':
	exit_code_for_exithandler = None
	try:
		#...
		sys.exit(5)
		pass
		#...
	except SystemExit, e:
		exit_code_for_exithandler = e.code
		print "sys.exit called"
	else:
		print "end of script"

w.
0
7/11/2007 11:32:22 PM
On 12 jul, 01:32, Wojciech Mu a
<wojciech_m...@poczta.null.onet.pl.invalid> wrote:
> carl.dhall...@gmail.com wrote:
> > I am playing with theatexitmodule but I don't find a way to see the
> > difference
> > between a script calling sys.exit(<returncode>) and the interpreting
> > arriving at the end
> > of the source code file. This has a semantic difference for my
> > applications.
> > Is there a way to determine in an exithandler (that is registered
> > usingatexit.register)
> > how I exited?
>
> Actually sys.exit raises exception SystemExit, but if interpreter
> reaches end of script exception is not raised.  Try something
> like this:
>
> if __name__ == '__main__':
>         exit_code_for_exithandler = None
>         try:
>                 #...
>                 sys.exit(5)
>                 pass
>                 #...
>         except SystemExit, e:
>                 exit_code_for_exithandler = e.code
>                 print "sys.exit called"
>         else:
>                 print "end of script"
>
> w.

I am getting quite confused with this SystemExit.
If I register a custom sys.excepthook then it is never invoked
when I do sys.exit(.)

0
7/12/2007 9:30:45 AM
En Thu, 12 Jul 2007 06:30:45 -0300, carl.dhalluin@gmail.com  
<carl.dhalluin@gmail.com> escribi�:

> I am getting quite confused with this SystemExit.
> If I register a custom sys.excepthook then it is never invoked
> when I do sys.exit(.)

No, sys.excepthook won't be called for SystemExit:

py> help(sys)
Help on built-in module sys:
[...]
     excepthook -- called to handle any uncaught exception other than  
SystemExit

Unfortunately help(sys.excepthook) does not provide the same information,  
neither the docs for the sys module. (Should be corrected...)

-- 
Gabriel Genellina

0
gagsl-py2 (3707)
7/13/2007 12:15:58 AM
Reply:

Similar Artilces:

sys.stderr.write and sys.exit
Is the same use _sys.stderr.write('error message'); sys.exit(1)_ than _sys.exit('error message')_ ? Note: help(sys.exit) If the status is omitted or None, it defaults to zero (i.e., success). If the status is numeric, it will be used as the system exit status. If it is another kind of object, it will be printed and the system exit status will be one (i.e., failure). "GinTon" <jonas.esp@googlemail.com> writes: > Is the same use _sys.stderr.write('error message'); sys.exit(1)_ > than _sys.exit('error message')_ ? (Note: The underscore &...

exit() or sys.exit()
What is the difference on exit() and sys.exit() when called in the main body of a script? From the command line they seem to have the same effect. Aside: Just used a python dictionary in which the keys were compiled regular expressions. Provided a very elegant solution. Have to love it. Brendan wrote: > What is the difference on exit() and sys.exit() when called in the > main body of a script? From the command line they seem to have the > same effect. In Python <=2.4 you had to use sys.exit() because __builtins__.exit() griped: tchase@asgix:~$ python2.4 Python 2.4.4 (#2,...

Can I get the exit code "n" passed to sys.exit(n) ?
Hello everybody, In the following code of the finish() function, is there any way to get the exit code passed to sys.exit() ? def finish() : RETURN_CODE_FROM_SYS_EXIT = ???? # how can I get it ? if RETURN_CODE_FROM_SYS_EXIT = 0 : # process ended OK else : # process ended with some error # execute something atexit.register(finish) # this is my main program.... ERR_CODE=3 sys.exit(ERR_CODE) Regards, Yujo On Apr 10, 1:15 pm, "Yujo" <yujorodrig...@gmail.com> wrote: > Hello everybody, > > In the following code of the finish() function, i...

What are files Z80.SYS, Z80CCP.SYS and PRMTVPVT.SYS for?
Had a good Christmas this weekend? I came across diskettes with "Mailmerge 3.0 and Calcstar version 1.45 for CP/M 8080", that seem to be for a Dec Rainbow machine. Besides a Mailmerge overlay and the Calcstar files, some CP/M and some CP/M-86 files, there are three files on the diskettes that I never have heard of before: Z80.SYS, Z80CCP.SYS and PRMTVPVT.SYS. There are no ASCII texts in PRMTVPVT.SYS. The only ASCII text in Z80.SYS (at the end of the file) is "EI SPHLDI XCHGPCHLXTHLRET HLT CMC STC CMA DAA RAR RAL RRC RLC NOP CPI ORI XRI ANI SBI IN SUI OUT ACI ADI CALLJMP LDA...

source code for sys files
would like to get the source code for somthing comparable to the command.com dos.sys and io.sys files is it posible to get rbenjudah@aol.com On 13 Dec 2003 05:06:03 GMT, hodedow12@aol.com (Hodedow12) wrote: >would like to get the source code for somthing comparable to the command.com >dos.sys and io.sys files is it posible to get rbenjudah@aol.com http://www.freedos.org would be a good place to start. Also, Caldera at one point released source to a version of DR DOS, which is doubtless still floating around the net. Try Googling for DOSSRC.ZIP; it should be abo...

Sys.exit() does not fully exit
I have this script: import os, thread, threading, time, sys class Script1(threading.Thread): def run(self): os.system('runScript1.py') class Script2(threading.Thread): def run(self): os.system('runScript2.py') if __name__ == '__main__': s = Script1() s.start() time.sleep(5) a = Script2() a.start() time.sleep(5) while True: answer = raw_input('Type "x" to shutdown: ') if answer == 'x': print 'Shutting down....' time.sleep(1) pri...

Python style: exceptions vs. sys.exit()
I have a general question of Python style, or perhaps just good programming practice. My group is developing a medium-sized library of general-purpose Python functions, some of which do I/O. Therefore it is possible for many of the library functions to raise IOError Exceptions. The question is: should the library function be able to just dump to sys.exit() with a message about the error (like "couldn't open this file"), or should the exception propagate to the calling program which handles the issue? Thanks in advance for anyone who can either answer my question or point me to ...

How to get file descriptors of sys.stdin, sys.stdout and sys.stderr?
How to get file descriptors of sys.stdin, sys.stdout and sys.stderr? On 11/13/2014 7:51 PM, satishmlmlml@gmail.com wrote: in 4 different threads > How to get file descriptors of sys.stdin, sys.stdout and sys.stderr? > fileno() in not supported. Is it only in 3.1? What is the workaround? > io.UnsupportedOperation: fileno > How to give a file descriptor number to this function? How to get a file descriptor number? Satish, you are acting like the variety of troll called a Help Vampire. One of the symptoms is hogging the newsgroup by starting multiple threads a da...

How to get file descriptors of sys.stdin, sys.stdout and sys.stderr? #2
How to get file descriptors of sys.stdin, sys.stdout and sys.stderr? On 11/13/14 7:54 PM, satishmlmlml@gmail.com wrote: > How to get file descriptors of sys.stdin, sys.stdout and sys.stderr? > You don't seem to be reading any of the responses you are getting. At the very least, you don't seem to be understanding them, or engaging with the authors. You are misbehaving on this list. Everyone else: I recommend that people stop responding. Satish seems unlikely to improve. -- Ned Batchelder, http://nedbatchelder.com ...

sys.exit()
In a code such as: if len(sys.argv) < 2: print "I need arguments!" sys.exit(1) Is sys.exit() really a good choice? Is there something more elegant? (I tried return but it is valid only in a function) -- -- Every sufficiently advanced magic is indistinguishable from technology - Arthur C Anticlarke "Ivan Voras" <ivoras@fer.hr> wrote in news:bm3msp$fjo$1@bagan.srce.hr: > In a code such as: > > if len(sys.argv) < 2: > print "I need arguments!" > sys.exit(1) > > Is sys.exit() really a good choice? Is ther...

sys
Dear Friend, I have found some websites: for msn messenger, http://www.hotmailpk.com/ For entertainment,spotlight.articles,interviews,dailyupdate htttp://www.familyandtwist.com/ for sms messeges, smsjokes, http://www.smstwist.com/ for articles,entertainment,beauty tips,news,showbiz and fashion http://http://www.familypk.com Visit these websites: Thanks ...

sys
computer common oriented http://www.freewebs.com/buildem/ http://amigos.com/go/g904313-pmem ...

sys
Is it OT here to talk about IPC and pipes? Or are all aspects of kernle programming ok here? Bill Bill Cunningham <nospam@nspam.com> wrote: > Is it OT here to talk about IPC and pipes? Or are all aspects of kernle > programming ok here? If you want to discuss how IPC and pipes are implemented in the kernel it's completely ok. But if you're interested in how to use IPC or pipes from a userland program then posting to e.g. comp.unix.programmer or comp.os.linux.development.apps will probably make a lot more sense. R...

cscope like source code navigation for python code
Hi, I'm using g?vim and its c-scope plugin for browsing C-code. What would be a good way of navigating larger python projects with vim? thanks for any suggestions On 2010-08-07, Gelonida <gelonida@gmail.com> wrote: > I'm using g?vim and its c-scope plugin for browsing C-code. > What would be a good way of navigating larger python projects with vim? ctags: http://ctags.sourceforge.net/ On 08/08/2010 01:42 AM, Tim Harig wrote: > On 2010-08-07, Gelonida <gelonida@gmail.com> wrote: >> I'm using g?vim and its c-scope plugin for br...