MCPcopy Index your code
hub / github.com/ipython/ipython / paste

Method paste

IPython/terminal/magics.py:153–206  ·  view source on GitHub ↗

Paste & execute a pre-formatted code block from clipboard. The text is pulled directly from the clipboard without user intervention and printed back on the screen before execution (unless the -q flag is given to force quiet mode). The block is dedented prior to exec

(self, parameter_s='')

Source from the content-addressed store, hash-verified

151
152 @line_magic
153 def paste(self, parameter_s=''):
154 """Paste & execute a pre-formatted code block from clipboard.
155
156 The text is pulled directly from the clipboard without user
157 intervention and printed back on the screen before execution (unless
158 the -q flag is given to force quiet mode).
159
160 The block is dedented prior to execution to enable execution of method
161 definitions. '>' and '+' characters at the beginning of a line are
162 ignored, to allow pasting directly from e-mails, diff files and
163 doctests (the '...' continuation prompt is also stripped). The
164 executed block is also assigned to variable named 'pasted_block' for
165 later editing with '%edit pasted_block'.
166
167 You can also pass a variable name as an argument, e.g. '%paste foo'.
168 This assigns the pasted block to variable 'foo' as string, without
169 executing it (preceding >>> and + is still stripped).
170
171 Options:
172
173 -r: re-executes the block previously entered by cpaste.
174
175 -q: quiet mode: do not echo the pasted text back to the terminal.
176
177 IPython statements (magics, shell escapes) are not supported (yet).
178
179 See Also
180 --------
181 cpaste : manually paste code into terminal until you mark its end.
182 """
183 opts, name = self.parse_options(parameter_s, 'rq', mode='string')
184 if 'r' in opts:
185 self.rerun_pasted()
186 return
187 try:
188 block = self.shell.hooks.clipboard_get()
189 except TryNext as clipboard_exc:
190 message = getattr(clipboard_exc, 'args')
191 if message:
192 error(message[0])
193 else:
194 error('Could not get text from the clipboard.')
195 return
196 except ClipboardEmpty as e:
197 raise UsageError("The clipboard appears to be empty") from e
198
199 # By default, echo back to terminal unless quiet mode is requested
200 if 'q' not in opts:
201 sys.stdout.write(self.shell.pycolorize(block))
202 if not block.endswith("\n"):
203 sys.stdout.write("\n")
204 sys.stdout.write("## -- End pasted text --\n")
205
206 self.store_or_execute(block, name, store_history=True)
207
208 # Class-level: add a '%cls' magic only on Windows
209 if sys.platform == 'win32':

Callers

nothing calls this directly

Calls 5

rerun_pastedMethod · 0.95
store_or_executeMethod · 0.95
UsageErrorClass · 0.90
parse_optionsMethod · 0.80
writeMethod · 0.45

Tested by

no test coverage detected