diff --git a/IPython/core/inputsplitter.py b/IPython/core/inputsplitter.py index 1bd15a6..1e81a90 100644 --- a/IPython/core/inputsplitter.py +++ b/IPython/core/inputsplitter.py @@ -672,15 +672,18 @@ def transform_ipy_prompt(line): return line -def _make_help_call(target, esc, lspace): +def _make_help_call(target, esc, lspace, next_input=None): """Prepares a pinfo(2)/psearch call from a target name and the escape (i.e. ? or ??)""" method = 'pinfo2' if esc == '??' \ else 'psearch' if '*' in target \ else 'pinfo' - tpl = '%sget_ipython().magic(u"%s %s")' - return tpl % (lspace, method, target) + if next_input: + tpl = '%sget_ipython().magic(u"%s %s", next_input=%s)' + return tpl % (lspace, method, target, make_quoted_expr(next_input)) + else: + return '%sget_ipython().magic(u"%s %s")' % (lspace, method, target) _initial_space_re = re.compile(r'\s*') _help_end_re = re.compile(r"""(%? @@ -700,11 +703,9 @@ def transform_help_end(line): newline = _make_help_call(target, esc, lspace) # If we're mid-command, put it back on the next prompt for the user. - if line.strip() != m.group(0): - newline += "; get_ipython().set_next_input(%s)" % \ - make_quoted_expr(line.rstrip('?')) + next_input = line.rstrip('?') if line.strip() != m.group(0) else None - return newline + return _make_help_call(target, esc, lspace, next_input) class EscapedTransformer(object): diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 873465e..73ecc4a 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -1703,7 +1703,8 @@ class InteractiveShell(SingletonConfigurable, Magic): [D:\ipython]|1> _ip.set_next_input("Hello Word") [D:\ipython]|2> Hello Word_ # cursor is here """ - + if isinstance(s, unicode): + s = s.encode(self.stdin_encoding) self.rl_next_input = s # Maybe move this to the terminal subclass? @@ -1841,7 +1842,7 @@ class InteractiveShell(SingletonConfigurable, Magic): from . import history history.init_ipython(self) - def magic(self,arg_s): + def magic(self, arg_s, next_input=None): """Call a magic function by name. Input: a string containing the name of the magic function to call and @@ -1858,6 +1859,11 @@ class InteractiveShell(SingletonConfigurable, Magic): valid Python code you can type at the interpreter, including loops and compound statements. """ + # Allow setting the next input - this is used if the user does `a=abs?`. + # We do this first so that magic functions can override it. + if next_input: + self.set_next_input(next_input) + args = arg_s.split(' ',1) magic_name = args[0] magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) diff --git a/IPython/core/tests/test_inputsplitter.py b/IPython/core/tests/test_inputsplitter.py index 83d0ed7..1b8422a 100644 --- a/IPython/core/tests/test_inputsplitter.py +++ b/IPython/core/tests/test_inputsplitter.py @@ -454,9 +454,9 @@ syntax = \ ('%hist?', 'get_ipython().magic(u"pinfo %hist")'), ('f*?', 'get_ipython().magic(u"psearch f*")'), ('ax.*aspe*?', 'get_ipython().magic(u"psearch ax.*aspe*")'), - ('a = abc?', 'get_ipython().magic(u"pinfo abc"); get_ipython().set_next_input(u"a = abc")'), - ('a = abc.qe??', 'get_ipython().magic(u"pinfo2 abc.qe"); get_ipython().set_next_input(u"a = abc.qe")'), - ('a = *.items?', 'get_ipython().magic(u"psearch *.items"); get_ipython().set_next_input(u"a = *.items")'), + ('a = abc?', 'get_ipython().magic(u"pinfo abc", next_input=u"a = abc")'), + ('a = abc.qe??', 'get_ipython().magic(u"pinfo2 abc.qe", next_input=u"a = abc.qe")'), + ('a = *.items?', 'get_ipython().magic(u"psearch *.items", next_input=u"a = *.items")'), ('a*2 #comment?', 'a*2 #comment?'), ],