Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/templatefuncs.py @ 38279:fb874fc1d9b4
templater: abstract ifcontains() over wrapped types
This allows us to make .keytype private.
There's a minor BC that a hybrid dict/list of keytype=None now strictly
checks the type of the needle. For example, {ifcontains(rev, files)} no longer
matches a file named "1" at the rev=1. I made this change for consistency
with the get(dict, key) function. We can restore the old behavior by making
keytype=bytes the default if desired.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 19 Mar 2018 00:23:20 +0900 |
parents | 41ae9b3cbfb9 |
children | 8d6109b49b31 |
comparison
equal
deleted
inserted
replaced
38278:80f423a14c90 | 38279:fb874fc1d9b4 |
---|---|
289 on whether the item "needle" is in "haystack".""" | 289 on whether the item "needle" is in "haystack".""" |
290 if not (3 <= len(args) <= 4): | 290 if not (3 <= len(args) <= 4): |
291 # i18n: "ifcontains" is a keyword | 291 # i18n: "ifcontains" is a keyword |
292 raise error.ParseError(_("ifcontains expects three or four arguments")) | 292 raise error.ParseError(_("ifcontains expects three or four arguments")) |
293 | 293 |
294 haystack = evalfuncarg(context, mapping, args[1]) | 294 haystack = evalwrapped(context, mapping, args[1]) |
295 keytype = getattr(haystack, 'keytype', None) | |
296 try: | 295 try: |
297 needle = evalrawexp(context, mapping, args[0]) | 296 needle = evalrawexp(context, mapping, args[0]) |
298 needle = templateutil.unwrapastype(context, mapping, needle, | 297 found = haystack.contains(context, mapping, needle) |
299 keytype or bytes) | |
300 found = (needle in haystack) | |
301 except error.ParseError: | 298 except error.ParseError: |
302 found = False | 299 found = False |
303 | 300 |
304 if found: | 301 if found: |
305 return evalrawexp(context, mapping, args[2]) | 302 return evalrawexp(context, mapping, args[2]) |