@@ -321,6 +321,7 @@ def __init__(self, scr, interp, statusbar, config, idle=None):
321321 self .list_win = newwin (get_colpair (config , 'background' ), 1 , 1 , 1 , 1 )
322322 self .cpos = 0
323323 self .do_exit = False
324+ self .exit_value = None
324325 self .f_string = ''
325326 self .idle = idle
326327 self .in_hist = False
@@ -1066,9 +1067,10 @@ def push(self, s, insert_into_history=True):
10661067 curses .raw (False )
10671068 try :
10681069 return repl .Repl .push (self , s , insert_into_history )
1069- except SystemExit :
1070+ except SystemExit , e :
10701071 # Avoid a traceback on e.g. quit()
10711072 self .do_exit = True
1073+ self .exit_value = e .args
10721074 return False
10731075 finally :
10741076 curses .raw (True )
@@ -1131,6 +1133,7 @@ def repl(self):
11311133 if not more :
11321134 self .prev_block_finished = stdout_position
11331135 self .s = ''
1136+ return self .exit_value
11341137
11351138 def reprint_line (self , lineno , tokens ):
11361139 """Helper function for paren highlighting: Reprint line at offset
@@ -1821,6 +1824,9 @@ def main_curses(scr, args, config, interactive=True, locals_=None,
18211824 I've tried to keep it well factored but it needs some
18221825 tidying up, especially in separating the curses stuff
18231826 from the rest of the repl.
1827+
1828+ Returns a tuple (exit value, output), where exit value is a tuple
1829+ with arguments passed to SystemExit.
18241830 """
18251831 global stdscr
18261832 global DO_RESIZE
@@ -1873,7 +1879,7 @@ def main_curses(scr, args, config, interactive=True, locals_=None,
18731879 if banner is not None :
18741880 clirepl .write (banner )
18751881 clirepl .write ('\n ' )
1876- clirepl .repl ()
1882+ exit_value = clirepl .repl ()
18771883
18781884 main_win .erase ()
18791885 main_win .refresh ()
@@ -1886,7 +1892,7 @@ def main_curses(scr, args, config, interactive=True, locals_=None,
18861892 signal .signal (signal .SIGWINCH , old_sigwinch_handler )
18871893 signal .signal (signal .SIGCONT , old_sigcont_handler )
18881894
1889- return clirepl .getstdout ()
1895+ return ( exit_value , clirepl .getstdout () )
18901896
18911897
18921898def main (args = None , locals_ = None , banner = None ):
@@ -1901,23 +1907,23 @@ def main(args=None, locals_=None, banner=None):
19011907 orig_stderr = sys .stderr
19021908
19031909 try :
1904- o = curses_wrapper (main_curses , exec_args , config ,
1905- options .interactive , locals_ ,
1906- banner = banner )
1910+ ( exit_value , output ) = curses_wrapper (
1911+ main_curses , exec_args , config , options .interactive , locals_ ,
1912+ banner = banner )
19071913 finally :
19081914 sys .stdin = orig_stdin
19091915 sys .stderr = orig_stderr
19101916 sys .stdout = orig_stdout
19111917
19121918 # Fake stdout data so everything's still visible after exiting
19131919 if config .flush_output and not options .quiet :
1914- sys .stdout .write (o )
1920+ sys .stdout .write (output )
19151921 if hasattr (sys .stdout , 'flush' ):
19161922 sys .stdout .flush ()
1917-
1923+ return repl . extract_exit_value ( exit_value )
19181924
19191925if __name__ == '__main__' :
19201926 from bpython .cli import main
1921- main ()
1927+ sys . exit ( main () )
19221928
19231929# vim: sw=4 ts=4 sts=4 ai et
0 commit comments