ipython does not like white space at end of file #1027

Closed
jstenar opened this Issue Nov 22, 2011 · 8 comments

3 participants

@jstenar
IPython member

ipython does not handle whitespace at end of files in the same way as python itself does.

Test file 'whitespace_at_eof.py at: https://gist.github.com/1385125

This file contains one print statement and on the last line there are four spaces.

Regular python:

C:\python\bugreports\ipython> python .\whitespace_at_eof.py
hello world!

ipython from commandline:

C:\python\bugreports\ipython> ipython .\whitespace_at_eof.py
---------------------------------------------------------------------------
IndentationError                          Traceback (most recent call last)
c:\python26\external\ipython\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
    163             else:
    164                 filename = fname
--> 165             exec compile(scripttext, filename, 'exec') in glob, loc
    166     else:
    167         def execfile(fname, *where):

IndentationError: unexpected indent (whitespace_at_eof.py, line 2)

ipython %run:

In [1]: %run whitespace_at_eof.py
---------------------------------------------------------------------------
IndentationError                          Traceback (most recent call last)
c:\python26\external\ipython\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
    163             else:
    164                 filename = fname
--> 165             exec compile(scripttext, filename, 'exec') in glob, loc
    166     else:
    167         def execfile(fname, *where):

IndentationError: unexpected indent (whitespace_at_eof.py, line 2)

This was tested on windows 7 python2.6 ipython master 30385c4

@takluyver
IPython member

This is probably just a matter of doing .rstrip() + '\n'.

@fperez
IPython member

@jstenar, on what version of python do you see this? On 2.6 it works fine for me, both at the cmd line and via %run...

@takluyver
IPython member

Note that the error appears in a Windows specific branch - on Posix systems, we call the built in execfile instead of compile-ing the source ourselves. However, when I call compile manually on the equivalent text, it doesn't appear to raise any errors, so maybe it's a difference in Python on Windows.

@jstenar
IPython member

It was on python 2.6

@fperez
IPython member

Ah, thanks for the info @takluyver. Indeed, tailing whitespace is disallowed:

In [2]: compile('print 1\n   ', 'foo', 'exec')
---------------------------------------------------------------------------
IndentationError                          Traceback (most recent call last)
/home/fperez/tmp/junk/<ipython-input-2-1b7e05c14fb1> in <module>()
----> 1 compile('print 1\n   ', 'foo', 'exec')

IndentationError: unexpected indent (foo, line 2)

In [3]: compile('print 1\n', 'foo', 'exec')
Out[3]: <code object <module> at 0x1404738, file "foo", line 1>

Fixing it now (I'll do only rstrip, the extra newline isn't necessary and saves rebuilding yet another string.

@fperez fperez closed this in 59c4f31 Nov 23, 2011
@fperez
IPython member

Note that the bug is only in python 2.6, I checked and both 2.7 and 3.x work ok with trailing whitespace. Fix pushed.

@jstenar
IPython member

It seems we should have added the "\n" at the end of the line, I guess things work differently when there is a comment at the end of the file.

I get this error message now when starting ipython:

[TerminalIPythonApp] Exception while loading config file C:\Users\jstenar\.ipython\profile_default\ipython_config.py
Traceback (most recent call last):
  File "c:\python\external\ipython\IPython\config\application.py", line 440, in load_config_file
    config = loader.load_config()
  File "c:\python\external\ipython\IPython\config\loader.py", line 268, in load_config
    self._read_file_as_dict()
  File "c:\python\external\ipython\IPython\config\loader.py", line 321, in _read_file_as_dict
    py3compat.execfile(conf_filename, namespace)
  File "c:\python\external\ipython\IPython\utils\py3compat.py", line 168, in execfile
    exec compile(scripttext, filename, 'exec') in glob, loc
  File "C:\Users\jstenar\.ipython\profile_default\ipython_config.py", line 379
     # c.PlainTextFormatter.singleton_printers = {}
                                                  ^
 SyntaxError: invalid syntax

It goes away if I add +"\n" at the end of line 161 in py3compat.py.

diff --git a/IPython/utils/py3compat.py b/IPython/utils/py3compat.py
index 57eca72..ac4c531 100644
--- a/IPython/utils/py3compat.py
+++ b/IPython/utils/py3compat.py
@@ -158,7 +158,7 @@ else:
         # The rstrip() is necessary b/c trailing whitespace in files will
         # cause an IndentationError in Python 2.6 (this was fixed in 2.7,
         # but we still support 2.6).  See issue 1027.
-            scripttext = __builtin__.open(fname).read().rstrip()
+            scripttext = __builtin__.open(fname).read().rstrip()+"\n"
         # compile converts unicode filename to str assuming
         # ascii. Let's do the conversion before calling compile
         if isinstance(fname, unicode):
@fperez fperez reopened this Nov 29, 2011
@fperez
IPython member

Will fix now with the `\n' addition you suggest.

@fperez fperez added a commit that closed this issue Nov 29, 2011
@fperez fperez Fixes #1027, the previous fix had a problem under windows.
Ensure that scripts are newline-terminated, apparently py3 complains when there's a missing newline and the last line ends in a comment.  This is probably a python bug, but we should protect against it nonetheless.
206d352
@fperez fperez closed this in 206d352 Nov 29, 2011
@mattvonrocketstein mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
@fperez fperez Work around issue in py2.6 where trailing whitespace confuses compile().
Closes #1027.
a71d2e9
@mattvonrocketstein mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
@fperez fperez Fixes #1027, the previous fix had a problem under windows.
Ensure that scripts are newline-terminated, apparently py3 complains when there's a missing newline and the last line ends in a comment.  This is probably a python bug, but we should protect against it nonetheless.
83e8f35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment