Issue 4262: import and compile() do not treat trailing whitespace the same (original) (raw)

compile() raises a SyntaxError if the source code it is given contains trailing spaces or a trailing carriage return character, but this does happen if the same source code is imported or executed.

import subprocess

data = "if True:\n pass\n " filename = "/tmp/test.py" fh = open(filename, "w") fh.write(data) fh.close() subprocess.check_call(["python", filename]) subprocess.check_call(["python", "-c", "import test"], cwd="/tmp") compile(data, filename, "exec")

This gives:

Traceback (most recent call last): File "whitespace.py", line 11, in compile(data, filename, "exec") File "/tmp/test.py", line 3

SyntaxError: invalid syntax

This also happens if the data is changed to: data = "if True:\n pass\r"

Both differences are covered in the 2.x docs:

"When compiling multi-line statements, two caveats apply: line endings must be represented by a single newline character ('\n'), and the input must be terminated by at least one newline character.

The \r difference is not really a difference because \r is replaced by \n before import starts compiling. I suspect that EOF effectively gets converted to \n also. The doc issue is that 'must be terminated...' is not always true.

This caveat is missing in 3.0, even though the limitation is the same. see #4118.

I thought this was discussed in a c.l.p thread.