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

E

Edward K Ream

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]
strings = 'abc'.split("\n")
'''strings = 'abc'.split("
")

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

Edward
 
D

Diez B. Roggisch

Edward said:
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]
strings = 'abc'.split("\n")
'''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
 
S

Steve Holden

Edward said:
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]
strings = 'abc'.split("\n")
'''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:
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 -------------
 
S

Steve Holden

Edward said:
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]
strings = 'abc'.split("\n")
'''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:
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 -------------
 
E

Edward K Ream

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
 
S

Stargaming

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top