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

Discussion in 'Python' started by jmfauth, Dec 29, 2010.

  1. jmfauth

    jmfauth Guest

    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
    jmfauth, Dec 29, 2010
    #1
    1. Advertising

  2. jmfauth

    Terry Reedy Guest

    On 12/29/2010 11:07 AM, jmfauth wrote:
    > 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.


    I am a bit surprised, but I presume this is one on many
    back-compatibility holdovers still in 2.7. I believe 2.7 normally reads
    input with universal newline support, so that line endings are fixed on
    input, where they should be. Within Python, 'newline' is '\n'.

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


    In 3.0, there were many simplifications where old things got dropped.

    > (Python 3.2.a/b not checked).


    I have not heard of any change. The compile() entry has the following:

    "Changed in version 3.2: Allowed use of Windows and Mac newlines. Also
    input in 'exec' mode does not have to end in a newline anymore. Added
    the optimize parameter."

    The second and third statement are true, but

    >>> compile('print(999)\r\n', '<in>', 'exec')

    Traceback (most recent call last):
    File "<pyshell#1>", line 1, in <module>
    compile('print(999)\r\n', '<in>', 'exec')
    File "<in>", line 1
    print(999)

    ^
    SyntaxError: invalid syntax

    I will inquire on pydev.

    > Bug, regression, deliberate choice?


    I presume deliberate simplification, but you can wait for another answer
    or check svn logs of the appropriate source file.

    Or there might be an entry in 3.0 NEWS or What's New files.

    --
    Terry Jan Reedy
    Terry Reedy, Dec 29, 2010
    #2
    1. Advertising

  3. jmfauth

    Terry Reedy Guest

    On 12/29/2010 2:31 PM, Terry Reedy wrote:
    > "Changed in version 3.2: Allowed use of Windows and Mac newlines. Also
    > input in 'exec' mode does not have to end in a newline anymore. Added
    > the optimize parameter."


    Retest shows that above is correct.

    >>> compile("print(999)\r\n", "blah", "exec")


    <code object <module> at 0x00F5EC50, file "blah", line 1>

    For most development purposes (not just yours), 3.2b2 is already better
    than 3.1.

    Terry Jan Reedy
    Terry Reedy, Dec 29, 2010
    #3
  4. jmfauth

    jmfauth Guest

    On 29 Dez., 21:14, Terry Reedy <> wrote:
    > On 12/29/2010 2:31 PM, Terry Reedy wrote:
    >
    > > "Changed in version 3.2: Allowed use of Windows and Mac newlines. Also
    > > input in 'exec' mode does not have to end in a newline anymore. Added
    > > the optimize parameter."

    >
    > Retest shows that above is correct.
    >
    >  >>> compile("print(999)\r\n", "blah", "exec")
    >
    > <code object <module> at 0x00F5EC50, file "blah", line 1>
    >


    Ok, I see. Thanks.

    The '\r\n' acceptance has been introduced in Python 2.7
    and I was a little bit suprised with Python 3.1.

    For the story, I'm not using directly the compile()
    command, but something like:

    <my_interactive_interpreter_instance>.runsource(source)

    where source is coming from a GUI toolkit text widget.
    jmfauth, Dec 29, 2010
    #4
  5. jmfauth

    Terry Reedy Guest

    On 12/29/2010 4:06 PM, jmfauth wrote:
    > On 29 Dez., 21:14, Terry Reedy<> wrote:
    >> On 12/29/2010 2:31 PM, Terry Reedy wrote:
    >>
    >>> "Changed in version 3.2: Allowed use of Windows and Mac newlines. Also
    >>> input in 'exec' mode does not have to end in a newline anymore. Added
    >>> the optimize parameter."

    >>
    >> Retest shows that above is correct.
    >>
    >> >>> compile("print(999)\r\n", "blah", "exec")

    >>
    >> <code object<module> at 0x00F5EC50, file "blah", line 1>
    >>

    >
    > Ok, I see. Thanks.
    >
    > The '\r\n' acceptance has been introduced in Python 2.7
    > and I was a little bit suprised with Python 3.1.


    2.6 was followed by 3.0 and then 3.1.
    2.7 will be followed by 3.2 in a couple more month.
    That feature was added to 2.7 and 3.2 in Nov 2009 long after 3.1.

    --
    Terry Jan Reedy
    Terry Reedy, Dec 29, 2010
    #5
  6. jmfauth

    jmfauth Guest

    On 30 Dez., 00:11, Terry Reedy <> wrote:
    > On 12/29/2010 4:06 PM, jmfauth wrote:
    >
    >
    >
    > > On 29 Dez., 21:14, Terry Reedy<>  wrote:
    > >> On 12/29/2010 2:31 PM, Terry Reedy wrote:

    >
    > >>> "Changed in version 3.2: Allowed use of Windows and Mac newlines. Also
    > >>> input in 'exec' mode does not have to end in a newline anymore. Added
    > >>> the optimize parameter."

    >
    > >> Retest shows that above is correct.

    >
    > >>   >>>  compile("print(999)\r\n", "blah", "exec")

    >
    > >> <code object<module>  at 0x00F5EC50, file "blah", line 1>

    >
    > > Ok, I see. Thanks.

    >
    > > The '\r\n' acceptance has been introduced in Python 2.7
    > > and I was a little bit suprised with Python 3.1.

    >
    > 2.6 was followed by 3.0 and then 3.1.
    > 2.7 will be followed by 3.2 in a couple more month.
    > That feature was added to 2.7 and 3.2 in Nov 2009 long after 3.1.
    >
    > --
    > Terry Jan Reedy


    Thanks for these precisions and don't worry too much, it's
    not a real isssue.

    (I'v seen the "noise" on the pydev list).

    Thanks again.
    jmfauth, Dec 30, 2010
    #6
  7. jmfauth

    jmfauth Guest

    Just an info, addendum.

    >>> sys.version

    '3.2rc1 (r32rc1:88035, Jan 15 2011, 21:05:51) [MSC v.1500 32 bit
    (Intel)]'
    >>> compile('if True:\r\n print(999)\r\n', '<in>', 'exec')


    <code object <module> at 0x023CA610, file "<in>", line 2>
    >>> exec(compile('if True:\r\n print(999)\r\n', '<in>', 'exec'))

    999
    >>>
    jmfauth, Jan 16, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Jones
    Replies:
    5
    Views:
    15,984
    Robert Jones
    Jul 10, 2003
  2. Anonymous
    Replies:
    2
    Views:
    28,732
    phamlen
    Aug 31, 2006
  3. Mr. Ken
    Replies:
    6
    Views:
    2,383
    Marcus Harnisch
    Jan 5, 2007
  4. Gernot Frisch
    Replies:
    6
    Views:
    342
    Gernot Frisch
    Nov 29, 2005
  5. Nagaraj
    Replies:
    1
    Views:
    856
    Lionel B
    Mar 1, 2007
Loading...

Share This Page