CR+LF problem

John Roth johnroth at ameritech.net
Fri Jun 21 07:59:02 EDT 2002


"Manuel Ruiz" <mruiz at safa.es> wrote in message
news:3d1309ff.1215379262 at news.mad.ttd.net...
>
> Hi,
>
> char * txt = "print 123\r\n" is only a sample, I am storing scripts in
> a database, I put the script into the database with \n but when I get
> it, It has \r\n as end of line, I have assumed this is a database
> problem.

Thanks. That defines it better. Yes, it's a data base interface
problem. Whatever you're using isn't using the C standard for
newlines, it's using the platform standard, and returning \r\n.
It should return either \n, or nothing.

> The rare thing is that PyRun_Simple() function can't execute this kind
> of python code using \r\n as end of line, however I saved that code to
> file and ran "python2 myfile.py" (with \r\n as end of line too) and it
> ran fine, very fine.

Well, Python is built on top of C. In C (and C++) all platform
dependent line endings are translated to a simple \n.

> Where is the diferrence between the api of python and the interprete
> itself. Because first one can't execute this code and the second one
> can do it ?

There's no difference once it actually tries to execute.
What's happening to you is that the process
of writing it to the file and then reading it back is cleaning up the
input. More specifically, the process of reading it back is removing
the \r, and on input, the interpreter expects lines to end with \n, and
removes them before passing it to the execute function.

For your problem, there are two possible solutions:

1. Your data base is misconfigured. If you tell it that the program
is a C (or C++) interface, it might not give you the \r. I have no
idea if this is true, or if your data base even supports this kind of
smarts, and I'm not the one that can help you on it.

2. You need to remove the \r\n sequence yourself. Please
notice that this is a requirement when you read lines from a
file - Python always leaves the \n in place, for reasons that
are spelled out in the FAQ (I think.) In other words, you will
always have to remove the \n, if it exists.

John Roth






More information about the Python-list mailing list