Patchwork [5,of,5] util: make safehasattr() a pycompat function

login
register
mail settings
Submitter Yuya Nishihara
Date March 24, 2018, 9:53 a.m.
Message ID <92e5b042b91626cb9016.1521885237@mimosa>
Download mbox | patch
Permalink /patch/29815/
State Accepted
Headers show

Comments

Yuya Nishihara - March 24, 2018, 9:53 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521869072 -32400
#      Sat Mar 24 14:24:32 2018 +0900
# Node ID 92e5b042b91626cb9016e841ba823515d56f787e
# Parent  5814bdd0d29ae544f62ca6a02678137b7e9de397
util: make safehasattr() a pycompat function

So safehasattr() can be imported by utils.* modules. util.safehasattr() still
remains as an alias since it is pretty basic utility available for years.

On current Python 3, the builtin hasattr() should have no problem.

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -243,6 +243,8 @@  if ispy3:
     def open(name, mode='r', buffering=-1, encoding=None):
         return builtins.open(name, sysstr(mode), buffering, encoding)
 
+    safehasattr = _wrapattrfunc(builtins.hasattr)
+
     def _getoptbwrapper(orig, args, shortlist, namelist):
         """
         Takes bytes arguments, converts them to unicode, pass them to
@@ -326,6 +328,11 @@  else:
     def getdoc(obj):
         return getattr(obj, '__doc__', None)
 
+    _notset = object()
+
+    def safehasattr(thing, attr):
+        return getattr(thing, attr, _notset) is not _notset
+
     def _getoptbwrapper(orig, args, shortlist, namelist):
         return orig(args, shortlist, namelist)
 
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -67,6 +67,7 @@  empty = pycompat.empty
 httplib = pycompat.httplib
 pickle = pycompat.pickle
 queue = pycompat.queue
+safehasattr = pycompat.safehasattr
 socketserver = pycompat.socketserver
 stderr = pycompat.stderr
 stdin = pycompat.stdin
@@ -177,9 +178,6 @@  except AttributeError:
 
 _notset = object()
 
-def safehasattr(thing, attr):
-    return getattr(thing, attr, _notset) is not _notset
-
 def _rapply(f, xs):
     if xs is None:
         # assume None means non-value of optional data