[Python-Dev] Unix line endings required for PyRun* breaking embedded Python

Skip Montanaro skip at pobox.com
Thu Jan 20 02:42:03 CET 2005


    Stuart> I don't think it is possible for plpythonu to fix this by simply
    Stuart> translating the line endings, as this would require significant
    Stuart> knowledge of Python syntax to do correctly (triple quoted
    Stuart> strings and character escaping I think).

I don't see why not.  If you treat the string as a file in text mode, I
think you'd replace all [\r\n]+ with \n, even if it was embedded in a
string:

    >>> s
    'from math import pi\r\n"""triple-quoted string embedding CR:\rrest of string"""\r\nprint 2*pi*7\r'
    >>> open("foo", "w").write(s)       
    >>> open("foo", "rU").read()
    'from math import pi\n"""triple-quoted string embedding CR:\nrest of string"""\nprint 2*pi*7\n'

Just re.sub("[\r\n]+", "\n", s) and I think you're good to go.

Skip


More information about the Python-Dev mailing list