Refactor paste magics and ipdoctest · seumasmorrison/ipython@801b9cb · GitHub
Skip to content

Commit 801b9cb

Browse files
committed
Refactor paste magics and ipdoctest
1 parent 5cf9891 commit 801b9cb

3 files changed

Lines changed: 15 additions & 71 deletions

File tree

IPython/core/tests/test_magic_terminal.py

Lines changed: 2 additions & 1 deletion

IPython/frontend/terminal/interactiveshell.py

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -83,69 +83,6 @@ class TerminalMagics(Magics):
8383
def __init__(self, shell):
8484
super(TerminalMagics, self).__init__(shell)
8585
self.input_splitter = IPythonInputSplitter()
86-
87-
def cleanup_input(self, block):
88-
"""Apply all possible IPython cleanups to an input block.
89-
90-
This means:
91-
92-
- remove any global leading whitespace (dedent)
93-
- remove any email quotes ('>') if they are present in *all* lines
94-
- apply all static inputsplitter transforms and break into sub-blocks
95-
- apply prefilter() to each sub-block that is a single line.
96-
97-
Parameters
98-
----------
99-
block : str
100-
A possibly multiline input string of code.
101-
102-
Returns
103-
-------
104-
transformed block : str
105-
The input, with all transformations above applied.
106-
"""
107-
# We have to effectively implement client-side the loop that is done by
108-
# the terminal frontend, and furthermore do it on a block that can
109-
# possibly contain multiple statments pasted in one go.
110-
111-
# First, run the input through the block splitting code. We should
112-
# eventually make this a self-contained method in the inputsplitter.
113-
isp = self.input_splitter
114-
isp.reset()
115-
b = textwrap.dedent(block)
116-
117-
# Remove email quotes first. These must be consistently applied to
118-
# *all* lines to be removed
119-
b = strip_email_quotes(b)
120-
121-
# Split the input into independent sub-blocks so we can later do
122-
# prefiltering (which must be done *only* to single-line inputs)
123-
blocks = []
124-
last_block = []
125-
for line in b.splitlines():
126-
isp.push(line)
127-
last_block.append(line)
128-
if not isp.push_accepts_more():
129-
blocks.append(isp.source_reset())
130-
last_block = []
131-
if last_block:
132-
blocks.append('\n'.join(last_block))
133-
134-
# Now, apply prefiltering to any one-line block to match the behavior
135-
# of the interactive terminal
136-
final_blocks = []
137-
for block in blocks:
138-
lines = block.splitlines()
139-
if len(lines) == 1:
140-
final_blocks.append(self.shell.prefilter(lines[0]))
141-
else:
142-
final_blocks.append(block)
143-
144-
# We now have the final version of the input code as a list of blocks,
145-
# with all inputsplitter transformations applied and single-line blocks
146-
# run through prefilter. For further processing, turn into a single
147-
# string as the rest of our apis use string inputs.
148-
return '\n'.join(final_blocks)
14986

15087
def store_or_execute(self, block, name):
15188
""" Execute a block, or store it in a variable, per the user's request.
@@ -155,13 +92,19 @@ def store_or_execute(self, block, name):
15592
self.shell.user_ns[name] = SList(block.splitlines())
15693
print("Block assigned to '%s'" % name)
15794
else:
158-
self.shell.user_ns['pasted_block'] = block
159-
b = self.shell.input_transformer_manager.transform_cell(block)
95+
b = self.preclean_input(block)
96+
self.shell.user_ns['pasted_block'] = b
16097
self.shell.using_paste_magics = True
16198
try:
16299
self.shell.run_cell(b)
163100
finally:
164101
self.shell.using_paste_magics = False
102+
103+
def preclean_input(self, block):
104+
lines = block.splitlines()
105+
while lines and not lines[0].strip():
106+
lines = lines[1:]
107+
return strip_email_quotes('\n'.join(lines))
165108

166109
def rerun_pasted(self, name='pasted_block'):
167110
""" Rerun a previously pasted command.

IPython/testing/plugin/ipdoctest.py

Lines changed: 5 additions & 5 deletions

0 commit comments

Comments
 (0)