[Python-bugs-list] [ python-Bugs-594063 ] r'\' => Syntax error

noreply@sourceforge.net noreply@sourceforge.net
Mon, 12 Aug 2002 09:20:27 -0700


Bugs item #594063, was opened at 2002-08-12 10:32
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=594063&group_id=5470

Category: Parser/Compiler
Group: Not a Bug
Status: Closed
Resolution: Wont Fix
Priority: 5
Submitted By: a.hofkamp (axhlkhb)
Assigned to: Nobody/Anonymous (nobody)
Summary: r'\' => Syntax error

Initial Comment:
The raw string is supposed to treat \ as literal
characters. It does that with

>>> print r'\aslkjv'
\aslkjv

and

>>> print r'\'kdsr'
\'kdsr

but not with

>>> print r'\'
  File "<stdin>", line 1
    print r'\'
             ^
SyntaxError: invalid token

In the last case, I intend to have a string containing
a \ only.

This happens with Python 1.5.2 and 2.1.1
(on FreeBSD and/or Linux)


----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2002-08-12 12:20

Message:
Logged In: YES 
user_id=31435

Sorry, you're still out of luck.  This isn't going to change 
(you don't imagine it became a FAQ because you're the 
first person to argue about it <wink>?).

It's not uncommon to need both kinds of quotes in regexps, 
BTW, and that's the primary purpose of r-strings.

Backslashes are retained literally -- no exceptions.

It's not true that it requires "advanced trickery" to use both 
kinds of quotes in an r-string.  It's straightforward.

About docs, please read the section on string literals in the 
Python Reference Manual.  It's quite clear about the rules 
for r-strings.

----------------------------------------------------------------------

Comment By: a.hofkamp (axhlkhb)
Date: 2002-08-12 12:04

Message:
Logged In: YES 
user_id=438586

Hello Tim,

Hmm, the Python documentation is really good :-)

I used the Python pocket reference, and they say
'backslashed are retained literally'. From that statement
I deduced that r'\' should work. Apparently O'Reilly was
wrong in this case.

I find your argument of quoting string delimiters not really
convincing.
I can use either " or ' around the string, and I then have
the other quote character freely available.
The case that both quote characters are needed in the string
is extremely rare in my experience.

Therefore, it may be nicer for users to allow r'\' (i.e.
allow an odd number of back slashes), and only do trickery
like described in the FAQ (6.29) when both ' and " are
needed as quoting characters in the string.

Note that when one needs both ' and " as character, some
(advanced) trickery with back-slashes to escape either ' or
" is needed anyway in Python in that case. Not being able to
use raw strings then makes the problem not much
bigger/complexer.

Albert


----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2002-08-12 11:26

Message:
Logged In: YES 
user_id=31435

Sorry, you're out of luck -- raw strings can't end with an odd 
number of backslashes, else there would be no way to write 
a raw string containing the quoting character.  See FAQ 
entry 6.29:

<http://www.python.org/cgi-bin/faqw.py?
req=show&file=faq06.029.htp>

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=594063&group_id=5470