comparison mercurial/pycompat.py @ 37099: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 434e520adb8c
children 8da30ceae88f
comparison
equal deleted inserted replaced
37098:a9ea2b1e5c4b 37099:6ca5f825a0ca
241 unicode = str 241 unicode = str
242 242
243 def open(name, mode='r', buffering=-1, encoding=None): 243 def open(name, mode='r', buffering=-1, encoding=None):
244 return builtins.open(name, sysstr(mode), buffering, encoding) 244 return builtins.open(name, sysstr(mode), buffering, encoding)
245 245
246 safehasattr = _wrapattrfunc(builtins.hasattr)
247
246 def _getoptbwrapper(orig, args, shortlist, namelist): 248 def _getoptbwrapper(orig, args, shortlist, namelist):
247 """ 249 """
248 Takes bytes arguments, converts them to unicode, pass them to 250 Takes bytes arguments, converts them to unicode, pass them to
249 getopt.getopt(), convert the returned values back to bytes and then 251 getopt.getopt(), convert the returned values back to bytes and then
250 return them for Python 3 compatibility as getopt.getopt() don't accepts 252 return them for Python 3 compatibility as getopt.getopt() don't accepts
323 # better not to touch Python 2 part as it's already working fine. 325 # better not to touch Python 2 part as it's already working fine.
324 fsdecode = identity 326 fsdecode = identity
325 327
326 def getdoc(obj): 328 def getdoc(obj):
327 return getattr(obj, '__doc__', None) 329 return getattr(obj, '__doc__', None)
330
331 _notset = object()
332
333 def safehasattr(thing, attr):
334 return getattr(thing, attr, _notset) is not _notset
328 335
329 def _getoptbwrapper(orig, args, shortlist, namelist): 336 def _getoptbwrapper(orig, args, shortlist, namelist):
330 return orig(args, shortlist, namelist) 337 return orig(args, shortlist, namelist)
331 338
332 strkwargs = identity 339 strkwargs = identity