The 'is' operator and check-code
Adrian Buehlmann
adrian at cadifra.com
Wed Nov 17 04:04:36 CST 2010
On 16.11.2010 20:40, Matt Mackall wrote:
> I'm throwing this out in case any Python experts have opinions.
>
> The 'is' operator is used in a bunch of places as a synonym for ==. This
> happens to work with short strings and small integers (0 to 256) in
> CPython, but that's by no means guaranteed by the language, so there are
> places where we're just getting lucky. So I propose we disallow using it
> for anything that's not one of the True/False/None singletons. Here's my
> regex hack:
>
> diff -r 340c46028ebd contrib/check-code.py
> --- a/contrib/check-code.py Tue Nov 16 12:55:07 2010 -0600
> +++ b/contrib/check-code.py Tue Nov 16 13:35:04 2010 -0600
> @@ -130,6 +130,7 @@
> (r'[\x80-\xff]', "non-ASCII character literal"),
> (r'("\')\.format\(', "str.format() not available in Python 2.4"),
> (r'^\s*with\s+', "with not available in Python 2.4"),
> + (r' is\s+(not\s+)?[^TFNn]', "object comparison with non-singleton"),
> (r'(?<!def)\s+(any|all|format)\(',
> "any/all/format not available in Python 2.4"),
> (r'(?<!def)\s+(callable)\(',
>
and elsewhere:
On 17.11.2010 09:05, Martin Geisler wrote:
> Mads Kiilerich <mads at kiilerich.com> writes:
>
>> I won't claim to be a Python expert, but I don't think the check-code
>> check proposed by Matt is a good idea. It can't be made reliable -
>> unless we say that all uses of 'is' has to be white-listed somehow.
>
> I propose that we just blacklist using of 'is' with string and numeric
> literals. That would be something like this:
>
> (r''' is\s+(not\s+)?["'0-9]''', "object comparison with literal"),
Would it be possible to catch negative numbers too?
> This does not catch the
>
> if err.errno is errno.ENOENT:
>
> line, but I can live with that.
I think Martin's proposal is an improvement over the status quo, so I'm
+1 for doing that.
More information about the Mercurial-devel
mailing list