Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove set-next input when triggering help. #13625

Merged
merged 1 commit into from Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 9 additions & 11 deletions IPython/core/inputtransformer.py
Expand Up @@ -193,7 +193,7 @@ def assemble_logical_lines():
line = ''.join(parts)

# Utilities
def _make_help_call(target, esc, lspace, next_input=None):
def _make_help_call(target, esc, lspace):
"""Prepares a pinfo(2)/psearch call from a target name and the escape
(i.e. ? or ??)"""
method = 'pinfo2' if esc == '??' \
Expand All @@ -203,12 +203,13 @@ def _make_help_call(target, esc, lspace, next_input=None):
#Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
t_magic_name, _, t_magic_arg_s = arg.partition(' ')
t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
if next_input is None:
return '%sget_ipython().run_line_magic(%r, %r)' % (lspace, t_magic_name, t_magic_arg_s)
else:
return '%sget_ipython().set_next_input(%r);get_ipython().run_line_magic(%r, %r)' % \
(lspace, next_input, t_magic_name, t_magic_arg_s)

return "%sget_ipython().run_line_magic(%r, %r)" % (
lspace,
t_magic_name,
t_magic_arg_s,
)


# These define the transformations for the different escape characters.
def _tr_system(line_info):
"Translate lines escaped with: !"
Expand Down Expand Up @@ -349,10 +350,7 @@ def help_end(line):
esc = m.group(3)
lspace = _initial_space_re.match(line).group(0)

# If we're mid-command, put it back on the next prompt for the user.
next_input = line.rstrip('?') if line.strip() != m.group(0) else None

return _make_help_call(target, esc, lspace, next_input)
return _make_help_call(target, esc, lspace)


@CoroutineInputTransformer.wrap
Expand Down
16 changes: 4 additions & 12 deletions IPython/core/inputtransformer2.py
Expand Up @@ -325,7 +325,7 @@ def transform(self, lines: List[str]):
ESCAPE_SINGLES = {'!', '?', '%', ',', ';', '/'}
ESCAPE_DOUBLES = {'!!', '??'} # %% (cell magic) is handled separately

def _make_help_call(target, esc, next_input=None):
def _make_help_call(target, esc):
"""Prepares a pinfo(2)/psearch call from a target name and the escape
(i.e. ? or ??)"""
method = 'pinfo2' if esc == '??' \
Expand All @@ -335,11 +335,8 @@ def _make_help_call(target, esc, next_input=None):
#Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
t_magic_name, _, t_magic_arg_s = arg.partition(' ')
t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
if next_input is None:
return 'get_ipython().run_line_magic(%r, %r)' % (t_magic_name, t_magic_arg_s)
else:
return 'get_ipython().set_next_input(%r);get_ipython().run_line_magic(%r, %r)' % \
(next_input, t_magic_name, t_magic_arg_s)
return "get_ipython().run_line_magic(%r, %r)" % (t_magic_name, t_magic_arg_s)


def _tr_help(content):
"""Translate lines escaped with: ?
Expand Down Expand Up @@ -480,13 +477,8 @@ def transform(self, lines):
target = m.group(1)
esc = m.group(3)

# If we're mid-command, put it back on the next prompt for the user.
next_input = None
if (not lines_before) and (not lines_after) \
and content.strip() != m.group(0):
next_input = content.rstrip('?\n')

call = _make_help_call(target, esc, next_input=next_input)
call = _make_help_call(target, esc)
new_line = indent + call + '\n'

return lines_before + [new_line] + lines_after
Expand Down
189 changes: 87 additions & 102 deletions IPython/core/tests/test_inputtransformer.py
Expand Up @@ -59,108 +59,93 @@ def transform_checker(tests, transformer, **kwargs):
('x=1', 'x=1'), # normal input is unmodified
(' ',' '), # blank lines are kept intact
("a, b = %foo", "a, b = get_ipython().run_line_magic('foo', '')"),
],

classic_prompt =
[('>>> x=1', 'x=1'),
('x=1', 'x=1'), # normal input is unmodified
(' ', ' '), # blank lines are kept intact
],

ipy_prompt =
[('In [1]: x=1', 'x=1'),
('x=1', 'x=1'), # normal input is unmodified
(' ',' '), # blank lines are kept intact
],

# Tests for the escape transformer to leave normal code alone
escaped_noesc =
[ (' ', ' '),
('x=1', 'x=1'),
],

# System calls
escaped_shell =
[ ('!ls', "get_ipython().system('ls')"),
# Double-escape shell, this means to capture the output of the
# subprocess and return it
('!!ls', "get_ipython().getoutput('ls')"),
],

# Help/object info
escaped_help =
[ ('?', 'get_ipython().show_usage()'),
('?x1', "get_ipython().run_line_magic('pinfo', 'x1')"),
('??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"),
('?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"),
('?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"),
('?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"),
('?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"),
],

end_help =
[ ('x3?', "get_ipython().run_line_magic('pinfo', 'x3')"),
('x4??', "get_ipython().run_line_magic('pinfo2', 'x4')"),
('%hist1?', "get_ipython().run_line_magic('pinfo', '%hist1')"),
('%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"),
('%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"),
('%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"),
('π.foo?', "get_ipython().run_line_magic('pinfo', 'π.foo')"),
('f*?', "get_ipython().run_line_magic('psearch', 'f*')"),
('ax.*aspe*?', "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"),
('a = abc?', "get_ipython().set_next_input('a = abc');"
"get_ipython().run_line_magic('pinfo', 'abc')"),
('a = abc.qe??', "get_ipython().set_next_input('a = abc.qe');"
"get_ipython().run_line_magic('pinfo2', 'abc.qe')"),
('a = *.items?', "get_ipython().set_next_input('a = *.items');"
"get_ipython().run_line_magic('psearch', '*.items')"),
('plot(a?', "get_ipython().set_next_input('plot(a');"
"get_ipython().run_line_magic('pinfo', 'a')"),
('a*2 #comment?', 'a*2 #comment?'),
],

# Explicit magic calls
escaped_magic =
[ ('%cd', "get_ipython().run_line_magic('cd', '')"),
('%cd /home', "get_ipython().run_line_magic('cd', '/home')"),
# Backslashes need to be escaped.
('%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"),
(' %magic', " get_ipython().run_line_magic('magic', '')"),
],

# Quoting with separate arguments
escaped_quote =
[ (',f', 'f("")'),
(',f x', 'f("x")'),
(' ,f y', ' f("y")'),
(',f a b', 'f("a", "b")'),
],

# Quoting with single argument
escaped_quote2 =
[ (';f', 'f("")'),
(';f x', 'f("x")'),
(' ;f y', ' f("y")'),
(';f a b', 'f("a b")'),
],

# Simply apply parens
escaped_paren =
[ ('/f', 'f()'),
('/f x', 'f(x)'),
(' /f y', ' f(y)'),
('/f a b', 'f(a, b)'),
],

# Check that we transform prompts before other transforms
mixed =
[ ('In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
('>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
('In [2]: !ls', "get_ipython().system('ls')"),
('In [3]: abs?', "get_ipython().run_line_magic('pinfo', 'abs')"),
('In [4]: b = %who', "b = get_ipython().run_line_magic('who', '')"),
],
)
],
classic_prompt=[
(">>> x=1", "x=1"),
("x=1", "x=1"), # normal input is unmodified
(" ", " "), # blank lines are kept intact
],
ipy_prompt=[
("In [1]: x=1", "x=1"),
("x=1", "x=1"), # normal input is unmodified
(" ", " "), # blank lines are kept intact
],
# Tests for the escape transformer to leave normal code alone
escaped_noesc=[
(" ", " "),
("x=1", "x=1"),
],
# System calls
escaped_shell=[
("!ls", "get_ipython().system('ls')"),
# Double-escape shell, this means to capture the output of the
# subprocess and return it
("!!ls", "get_ipython().getoutput('ls')"),
],
# Help/object info
escaped_help=[
("?", "get_ipython().show_usage()"),
("?x1", "get_ipython().run_line_magic('pinfo', 'x1')"),
("??x2", "get_ipython().run_line_magic('pinfo2', 'x2')"),
("?a.*s", "get_ipython().run_line_magic('psearch', 'a.*s')"),
("?%hist1", "get_ipython().run_line_magic('pinfo', '%hist1')"),
("?%%hist2", "get_ipython().run_line_magic('pinfo', '%%hist2')"),
("?abc = qwe", "get_ipython().run_line_magic('pinfo', 'abc')"),
],
end_help=[
("x3?", "get_ipython().run_line_magic('pinfo', 'x3')"),
("x4??", "get_ipython().run_line_magic('pinfo2', 'x4')"),
("%hist1?", "get_ipython().run_line_magic('pinfo', '%hist1')"),
("%hist2??", "get_ipython().run_line_magic('pinfo2', '%hist2')"),
("%%hist3?", "get_ipython().run_line_magic('pinfo', '%%hist3')"),
("%%hist4??", "get_ipython().run_line_magic('pinfo2', '%%hist4')"),
("π.foo?", "get_ipython().run_line_magic('pinfo', 'π.foo')"),
("f*?", "get_ipython().run_line_magic('psearch', 'f*')"),
("ax.*aspe*?", "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"),
("a = abc?", "get_ipython().run_line_magic('pinfo', 'abc')"),
("a = abc.qe??", "get_ipython().run_line_magic('pinfo2', 'abc.qe')"),
("a = *.items?", "get_ipython().run_line_magic('psearch', '*.items')"),
("plot(a?", "get_ipython().run_line_magic('pinfo', 'a')"),
("a*2 #comment?", "a*2 #comment?"),
],
# Explicit magic calls
escaped_magic=[
("%cd", "get_ipython().run_line_magic('cd', '')"),
("%cd /home", "get_ipython().run_line_magic('cd', '/home')"),
# Backslashes need to be escaped.
("%cd C:\\User", "get_ipython().run_line_magic('cd', 'C:\\\\User')"),
(" %magic", " get_ipython().run_line_magic('magic', '')"),
],
# Quoting with separate arguments
escaped_quote=[
(",f", 'f("")'),
(",f x", 'f("x")'),
(" ,f y", ' f("y")'),
(",f a b", 'f("a", "b")'),
],
# Quoting with single argument
escaped_quote2=[
(";f", 'f("")'),
(";f x", 'f("x")'),
(" ;f y", ' f("y")'),
(";f a b", 'f("a b")'),
],
# Simply apply parens
escaped_paren=[
("/f", "f()"),
("/f x", "f(x)"),
(" /f y", " f(y)"),
("/f a b", "f(a, b)"),
],
# Check that we transform prompts before other transforms
mixed=[
("In [1]: %lsmagic", "get_ipython().run_line_magic('lsmagic', '')"),
(">>> %lsmagic", "get_ipython().run_line_magic('lsmagic', '')"),
("In [2]: !ls", "get_ipython().system('ls')"),
("In [3]: abs?", "get_ipython().run_line_magic('pinfo', 'abs')"),
("In [4]: b = %who", "b = get_ipython().run_line_magic('who', '')"),
],
)

# multiline syntax examples. Each of these should be a list of lists, with
# each entry itself having pairs of raw/transformed input. The union (with
Expand Down