Does Python 3.1 accept \r\n in compile()?

jmfauth wxjmfauth at gmail.com
Wed Dec 29 11:07:25 EST 2010


I wrote miscellaneous interactive interpreters and
I fall on this.

In Python 2.7 (understand Python > 2.6), a source code
can be compiled with "native" '\r\n' as eol.

In Python 3.1, it does not seem to be the case.

(Python 3.2.a/b not checked).

Bug, regression, deliberate choice?


>>> sys.version
2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit
(Intel)]
>>> compile('if True:\n    print 999\n', '<in>', 'exec')
<code object <module> at 02858DA0, file "<in>", line 1>
>>> compile('if True:\r\n    print 999\r\n', '<in>', 'exec')
<code object <module> at 02858E30, file "<in>", line 1>
>>> exec(compile('if True:\r\n    print 999\r\n', '<in>', 'exec'))
999

>>> sys.version
'3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit
(Intel)]'
compile('if True:\n    print(999)\n', '<in>', 'exec')
<code object <module> at 0x01FE5458, file "<in>", line 2>
>>> exec(compile('if True:\n    print(999)\n', '<in>', 'exec'))
999
>>> # this fails
>>> compile('if True:\r\n    print(999)\r\n', '<in>', 'exec')
Traceback (most recent call last):
  File "<qsi last command>", line 1, in <module>
  File "<in>", line 1
    if True:

^
SyntaxError: invalid syntax



More information about the Python-list mailing list