[Pythonmac-SIG] \r in string causes eval to fail

Kevin Altis altis at semi-retired.com
Fri Sep 10 20:25:08 CEST 2004


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)
{'position': (10, 10), 'type': 'Button', 'name': 'Button1', 'label': 
'Button1'}
 >>> eval(s)
Traceback (most recent call last):
   File "<input>", line 1, in ?
   File "<string>", line 1
     {'type':'Button',
'name':'Button1',
'position':(10, 10),
'label':'Button1',
}
                      ^
SyntaxError: invalid syntax
 >>>

ka



More information about the Pythonmac-SIG mailing list