##// END OF EJS Templates
show UsageError for cell magics mid-cell
MinRK -
Show More
@@ -3,6 +3,7 b' import functools'
3 3 import re
4 4 from StringIO import StringIO
5 5
6 from IPython.core.error import UsageError
6 7 from IPython.core.splitinput import LineInfo
7 8 from IPython.utils import tokenize2
8 9 from IPython.utils.openpy import cookie_comment_re
@@ -227,9 +228,20 b' def _tr_help(line_info):'
227 228 def _tr_magic(line_info):
228 229 "Translate lines escaped with: %"
229 230 tpl = '%sget_ipython().magic(%r)'
231 if line_info.line.startswith(ESC_MAGIC2):
232 return line_info.line
230 233 cmd = ' '.join([line_info.ifun, line_info.the_rest]).strip()
231 234 return tpl % (line_info.pre, cmd)
232 235
236 def _tr_cellmagic(line_info):
237 """Translate lines escaped with %%
238
239 Only happens when cell magics are mid-cell, which is an error.
240 """
241 raise UsageError("Cannot call cell magics (%s%s) mid-cell" %
242 (ESC_MAGIC2, line_info.ifun)
243 )
244
233 245 def _tr_quote(line_info):
234 246 "Translate lines escaped with: ,"
235 247 return '%s%s("%s")' % (line_info.pre, line_info.ifun,
@@ -250,6 +262,7 b' tr = { ESC_SHELL : _tr_system,'
250 262 ESC_HELP : _tr_help,
251 263 ESC_HELP2 : _tr_help,
252 264 ESC_MAGIC : _tr_magic,
265 ESC_MAGIC2 : _tr_cellmagic,
253 266 ESC_QUOTE : _tr_quote,
254 267 ESC_QUOTE2 : _tr_quote2,
255 268 ESC_PAREN : _tr_paren }
@@ -2593,10 +2593,14 b' class InteractiveShell(SingletonConfigurable):'
2593 2593
2594 2594 if silent:
2595 2595 store_history = False
2596
2597 self.input_transformer_manager.push(raw_cell)
2598 cell = self.input_transformer_manager.source_reset()
2599
2596
2597 try:
2598 self.input_transformer_manager.push(raw_cell)
2599 cell = self.input_transformer_manager.source_reset()
2600 except UsageError:
2601 self.showtraceback()
2602 return
2603
2600 2604 # Our own compiler remembers the __future__ environment. If we want to
2601 2605 # run code with a separate __future__ environment, use the default
2602 2606 # compiler
@@ -43,7 +43,7 b' from IPython.utils.encoding import get_stream_enc'
43 43
44 44 line_split = re.compile("""
45 45 ^(\s*) # any leading space
46 ([,;/%]|!!?|\?\??)? # escape character or characters
46 ([,;/]|%%?|!!?|\?\??)? # escape character or characters
47 47 \s*(%{0,2}[\w\.\*]*) # function/method, possibly with leading %
48 48 # to correctly treat things like '?%magic'
49 49 (.*?$|$) # rest of line
General Comments 0
You need to be logged in to leave comments. Login now