##// END OF EJS Templates
Merge pull request #1155 from minrk/postexec...
Merge pull request #1155 from minrk/postexec Be less aggressive in de-registering failing post-execute functions so that normal errors don't disable them permanently. Two changes: 1. Don't unregister failing post-exec callbacks automatically. Instead, print a message regarding the failure, pointing to new `disable_failing_post_execute` trait for skipping failing callbacks. - When this flag is False (the default), failing callbacks will continue to be called. - When True, behavior is unchanged from previous, where callbacks are only allowed to fail once. 2. protect `flush_figures()` post-exec function from user error. Invalid matplotlib data may raise inside `print_figure()`. flush_figures() is a post-exec function, so user errors should not raise. Instead, call `get_ipython().showtraceback()` if called from IPython, raising as before otherwise. closes #1154

File last commit:

r4872:34c10438
r5737:2c683b72 merge
Show More
ext_rescapture.py
59 lines | 1.3 KiB | text/x-python | PythonLexer
# -*- coding: utf-8 -*-
""" IPython extension: new prefilters for output grabbing
Provides
var = %magic blah blah
var = !ls
"""
from IPython.core import ipapi
from IPython.core.error import TryNext
from IPython.utils.text import make_quoted_expr
from IPython.utils.genutils import *
ip = ipapi.get()
import re
def hnd_magic(line,mo):
""" Handle a = %mymagic blah blah """
var = mo.group('varname')
cmd = mo.group('cmd')
expr = make_quoted_expr(cmd)
return itpl('$var = get_ipython().magic($expr)')
def hnd_syscmd(line,mo):
""" Handle a = !ls """
var = mo.group('varname')
cmd = mo.group('cmd')
expr = make_quoted_expr(itpl("sc -l =$cmd"))
return itpl('$var = get_ipython().magic($expr)')
def install_re_handler(pat, hnd):
ip.meta.re_prefilters.append((re.compile(pat), hnd))
def init_handlers():
ip.meta.re_prefilters = []
install_re_handler('(?P<varname>[\w\.]+)\s*=\s*%(?P<cmd>.*)',
hnd_magic
)
install_re_handler('(?P<varname>[\w\.]+)\s*=\s*!(?P<cmd>.*)',
hnd_syscmd
)
init_handlers()
def regex_prefilter_f(self,line):
for pat, handler in ip.meta.re_prefilters:
mo = pat.match(line)
if mo:
return handler(line,mo)
raise TryNext
ip.set_hook('input_prefilter', regex_prefilter_f)