From 2ccc90d68ac01145ab97ba9cd2457e3911540ea4 2011-01-28 16:35:05 From: Robert Kern Date: 2011-01-28 16:35:05 Subject: [PATCH] Fix bug where bare strings would be silently ignored in input. Bare strings are parsed by the AST module as the docstring of the 'module' (in our case, the string of user input), so they need to be special-cased. --- diff --git a/IPython/core/inputsplitter.py b/IPython/core/inputsplitter.py index 9267770..c54f922 100644 --- a/IPython/core/inputsplitter.py +++ b/IPython/core/inputsplitter.py @@ -225,6 +225,13 @@ def split_blocks(python): # to put in a more sophisticated test. linenos = [x.lineno-1 for x in ast.node if x.lineno is not None] + # When we have a bare string as the first statement, it does not end up as + # a Discard Node in the AST as we might expect. Instead, it gets interpreted + # as the docstring of the module. Check for this case and prepend 0 (the + # first line number) to the list of linenos to account for it. + if ast.doc is not None: + linenos.insert(0, 0) + # When we finally get the slices, we will need to slice all the way to # the end even though we don't have a line number for it. Fortunately, # None does the job nicely. @@ -347,7 +354,7 @@ class InputSplitter(object): return out def push(self, lines): - """Push one ore more lines of input. + """Push one or more lines of input. This stores the given lines and returns a status code indicating whether the code forms a complete Python block or not. diff --git a/IPython/core/tests/test_inputsplitter.py b/IPython/core/tests/test_inputsplitter.py index adc0b4f..9ebc831 100644 --- a/IPython/core/tests/test_inputsplitter.py +++ b/IPython/core/tests/test_inputsplitter.py @@ -329,6 +329,15 @@ class InputSplitterTestCase(unittest.TestCase): [['for i in range(10):' ' x=i**2'], ['z = 1']], + + [['"asdf"']], + + [['"asdf"'], + ['10'], + ], + + [['"""foo', + 'bar"""']], ] for block_lines in all_blocks: self.check_split(block_lines)