[issue4262] import and compile() do not treat trailing whitespace the same

Mark Seaborn report at bugs.python.org
Wed Nov 5 11:57:29 CET 2008


New submission from Mark Seaborn <mseaborn at cmedresearch.com>:

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"

----------
components: Interpreter Core
messages: 75521
nosy: mseaborn
severity: normal
status: open
title: import and compile() do not treat trailing whitespace the same
type: behavior
versions: Python 2.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4262>
_______________________________________


More information about the Python-bugs-list mailing list