This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: import and compile() do not treat trailing whitespace the same
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, mseaborn, terry.reedy
Priority: normal Keywords:

Created on 2008-11-05 10:56 by mseaborn, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg75521 - (view) Author: Mark Seaborn (mseaborn) Date: 2008-11-05 10:56
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 <module>
    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"
msg75617 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2008-11-07 21:47
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.
History
Date User Action Args
2022-04-11 14:56:40adminsetgithub: 48512
2010-04-29 17:44:36terry.reedysetstatus: pending -> closed
2008-11-07 21:47:56terry.reedysetstatus: open -> pending
resolution: not a bug
messages: + msg75617
nosy: + terry.reedy
2008-11-05 21:07:39brett.cannonsetnosy: + brett.cannon
2008-11-05 10:56:28mseaborncreate