r'\' - python parser bug?

Fuzzyman michael at foord.net
Wed May 26 03:39:45 EDT 2004


"Fredrik Lundh" <fredrik at pythonware.com> wrote in message news:<mailman.274.1085497902.6949.python-list at python.org>...
> Tim Peters wrote:
> 
> > Yup.  Right now all tools (including Python itself) that scan over strings
> > in Python source can (and usually do) treat backslashes identically, whether
> > in loops or in regexps.
> 
> Or in other words, the point here is that the prefix flag (u, r, whatever) doesn't
> affect how a string literal is *parsed*.  When the parser sees a backslash inside
> a string literal, it always skips the next character.  There's no separate grammar
> for "raw string literals".
> 
> </F>


Wrong, surely ?

>>> print '\\'
\
>>> print r'\\'
\\
>>> print r'c:\subdir\'
SyntaxError: EOL while scanning single-quoted string
>>> 

> When the parser sees a backslash inside
> a string literal, it always skips the next character. 
In the above example the parser *only* skips the next character if it
is at the end of the string... surely illogical. The reason given is
effectively 'raw strings were created for regular expressions, so it
doesn't matter if the behaviour is illogical' (and precludes other
reasonable uses!!)..........

Regards,


Fuzzy



More information about the Python-list mailing list