473,390 Members | 953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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

Jul 11 '07 #1
3 3310
ca***********@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.
Jul 11 '07 #2
On 12 jul, 01:32, Wojciech Mu a
<wojciech_m...@poczta.null.onet.pl.invalidwrote:
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(.)

Jul 12 '07 #3
En Thu, 12 Jul 2007 06:30:45 -0300, ca***********@gmail.com
<ca***********@gmail.comescribió:
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:

pyhelp(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

Jul 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hameed Khan | last post by:
hi, i was reading the manual for sys module. i found a description for a function known as exitfunc. and it says the function installed by sys.exitfunc(cleanUpFunc) call will be called when the...
0
by: Bengt Richter | last post by:
Still looking for a unifying concept ;-) It struck me that @deco1 @deco2 def foo():pass uses '@' to register at-next-name-binding processing in a way similar to atexit's registering of...
3
by: Serve Laurijssen | last post by:
Does the C++ standard say whether destructors are called before or after "atexit" functions are called. Or is nothing said about that?
8
by: JKop | last post by:
Let's say that when your program ends (no matter how) that you want a certain block of code to be executed at the end. Here's the code: std::cout << "The program will now end.\n";...
2
by: Steve Lambert | last post by:
Hi, Is it possible for the functions registered with atexit() to have access to parameter to exit i.e EXIT_SUCCESS or EXIT_FAILURE? I'd like to register a set of routines to be executed on...
7
by: David Rushby | last post by:
Consider the following program (underscores are used to force indentation): ------------------------------------------------ import atexit, threading, time def atExitFunc(): ____print...
20
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static...
16
by: Laurent Deniau | last post by:
I would like to know if the use of the pointer ref in the function cleanup() below is valid or if something in the norm prevents this kind of cross-reference during exit(). I haven't seen anything...
2
by: Christopher Pisz | last post by:
I am attempting to write a "Phoenix Singleton" using the book "Modern C++ Design" by Alexandrescu I do not understand his use of... #ifndef ATEXIT_FIXED std::atexit(Kill); #endif ....in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.