2424exit_now = False
2525quitting = False
2626
27- def main ():
27+ def main (del_exitfunc = False ):
2828 """Start the Python execution server in a subprocess
2929
3030 In the Python subprocess, RPCServer is instantiated with handlerclass
@@ -44,6 +44,8 @@ def main():
4444 """
4545 global exit_now
4646 global quitting
47+ global no_exitfunc
48+ no_exitfunc = del_exitfunc
4749 port = 8833
4850 if sys .argv [1 :]:
4951 port = int (sys .argv [1 ])
@@ -57,7 +59,7 @@ def main():
5759 try :
5860 if exit_now :
5961 try :
60- sys . exit (0 )
62+ exit ()
6163 except KeyboardInterrupt :
6264 # exiting but got an extra KBI? Try again!
6365 continue
@@ -83,7 +85,7 @@ def main():
8385 except :
8486 # Link didn't work, print same exception to __stderr__
8587 traceback .print_exception (type , value , tb , file = sys .__stderr__ )
86- sys . exit (0 )
88+ exit ()
8789 else :
8890 continue
8991
@@ -159,6 +161,16 @@ def flush_stdout():
159161 except (AttributeError , EOFError ):
160162 pass
161163
164+ def exit ():
165+ """Exit subprocess, possibly after first deleting sys.exitfunc
166+
167+ If config-main.cfg/.def 'General' 'delete-exitfunc' is True, then any
168+ sys.exitfunc will be removed before exiting. (VPython support)
169+
170+ """
171+ if no_exitfunc :
172+ del sys .exitfunc
173+ sys .exit (0 )
162174
163175class MyRPCServer (rpc .RPCServer ):
164176
@@ -186,7 +198,7 @@ def handle_error(self, request, client_address):
186198 traceback .print_exc (file = erf )
187199 print >> erf , '\n *** Unrecoverable, server exiting!'
188200 print >> erf , '-' * 40
189- sys . exit (0 )
201+ exit ()
190202
191203
192204class MyHandler (rpc .RPCHandler ):
@@ -229,7 +241,7 @@ def runcode(self, code):
229241 exec code in self .locals
230242 except :
231243 if quitting :
232- sys . exit (0 )
244+ exit ()
233245 # even print a user code SystemExit exception, continue
234246 print_exception ()
235247 else :
0 commit comments