473,385 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
5 2622
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Good Man | last post by:
Hi there If I enter the following at the command line, I successfully get an email sent to the correct name/email: php /var/www/html/admin/adminscripts/welcome-cli.php 'Me' 'me@me.net'
2
by: Sridhar R | last post by:
When writing a python library, we can use docstrings for methods and functions that are part of API. But what about comments for non-API objects or python application code? For applications,...
0
by: Craig Ringer | last post by:
Hi folks I'm currently working on a fairly well internationalised app that embeds a Python intepreter. I'd like to make the docstrings translatable, but am running into the issue that the...
2
by: Steven Bethard | last post by:
I have two classes that implement the same interface, e.g. something like: class C(object): def foo(self): """Foo things""" ... def bar(self): """Bar things""" ... def baz(self):
6
by: Kamilche | last post by:
I have a large project that is getting complex, and I would like to print the docstrings without importing the modules. The only Python utility I could find references is apparently defunct and...
0
by: Edward Loper | last post by:
Epydoc 3 supports extracting information about Python modules by parsing. As a result, it can extract "docstrings" for variables. There are several possible ways these docstrings could be...
1
bartonc
by: bartonc | last post by:
You will be rewarded sooner than you think when you use docstrings to document your classes and functions. Whether you ever intend to publish your work or not, docstrings will help you in several...
0
by: tjhnson | last post by:
The topic of docstrings for variables has come up many times before. In fact, a PEP was proposed and rejected on this very topic. http://www.python.org/dev/peps/pep-0224/ When creating...
1
by: Matimus | last post by:
I think I'm going to create a new issue in Pythons issue database, but I wanted to run it by the news group first. See if I can get any useful feed back. The following session demonstrates the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.