Permalink
Browse files

New ast_nodes method, and change push_accepts_more to use it.

  • Loading branch information...
takluyver committed Apr 3, 2011
1 parent b6d30f0 commit 35a008f52e81b168ce8a9fc921bbdea76822612f
Showing with 23 additions and 2 deletions.
  1. +23 −2 IPython/core/inputsplitter.py
@@ -445,9 +445,13 @@ def push_accepts_more(self):
if not self._full_dedent:
return False
else:
- nblocks = len(split_blocks(''.join(self._buffer)))
- if nblocks==1:
+ try:
+ nodes = self.ast_nodes()
+ except Exception:
return False
+ else:
+ if len(nodes) == 1:
+ return False
# When input is complete, then termination is marked by an extra blank
# line at the end.
@@ -535,6 +539,23 @@ def split_blocks(self, lines):
# 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

0 comments on commit 35a008f

Please sign in to comment.