[PATCH 3 of 8] url: remove is-comparisons with non-singletons
Mads Kiilerich
mads at kiilerich.com
Tue Nov 16 17:21:48 CST 2010
Dan Villiom Podlaski Christiansen wrote, On 11/16/2010 09:36 PM:
> # HG changeset patch
> # User Dan Villiom Podlaski Christiansen<danchr at gmail.com>
> # Date 1289939758 -3600
> # Branch stable
> # Node ID 72e7c8769e4bb6cbfec1e796fd06e3416da51b26
> # Parent b5093e797e19679cc8e7b01a7fd41feba94c6c27
> url: remove is-comparisons with non-singletons.
>
> There are two types of is-comparisons in this file:
>
> The first one is unnessecary as _GLOBAL_DEFAULT_TIMEOUT is an
> unspecialized object, and thus has no notion of equality other than
> its identity.
>
> The second one, in the auth handlers, seem unnecessary as well. The
> urllib2.Request type does not provide an equality implementation, so
> regular comparison should fall back to the exact same semantics.
FWIW I agree with mpm that it is important that we don't use 'is' to
compare integers and strings.
But 'is' is the right operator to use when we want to check if it really
is the same object (which is different from x==y but almost the same as
id(x)==id(y)). '==' might fall back to 'is', but using 'is' is more
explicit and clear.
> @@ -593,7 +593,7 @@ class httpdigestauthhandler(urllib2.HTTP
>
> def http_error_auth_reqed(self, auth_header, host, req, headers):
> # Reset the retry counter once for each request.
> - if req is not self.retried_req:
> + if req != self.retried_req:
> self.retried_req = req
> self.retried = 0
> # In python< 2.5 AbstractDigestAuthHandler raises a ValueError if
> @@ -621,7 +621,7 @@ class httpbasicauthhandler(urllib2.HTTPB
>
> def http_error_auth_reqed(self, auth_header, host, req, headers):
> # Reset the retry counter once for each request.
> - if req is not self.retried_req:
> + if req != self.retried_req:
> self.retried_req = req
> self.retried = 0
> return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed(
Objection. I intentionally wrote it this way to express that retried is
reset if it no longer is the same request we saw last time we retried.
Are two different requests for the same URL equal? Perhaps - that
depends on the implementation. But they are not the same request - and
that is what matters here.
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.
/Mads
More information about the Mercurial-devel
mailing list