jcartledge / sublime-worksheet

An inline REPL for JavaScript, PHP, Ruby & more in Sublime Text 2 and 3
MIT License
333 stars 18 forks source link

Erroneous "IndentationError" with python #23

Closed ianrose14 closed 11 years ago

ianrose14 commented 11 years ago

Firstly, I admit that I'm not sure if this is more properly a bug in sublime-worksheet or a bug in python's interactive mode, but either way this unfortunately prevents me (and others, I would guess?) from using this tool which otherwise seems pretty cool...

Given code like this:

#!/usr/bin/env python
import sys

def main():
  args = sys.argv[1:]
  for arg in args:
    if '.' in arg:
      print 'great!'

    print arg

if __name__ == '__main__':
  main()

This runs just fine from the command line, but worksheet gives "IndentationError: unexpected indent" on line 10 (print arg). Is there anything that can be done about this? Unfortunately I get the same error if I save this file as foo.py and do:

cat foo.py | python -i
jcartledge commented 11 years ago

Hi. I think this is a Python syntax issue.

It looks like the problem is that Python needs whitespace on line 9 (the empty line) to the level of either print statement to indicate that the second print is still inside the for loop, but Sublime Text is automatically stripping it.

If I add whitespace on that line the script runs fine as a worksheet. (Note that it doesn't actually do anything because it is running in REPL mode, not as a command line script.)

Check what you have in your Preferences.sublime-settings for "trim_automatic_white_space".

ianrose14 commented 11 years ago

I agree that's what Python (in -i mode) is expecting and failing on - the problem is that the program is legal Python even without that whitespace indentation on blank lines. In other words, it appears that python in interactive mode is pickier about its input than python is in normal (compiled?) mode. And thus worksheet unfortunately will fail on many files that would otherwise run just fine from the command line. Which is a bummer...

jcartledge commented 11 years ago

You're right, sorry. There's a discussion of the issue here (wrt style): http://stackoverflow.com/questions/2727988/python-indentation-in-empty-lines

Maybe I should add a note to the readme.