def stdinconsoleMain(): import appinfo if appinfo.args.shell: print "Setting up Python shell interface" # This is hacky for now but actually I don't really know a better way. # The problem is that the main thread might be used by the GUI. # Thus, we can only cancel this here via the shell itself. from better_exchook import debug_shell from queue import queue import utils shellGlobals = { "state": state, "queue": queue, } shellGlobals.update(utils.__dict__) debug_shell( shellGlobals, shellGlobals, execWrapper=utils.do_in_mainthread) print "Python shell quit." # The shell exited. Quit. state.quit() return fd = sys.stdin.fileno() if not os.isatty(fd): return # don't do anything # the main thread is pushing stdin updates to stdinQueue. setTtyNoncanonical(sys.stdin.fileno()) print "stdin input ready" for ch in stdinQueue.read(): handleInput(ch) restoreTty(fd)
def debug_shell(user_ns=None, user_global_ns=None, exit_afterwards=True): """ Provides some interactive Python shell. Uses IPython if possible. Wraps to ``better_exchook.debug_shell``. :param dict[str]|None user_ns: :param dict[str]|None user_global_ns: :param bool exit_afterwards: will do sys.exit(1) at the end """ print("Debug shell:") from Util import ObjAsDict import DebugHelpers user_global_ns_new = dict(ObjAsDict(DebugHelpers).items()) if user_global_ns: user_global_ns_new.update( user_global_ns) # may overwrite vars from DebugHelpers user_global_ns_new["debug"] = DebugHelpers # make this available always print("Available debug functions/utils (via DebugHelpers):") for k, v in sorted(vars(DebugHelpers).items()): if k[:1] == "_": continue print(" %s (%s)" % (k, type(v))) print("Also DebugHelpers available as 'debug'.") if not user_ns: user_ns = {} if user_ns: print("Locals:") for k, v in sorted(user_ns.items()): print(" %s (%s)" % (k, type(v))) import better_exchook better_exchook.debug_shell(user_ns, user_global_ns_new) if exit_afterwards: print("Debug shell exit. Exit now.") sys.exit(1)
def debug_shell(user_ns=None, user_global_ns=None, exit_afterwards=True): print("Debug shell:") from Util import ObjAsDict import DebugHelpers user_global_ns_new = dict(ObjAsDict(DebugHelpers).items()) if user_global_ns: user_global_ns_new.update( user_global_ns) # may overwrite vars from DebugHelpers user_global_ns_new["debug"] = DebugHelpers # make this available always print("Available debug functions/utils (via DebugHelpers):") for k, v in sorted(vars(DebugHelpers).items()): if k[:1] == "_": continue print(" %s (%s)" % (k, type(v))) print("Also DebugHelpers available as 'debug'.") if not user_ns: user_ns = {} if user_ns: print("Locals:") for k, v in sorted(user_ns.items()): print(" %s (%s)" % (k, type(v))) import better_exchook better_exchook.debug_shell(user_ns, user_global_ns_new) if exit_afterwards: print("Debug shell exit. Exit now.") sys.exit(1)
def _debug_shell(): debug_shell(locals(), globals())
import TaskSystem # important to be initially imported in the main thread if Args.numWorkers: TaskSystem.kNumWorkers = Args.numWorkers TaskSystem.kMinQueuedActions = Args.numWorkers TaskSystem.kSuggestedMaxQueuedActions = Args.numWorkers * 2 if Args.shell: TaskSystem.kNumWorkers = 0 TaskSystem.setup() def main_entry(): import TaskSystem import Logging try: TaskSystem.main_loop() except KeyboardInterrupt: Logging.log("KeyboardInterrupt") # Has the effect that this module is know as 'main' and not just '__main__'. import main if __name__ == "__main__": main.setup(*sys.argv[1:]) if main.Args.shell: import better_exchook better_exchook.debug_shell(globals(), globals()) sys.exit() main.main_entry()