[PATCH 2 of 6 V2] perf: define util.safehasattr forcibly for Mercurial earlier than 1.9.3

Yuya Nishihara yuya at tcha.org
Sat Jul 9 07:54:34 EDT 2016


On Tue, 05 Jul 2016 07:37:03 +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
> # Date 1467671151 -32400
> #      Tue Jul 05 07:25:51 2016 +0900
> # Node ID bb7723a5b052c2169498e568a671f46ec7a82ae4
> # Parent  121d38c22d5a11fc793aae9f3919371c9eb1f6b7
> perf: define util.safehasattr forcibly for Mercurial earlier than 1.9.3
> 
> Before this patch, using util.safehasattr() prevents perf.py from
> being loaded by Mercurial earlier than 1.9.3 (or 94b200a11cf7),
> because util.safehasattr() isn't available in such Mercurial, even
> though there are some code paths for Mercurial earlier than 1.9.3.
> 
> For example, setting "_prereadsize" attribute in perfindex() and
> perfnodelookup() is effective only with Mercurial earlier than 1.8 (or
> 61c9bc3da402).
> 
> This patch is a preparation for using util.safehasattr() safely in
> subsequent patches.
> 
> This patch defines util.safehasattr() forcibly without examining
> whether it is available or not, because:
> 
>   - examining existence of "safehasattr" safely itself needs similar logic
>   - safehasattr() is small enough to define locally
> 
> diff --git a/contrib/perf.py b/contrib/perf.py
> --- a/contrib/perf.py
> +++ b/contrib/perf.py
> @@ -39,6 +39,14 @@ from mercurial import (
>      util,
>  )
>  
> +# for "historical portability":
> +# define util.safehasattr forcibly, because util.safehasattr has been
> +# available since 1.9.3 (or 94b200a11cf7)
> +_undefined = object()
> +def safehasattr(thing, attr):
> +    return getattr(thing, attr, _undefined) is not _undefined
> +setattr(util, 'safehasattr', safehasattr)

I'm not sure why we have to inject safehasattr() to util instead of simply
using safehasattr(), but that isn't a big deal.


More information about the Mercurial-devel mailing list