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

import and compile() do not treat trailing whitespace the same #48512

Closed
mseaborn mannequin opened this issue Nov 5, 2008 · 2 comments
Closed

import and compile() do not treat trailing whitespace the same #48512

mseaborn mannequin opened this issue Nov 5, 2008 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@mseaborn
Copy link
Mannequin

mseaborn mannequin commented Nov 5, 2008

BPO 4262
Nosy @brettcannon, @terryjreedy

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2010-04-29.17:44:36.700>
created_at = <Date 2008-11-05.10:56:28.572>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = 'import and compile() do not treat trailing whitespace the same'
updated_at = <Date 2010-04-29.17:44:36.699>
user = 'https://bugs.python.org/mseaborn'

bugs.python.org fields:

activity = <Date 2010-04-29.17:44:36.699>
actor = 'terry.reedy'
assignee = 'none'
closed = True
closed_date = <Date 2010-04-29.17:44:36.700>
closer = 'terry.reedy'
components = ['Interpreter Core']
creation = <Date 2008-11-05.10:56:28.572>
creator = 'mseaborn'
dependencies = []
files = []
hgrepos = []
issue_num = 4262
keywords = []
message_count = 2.0
messages = ['75521', '75617']
nosy_count = 3.0
nosy_names = ['brett.cannon', 'terry.reedy', 'mseaborn']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue4262'
versions = ['Python 2.5']

@mseaborn
Copy link
Mannequin Author

mseaborn mannequin commented Nov 5, 2008

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"

@mseaborn mseaborn mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Nov 5, 2008
@terryjreedy
Copy link
Member

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 bpo-4118.

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

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant