[PATCH 2 of 3 RFC] Remove all uses of hasattr(a, b) in favor of getattr(a, b, None)

Pierre-Yves David pierre-yves.david at logilab.fr
Thu Mar 3 03:40:53 CST 2011


On Wed, Mar 02, 2011 at 04:17:33PM -0600, Augie Fackler wrote:
> On Mar 2, 2011, at 4:16 PM, Matt Mackall wrote:
> > 
> > On Tue, 2011-03-01 at 23:36 -0600, Augie Fackler wrote:
> >> # HG changeset patch
> >> # User Augie Fackler <durin42 at gmail.com>
> >> # Date 1299024193 21600
> >> # Branch stable
> >> # Node ID 75e012dfe383900f1932bdb0831eb44a11d35876
> >> # Parent  f202bb8bc5e81a41893144065cc634a786d549b0
> >> Remove all uses of hasattr(a, b) in favor of getattr(a, b, None)
> >> 
> >> hasattr() in Python swallows all exceptions rather than only
> >> AttributeError, making it unsafe for general use. getattr(a, b, None)
> >> is a mostly-compatible replacement (it fails to distinguish missing
> >> attributes and None-value attributes) that we can use instead.
> > 
> > I can't say I'm thrilled about papering over this Python wart in our
> > code. I'd rather leave our code as it is and complain at the Python
> > folks.
> 
> FWIW, python-dev has already made it clear this will *not* change in Python 2.x, and it's up in the air last I could tell if it would change in the 3.x series.

I read the fix in the 3.2 changelog.

> > Is this change likely to catch real bugs? How do other folks feel?

Last week I caught a bug silently running for some weeks because of this python bug.

-- 
Pierre-Yves
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20110303/877fe07b/attachment.pgp>


More information about the Mercurial-devel mailing list