From 8f249fb7a8f3669d6095dcb6cf39543abb51d064 2011-04-07 20:44:15 From: Thomas Kluyver Date: 2011-04-07 20:44:15 Subject: [PATCH] Simplify - call ast.parse inline, rather than via a separate method. --- diff --git a/IPython/core/inputsplitter.py b/IPython/core/inputsplitter.py index 8351a71..48e5b35 100644 --- a/IPython/core/inputsplitter.py +++ b/IPython/core/inputsplitter.py @@ -446,11 +446,11 @@ class InputSplitter(object): return False else: try: - nodes = self.ast_nodes() + code_ast = ast.parse(u''.join(self._buffer)) except Exception: return False else: - if len(nodes) == 1: + if len(code_ast.body) == 1: return False # When input is complete, then termination is marked by an extra blank @@ -539,23 +539,6 @@ class InputSplitter(object): # python syntax, feed it back a second time through the AST-based # splitter, which is more accurate than ours. return split_blocks(''.join(blocks)) - - def ast_nodes(self, lines=None): - """Turn the lines into a list of AST nodes. - - Parameters - ---------- - lines : str - A (possibly multiline) string of Python code. If None (default), it - will use the InputSplitter's current code buffer. - - Returns - ------- - A list of AST (abstract syntax tree) nodes representing the code. - """ - if lines is None: - lines = u"".join(self._buffer) - return ast.parse(lines).body #------------------------------------------------------------------------ # Private interface diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index bea2593..62d46a9 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2125,7 +2125,7 @@ class InteractiveShell(Configurable, Magic): with self.display_trap: try: - nodes = self.input_splitter.ast_nodes(cell) + code_ast = ast.parse(cell) except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): # Case 1 self.showsyntaxerror(filename) @@ -2135,7 +2135,7 @@ class InteractiveShell(Configurable, Magic): if len(cell.splitlines()) == 1: interactivity = 2 # Single line; run fully interactive - self.run_ast_nodes(nodes, interactivity) + self.run_ast_nodes(code_ast.body, interactivity) if store_history: # Write output to the database. Does nothing unless