Bug? exec converts '\n' to newline in docstrings!?

Discussion in 'Python' started by Edward K Ream, Jul 30, 2007.

  1. It looks like both exec and execfile are converting "\n" to an actual
    newline
    in docstrings!

    Start idle:

    Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on
    win32
    [rest of signon deleted]

    >>> s = '''\

    strings = 'abc'.split("\n")
    '''
    >>> print s

    strings = 'abc'.split("
    ")

    I see this in my own calls to exec and execfile. Is this a bug or am I
    missing something?

    Edward
    -------------------------------------------------------------------
    Edward K. Ream email:
    Leo: http://webpages.charter.net/edreamleo/front.html
    --------------------------------------------------------------------
     
    Edward K Ream, Jul 30, 2007
    #1
    1. Advertising

  2. Edward K Ream wrote:

    > It looks like both exec and execfile are converting "\n" to an actual
    > newline
    > in docstrings!
    >
    > Start idle:
    >
    > Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
    > on win32
    > [rest of signon deleted]
    >
    >>>> s = '''\

    > strings = 'abc'.split("\n")
    > '''
    >>>> print s

    > strings = 'abc'.split("
    > ")
    >
    > I see this in my own calls to exec and execfile. Is this a bug or am I
    > missing something?


    AFAIK docstrings are nothing special. So \-escaping is of course available,
    as it's an important feature for strings in general.

    For the case at hand,

    strings = 'abc'.split("\\n")

    might help.

    Diez
     
    Diez B. Roggisch, Jul 30, 2007
    #2
    1. Advertising

  3. Edward K Ream

    Steve Holden Guest

    Edward K Ream wrote:
    > It looks like both exec and execfile are converting "\n" to an actual
    > newline
    > in docstrings!
    >
    > Start idle:
    >
    > Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on
    > win32
    > [rest of signon deleted]
    >
    >>>> s = '''\

    > strings = 'abc'.split("\n")
    > '''
    >>>> print s

    > strings = 'abc'.split("
    > ")
    >
    > I see this in my own calls to exec and execfile. Is this a bug or am I
    > missing something?
    >

    Python is doing exactly what you told it to do. You created a string
    with triple single-quote delimiters. Nothing in the string literal
    syntax says that escape sequences will not be actioned, so the literal
    has a value that includes a newline.

    This has nothing to do with exec, and I don't believe it will happen
    with execfile should you create a file with a legal Python program
    inside it. The problem is because you are trying to represent a Python
    program as a Python string literal, and doing it incorrectly.

    What you did is no different from writing:

    >>> s = '''\

    .... This is a string with\nan embedded newline'''
    >>> print s

    This is a string with
    an embedded newline
    >>>


    It just doesn't match your expectations, is all.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
     
    Steve Holden, Jul 30, 2007
    #3
  4. Edward K Ream

    Steve Holden Guest

    Edward K Ream wrote:
    > It looks like both exec and execfile are converting "\n" to an actual
    > newline
    > in docstrings!
    >
    > Start idle:
    >
    > Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on
    > win32
    > [rest of signon deleted]
    >
    >>>> s = '''\

    > strings = 'abc'.split("\n")
    > '''
    >>>> print s

    > strings = 'abc'.split("
    > ")
    >
    > I see this in my own calls to exec and execfile. Is this a bug or am I
    > missing something?
    >

    Python is doing exactly what you told it to do. You created a string
    with triple single-quote delimiters. Nothing in the string literal
    syntax says that escape sequences will not be actioned, so the literal
    has a value that includes a newline.

    This has nothing to do with exec, and I don't believe it will happen
    with execfile should you create a file with a legal Python program
    inside it. The problem is because you are trying to represent a Python
    program as a Python string literal, and doing it incorrectly.

    What you did is no different from writing:

    >>> s = '''\

    .... This is a string with\nan embedded newline'''
    >>> print s

    This is a string with
    an embedded newline
    >>>


    It just doesn't match your expectations, is all.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
     
    Steve Holden, Jul 30, 2007
    #4
  5. > The problem is because you are trying to represent a Python
    program as a Python string literal, and doing it incorrectly.

    Yes, that is exactly the problem. Thanks to all who replied. Changing
    changing '\n' to '\\n' fixed the problem.

    Edward
    --------------------------------------------------------------------
    Edward K. Ream email:
    Leo: http://webpages.charter.net/edreamleo/front.html
    --------------------------------------------------------------------
     
    Edward K Ream, Jul 30, 2007
    #5
  6. Edward K Ream

    Stargaming Guest

    On Mon, 30 Jul 2007 11:00:14 -0500, Edward K Ream wrote:

    >> The problem is because you are trying to represent a Python

    > program as a Python string literal, and doing it incorrectly.
    >
    > Yes, that is exactly the problem. Thanks to all who replied. Changing
    > changing '\n' to '\\n' fixed the problem.


    Raw strings (r'this \n will stay') might help, too. See http://
    docs.python.org/ref/strings.html#l2h-14
     
    Stargaming, Jul 30, 2007
    #6
    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. Hal Vaughan
    Replies:
    11
    Views:
    1,154
    Gordon Beaton
    May 22, 2006
  2. tedsuzman
    Replies:
    2
    Views:
    7,123
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    478
    Duncan Booth
    Jul 22, 2004
  4. Ivar
    Replies:
    13
    Views:
    499
    werasm
    Nov 17, 2005
  5. Guillermo Riojas
    Replies:
    0
    Views:
    184
    Guillermo Riojas
    Nov 26, 2010
Loading...

Share This Page