[PATCH rfc] check-code: detect r'...\'...'

Martin Geisler mg at lazybytes.net
Wed Nov 9 12:15:27 CST 2011


Matt Mackall <mpm at selenic.com> writes:

> On Wed, 2011-11-09 at 18:19 +0100, Martin Geisler wrote:
>> Mads Kiilerich <mads at kiilerich.com> writes:
>> 
>> > On 11/09/2011 03:31 PM, Matt Mackall wrote:
>> >> On Wed, 2011-11-09 at 02:37 +0100, Mads Kiilerich wrote:
>> >>> # HG changeset patch
>> >>> # User Mads Kiilerich<mads at kiilerich.com>
>> >>> # Date 1320802647 -3600
>> >>> # Node ID 20fe74e6bcd0518b108f85270c880f330963c490
>> >>> # Parent  de7e2fba4326cad80bda0cb100d2ae2f58e67ee8
>> >>> check-code: detect r'...\'...'
>> >>
>> >> Not sure why this matters:
>> >>
>> >>>>> a = r'x\'x'
>> >>>>> b = 'x\'x'
>> >>>>> a == b
>> >> False
>> >>>>> b.replace(a, 'foo') # shouldn't work
>> >> "x'x"
>> >>>>> import re
>> >>>>> re.sub(a, 'foo', b) # should work
>> >> 'foo'
>> >>
>> >> Despite the fact that r'x\'x' is really "x\\'x", the regex engine
>> >> turns "\\'" back into a "'" and the expression functions as expected.
>> >
>> > You are right. Assuming the raw strings are used as regexp then it for
>> > all(?) practical purposes doesn't matter that there are extra \'s in
>> > the string. It just looks confusing and sloppy, IMHO.
>> 
>> I agree on the sloppiness.
>> 
>> But there's also another small reason when it comes to docstrings:
>> they're extracted by i18n/hggettext and to add line numbers to the .po
>> file, it searches for the docstring in the original .py file. If there
>> are extra backslashes in the docstring, then it cannot find the
>> docstring and so it defaults to a line number of 1.
>
> Do we have docstrings prefixed with 'r'?

No, I don't think so, but the same applies to triple-quoted strings
where " and \" are the same. If this was only about single-quoted
strings, then of course my answer makes no sense :-)

-- 
Martin Geisler

Mercurial links: http://mercurial.ch/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20111109/e6f5e0e9/attachment.pgp>


More information about the Mercurial-devel mailing list