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

Method run_cell_magic

IPython/core/interactiveshell.py:2554–2603  ·  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

(self, magic_name, line, cell)

Source from the content-addressed store, hash-verified

2552 return sys._getframe(stack_depth + 1).f_locals
2553
2554 def run_cell_magic(self, magic_name, line, cell):
2555 """Execute the given cell magic.
2556
2557 Parameters
2558 ----------
2559 magic_name : str
2560 Name of the desired magic function, without '%' prefix.
2561 line : str
2562 The rest of the first input line as a single string.
2563 cell : str
2564 The body of the cell as a (possibly multiline) string.
2565 """
2566 fn = self._find_with_lazy_load("cell", magic_name)
2567 if fn is None:
2568 lm = self.find_line_magic(magic_name)
2569 etpl = "Cell magic `%%{0}` not found{1}."
2570 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2571 'did you mean that instead?)'.format(magic_name))
2572 raise UsageError(etpl.format(magic_name, extra))
2573 elif cell == '':
2574 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2575 if self.find_line_magic(magic_name) is not None:
2576 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2577 raise UsageError(message)
2578 else:
2579 # Note: this is the distance in the stack to the user's frame.
2580 # This will need to be updated if the internal calling logic gets
2581 # refactored, or else we'll be expanding the wrong variables.
2582 stack_depth = 2
2583 if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False):
2584 # magic has opted out of var_expand
2585 magic_arg_s = line
2586 else:
2587 magic_arg_s = self.var_expand(line, stack_depth)
2588 kwargs = {}
2589 if getattr(fn, "needs_local_scope", False):
2590 kwargs['local_ns'] = self.user_ns
2591
2592 with self.builtin_trap:
2593 args = (magic_arg_s, cell)
2594 result = fn(*args, **kwargs)
2595
2596 # The code below prevents the output from being displayed
2597 # when using magics with decorator @output_can_be_silenced
2598 # when the last Python token in the expression is a ';'.
2599 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):
2600 if DisplayHook.semicolon_at_end_of_expression(cell):
2601 return None
2602
2603 return result
2604
2605 def find_line_magic(self, magic_name):
2606 """Find and return a line magic by name.

Callers 15

test_prun_special_syntaxFunction · 0.80
check_identFunction · 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

Calls 6

_find_with_lazy_loadMethod · 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_identFunction · 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