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

Augie Fackler durin42 at gmail.com
Wed Mar 2 16:17:33 CST 2011


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.

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





More information about the Mercurial-devel mailing list