0

The way to connect, run, display, show errors over inprocess ipython kernel is

import re, sys, time, os
from StringIO import StringIO
from IPython.kernel.inprocess.blocking import BlockingInProcessKernelClient
from IPython.kernel.inprocess.manager import InProcessKernelManager
from IPython.kernel.inprocess.ipkernel import InProcessKernel
from IPython.core.interactiveshell import InteractiveShell
from IPython.utils.io import capture_output

def decorated_run_code(fn):
    def new_run_code(*args, **kwargs):
        res = fn(*args, **kwargs)
        setattr(args[0], "last_known_outflag", res)
        return res
    return new_run_code
InteractiveShell.run_code = decorated_run_code(InteractiveShell.run_code)    

km = InProcessKernelManager()
km.start_kernel()
kc = BlockingInProcessKernelClient(kernel=km.kernel)
kc.start_channels()
kc.shell_channel.execute('%pylab inline')
kernel = InProcessKernel()    
ip = kernel.shell.get_ipython()

with capture_output() as io:
    ip.run_cell(fail)
print "res", io.stdout
etype, value, tb = kernel.shell._get_exc_info(None)
print kernel.shell.last_known_outflag
if kernel.shell.last_known_outflag: print (etype, "/", value, "/", tb)

I had to decorate run_code to capture its result and set it on self, because _get_exc_info returns the same error until another one occurs, so there was no other way to see if an error occured or not. If anyone has a better method I'd like to hear it.

1 Answer 1

0

Error conditions are much easier to catch in iptyhon 3.0, see here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.