changeset 37102:6ca5f825a0ca

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.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 24 Mar 2018 14:24:32 +0900
parents a9ea2b1e5c4b
children 9e6d3465f17e
files mercurial/pycompat.py mercurial/util.py
diffstat 2 files changed, 8 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/pycompat.py	Sat Mar 24 14:13:37 2018 +0900
+++ b/mercurial/pycompat.py	Sat Mar 24 14:24:32 2018 +0900
@@ -243,6 +243,8 @@
     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 @@
     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)
 
--- a/mercurial/util.py	Sat Mar 24 14:13:37 2018 +0900
+++ b/mercurial/util.py	Sat Mar 24 14:24:32 2018 +0900
@@ -67,6 +67,7 @@
 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 @@
 
 _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