##// END OF EJS Templates
Simplify - call ast.parse inline, rather than via a separate method.
Thomas Kluyver -
Show More
@@ -446,11 +446,11 b' class InputSplitter(object):'
446 return False
446 return False
447 else:
447 else:
448 try:
448 try:
449 nodes = self.ast_nodes()
449 code_ast = ast.parse(u''.join(self._buffer))
450 except Exception:
450 except Exception:
451 return False
451 return False
452 else:
452 else:
453 if len(nodes) == 1:
453 if len(code_ast.body) == 1:
454 return False
454 return False
455
455
456 # When input is complete, then termination is marked by an extra blank
456 # When input is complete, then termination is marked by an extra blank
@@ -539,23 +539,6 b' class InputSplitter(object):'
539 # python syntax, feed it back a second time through the AST-based
539 # python syntax, feed it back a second time through the AST-based
540 # splitter, which is more accurate than ours.
540 # splitter, which is more accurate than ours.
541 return split_blocks(''.join(blocks))
541 return split_blocks(''.join(blocks))
542
543 def ast_nodes(self, lines=None):
544 """Turn the lines into a list of AST nodes.
545
546 Parameters
547 ----------
548 lines : str
549 A (possibly multiline) string of Python code. If None (default), it
550 will use the InputSplitter's current code buffer.
551
552 Returns
553 -------
554 A list of AST (abstract syntax tree) nodes representing the code.
555 """
556 if lines is None:
557 lines = u"".join(self._buffer)
558 return ast.parse(lines).body
559
542
560 #------------------------------------------------------------------------
543 #------------------------------------------------------------------------
561 # Private interface
544 # Private interface
@@ -2125,7 +2125,7 b' class InteractiveShell(Configurable, Magic):'
2125
2125
2126 with self.display_trap:
2126 with self.display_trap:
2127 try:
2127 try:
2128 nodes = self.input_splitter.ast_nodes(cell)
2128 code_ast = ast.parse(cell)
2129 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2129 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2130 # Case 1
2130 # Case 1
2131 self.showsyntaxerror(filename)
2131 self.showsyntaxerror(filename)
@@ -2135,7 +2135,7 b' class InteractiveShell(Configurable, Magic):'
2135 if len(cell.splitlines()) == 1:
2135 if len(cell.splitlines()) == 1:
2136 interactivity = 2 # Single line; run fully interactive
2136 interactivity = 2 # Single line; run fully interactive
2137
2137
2138 self.run_ast_nodes(nodes, interactivity)
2138 self.run_ast_nodes(code_ast.body, interactivity)
2139
2139
2140 if store_history:
2140 if store_history:
2141 # Write output to the database. Does nothing unless
2141 # Write output to the database. Does nothing unless
General Comments 0
You need to be logged in to leave comments. Login now