def main(quiet=True): try: get_ipython except NameError: nested = 0 cfg = load_default_config() cfg.TerminalInteractiveShell.prompts_class = CustPrompt else: print 'Running nested copies of IPython. Augmenting configuration...' cfg = load_default_config() nested = 1 from IPython.terminal.embed import InteractiveShellEmbed cfg.TerminalInteractiveShell.confirm_exit = False cfg.TerminalInteractiveShell.debug = True ipshell = InteractiveShellEmbed.instance( config=cfg, banner1='Welcome to the sqlenv IPython Shell...\n') # Setup sqlitedict as `sqdb` sqdb_path = os.path.join(os.path.abspath('.'), '.local_db.sqlite') sqdb = sqlitedict.SqliteDict(sqdb_path) ipshell()
def shell(): """Runs a shell in the app context. Runs an interactive Python shell in the context of a given Flask application. The application will populate the default namespace of this shell according to it's configuration. This is useful for executing small snippets of management code without having to manually configuring the application. """ import IPython from IPython.terminal.ipapp import load_default_config from traitlets.config.loader import Config from flask.globals import _app_ctx_stack app = _app_ctx_stack.top.app if 'IPYTHON_CONFIG' in app.config: config = Config(app.config['IPYTHON_CONFIG']) else: config = load_default_config() config.TerminalInteractiveShell.banner1 = 'Python %s on %s\nIPython: %s\nApp: %s%s\nInstance: %s' % ( sys.version, sys.platform, IPython.__version__, app.import_name, app.debug and ' [debug]' or '', app.instance_path) IPython.start_ipython(argv=[], config=config)
def embed(**kwargs): """Call this to embed IPython at the current point in your program. The first invocation of this will create an :class:`InteractiveShellEmbed` instance and then call it. Consecutive calls just call the already created instance. If you don't want the kernel to initialize the namespace from the scope of the surrounding function, and/or you want to load full IPython configuration, you probably want `IPython.start_ipython()` instead. Here is a simple example:: from IPython import embed a = 10 b = 20 embed('First time') c = 30 d = 40 embed Full customization can be done by passing a :class:`Config` in as the config argument. """ config = kwargs.get('config') header = kwargs.pop('header', '') compile_flags = kwargs.pop('compile_flags', None) if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs['config'] = config shell = InteractiveShellEmbed.instance(**kwargs) shell(header=header, stack_depth=2, compile_flags=compile_flags)
def run_ipython_shell_v11(locals, globals, first_time): '''IPython shell from IPython version 0.11''' if first_time: banner = "Hit Ctrl-D to return to PuDB." else: banner = "" try: # IPython 1.0 got rid of the frontend intermediary, and complains with # a deprecated warning when you use it. from IPython.terminal.interactiveshell import TerminalInteractiveShell from IPython.terminal.ipapp import load_default_config except ImportError: from IPython.frontend.terminal.interactiveshell import \ TerminalInteractiveShell from IPython.frontend.terminal.ipapp import load_default_config # XXX: in the future it could be useful to load a 'pudb' config for the # user (if it exists) that could contain the user's macros and other # niceities. config = load_default_config() shell = TerminalInteractiveShell.instance(config=config, banner2=banner) # XXX This avoids a warning about not having unique session/line numbers. # See the HistoryManager.writeout_cache method in IPython.core.history. shell.history_manager.new_session() # Save the originating namespace old_locals = shell.user_ns old_globals = shell.user_global_ns # Update shell with current namespace _update_ns(shell, locals, globals) shell.mainloop(banner) # Restore originating namespace _update_ns(shell, old_locals, old_globals)
def embed(**kwargs): """Call this to embed IPython at the current point in your program. The first invocation of this will create an :class:`InteractiveShellEmbed` instance and then call it. Consecutive calls just call the already created instance. If you don't want the kernel to initialize the namespace from the scope of the surrounding function, and/or you want to load full IPython configuration, you probably want `IPython.start_ipython()` instead. Here is a simple example:: from IPython import embed a = 10 b = 20 embed(header='First time') c = 30 d = 40 embed() Full customization can be done by passing a :class:`Config` in as the config argument. """ config = kwargs.get('config') header = kwargs.pop('header', u'') compile_flags = kwargs.pop('compile_flags', None) if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs['config'] = config #save ps1/ps2 if defined ps1 = None ps2 = None try: ps1 = sys.ps1 ps2 = sys.ps2 except AttributeError: pass #save previous instance saved_shell_instance = InteractiveShell._instance if saved_shell_instance is not None: cls = type(saved_shell_instance) cls.clear_instance() frame = sys._getframe(1) shell = InteractiveShellEmbed.instance( _call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno), **kwargs) shell(header=header, stack_depth=2, compile_flags=compile_flags) InteractiveShellEmbed.clear_instance() #restore previous instance if saved_shell_instance is not None: cls = type(saved_shell_instance) cls.clear_instance() for subclass in cls._walk_mro(): subclass._instance = saved_shell_instance if ps1 is not None: sys.ps1 = ps1 sys.ps2 = ps2
def wrapper(namespace=namespace, banner=''): config = load_default_config() # Always use .instace() to ensure _instance propagation to all parents # this is needed for <TAB> completion works well for new imports shell = InteractiveShellEmbed.instance( banner1=banner, user_ns=namespace, config=config) shell()
def embed(**kwargs): """Call this to embed IPython at the current point in your program. The first invocation of this will create an :class:`InteractiveShellEmbed` instance and then call it. Consecutive calls just call the already created instance. Here is a simple example:: from IPython import embed a = 10 b = 20 embed('First time') c = 30 d = 40 embed Full customization can be done by passing a :class:`Config` in as the config argument. """ config = kwargs.get('config') header = kwargs.pop('header', u'') compile_flags = kwargs.pop('compile_flags', None) if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs['config'] = config shell = InteractiveShellEmbed.instance(**kwargs) shell(header=header, stack_depth=2, compile_flags=compile_flags)
def command(): """Runs a shell in the app context. Runs an interactive Python shell in the context of a given Flask application. The application will populate the default namespace of this shell according to it's configuration. This is useful for executing small snippets of management code without having to manually configuring the application. """ from flask.globals import _app_ctx_stack app = _app_ctx_stack.top.app banner1 = '''Python %s on %s App: %s%s Instance: %s Db: %s''' % (sys.version, sys.platform, app.import_name, app.debug and ' [debug]' or '', app.instance_path, json.dumps(app.config['DATABASE'], indent=2)) shell_name_space = app.make_shell_context() try: import IPython from IPython.terminal.ipapp import load_default_config config = load_default_config() config.TerminalInteractiveShell.banner1 = banner1 IPython.embed( user_ns=shell_name_space, config=config, ) except ImportError: sys.exit(code.interact(banner=banner1, local=shell_name_space))
def main(): """ Entry point for running as standalone APP """ from .session import Session from .core import Client from getpass import getpass session = Session() # generate header header_databases = generate_header_databases(session) header_aliases = generate_header_aliases(session) header = HELP_HEADER % {'databases': header_databases, 'aliases': header_aliases} _locals = { 'Client': Client, 'session': session, 'getpass': getpass } try: from IPython import embed try: from IPython.terminal.ipapp import load_default_config ip_config = load_default_config() except: ip_config = None embed(user_ns=_locals, header=header, config=ip_config) except ImportError: from code import interact interact(local=_locals, banner=header) session.save()
def shell_command(ipython_args): import IPython from IPython.terminal.ipapp import load_default_config from traitlets.config.loader import Config from flask.globals import _app_ctx_stack app = _app_ctx_stack.top.app banner = 'Python %s on %s\nIPython: %s\nApp: %s%s\nInstance: %s\n' % ( sys.version, sys.platform, IPython.__version__, app.import_name, app.debug and ' [debug]' or '', app.instance_path, ) ctx = {} if 'IPYTHON_CONFIG' in app.config: config = Config(app.config['IPYTHON_CONFIG']) else: config = load_default_config() config.TerminalInteractiveShell.banner1 = banner ctx.update(app.make_shell_context()) IPython.start_ipython(argv=ipython_args, user_ns=ctx, config=config)
def _get_debugger(): ''' Get a debugger object ''' # The code below is a complex attempt to load IPython # debugger which works with multiple versions of IPython. # # Unfortunately, their API kept changing prior to version 1.0. try: import IPython try: import IPython.core.debugger try: from IPython.terminal.ipapp import load_default_config config = load_default_config() colors = config.TerminalInteractiveShell.colors except: import IPython.core.ipapi ip = IPython.core.ipapi.get() colors = ip.colors try: return IPython.core.debugger.Pdb(colors.get_value(initial='Linux')) except AttributeError: return IPython.core.debugger.Pdb(colors) except ImportError: #Fallback to older version of IPython API import IPython.ipapi import IPython.Debugger shell = IPython.Shell.IPShell(argv=['']) ip = IPython.ipapi.get() return IPython.Debugger.Pdb(ip.options.colors) except ImportError: #Fallback to standard debugger import pdb return pdb.Pdb()
def __init__(self, cout): self._exc = None self._cout = cout self._external_commands = {} self._frame = None self._config = load_default_config() self._global_ns = {} self._local_ns = {} self._interactive = False self._script_lines = {} self._commands = CommandSet(cout) kwargs = {} kwargs['config'] = self._config kwargs['banner1'] = '' kwargs['banner2'] = '' c = self._config c.PromptManager.in_template = 'In[\#]: ' c.PromptManager.in2_template = ' .\D.: ' c.PromptManager.out_template = ' Out[\#]: ' c.PromptManager.justify = True self._ip = IPythonShell(**kwargs)
def main(): """ Entry point for running as standalone APP """ from .session import Session from .core import Client from getpass import getpass session = Session() # generate header header_databases = generate_header_databases(session) header_aliases = generate_header_aliases(session) header = HELP_HEADER % { 'databases': header_databases, 'aliases': header_aliases } _locals = {'Client': Client, 'session': session, 'getpass': getpass} try: from IPython import embed try: from IPython.terminal.ipapp import load_default_config ip_config = load_default_config() except: ip_config = None embed(user_ns=_locals, header=header, config=ip_config) except ImportError: from code import interact interact(local=_locals, banner=header) session.save()
def shell_command(ctx: Context): # pragma: no cover """ Run shell """ # Based on https://github.com/ei-grad/flask-shell-ipython app = ctx.obj if "IPYTHON_CONFIG" in app.settings: config = Config(app.settings["IPYTHON_CONFIG"]) else: config = load_default_config() config.TerminalInteractiveShell.banner1 = """ Python %s on %s IPython: %s App: %s [%s] Instance: %s """ % ( sys.version, sys.platform, IPython.__version__, type(app).__name__, app.import_name, app.root_path, ) sys.argv[0] = sys.argv[0] + " shell" IPython.start_ipython( argv=ctx.args, user_ns=app.make_shell_context(), config=config, )
def embed(**kwargs): """Call this to embed IPython at the current point in your program. The first invocation of this will create an :class:`InteractiveShellEmbed` instance and then call it. Consecutive calls just call the already created instance. If you don't want the kernel to initialize the namespace from the scope of the surrounding function, and/or you want to load full IPython configuration, you probably want `IPython.start_ipython()` instead. Here is a simple example:: from IPython import embed a = 10 b = 20 embed('First time') c = 30 d = 40 embed Full customization can be done by passing a :class:`Config` in as the config argument. """ config = kwargs.get('config') header = kwargs.pop('header', u'') compile_flags = kwargs.pop('compile_flags', None) if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs['config'] = config shell = InteractiveShellEmbed.instance(**kwargs) shell(header=header, stack_depth=2, compile_flags=compile_flags)
def _shell(): """ Run an interactive python shell. """ from IPython import start_ipython from IPython.terminal.ipapp import load_default_config c = load_default_config() settings_path, settings_name = os.environ['foxglove_settings_path'].split( ':') exec_lines = [ 'import asyncio, base64, math, hashlib, json, os, pickle, re, secrets, sys, time', 'from datetime import datetime, date, timedelta, timezone', 'from pathlib import Path', 'from pprint import pprint as pp', '', 'sys.path.append(os.getcwd())', f'ROOT_PATH = Path("{ROOT_PATH}")', 'sys.path.append(str(ROOT_PATH))', 'os.chdir(str(ROOT_PATH))', '', f'from {settings_path} import {settings_name}', 'settings = Settings()', ] exec_lines += [ 'print("\\n Python {v.major}.{v.minor}.{v.micro}\\n".format(v=sys.version_info))' ] + [f"print(' {line}')" for line in exec_lines] c.TerminalIPythonApp.display_banner = False c.TerminalInteractiveShell.confirm_exit = False c.InteractiveShellApp.exec_lines = exec_lines start_ipython(argv=(), config=c)
def shell(args, settings: BaseSettings): """ Run an interactive python shell """ from IPython import start_ipython from IPython.terminal.ipapp import load_default_config c = load_default_config() settings_path, settings_name = args.settings_path.rsplit('.', 1) exec_lines = [ 'import asyncio, base64, math, hashlib, json, os, pickle, re, secrets, sys, time', 'from datetime import datetime, date, timedelta, timezone', 'from pathlib import Path', 'from pprint import pprint as pp', '', f'root_dir = "{args.root}"', 'sys.path.append(root_dir)', 'os.chdir(root_dir)', '', f'from {settings_path} import {settings_name}', 'settings = Settings()', ] exec_lines += [ 'print("\\n Python {v.major}.{v.minor}.{v.micro}\\n".format(v=sys.version_info))' ] + [f"print(' {l}')" for l in exec_lines] c.TerminalIPythonApp.display_banner = False c.TerminalInteractiveShell.confirm_exit = False c.InteractiveShellApp.exec_lines = exec_lines start_ipython(argv=(), config=c)
def embed(**kwargs): """Call this to embed IPython at the current point in your program. The first invocation of this will create an :class:`InteractiveShellEmbed` instance and then call it. Consecutive calls just call the already created instance. If you don't want the kernel to initialize the namespace from the scope of the surrounding function, and/or you want to load full IPython configuration, you probably want `IPython.start_ipython()` instead. Here is a simple example:: from IPython import embed a = 10 b = 20 embed(header='First time') c = 30 d = 40 embed() Full customization can be done by passing a :class:`Config` in as the config argument. """ config = kwargs.get('config') header = kwargs.pop('header', u'') compile_flags = kwargs.pop('compile_flags', None) if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs['config'] = config #save ps1/ps2 if defined ps1 = None ps2 = None try: ps1 = sys.ps1 ps2 = sys.ps2 except AttributeError: pass #save previous instance saved_shell_instance = InteractiveShell._instance if saved_shell_instance is not None: cls = type(saved_shell_instance) cls.clear_instance() frame = sys._getframe(1) shell = InteractiveShellEmbed.instance(_init_location_id='%s:%s' % ( frame.f_code.co_filename, frame.f_lineno), **kwargs) shell(header=header, stack_depth=2, compile_flags=compile_flags, _call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno)) InteractiveShellEmbed.clear_instance() #restore previous instance if saved_shell_instance is not None: cls = type(saved_shell_instance) cls.clear_instance() for subclass in cls._walk_mro(): subclass._instance = saved_shell_instance if ps1 is not None: sys.ps1 = ps1 sys.ps2 = ps2
def wrapper(namespace=namespace, banner=''): config = load_default_config() # Always use .instace() to ensure _instance propagation to all parents # this is needed for <TAB> completion works well for new imports shell = InteractiveShellEmbed.instance(banner1=banner, user_ns=namespace, config=config) shell()
def _ipython(local, banner): from IPython.terminal.embed import InteractiveShellEmbed from IPython.terminal.ipapp import load_default_config InteractiveShellEmbed.clear_instance() shell = InteractiveShellEmbed.instance(banner1=banner, user_ns=local, config=load_default_config()) shell()
def get(self): from IPython.terminal.ipapp import load_default_config extensions = load_default_config( ).InteractiveShellApp.extensions.to_dict() if any(["pyflyby" in val for val in extensions.values()]): self.finish({"status": "loaded"}) else: self.finish({"status": "not-loaded"})
def shell(): from IPython import start_ipython from IPython.terminal.ipapp import load_default_config c = load_default_config() c.TerminalIPythonApp.display_banner = False c.TerminalInteractiveShell.confirm_exit = False c.InteractiveShellApp.exec_lines = EXEC_LINES start_ipython(argv=(), config=c)
def __init__(self): # Create and initialize our IPython instance. if hasattr(PyDevTerminalInteractiveShell, '_instance') and PyDevTerminalInteractiveShell._instance is not None: self.ipython = PyDevTerminalInteractiveShell._instance else: self.ipython = PyDevTerminalInteractiveShell.instance(config=load_default_config()) self._curr_exec_line = 0 self._curr_exec_lines = []
def do_debug(self, line): '''Internal debugger for development. Requires IPython module installed ''' parser = parsing_opts.gen_parser(self.client, "debug", self.do_debug.__doc__) opts = parser.parse_args(line.split()) try: from IPython.terminal.ipapp import load_default_config from IPython.terminal.embed import InteractiveShellEmbed from IPython import embed except ImportError: embed = None if not embed: try: import code except ImportError: self.client.logger.info( format_text( "\n*** 'IPython' and 'code' library are not available ***\n", 'bold')) return auto_completer = readline.get_completer() console_history_file = self._push_history() client = self.client descr = 'IPython' if embed else "'code' library" self.client.logger.info( format_text( "\n*** Starting Python shell (%s)... use 'client' as client object, Ctrl + D to exit ***\n" % descr, 'bold')) try: if embed: cfg = load_default_config() cfg['TerminalInteractiveShell']['confirm_exit'] = False embed(config=cfg, display_banner=False) #InteractiveShellEmbed.clear_instance() else: ns = {} ns.update(globals()) ns.update(locals()) code.InteractiveConsole(ns).interact('') finally: readline.set_completer(auto_completer) self._pop_history(console_history_file) self.client.logger.info( format_text("\n*** Leaving Python shell ***\n"))
def embed(stack_depth=2, **kwargs): """Based on IPython.terminal.embed.embed.""" config = kwargs.get('config') header = kwargs.pop('header', '') compile_flags = kwargs.pop('compile_flags', None) if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs['config'] = config using = kwargs.get('using', 'sync') if using: kwargs['config'].update({ 'TerminalInteractiveShell': { 'loop_runner': using, 'colors': 'NoColor', 'autoawait': using != 'sync' } }) # save ps1/ps2 if defined ps1 = None ps2 = None try: ps1 = sys.ps1 ps2 = sys.ps2 except AttributeError: pass # save previous instance saved_shell_instance = InteractiveShell._instance if saved_shell_instance is not None: cls = type(saved_shell_instance) cls.clear_instance() frame = sys._getframe(1) shell = CoconutShellEmbed.instance(_init_location_id='%s:%s' % ( frame.f_code.co_filename, frame.f_lineno, ), **kwargs) shell( header=header, stack_depth=stack_depth, compile_flags=compile_flags, _call_location_id='%s:%s' % ( frame.f_code.co_filename, frame.f_lineno, ), ) CoconutShellEmbed.clear_instance() # restore previous instance if saved_shell_instance is not None: cls = type(saved_shell_instance) cls.clear_instance() for subclass in cls._walk_mro(): subclass._instance = saved_shell_instance if ps1 is not None: sys.ps1 = ps1 sys.ps2 = ps2
def wrapper(namespace=namespace, banner=''): config = load_default_config() # Always use .instace() to ensure _instance propagation to all parents # this is needed for <TAB> completion works well for new imports # and clear the instance to always have the fresh env # on repeated breaks like with inspect_response() InteractiveShellEmbed.clear_instance() shell = InteractiveShellEmbed.instance( banner1=banner, user_ns=namespace, config=config) shell()
def wrapper(namespace=namespace, banner=''): config = load_default_config() # Always use .instance() to ensure _instance propagation to all parents # this is needed for <TAB> completion works well for new imports # and clear the instance to always have the fresh env # on repeated breaks like with inspect_response() InteractiveShellEmbed.clear_instance() shell = InteractiveShellEmbed.instance( banner1=banner, user_ns=namespace, config=config) shell()
def _ipython(local, banner): # noinspection PyUnresolvedReferences from IPython.terminal.embed import InteractiveShellEmbed # noinspection PyUnresolvedReferences from IPython.terminal.ipapp import load_default_config InteractiveShellEmbed.clear_instance() shell = InteractiveShellEmbed.instance(banner1=banner, user_ns=local, config=load_default_config()) shell()
def shell(ipython_args): config = load_default_config() config.TerminalInteractiveShell.banner1 = '''Python %s on %s IPython: %s App: %s''' % (sys.version, sys.platform, IPython.__version__, app.name) IPython.start_ipython( argv=ipython_args, config=config, )
def _ipython(local, banner): from IPython.terminal.embed import InteractiveShellEmbed from IPython.terminal.ipapp import load_default_config InteractiveShellEmbed.clear_instance() shell = InteractiveShellEmbed.instance( banner1=banner, user_ns=local, config=load_default_config() ) shell()
def shell(ipython_args): """Starts an ipython shell importing our app. Useful for debugging.""" import IPython from IPython.terminal.ipapp import load_default_config config = load_default_config() config.TerminalInteractiveShell.banner1 = f"""Python {sys.version} on {sys.platform} IPython: {IPython.__version__}""" IPython.start_ipython(argv=ipython_args, user_ns={}, config=config)
def shell(self, uid=None): api = self.login(uid) namespace = {"api": api, "settings": self.config, "db": self.db, "redis": self.redis, "apk_bucket": self.apk_bucket, "image_bucket": self.image_bucket} shell = InteractiveShellEmbed( banner1=BANNER, user_ns=namespace, config=load_default_config()) shell() print("bye.")
def embed_ipython_shell(namespace=None, history_filename=None): """Start an IPython Shell""" from IPython import embed from IPython.terminal.ipapp import load_default_config import nest_asyncio nest_asyncio.apply() c = load_default_config() c.HistoryAccessor.hist_file = history_filename c.InteractiveShellEmbed = c.TerminalInteractiveShell embed(user_ns=namespace, using="asyncio", config=c)
def start_python_console(namespace=None, noipython=False, banner=None): """Start Python console bound to the given namespace. If IPython or bpython are available, they will be started instead, unless `noipython` is True. If both IPython and bpython are available, IPython will be preferred. If neither is available, the built in console will be used, with tab completion enabled if on a system with readline. """ if namespace is None: namespace = {} if banner is None: banner = '' try: try: # use IPython if available if noipython: raise ImportError() try: from IPython.terminal.embed import InteractiveShellEmbed from IPython.terminal.ipapp import load_default_config except ImportError: from IPython.frontend.terminal.embed import InteractiveShellEmbed from IPython.frontend.terminal.ipapp import load_default_config except ImportError: pass else: config = load_default_config() shell = InteractiveShellEmbed( banner1=banner, user_ns=namespace, config=config) shell() return try: import bpython except ImportError: pass else: # start bpython bpython.embed(locals_=namespace, banner=banner) return import code try: # readline module is only available on unix systems import readline except ImportError: pass else: import rlcompleter readline.parse_and_bind("tab:complete") code.interact(banner=banner, local=namespace) except SystemExit: # raised when using exit() in python code.interact pass
def enter(context): """ 进入IPython的开发shell,类似于flask shell """ from app import app from IPython.terminal.ipapp import load_default_config from traitlets.config.loader import Config import pprint import sys import flask import IPython from app.modules.users.models import UserInfo from app.modules.auth.models import User from app.extensions import db flask_app = app.app def shell_context(): context = dict(pprint=pprint.pprint) context.update(vars(flask)) context.update(vars(app)) context["User"] = User context["db"] = db context["UserInfo"] = UserInfo return context if "IPYTHON_CONFIG" in flask_app.config: config = Config(flask_app.config["IPYTHON_CONFIG"]) else: config = load_default_config() config.TerminalInteractiveShell.banner1 = """Python %s on %s IPython: %s App: %s [%s] Instance: %s""" % ( sys.version, sys.platform, IPython.__version__, flask_app.import_name, flask_app.env, flask_app.instance_path, ) flask_app.shell_context_processors.append(shell_context) with flask_app.app_context(): IPython.start_ipython( argv=[], user_ns=flask_app.make_shell_context(), config=config, )
def embed(**kwargs): """ Copied from `IPython/terminal/embed.py`, but using our `InteractiveShellEmbed` instead. """ config = kwargs.get('config') header = kwargs.pop('header', u'') compile_flags = kwargs.pop('compile_flags', None) if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs['config'] = config shell = InteractiveShellEmbed.instance(**kwargs) shell(header=header, stack_depth=2, compile_flags=compile_flags)
def run_ipython_shell_v11(globals, locals): """IPython shell from IPython version 0.11""" if SHELL_FIRST_TIME: banner = "Hit Ctrl-D to return to PuDB." SHELL_FIRST_TIME.pop() else: banner = "" try: # IPython 1.0 got rid of the frontend intermediary, and complains with # a deprecated warning when you use it. from IPython.terminal.interactiveshell import TerminalInteractiveShell from IPython.terminal.ipapp import load_default_config except ImportError: from IPython.frontend.terminal.interactiveshell import \ TerminalInteractiveShell from IPython.frontend.terminal.ipapp import load_default_config # XXX: in the future it could be useful to load a 'pudb' config for the # user (if it exists) that could contain the user's macros and other # niceities. config = load_default_config() shell = TerminalInteractiveShell.instance(config=config, banner2=banner) # XXX This avoids a warning about not having unique session/line numbers. # See the HistoryManager.writeout_cache method in IPython.core.history. shell.history_manager.new_session() # Save the originating namespace old_locals = shell.user_ns old_globals = shell.user_global_ns # Update shell with current namespace _update_ipython_ns(shell, globals, locals) args = [] if ipython_version() < (5, 0, 0): args.append(banner) else: print(banner) # XXX Quick and dirty way to fix issues with IPython 8.0.0+, introduced # by commit 08d54c0e367b535fd88aca5273fd09e5e70d08f8. # Setting _atexit_once_called = True will prevent call to # IPython.core.interactiveshell.InteractiveShell._atexit_once() from inside # IPython.terminal.interactiveshell.TerminalInteractiveShell.mainloop() # This allows us to repeatedly re-call mainloop() and the whole # run_ipython_shell_v11() function shell._atexit_once_called = True shell.mainloop(*args) del shell._atexit_once_called # Restore originating namespace _update_ipython_ns(shell, old_globals, old_locals)
def shell(store, options, jugspace): ''' shell(store, options, jugspace) Implement 'shell' command. Currently depends on Ipython being installed. ''' try: import IPython if IPython.version_info[0] >= 1: from IPython.terminal.embed import InteractiveShellEmbed from IPython.terminal.ipapp import load_default_config else: from IPython.frontend.terminal.embed import InteractiveShellEmbed from IPython.frontend.terminal.ipapp import load_default_config config = load_default_config() ipshell = InteractiveShellEmbed(config=config, display_banner=_ipython_banner) except ImportError: try: # Fallback for older Python: from IPython.Shell import IPShellEmbed ipshell = IPShellEmbed(banner=_ipython_banner) except ImportError: import sys sys.stderr.write(_ipython_not_found_msg) sys.exit(1) def _load_all(): ''' load_all() Loads all task results. ''' load_all(jugspace, local_ns) local_ns = { 'load_all': _load_all, 'value': value, } # This is necessary for some versions of Ipython. See: # http://groups.google.com/group/pylons-discuss/browse_thread/thread/312e3ead5967468a try: del jugspace['__builtins__'] except KeyError: pass jugspace.update(local_ns) local_ns['__name__'] = '__jugfile__' ipshell(global_ns=jugspace, local_ns=local_ns)
def start_python_console(user_ns: dict = None, banner: str = ""): """ Start a python interactive terminal :param user_ns: :param banner: :return: """ config = load_default_config() InteractiveShellEmbed.clear_instance() python_shell = InteractiveShellEmbed.instance(banner1=banner, user_ns=user_ns, config=config) python_shell()
def embed(**kwargs): """ Copied from `IPython/terminal/embed.py`, but using our `InteractiveShellEmbed` instead. """ config = kwargs.get("config") header = kwargs.pop("header", "") compile_flags = kwargs.pop("compile_flags", None) if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs["config"] = config shell = InteractiveShellEmbed.instance(**kwargs) initialize_extensions(shell, config["InteractiveShellApp"]["extensions"]) shell(header=header, stack_depth=2, compile_flags=compile_flags)
def shell(store, options, jugspace): ''' shell(store, options, jugspace) Implement 'shell' command. Currently depends on Ipython being installed. ''' try: import IPython if IPython.version_info[0]>=1: from IPython.terminal.embed import InteractiveShellEmbed from IPython.terminal.ipapp import load_default_config else: from IPython.frontend.terminal.embed import InteractiveShellEmbed from IPython.frontend.terminal.ipapp import load_default_config config = load_default_config() ipshell = InteractiveShellEmbed(config=config, display_banner=_ipython_banner) except ImportError: try: # Fallback for older Python: from IPython.Shell import IPShellEmbed ipshell = IPShellEmbed(banner=_ipython_banner) except ImportError: import sys sys.stderr.write(_ipython_not_found_msg) sys.exit(1) def _load_all(): ''' load_all() Loads all task results. ''' load_all(jugspace, local_ns) local_ns = { 'load_all' : _load_all, 'value' : value, } # This is necessary for some versions of Ipython. See: # http://groups.google.com/group/pylons-discuss/browse_thread/thread/312e3ead5967468a try: del jugspace['__builtins__'] except KeyError: pass jugspace.update(local_ns) local_ns['__name__'] = '__jugfile__' ipshell(global_ns=jugspace, local_ns=local_ns)
def embed(env, header): """ Embed into shell Just a simple wrapper around IPython embed with fallback to Python's interact """ try: from IPython import embed try: from IPython.terminal.ipapp import load_default_config ip_config = load_default_config() except: # TODO: handle correct exception? ip_config = None embed(user_ns=env, header=header, config=ip_config) except ImportError: from code import interact interact(local=env, banner=header)
def _my_embed(**kwargs): """ Since we need to control the stack_depth, the only way is to copy-and-paste the implementation and change the stack_depth argument :( """ config = kwargs.get('config') header = kwargs.pop('header', u'') stack_depth = kwargs.pop('stack_depth', 2) if config is None: config = ipapp.load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs['config'] = config global _embedded_shell if _embedded_shell is None: _embedded_shell = embed.InteractiveShellEmbed(**kwargs) _embedded_shell(header=header, stack_depth=stack_depth)
def _embed_ipython(self, load_extension=None, kwargs=None): from IPython.terminal.ipapp import load_default_config from IPython.terminal.embed import InteractiveShellEmbed kwargs = kwargs or {} config = kwargs.get('config') header = kwargs.pop('header', u'') compile_flags = kwargs.pop('compile_flags', None) if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs['config'] = config kwargs.setdefault('display_banner', False) self._ishell = InteractiveShellEmbed.instance(**kwargs) if load_extension: load_extension(self._ishell) # Stack depth is 3 because we use self.embed first self._ishell(header=header, stack_depth=3, compile_flags=compile_flags) return self._ishell
def start(self) -> None: try: from ptpython.ipython import embed from ptpython.repl import run_config from IPython.terminal.ipapp import load_default_config except ImportError: raise ShellNotAvailableError("PtIPython shell not available.") config_dir = Path("~/.ptpython/").expanduser() # Apply config file def configure(repl): path = config_dir / "config.py" if path.exists(): run_config(repl, str(path)) # Startup path startup_paths = [] if "PYTHONSTARTUP" in os.environ: startup_paths.append(os.environ["PYTHONSTARTUP"]) # exec scripts from startup paths for path in startup_paths: if Path(path).exists(): with Path(path).open("rb") as f: code = compile(f.read(), path, "exec") exec(code, self.context, self.context) else: print(f"File not found: {path}\n\n") sys.exit(1) ipy_config = load_default_config() ipy_config.InteractiveShellEmbed = ipy_config.TerminalInteractiveShell ipy_config["InteractiveShellApp"]["extensions"] = self.ipy_extensions configure_ipython_prompt(ipy_config, prompt=self.prompt, output=self.output) embed( config=ipy_config, configure=configure, history_filename=config_dir / "history", user_ns=self.context, header=self.banner, vi_mode=self.ptpy_vi_mode, ) return None
def IPS(): # let the user know, where this shell is 'waking up' # construct frame list # this will be printed in the header frame_info_list = [] frame_list = [] frame = inspect.currentframe() while not frame == None: frame_list.append(frame) info = inspect.getframeinfo(frame) frame_info_list.append(info) frame = frame.f_back frame_info_list.reverse() frame_list.reverse() frame_info_str_list = [format_frameinfo(fi) for fi in frame_info_list] custom_header1 = "----- frame list -----\n\n" frame_info_str = "\n--\n".join(frame_info_str_list[:-1]) custom_header2 = "\n----- end of frame list -----\n" custom_header = "{0}{1}{2}".format(custom_header1, frame_info_str, custom_header2) # prevent IPython shell to be launched in IP-Notebook test_str = str(frame_info_list[0]) + str(frame_info_list[1]) # print test_str if "IPython" in test_str and "zmq" in test_str: print "\n- Not entering IPython embedded shell -\n" return # copied (and modified) from IPython/terminal/embed.py config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell # these two lines prevent problems in related to the initialization # of ultratb.FormattedTB below InteractiveShellEmbed.clear_instance() InteractiveShellEmbed._instance = None shell = InteractiveShellEmbed.instance() shell(header=custom_header, stack_depth=2)
def start_python_console(namespace=None, noipython=False, banner=''): """Start Python console bound to the given namespace. If IPython is available, an IPython console will be started instead, unless `noipython` is True. Also, tab completion will be used on Unix systems. """ if namespace is None: namespace = {} try: try: # use IPython if available if noipython: raise ImportError() try: from IPython.terminal.embed import InteractiveShellEmbed from IPython.terminal.ipapp import load_default_config except ImportError: from IPython.frontend.terminal.embed import InteractiveShellEmbed from IPython.frontend.terminal.ipapp import load_default_config config = load_default_config() shell = InteractiveShellEmbed( banner1=banner, user_ns=namespace, config=config) shell() except ImportError: import code try: # readline module is only available on unix systems import readline except ImportError: pass else: pass # import rlcompleter readline.parse_and_bind("tab:complete") code.interact(banner=banner, local=namespace) except SystemExit: # raised when using exit() in python code.interact pass
def embed(**kwargs): stack_depth = kwargs.pop('stack_depth', 2) force_active = kwargs.pop('force_active', False) ex_data = kwargs.pop('ex_data', None) config = kwargs.get('config') header = kwargs.pop('header', u'') if config is None: config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell kwargs['config'] = config if "display_banner" not in kwargs: kwargs["display_banner"] = False ex_cls = kwargs.pop("ex_cls", None) ex = kwargs.pop("ex", None) if "frames" in kwargs: frames = kwargs.pop("frames") else: # Get from local frames = [] i = 0 while True: try: frames.append(sys._getframe(i).f_back) i += 1 except ValueError: break #frames = frames[-2::-1] frames = frames[:-1] frames = frames[::-1] tb = kwargs.pop("tb", None) old_excepthool = sys.excepthook shell = MyInteractiveShellEmbed.instance(**kwargs) if force_active: shell.embedded_active = True shell(frames=frames, ex_cls=ex_cls, ex=ex, tb=tb, header=header, stack_depth=stack_depth) sys.excepthook = old_excepthool
def shell(ipython_args): """Runs a shell in the app context. Runs an interactive Python shell in the context of a given Flask application. The application will populate the default namespace of this shell according to it's configuration. This is useful for executing small snippets of management code without having to manually configuring the application. """ import IPython from IPython.terminal.ipapp import load_default_config from traitlets.config.loader import Config from flask.globals import _app_ctx_stack app = _app_ctx_stack.top.app if 'IPYTHON_CONFIG' in app.config: config = Config(app.config['IPYTHON_CONFIG']) else: config = load_default_config() config.TerminalInteractiveShell.banner1 = '''Python %s on %s IPython: %s App: %s [%s] Instance: %s''' % (sys.version, sys.platform, IPython.__version__, app.import_name, app.env, app.instance_path) IPython.start_ipython( argv=ipython_args, user_ns=app.make_shell_context(), config=config, )
def execution_loop(tasks, options): from time import sleep logging.info('Execute start (%s tasks)' % len(tasks)) # For the special (but common) case where most (if not all) of the tasks # can be loaded directly, just skip them as fast as possible: first_unloadable = 0 while (first_unloadable < len(tasks)) and tasks[first_unloadable].can_load(): t = tasks[first_unloadable] jug_hook('execute.task-loadable', (tasks[first_unloadable],)) first_unloadable += 1 del tasks[:first_unloadable] while tasks: upnext = [] # tasks that can be run nr_wait_cycles = int(options.execute_nr_wait_cycles) for i in range(nr_wait_cycles): max_cannot_run = min(len(tasks), 128) if i == nr_wait_cycles - 1: # in the last loop iteration, check all tasks to ensure we don't miss any max_cannot_run = len(tasks) for i in range(max_cannot_run): # The argument for this is the following: # if T' is dependent on the result of T, it is better if the # processor that ran T, also runs T'. By having everyone else # push T' to the end of tasks, this is more likely to happen. # # Furthermore, this avoids always querying the same tasks. if tasks[0].can_run(): break tasks.append(tasks.pop(0)) while tasks and tasks[0].can_run(): upnext.append(tasks.pop(0)) if upnext: break for ti,t in enumerate(tasks): if t.can_run(): upnext.append(tasks.pop(ti)) break if upnext: break logging.info('waiting %s secs for an open task...' % options.execute_wait_cycle_time_secs) sleep(int(options.execute_wait_cycle_time_secs)) if not upnext: logging.info('No tasks can be run!') break for t in upnext: if t.can_load(): jug_hook('execute.task-loadable', (t,)) continue locked = False try: locked = t.lock() if t.can_load(): # This can be true if the task ran between the check above and this one jug_hook('execute.task-loadable', (t,)) elif locked: logging.info('Executing %s...' % t.name) jug_hook('execute.task-pre-execute', (t,)) t.run(debug_mode=options.debug) jug_hook('execute.task-executed1', (t,)) else: logging.info('Already in execution %s...' % t.name) except SystemExit: raise except Exception as e: if options.pdb: import sys _,_, tb = sys.exc_info() # The code below is a complex attempt to load IPython # debugger which works with multiple versions of IPython. # # Unfortunately, their API kept changing prior to the 1.0. try: import IPython try: import IPython.core.debugger try: from IPython.terminal.ipapp import load_default_config config = load_default_config() colors = config.TerminalInteractiveShell.colors except: import IPython.core.ipapi ip = IPython.core.ipapi.get() colors = ip.colors try: debugger = IPython.core.debugger.Pdb(colors.get_value(initial='Linux')) except AttributeError: debugger = IPython.core.debugger.Pdb(colors) except ImportError: #Fallback to older version of IPython API import IPython.ipapi import IPython.Debugger shell = IPython.Shell.IPShell(argv=['']) ip = IPython.ipapi.get() debugger = IPython.Debugger.Pdb(ip.options.colors) except ImportError: #Fallback to standard debugger import pdb debugger = pdb.Pdb() debugger.reset() debugger.interaction(None, tb) else: import itertools logging.critical('Exception while running %s: %s' % (t.name,e)) for other in itertools.chain(upnext, tasks): for dep in other.dependencies(): if dep is t: logging.critical('Other tasks are dependent on this one! Parallel processors will be held waiting!') if not options.execute_keep_going: raise finally: if locked: t.unlock()
def wrapper(namespace=namespace, banner=''): config = load_default_config() shell = InteractiveShellEmbed( banner1=banner, user_ns=namespace, config=config) shell()
def run(self, store, options, jugspace, *args, **kwargs): try: import IPython if IPython.version_info[0] >= 1: from IPython.terminal.embed import InteractiveShellEmbed from IPython.terminal.ipapp import load_default_config else: from IPython.frontend.terminal.embed import InteractiveShellEmbed from IPython.frontend.terminal.ipapp import load_default_config config = load_default_config() ipshell = InteractiveShellEmbed(config=config, display_banner=_ipython_banner) except ImportError: try: # Fallback for older Python: from IPython.Shell import IPShellEmbed ipshell = IPShellEmbed(banner=_ipython_banner) except ImportError: import sys sys.stderr.write(_ipython_not_found_msg) sys.exit(1) def _load_all(): ''' load_all() Loads all task results. ''' load_all(jugspace, local_ns) reverse_cache = {} def _invalidate(t): '''Recursively invalidates its argument, i.e. removes from the store results of any task which may (even indirectly) depend on its argument. This is analogous to the ``jug invalidate`` subcommand. Parameters ---------- t : a Task Returns ------- None ''' from ..task import alltasks return invalidate(alltasks, reverse_cache, t) def _get_tasks(): '''Returns a list of all tasks seen by jug ''' from ..task import alltasks return alltasks local_ns = { 'load_all': _load_all, 'value': value, 'invalidate': _invalidate, 'get_tasks': _get_tasks, } # This is necessary for some versions of Ipython. See: # http://groups.google.com/group/pylons-discuss/browse_thread/thread/312e3ead5967468a try: del jugspace['__builtins__'] except KeyError: pass jugspace.update(local_ns) local_ns['__name__'] = '__jugfile__' if IPython.version_info[0] >= 5: from sys import modules # This is tricky, but the following WOULD NOT work: # for mod in modules.values(): # .. # Some modules use https://pypi.python.org/pypi/apipkg which triggers # name loading when __dict__ is accessed. This may itself import other # modules, thus raising an error: "RuntimeError: dictionary changed # size during iteration" Whether this happens depends on exactly which # modules the user uses/has loaded modules = list(modules.values()) for mod in modules: if getattr(mod, '__dict__', None) is jugspace: break else: raise KeyError("Could not find jug module") ipshell(module=mod, local_ns=local_ns) else: ipshell(global_ns=jugspace, local_ns=local_ns)
pass BdbQuit_excepthook = None try: import bpython has_bpython = True except ImportError: has_bpython = False try: import IPython from IPython.core.debugger import BdbQuit_excepthook from IPython.core import page from IPython.terminal.ipapp import load_default_config from IPython.core.magic import (magics_class, line_magic, Magics) ipython_config = load_default_config() ipython_config.TerminalInteractiveShell.confirm_exit = False old_init = IPython.terminal.embed.InteractiveShellEmbed.__init__ def new_init(self, *k, **kw): frames = kw.pop("frames", None) old_init(self, *k, **kw) from pry import get_context, highlight, terminal_size @magics_class class MyMagics(Magics): def __init__(self, shell, frames): # You must call the parent constructor super(MyMagics, self).__init__(shell)
import sys sys.meta_path.append(id) # in case PySide is installed, hide it to prevent conflicts between it and PyQt4 in IPython: disable('PySide') import os import sys import platform # instantiate an IPython embedded shell which shows up in the terminal on demand # and on every exception in the GUI code: import IPython from IPython.terminal.ipapp import load_default_config from IPython.terminal.embed import InteractiveShellEmbed config = load_default_config() # automatically call the pdb debugger after every exception, override default config: config.TerminalInteractiveShell.pdb = True ipshell = InteractiveShellEmbed(display_banner=False, config=config) # IPython GUI imports, have to come before Qt imports: from qtconsole.rich_jupyter_widget import RichJupyterWidget from IPython.lib import guisupport from IPython.paths import get_ipython_dir from traitlets.config.loader import PyFileConfigLoader from qtconsole.qt import QtCore, QtGui from PyQt4 import uic NeuropyUi, NeuropyUiBase = uic.loadUiType('neuropy.ui') from __init__ import __version__
def IPS(copy_namespaces=True, overwrite_globals=False): """Starts IPython embedded shell. This is similar to IPython.embed() but with some additional features: 1. Print a list of the calling frames before entering the prompt 2. (optionally) copy local name space to global one to prevent certain IPython bug. 3. while doing so optinally overwrite names in the global namespace """ # let the user know, where this shell is 'waking up' # construct frame list # this will be printed in the header frame_info_list = [] frame_list = [] frame = inspect.currentframe() while frame is not None: frame_list.append(frame) info = inspect.getframeinfo(frame) frame_info_list.append(info) frame = frame.f_back frame_info_list.reverse() frame_list.reverse() frame_info_str_list = [format_frameinfo(fi) for fi in frame_info_list] custom_header1 = "----- frame list -----\n\n" frame_info_str = "\n--\n".join(frame_info_str_list[:-1]) custom_header2 = "\n----- end of frame list -----\n" custom_header = "{0}{1}{2}".format(custom_header1, frame_info_str, custom_header2) # prevent IPython shell to be launched in IP-Notebook test_str = str(frame_info_list[0]) + str(frame_info_list[1]) if 'IPython' in test_str and 'zmq' in test_str: print("\n- Not entering IPython embedded shell -\n") return # copied (and modified) from IPython/terminal/embed.py config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell # these two lines prevent problems related to the initialization # of ultratb.FormattedTB below InteractiveShellEmbed.clear_instance() InteractiveShellEmbed._instance = None shell = InteractiveShellEmbed.instance() # achieve that custom macros are loade in interactive shell shell.magic('load_ext storemagic') if config.StoreMagics.autorestore: shell.magic('store -r') ar_keys = [k.split("/")[-1] for k in shell.db.keys() if k.startswith("autorestore/")] else: ar_keys = [] # adapt the namespaces to prevent missing names inside the shell # see: https://github.com/ipython/ipython/issues/62 # https://github.com/ipython/ipython/issues/10695 if copy_namespaces and len(frame_list) >= 2: # callers_frame to IPS() # note that frame_list and frame_info_list were reversed above f1 = frame_list[-2] lns = f1.f_locals gns = f1.f_globals l_keys = set(lns) g_keys = set(gns) u_keys = shell.user_ns.keys() # those keys which are in local ns but not in global safe_keys = l_keys - g_keys unsafe_keys = l_keys.intersection(g_keys) assert safe_keys.union(unsafe_keys) == l_keys gns.update({k:lns[k] for k in safe_keys}) if unsafe_keys and not overwrite_globals: custom_header += "following local keys have " \ "not been copied:\n{}\n".format(unsafe_keys) if unsafe_keys and overwrite_globals: gns.update({k:lns[k] for k in unsafe_keys}) custom_header += "following global keys have " \ "been overwritten:\n{}\n".format(unsafe_keys) # now update the gns with stuff from the user_ns (if it will not overwrite anything) # this could be implemented cleaner for k in ar_keys: if k not in gns: gns[k] = shell.user_ns[k] else: print("omitting key from user_namespace:", k) dummy_module = DummyMod() dummy_module.__dict__ = gns else: # unexpected few frames or no copying desired: lns = None dummy_module = None # now execute the shell shell(header=custom_header, stack_depth=2, local_ns=lns, module=dummy_module) custom_excepthook = getattr(sys, 'custom_excepthook', None) if custom_excepthook is not None: assert callable(custom_excepthook) sys.excepthook = custom_excepthook
def ip_shell_after_exception(frame): """ Launches an IPython embedded shell in the namespace where an exception occurred. :param frame: :return: """ # let the user know, where this shell is 'waking up' # construct frame list # this will be printed in the header frame_info_list = [] frame_list = [] original_frame = frame = frame or inspect.currentframe() local_ns = frame.f_locals # global_ns = frame.f_globals # this is deprecated by IPython dummy_module = DummyMod() dummy_module.__dict__ = frame.f_globals while frame is not None: frame_list.append(frame) info = inspect.getframeinfo(frame) frame_info_list.append(info) frame = frame.f_back frame_info_list.reverse() frame_info_str_list = [format_frameinfo(fi) for fi in frame_info_list] custom_header1 = "----- frame list -----\n\n" frame_info_str = "\n--\n".join(frame_info_str_list[:-1]) custom_header2 = "\n----- ERROR -----\n" custom_header = "{0}{1}{2}".format(custom_header1, frame_info_str, custom_header2) # prevent IPython shell to be launched in IP-Notebook if len(frame_info_list) >= 2: test_str = str(frame_info_list[0]) + str(frame_info_list[1]) if 'IPython' in test_str and 'zmq' in test_str: print("\n- Not entering IPython embedded shell -\n") return # copied (and modified) from IPython/terminal/embed.py config = load_default_config() config.InteractiveShellEmbed = config.TerminalInteractiveShell # these two lines prevent problems in related to the initialization # of ultratb.FormattedTB below InteractiveShellEmbedWithoutBanner.clear_instance() InteractiveShellEmbedWithoutBanner._instance = None shell = InteractiveShellEmbedWithoutBanner.instance() shell(header=custom_header, stack_depth=2, local_ns=local_ns, module=dummy_module) # if `diff_index` is not None it will be interpreted as index increment for the frame_list in the except hook # "__mu" means "move up" diff_index = local_ns.get("__mu") if not isinstance(diff_index, int): diff_index = None return diff_index
def execution_loop(tasks, options, tasks_executed, tasks_loaded): from time import sleep logging.info('Execute start (%s tasks)' % len(tasks)) while tasks: upnext = [] # tasks that can be run for i in range(int(options.execute_nr_wait_cycles)): max_cannot_run = min(len(tasks), 128) for i in range(max_cannot_run): # The argument for this is the following: # if T' is dependent on the result of T, it is better if the # processor that ran T, also runs T'. By having everyone else # push T' to the end of tasks, this is more likely to happen. # # Furthermore, this avoids always querying the same tasks. if tasks[0].can_run(): break tasks.append(tasks.pop(0)) while tasks and tasks[0].can_run(): upnext.append(tasks.pop(0)) if upnext: break for ti,t in enumerate(tasks): if t.can_run(): upnext.append(tasks.pop(ti)) break if upnext: break logging.info('waiting %s secs for an open task...' % options.execute_wait_cycle_time_secs) sleep(int(options.execute_wait_cycle_time_secs)) if not upnext: logging.info('No tasks can be run!') break for t in upnext: if t.can_load(): logging.info('Loadable %s...' % t.name) tasks_loaded[t.name] += 1 continue locked = False try: locked = t.lock() if t.can_load(): # This can be true if the task ran between the check above and this one logging.info('Loadable %s...' % t.name) tasks_loaded[t.name] += 1 elif locked: logging.info('Executing %s...' % t.name) t.run(debug_mode=options.debug) tasks_executed[t.name] += 1 if options.aggressive_unload: t.unload_recursive() else: logging.info('Already in execution %s...' % t.name) except Exception as e: if options.pdb: import sys _,_, tb = sys.exc_info() # The code below is a complex attempt to load IPython # debugger which works with multiple versions of IPython. # # Unfortunately, their API kept changing prior to the 1.0. try: import IPython try: import IPython.core.debugger try: from IPython.terminal.ipapp import load_default_config config = load_default_config() colors = config.TerminalInteractiveShell.colors except: import IPython.core.ipapi ip = IPython.core.ipapi.get() colors = ip.colors debugger = IPython.core.debugger.Pdb(colors) except ImportError: #Fallback to older version of IPython API import IPython.ipapi import IPython.Debugger shell = IPython.Shell.IPShell(argv=['']) ip = IPython.ipapi.get() debugger=IPythong.Debugger.Pdb(ip.options.colors) except ImportError: #Fallback to standard debugger import pdb debugger = pdb.Pdb() debugger.reset() debugger.interaction(None, tb) else: import itertools logging.critical('Exception while running %s: %s' % (t.name,e)) for other in itertools.chain(upnext, tasks): for dep in other.dependencies(): if dep is t: logging.critical('Other tasks are dependent on this one! Parallel processors will be held waiting!') if not options.execute_keep_going: raise finally: if locked: t.unlock()