Mercurial > public > mercurial-scm > hg
comparison mercurial/templateutil.py @ 50925:d718eddf01d9
safehasattr: drop usage in favor of hasattr
The two functions should now be equivalent at least in their usage in core.
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Thu, 31 Aug 2023 23:56:15 +0200 |
parents | 26e63204c31e |
children | 18c8c18993f0 |
comparison
equal
deleted
inserted
replaced
50924:7a8ea1397816 | 50925:d718eddf01d9 |
---|---|
279 item = unwrapastype(context, mapping, item, self._keytype) | 279 item = unwrapastype(context, mapping, item, self._keytype) |
280 return item in self._values | 280 return item in self._values |
281 | 281 |
282 def getmember(self, context, mapping, key): | 282 def getmember(self, context, mapping, key): |
283 # TODO: maybe split hybrid list/dict types? | 283 # TODO: maybe split hybrid list/dict types? |
284 if not util.safehasattr(self._values, 'get'): | 284 if not hasattr(self._values, 'get'): |
285 raise error.ParseError(_(b'not a dictionary')) | 285 raise error.ParseError(_(b'not a dictionary')) |
286 key = unwrapastype(context, mapping, key, self._keytype) | 286 key = unwrapastype(context, mapping, key, self._keytype) |
287 return self._wrapvalue(key, self._values.get(key)) | 287 return self._wrapvalue(key, self._values.get(key)) |
288 | 288 |
289 def getmin(self, context, mapping): | 289 def getmin(self, context, mapping): |
299 return self._wrapvalue(val, val) | 299 return self._wrapvalue(val, val) |
300 | 300 |
301 def _wrapvalue(self, key, val): | 301 def _wrapvalue(self, key, val): |
302 if val is None: | 302 if val is None: |
303 return | 303 return |
304 if util.safehasattr(val, '_makemap'): | 304 if hasattr(val, '_makemap'): |
305 # a nested hybrid list/dict, which has its own way of map operation | 305 # a nested hybrid list/dict, which has its own way of map operation |
306 return val | 306 return val |
307 return hybriditem(None, key, val, self._makemap) | 307 return hybriditem(None, key, val, self._makemap) |
308 | 308 |
309 def filter(self, context, mapping, select): | 309 def filter(self, context, mapping, select): |
310 if util.safehasattr(self._values, 'get'): | 310 if hasattr(self._values, 'get'): |
311 values = { | 311 values = { |
312 k: v | 312 k: v |
313 for k, v in self._values.items() | 313 for k, v in self._values.items() |
314 if select(self._wrapvalue(k, v)) | 314 if select(self._wrapvalue(k, v)) |
315 } | 315 } |
339 return bool(self._values) | 339 return bool(self._values) |
340 | 340 |
341 def tovalue(self, context, mapping): | 341 def tovalue(self, context, mapping): |
342 # TODO: make it non-recursive for trivial lists/dicts | 342 # TODO: make it non-recursive for trivial lists/dicts |
343 xs = self._values | 343 xs = self._values |
344 if util.safehasattr(xs, 'get'): | 344 if hasattr(xs, 'get'): |
345 return {k: unwrapvalue(context, mapping, v) for k, v in xs.items()} | 345 return {k: unwrapvalue(context, mapping, v) for k, v in xs.items()} |
346 return [unwrapvalue(context, mapping, x) for x in xs] | 346 return [unwrapvalue(context, mapping, x) for x in xs] |
347 | 347 |
348 | 348 |
349 class hybriditem(mappable, wrapped): | 349 class hybriditem(mappable, wrapped): |
856 b'Mercurial IO including templates is done' | 856 b'Mercurial IO including templates is done' |
857 b' with bytes, not strings, got %r' % thing | 857 b' with bytes, not strings, got %r' % thing |
858 ) | 858 ) |
859 elif thing is None: | 859 elif thing is None: |
860 pass | 860 pass |
861 elif not util.safehasattr(thing, '__iter__'): | 861 elif not hasattr(thing, '__iter__'): |
862 yield pycompat.bytestr(thing) | 862 yield pycompat.bytestr(thing) |
863 else: | 863 else: |
864 for i in thing: | 864 for i in thing: |
865 if isinstance(i, wrapped): | 865 if isinstance(i, wrapped): |
866 i = i.show(context, mapping) | 866 i = i.show(context, mapping) |
867 if isinstance(i, bytes): | 867 if isinstance(i, bytes): |
868 yield i | 868 yield i |
869 elif i is None: | 869 elif i is None: |
870 pass | 870 pass |
871 elif not util.safehasattr(i, '__iter__'): | 871 elif not hasattr(i, '__iter__'): |
872 yield pycompat.bytestr(i) | 872 yield pycompat.bytestr(i) |
873 else: | 873 else: |
874 for j in flatten(context, mapping, i): | 874 for j in flatten(context, mapping, i): |
875 yield j | 875 yield j |
876 | 876 |