I’ve been cleaning up a lot of old duplicate questions on Stack Overflow about various random IndentationErrors (mostly trivial matters for which we have two or three really solid canonicals), and one thing I’ve noticed is multiple people reporting that they fixed an IndentationError (or in some cases a more general SyntaxError) by fixing line endings to use the platform default (e.g. replacing CRLF sequences with LF on Linux).
I haven’t been able to reproduce that - i.e., code with the “wrong” line endings seems to work fine:
But I just wanted to make sure: did Python ever reject code as improperly indented or syntactically incorrect, due to having the wrong line endings? Or is there some specific arrangement of the code that would make it care about the line endings? Or are these people all just mistaken (i.e., they incidentally fixed mixed tab and space indentation at the same time)?
I have the vaguest memory of running into issues on Windows related to line endings years and years ago, but I can’t get it to replicate, even with python 3.0:
py = "if 1:\r\n if 2:\n print('hello')\r"
py2 = "if 1:\n\r if 2:\n print('hello')"
b = bytes(py, 'utf-8')
b2 = bytes(py2, 'utf-8')
with open('test.py', 'wb') as f:
f.write(b)
with open('test2.py', 'wb') as f:
f.write(b2)
Ah, it was part of the same change supporting universal newlines for files. Makes sense; the import/exec code could take advantage of the same functionality.