##// END OF EJS Templates
Add methods to run an IPython cell based on AST nodes. Have done basic testing manually.
Thomas Kluyver -
Show More
@@ -20,6 +20,7 b' from __future__ import absolute_import'
20 import __builtin__
20 import __builtin__
21 import __future__
21 import __future__
22 import abc
22 import abc
23 import ast
23 import atexit
24 import atexit
24 import codeop
25 import codeop
25 import inspect
26 import inspect
@@ -2098,6 +2099,87 b' class InteractiveShell(Configurable, Magic):'
2098 except:
2099 except:
2099 self.showtraceback()
2100 self.showtraceback()
2100 warn('Unknown failure executing file: <%s>' % fname)
2101 warn('Unknown failure executing file: <%s>' % fname)
2102
2103 def run_cell_NODE(self, cell, store_history=True):
2104 """Run a complete IPython cell.
2105
2106 Parameters
2107 ----------
2108 cell : str
2109 The code (including IPython code such as %magic functions) to run.
2110 store_history : bool
2111 If True, the raw and translated cell will be stored in IPython's
2112 history. For user code calling back into IPython's machinery, this
2113 should be set to False.
2114 """
2115 raw_cell = cell
2116 with self.builtin_trap:
2117 cell = self.prefilter_manager.prefilter_lines(cell)
2118
2119 # Store raw and processed history
2120 if store_history:
2121 self.history_manager.store_inputs(self.execution_count,
2122 cell, raw_cell)
2123
2124 self.logger.log(cell, raw_cell)
2125
2126 with self.display_trap:
2127 try:
2128 nodes = self.input_splitter.ast_nodes(cell)
2129 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2130 # Case 1
2131 self.showsyntaxerror(filename)
2132 return None
2133
2134 interactivity = 1 # Last node to be run interactive
2135 if len(cell.splitlines()) == 1:
2136 interactivity = 2 # Single line; run fully interactive
2137
2138 self.run_ast_nodes(nodes, interactivity)
2139
2140 if store_history:
2141 # Write output to the database. Does nothing unless
2142 # history output logging is enabled.
2143 self.history_manager.store_output(self.execution_count)
2144 # Each cell is a *single* input, regardless of how many lines it has
2145 self.execution_count += 1
2146
2147 def run_ast_nodes(self, nodelist, interactivity=1):
2148 """Run a sequence of AST nodes. The execution mode depends on the
2149 interactivity parameter.
2150
2151 Parameters
2152 ----------
2153 nodelist : list
2154 A sequence of AST nodes to run.
2155 interactivity : int
2156 At 0, all nodes are run in 'exec' mode. At '1', the last node alone
2157 is run in interactive mode (so the result of an expression is shown).
2158 At 2, all nodes are run in interactive mode.
2159 """
2160 if not nodelist:
2161 return
2162
2163 if interactivity == 0:
2164 to_run_exec, to_run_interactive = nodelist, []
2165 elif interactivity == 1:
2166 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2167 else:
2168 to_run_exec, to_run_interactive = [], nodelist
2169
2170 if to_run_exec:
2171 mod = ast.Module(to_run_exec)
2172 name = "<ipython-prompt-%d-exec>" % self.execution_count
2173 self.code_to_run = code = compile(mod, name, "exec")
2174 if self.run_code(code) == 1:
2175 return
2176
2177 if to_run_interactive:
2178 mod = ast.Interactive(to_run_interactive)
2179 name = "<ipython-prompt-%d-interactive>" % self.execution_count
2180 self.code_to_run = code = compile(mod, name, "single")
2181 return self.run_code(code)
2182
2101
2183
2102 def run_cell(self, cell, store_history=True):
2184 def run_cell(self, cell, store_history=True):
2103 """Run the contents of an entire multiline 'cell' of code, and store it
2185 """Run the contents of an entire multiline 'cell' of code, and store it
General Comments 0
You need to be logged in to leave comments. Login now