Skip to content

Commit

Permalink
Fix bug where bare strings would be silently ignored in input.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rkern authored and fperez committed Jan 28, 2011
1 parent 21940ff commit 2ccc90d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion IPython/core/inputsplitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -347,7 +354,7 @@ def source_reset(self):
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.
Expand Down
9 changes: 9 additions & 0 deletions IPython/core/tests/test_inputsplitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,15 @@ def test_split(self):
[['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)
Expand Down

0 comments on commit 2ccc90d

Please sign in to comment.