By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

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

P: n/a
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: ed*******@yahoo.com
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------


Jul 30 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Jul 30 '07 #2

P: n/a
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 -------------

Jul 30 '07 #3

P: n/a
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 -------------
Jul 30 '07 #4

P: n/a
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: ed*******@yahoo.com
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 30 '07 #5

P: n/a
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
Jul 30 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.