MCPcopy Create free account
hub / github.com/ipython/ipython / run_cell_magic

Method run_cell_magic

IPython/core/interactiveshell.py:2339–2382  ·  view source on GitHub ↗

Execute the given cell magic. Parameters ---------- magic_name : str Name of the desired magic function, without '%' prefix. line : str The rest of the first input line as a single string. cell : str The body of the cell as a (

(self, magic_name, line, cell)

Source from the content-addressed store, hash-verified

2337 return sys._getframe(stack_depth + 1).f_locals
2338
2339 def run_cell_magic(self, magic_name, line, cell):
2340 """Execute the given cell magic.
2341
2342 Parameters
2343 ----------
2344 magic_name : str
2345 Name of the desired magic function, without '%' prefix.
2346
2347 line : str
2348 The rest of the first input line as a single string.
2349
2350 cell : str
2351 The body of the cell as a (possibly multiline) string.
2352 """
2353 fn = self.find_cell_magic(magic_name)
2354 if fn is None:
2355 lm = self.find_line_magic(magic_name)
2356 etpl = "Cell magic `%%{0}` not found{1}."
2357 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2358 'did you mean that instead?)'.format(magic_name))
2359 raise UsageError(etpl.format(magic_name, extra))
2360 elif cell == '':
2361 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2362 if self.find_line_magic(magic_name) is not None:
2363 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2364 raise UsageError(message)
2365 else:
2366 # Note: this is the distance in the stack to the user's frame.
2367 # This will need to be updated if the internal calling logic gets
2368 # refactored, or else we'll be expanding the wrong variables.
2369 stack_depth = 2
2370 if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False):
2371 # magic has opted out of var_expand
2372 magic_arg_s = line
2373 else:
2374 magic_arg_s = self.var_expand(line, stack_depth)
2375 kwargs = {}
2376 if getattr(fn, "needs_local_scope", False):
2377 kwargs['local_ns'] = self.user_ns
2378
2379 with self.builtin_trap:
2380 args = (magic_arg_s, cell)
2381 result = fn(*args, **kwargs)
2382 return result
2383
2384 def find_line_magic(self, magic_name):
2385 """Find and return a line magic by name.

Callers 15

test_prun_special_syntaxFunction · 0.80
check_identMethod · 0.80
test_fileFunction · 0.80
test_file_single_quoteFunction · 0.80
test_file_double_quoteFunction · 0.80
test_file_var_expandFunction · 0.80
test_file_unicodeFunction · 0.80
test_file_amendFunction · 0.80
test_file_spacesFunction · 0.80
test_script_outFunction · 0.80

Calls 5

find_cell_magicMethod · 0.95
find_line_magicMethod · 0.95
var_expandMethod · 0.95
UsageErrorClass · 0.90
formatMethod · 0.45

Tested by 15

test_prun_special_syntaxFunction · 0.64
check_identMethod · 0.64
test_fileFunction · 0.64
test_file_single_quoteFunction · 0.64
test_file_double_quoteFunction · 0.64
test_file_var_expandFunction · 0.64
test_file_unicodeFunction · 0.64
test_file_amendFunction · 0.64
test_file_spacesFunction · 0.64
test_script_outFunction · 0.64