[Pythonmac-SIG] \r in string causes eval to fail
Bob Ippolito
bob at redivi.com
Fri Sep 10 20:41:50 CEST 2004
On Sep 10, 2004, at 2:25 PM, Kevin Altis wrote:
> I've run into a problem with eval on the Mac and I'm not sure whether
> it is a bug or expected behavior. The problem is that if \r is used as
> the line terminator instead of \n then it causes eval to fail. I ran
> into this while copying and pasting some text to eval and the
> clipboard had converted my newlines to returns. Here is an example
> done in the shell. As you can see below, using newlines as a separator
> is fine, but return (\r) isn't. This would probably only come up on
> the Mac where return (CR, \r) is the default line terminator, but I
> didn't know whether universal newlines support was supposed to deal
> with this kind of issue.
>
> >>> s = """{'type':'Button',\r'name':'Button1',\r'position':(10,
> 10),\r'label':'Button1',\r}"""
> >>> s2 = s.replace('\r', '\n')
> >>> eval(s2)
"Universal newlines" is only a mode for the file object (as far as I
know anyway). eval and compile take strings.
It would be a lot more portable to do this to normalize your input
strings:
'\n'.join(s.splitlines())
splitlines is essentially universal newlines support at the string
level. It might be smart to do this internally, maybe this deserves a
feature request or bug report?
-bob
More information about the Pythonmac-SIG
mailing list