Skip to content
This repository

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

Closed
jstenar opened this Issue November 21, 2011 · 8 comments

3 participants

Jörgen Stenarson Fernando Perez Thomas Kluyver
Jörgen Stenarson
Collaborator

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

Thomas Kluyver
Collaborator

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

Fernando Perez
Owner

@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...

Thomas Kluyver
Collaborator

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.

Jörgen Stenarson
Collaborator

It was on python 2.6

Fernando Perez
Owner

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.

Fernando Perez fperez closed this in 59c4f31 November 22, 2011
Fernando Perez
Owner

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.

Jörgen Stenarson
Collaborator

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):
Fernando Perez fperez reopened this November 29, 2011
Fernando Perez
Owner

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

Fernando Perez fperez closed this issue from a commit November 29, 2011
Fernando Perez 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
Fernando Perez fperez closed this in 206d352 November 29, 2011
Fernando Perez fperez referenced this issue from a commit January 10, 2012
Commit has since been removed from the repository and is no longer available.
Fernando Perez fperez referenced this issue from a commit January 10, 2012
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.