Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

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

jstenar opened this issue Nov 22, 2011 · 8 comments
Milestone

Comments

@jstenar
Copy link
Member

jstenar commented Nov 22, 2011

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
Copy link
Member

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

@fperez
Copy link
Member

fperez commented Nov 22, 2011

@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
Copy link
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
Copy link
Member Author

jstenar commented Nov 22, 2011

It was on python 2.6

@fperez
Copy link
Member

fperez commented Nov 23, 2011

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 as completed in 59c4f31 Nov 23, 2011
@fperez
Copy link
Member

fperez commented Nov 23, 2011

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
Copy link
Member Author

jstenar commented Nov 28, 2011

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
Copy link
Member

fperez commented Nov 29, 2011

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

@fperez fperez closed this as completed in 206d352 Nov 29, 2011
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants