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

Matt Mackall mpm at selenic.com
Wed Mar 2 16:16:21 CST 2011


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.

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

-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list