Example #1
0
def bind_kernel(**kwargs):
    """Bind an Engine's Kernel to be used as a full IPython kernel.
    
    This allows a running Engine to be used simultaneously as a full IPython kernel
    with the QtConsole or other frontends.
    
    This function returns immediately.
    """
    from IPython.kernel.zmq.kernelapp import IPKernelApp
    from ipython_parallel.apps.ipengineapp import IPEngineApp

    # first check for IPKernelApp, in which case this should be a no-op
    # because there is already a bound kernel
    if IPKernelApp.initialized() and isinstance(IPKernelApp._instance,
                                                IPKernelApp):
        return

    if IPEngineApp.initialized():
        try:
            app = IPEngineApp.instance()
        except MultipleInstanceError:
            pass
        else:
            return app.bind_kernel(**kwargs)

    raise RuntimeError("bind_kernel be called from an IPEngineApp instance")
Example #2
0
def bind_kernel(**kwargs):
    """Bind an Engine's Kernel to be used as a full IPython kernel.

    This allows a running Engine to be used simultaneously as a full IPython kernel
    with the QtConsole or other frontends.

    This function returns immediately.
    """
    from IPython.kernel.zmq.kernelapp import IPKernelApp
    from IPython.parallel.apps.ipengineapp import IPEngineApp

    # first check for IPKernelApp, in which case this should be a no-op
    # because there is already a bound kernel
    if IPKernelApp.initialized() and isinstance(IPKernelApp._instance, IPKernelApp):
        return

    if IPEngineApp.initialized():
        try:
            app = IPEngineApp.instance()
        except MultipleInstanceError:
            pass
        else:
            return app.bind_kernel(**kwargs)

    raise RuntimeError("bind_kernel be called from an IPEngineApp instance")
Example #3
0
def get_connection_file(app=None):
    """Return the path to the connection file of an app
    
    Parameters
    ----------
    app : IPKernelApp instance [optional]
        If unspecified, the currently running app will be used
    """
    if app is None:
        from IPython.kernel.zmq.kernelapp import IPKernelApp
        if not IPKernelApp.initialized():
            raise RuntimeError("app not specified, and not in a running Kernel")

        app = IPKernelApp.instance()
    return filefind(app.connection_file, ['.', app.profile_dir.security_dir])
Example #4
0
def get_connection_file(app=None):
    """Return the path to the connection file of an app
    
    Parameters
    ----------
    app : IPKernelApp instance [optional]
        If unspecified, the currently running app will be used
    """
    if app is None:
        from IPython.kernel.zmq.kernelapp import IPKernelApp
        if not IPKernelApp.initialized():
            raise RuntimeError("app not specified, and not in a running Kernel")

        app = IPKernelApp.instance()
    return filefind(app.connection_file, ['.', app.profile_dir.security_dir])
Example #5
0
    def start(self):
        if self._timer is not None:
            raise Exception("IPython kernel is already running.")

        # The IPKernelApp initialization is based on the IPython source for
        # IPython.embed_kernel available here:
        # https://github.com/ipython/ipython/blob/rel-3.2.1/IPython/kernel/zmq/embed.py

        if IPKernelApp.initialized():
            app = IPKernelApp.instance()
        else:
            app = IPKernelApp.instance(
                outstream_class='ipyida.kernel.IDATeeOutStream'
            )
            app.initialize()

            main = app.kernel.shell._orig_sys_modules_main_mod
            if main is not None:
                sys.modules[app.kernel.shell._orig_sys_modules_main_name] = main
        
            # IPython <= 3.2.x will send exception to sys.__stderr__ instead of
            # sys.stderr. IDA's console will not be able to display exceptions if we
            # don't send it to IDA's sys.stderr. To fix this, we call both the
            # ipython's and IDA's excepthook (IDA's excepthook is actually Python's
            # default).
            sys.excepthook = wrap_excepthook(sys.excepthook)

        # Load the calling scope
        (ida_module, ida_locals) = IPython.utils.frame.extract_module_locals(1)

        if 'idaapi' not in ida_locals:
            raise Exception("{0:s} must be called from idapythonrc.py or "
                            "IDA's prompt.".format("IPythonKernel.start"))

        app.kernel.user_module = ida_module
        app.kernel.user_ns = ida_locals

        app.shell.set_completer_frame()

        app.kernel.start()
        app.kernel.do_one_iteration()
    
        self.connection_file = app.connection_file

        def ipython_kernel_iteration():
            app.kernel.do_one_iteration()
            return int(1000 * app.kernel._poll_interval)
        self._timer = idaapi.register_timer(int(1000 * app.kernel._poll_interval), ipython_kernel_iteration)
def embed_kernel(module=None, local_ns=None, **kwargs):
    """Embed and start an IPython kernel in a given scope.

    Parameters
    ----------
    module : ModuleType, optional
        The module to load into IPython globals (default: caller)
    local_ns : dict, optional
        The namespace to load into IPython user namespace (default: caller)

    kwargs : various, optional
        Further keyword args are relayed to the IPKernelApp constructor,
        allowing configuration of the Kernel.  Will only have an effect
        on the first embed_kernel call for a given process.

    """
    # get the app if it exists, or set it up if it doesn't
    if IPKernelApp.initialized():
        app = IPKernelApp.instance()
    else:
        app = IPKernelApp.instance(**kwargs)
        app.initialize([])
        # Undo unnecessary sys module mangling from init_sys_modules.
        # This would not be necessary if we could prevent it
        # in the first place by using a different InteractiveShell
        # subclass, as in the regular embed case.
        main = app.kernel.shell._orig_sys_modules_main_mod
        if main is not None:
            sys.modules[app.kernel.shell._orig_sys_modules_main_name] = main

    # load the calling scope if not given
    (caller_module, caller_locals) = extract_module_locals(1)
    if module is None:
        module = caller_module
    if local_ns is None:
        local_ns = caller_locals

    app.kernel.user_module = module
    app.kernel.user_ns = local_ns
    # START custom
    if hasattr(app, 'shell') and app.shell:
        app.shell.set_completer_frame()
    # END custom
    app.start()
Example #7
0
    def execute(self, arbiter, props):
        shell = 'kernel-%d.json' % os.getpid()
        msg = None
        try:
            from IPython.kernel.zmq.kernelapp import IPKernelApp
            if not IPKernelApp.initialized():
                app = IPKernelApp.instance()
                app.initialize([])
                main = app.kernel.shell._orig_sys_modules_main_mod
                if main is not None:
                    sys.modules[
                        app.kernel.shell._orig_sys_modules_main_name] = main
                app.kernel.user_module = sys.modules[__name__]
                app.kernel.user_ns = {'arbiter': arbiter}
                app.shell.set_completer_frame()
                app.kernel.start()

        except Exception as e:
            shell = False
            msg = str(e)

        return {'shell': shell, 'msg': msg}
Example #8
0
    def execute(self, arbiter, props):
        shell = 'kernel-%d.json' % os.getpid()
        msg = None
        try:
            from IPython.kernel.zmq.kernelapp import IPKernelApp
            if not IPKernelApp.initialized():
                app = IPKernelApp.instance()
                app.initialize([])
                main = app.kernel.shell._orig_sys_modules_main_mod
                if main is not None:
                    sys.modules[
                        app.kernel.shell._orig_sys_modules_main_name
                    ] = main
                app.kernel.user_module = sys.modules[__name__]
                app.kernel.user_ns = {'arbiter': arbiter}
                app.shell.set_completer_frame()
                app.kernel.start()

        except Exception as e:
            shell = False
            msg = str(e)

        return {'shell': shell, 'msg': msg}