| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -321,6 +321,7 @@ def __init__(self, scr, interp, statusbar, config, idle=None): | |||
| 321 | 321 | self.list_win = newwin(get_colpair(config, 'background'), 1, 1, 1, 1) | |
| 322 | 322 | self.cpos = 0 | |
| 323 | 323 | self.do_exit = False | |
| 324 | + self.exit_value = None | ||
| 324 | 325 | self.f_string = '' | |
| 325 | 326 | self.idle = idle | |
| 326 | 327 | self.in_hist = False | |
@@ -1066,9 +1067,10 @@ def push(self, s, insert_into_history=True): | |||
| 1066 | 1067 | curses.raw(False) | |
| 1067 | 1068 | try: | |
| 1068 | 1069 | return repl.Repl.push(self, s, insert_into_history) | |
| 1069 | - except SystemExit: | ||
| 1070 | + except SystemExit, e: | ||
| 1070 | 1071 | # Avoid a traceback on e.g. quit() | |
| 1071 | 1072 | self.do_exit = True | |
| 1073 | + self.exit_value = e.args | ||
| 1072 | 1074 | return False | |
| 1073 | 1075 | finally: | |
| 1074 | 1076 | curses.raw(True) | |
@@ -1131,6 +1133,7 @@ def repl(self): | |||
| 1131 | 1133 | if not more: | |
| 1132 | 1134 | self.prev_block_finished = stdout_position | |
| 1133 | 1135 | self.s = '' | |
| 1136 | + return self.exit_value | ||
| 1134 | 1137 | ||
| 1135 | 1138 | def reprint_line(self, lineno, tokens): | |
| 1136 | 1139 | """Helper function for paren highlighting: Reprint line at offset | |
@@ -1821,6 +1824,9 @@ def main_curses(scr, args, config, interactive=True, locals_=None, | |||
| 1821 | 1824 | I've tried to keep it well factored but it needs some | |
| 1822 | 1825 | tidying up, especially in separating the curses stuff | |
| 1823 | 1826 | 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. | ||
| 1824 | 1830 | """ | |
| 1825 | 1831 | global stdscr | |
| 1826 | 1832 | global DO_RESIZE | |
@@ -1873,7 +1879,7 @@ def main_curses(scr, args, config, interactive=True, locals_=None, | |||
| 1873 | 1879 | if banner is not None: | |
| 1874 | 1880 | clirepl.write(banner) | |
| 1875 | 1881 | clirepl.write('\n') | |
| 1876 | - clirepl.repl() | ||
| 1882 | + exit_value = clirepl.repl() | ||
| 1877 | 1883 | ||
| 1878 | 1884 | main_win.erase() | |
| 1879 | 1885 | main_win.refresh() | |
@@ -1886,7 +1892,7 @@ def main_curses(scr, args, config, interactive=True, locals_=None, | |||
| 1886 | 1892 | signal.signal(signal.SIGWINCH, old_sigwinch_handler) | |
| 1887 | 1893 | signal.signal(signal.SIGCONT, old_sigcont_handler) | |
| 1888 | 1894 | ||
| 1889 | - return clirepl.getstdout() | ||
| 1895 | + return (exit_value, clirepl.getstdout()) | ||
| 1890 | 1896 | ||
| 1891 | 1897 | ||
| 1892 | 1898 | def main(args=None, locals_=None, banner=None): | |
@@ -1901,23 +1907,23 @@ def main(args=None, locals_=None, banner=None): | |||
| 1901 | 1907 | orig_stderr = sys.stderr | |
| 1902 | 1908 | ||
| 1903 | 1909 | 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) | ||
| 1907 | 1913 | finally: | |
| 1908 | 1914 | sys.stdin = orig_stdin | |
| 1909 | 1915 | sys.stderr = orig_stderr | |
| 1910 | 1916 | sys.stdout = orig_stdout | |
| 1911 | 1917 | ||
| 1912 | 1918 | # Fake stdout data so everything's still visible after exiting | |
| 1913 | 1919 | if config.flush_output and not options.quiet: | |
| 1914 | - sys.stdout.write(o) | ||
| 1920 | + sys.stdout.write(output) | ||
| 1915 | 1921 | if hasattr(sys.stdout, 'flush'): | |
| 1916 | 1922 | sys.stdout.flush() | |
| 1917 | - | ||
| 1923 | + return repl.extract_exit_value(exit_value) | ||
| 1918 | 1924 | ||
| 1919 | 1925 | if __name__ == '__main__': | |
| 1920 | 1926 | from bpython.cli import main | |
| 1921 | - main() | ||
| 1927 | + sys.exit(main()) | ||
| 1922 | 1928 | ||
| 1923 | 1929 | # vim: sw=4 ts=4 sts=4 ai et | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -336,6 +336,7 @@ def __init__(self, interpreter, config): | |||
| 336 | 336 | gtk.TextView.__init__(self) | |
| 337 | 337 | repl.Repl.__init__(self, interpreter, config) | |
| 338 | 338 | self.interp.writetb = self.writetb | |
| 339 | + self.exit_value = None | ||
| 339 | 340 | self.editing = Nested() | |
| 340 | 341 | self.reset_indent = False | |
| 341 | 342 | self.modify_font(pango.FontDescription(self.config.gtk_font)) | |
@@ -694,7 +695,8 @@ def push_line(self): | |||
| 694 | 695 | self.highlight_current_line() | |
| 695 | 696 | try: | |
| 696 | 697 | return self.push(line + '\n') | |
| 697 | - except SystemExit: | ||
| 698 | + except SystemExit, e: | ||
| 699 | + self.exit_value = e.args | ||
| 698 | 700 | self.emit('exit-event') | |
| 699 | 701 | return False | |
| 700 | 702 | ||
@@ -857,8 +859,8 @@ def main(args=None): | |||
| 857 | 859 | except KeyboardInterrupt: | |
| 858 | 860 | pass | |
| 859 | 861 | ||
| 860 | - return 0 | ||
| 862 | + return repl.extract_exit_value(repl_widget.exit_value) | ||
| 861 | 863 | ||
| 862 | 864 | if __name__ == '__main__': | |
| 863 | 865 | from bpython.gtk_ import main | |
| 864 | - main() | ||
| 866 | + sys.exit(main()) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1027,3 +1027,14 @@ def token_is_any_of(token): | |||
| 1027 | 1027 | return any(check(token) for check in is_token_types) | |
| 1028 | 1028 | ||
| 1029 | 1029 | return token_is_any_of | |
| 1030 | + | ||
| 1031 | + def extract_exit_value(args): | ||
| 1032 | + """Given the arguments passed to `SystemExit`, return the value that | ||
| 1033 | + should be passed to `sys.exit`. | ||
| 1034 | + """ | ||
| 1035 | + if len(args) == 0: | ||
| 1036 | + return None | ||
| 1037 | + elif len(args) == 1: | ||
| 1038 | + return args[0] | ||
| 1039 | + else: | ||
| 1040 | + return args | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -621,6 +621,8 @@ def __init__(self, event_loop, palette, interpreter, config): | |||
| 621 | 621 | self.current_output = None | |
| 622 | 622 | self._completion_update_suppressed = False | |
| 623 | 623 | ||
| 624 | + self.exit_value = None | ||
| 625 | + | ||
| 624 | 626 | load_urwid_command_map(config) | |
| 625 | 627 | ||
| 626 | 628 | # Subclasses of Repl need to implement echo, current_line, cw | |
@@ -913,7 +915,8 @@ def push(self, s, insert_into_history=True): | |||
| 913 | 915 | # Pretty blindly adapted from bpython.cli | |
| 914 | 916 | try: | |
| 915 | 917 | return repl.Repl.push(self, s, insert_into_history) | |
| 916 | - except SystemExit: | ||
| 918 | + except SystemExit, e: | ||
| 919 | + self.exit_value = e.args | ||
| 917 | 920 | raise urwid.ExitMainLoop() | |
| 918 | 921 | except KeyboardInterrupt: | |
| 919 | 922 | # KeyboardInterrupt happened between the except block around | |
@@ -1296,7 +1299,9 @@ def run_find_coroutine(): | |||
| 1296 | 1299 | ||
| 1297 | 1300 | if config.flush_output and not options.quiet: | |
| 1298 | 1301 | sys.stdout.write(myrepl.getstdout()) | |
| 1299 | - sys.stdout.flush() | ||
| 1302 | + if hasattr(sys.stdout, "flush"): | ||
| 1303 | + sys.stdout.flush() | ||
| 1304 | + return repl.extract_exit_value(myrepl.exit_value) | ||
| 1300 | 1305 | ||
| 1301 | 1306 | def load_urwid_command_map(config): | |
| 1302 | 1307 | urwid.command_map[key_dispatch[config.up_one_line_key]] = 'cursor up' | |
@@ -1325,4 +1330,4 @@ def load_urwid_command_map(config): | |||
| 1325 | 1330 | 'yank_from_buffer': 'C-y'}, | |
| 1326 | 1331 | """ | |
| 1327 | 1332 | if __name__ == '__main__': | |
| 1328 | - main() | ||
| 1333 | + sys.exit(main()) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments