views:

322

answers:

4

I'm pretty sure the following error is related to the fact that I'm sharing code via SVN with a colleague that is using a Windows system.

Myself, I use Python on Mac, editing with TextMate.

#!/usr/bin/python

import os

from google.appengine.api import users
from google.appengine.ext import webapp
...

When running that code, I get a syntax error:

events.py:2 invalid syntax

Is there an end-of-line issue when using SVN?

Thankful for every hint.


edit

The problem seems not to be caused by SVN.

Interestingly, executing directly at the Shell, there's no Syntax Error. But both validating with Textmate (using PyCheckMate) and trying to launch with GoogleAppEngineLauncher return the error.

+1  A: 

Byte order mark? No, native EOL needed.

I would be surprised if Python didn't just silently ignore the usual variations on record terminator styles.

A shell or kernel might not, but then you would see something like python: bad interpreter.

It's possible that there is a BOM, or byte order mark at the top of the file. Check it with od -c events.py.

The BOM is not needed and not recommended in UTF-8, but for some reason Windows Notepad has the annoying habit of inserting one as the very first character in a file.

So, we figured it out in the comments by noting that python events.py worked fine, making it clear that the \r was confusing the kernel's script execution. (#! interp [arg] is actually processed by the kernel, if that fails, the shell will try it, leading to the error seen.) The solution is in the svn manual in the property svn:eol-style.

DigitalRoss
Good hint, However, there's no BOM, as far as I can see $ od -c src/events.py0000000 # ! / u s r / b i n / p y t h o0000020 n \r \n \r \n i m p o r t o s \r \n
poezn
Hmm. Back to the drawing board. You know, that doesn't look like a Python error message, more like a shell complaint. If you run it with `python events.py` as opposed to maybe just `./events.py` does it DTRT?
DigitalRoss
yeah, that narrows it down... using the file as an executable I get an error, whereas interpreting it with python it workd. What's the ^M character at the end? -bash: ./events.py: /usr/bin/python^M: bad interpreter: No such file or directory
poezn
That's the \r (CR is ASCII 13 or Ctrl-M). Your Mac treats the \n as the end of the line, and anything before that - including the \r - as the command to run. You don't have a file called python\r, so it dies.
stevemegson
+1  A: 

While it's true that line endings are (usually) different between Windows and the rest of the computing world, Python is designed to be agnostic about the issue. Normally Python has no trouble with varied line endings.

I tried running a Python script on my Mac with a wide variety of line endings and had no problems. Note that I was running my script using the command:

python test.py

instead of

./test.py

It might be worth trying both forms to see whether your problem is really Python or whether it is related to your shell/kernel. I know that some environments do have trouble with CRLF endings on the shebang line.

Greg Hewgill
+1  A: 

Subversion ignores all end-of-line(EOL)-styles. It will never touch your files, unless you told it so.

How can you tell Subversion to use a different EOL-Style? By setting property svn:eol-style to files in question:

svn propset svn:eol-style LF /path/to/my/file/in/workingcopy

You can check if your files have these properties set by using:

svn proplist /path/to/my/file/in/workingcopy
Peter Parker
+4  A: 

While Python shouldn't care about line endings, your Mac won't like having a CRLF line ending on the first line, so this may be your problem.

0000000 # ! / u s r / b i n / p y t h o n \r \n
                                          ^^

SVN can be told to translate line endings by setting the svn:eol-style property to native. It will then translate your LF endings to CRLF when the file is checked out in Windows, and translate your colleague's CRLF endings to LF when you check out on your Mac.

stevemegson
that's probably right:-bash: ./events.py: /usr/bin/python^M: bad interpreter: No such file or directory
poezn
svn:eol-style native should sort it out then, so you both get to see the line endings you prefer. If you just change the endings to LF then you'll probably be fighting his editor to keep them that way.
stevemegson
perfect! Problem solved. Thanks!
poezn
I've found that on Linux, if executing a Python script via the kernel execution mechanism (chmod +x, then execute the file directly). Python is happy with either line ending, but the kernel, when parsing the first line #!/usr/etc isn't happy with the Windows-style line ending.
Craig McQueen