From 63d38bdf6568738837ca94991011ed0a1ffc167e 2010-09-07 09:20:59 From: Fernando Perez Date: 2010-09-07 09:20:59 Subject: [PATCH] Fix bug where 'if 1:' was being added to comment-only code. Found by trying to paste and run this example: http://matplotlib.sourceforge.net/examples/api/collections_demo.html that happened to have indented, comment-only lines of the type: code() # comment code() In these cases, we should *not* try to prepend 'if 1:' to the user's code since that comment by itself isn't executable, and it will cause a compiler IndentationError. --- diff --git a/IPython/core/inputsplitter.py b/IPython/core/inputsplitter.py index 4805f5d..f688362 100644 --- a/IPython/core/inputsplitter.py +++ b/IPython/core/inputsplitter.py @@ -103,6 +103,10 @@ ESC_PAREN = '/' dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') ini_spaces_re = re.compile(r'^([ \t\r\f\v]+)') +# regexp to match pure comment lines so we don't accidentally insert 'if 1:' +# before pure comments +comment_line_re = re.compile('^\s*\#') + def num_ini_spaces(s): """Return the number of initial spaces in a string. @@ -368,7 +372,9 @@ class InputSplitter(object): # this allows execution of indented pasted code. It is tempting # to add '\n' at the end of source to run commands like ' a=1' # directly, but this fails for more complicated scenarios - if not self._buffer and lines[:1] in [' ', '\t']: + + if not self._buffer and lines[:1] in [' ', '\t'] and \ + not comment_line_re.match(lines): lines = 'if 1:\n%s' % lines self._store(lines)