Use to_text on value to activate _fail_with_undefined_error (#58214)

* Use to_text on value to activate _fail_with_undefined_error. Fixes #12186

* Add comment explaining in depth what to_text gives us
This commit is contained in:
Matt Martz 2019-06-24 12:43:18 -05:00 committed by GitHub
parent 958f740cc3
commit 57d8b68c03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 0 deletions

View file

@ -0,0 +1,3 @@
bugfixes:
- regex tests - Fail with undefined error if the value is
undefined (https://github.com/ansible/ansible/issues/12186)

View file

@ -24,6 +24,7 @@ import operator as py_operator
from distutils.version import LooseVersion, StrictVersion from distutils.version import LooseVersion, StrictVersion
from ansible import errors from ansible import errors
from ansible.module_utils._text import to_text
from ansible.module_utils.common._collections_compat import MutableMapping, MutableSequence from ansible.module_utils.common._collections_compat import MutableMapping, MutableSequence
from ansible.utils.display import Display from ansible.utils.display import Display
@ -114,6 +115,9 @@ def regex(value='', pattern='', ignorecase=False, multiline=False, match_type='s
This is likely only useful for `search` and `match` which already This is likely only useful for `search` and `match` which already
have their own filters. have their own filters.
''' '''
# In addition to ensuring the correct type, to_text here will ensure
# _fail_with_undefined_error happens if the value is Undefined
value = to_text(value, errors='surrogate_or_strict')
flags = 0 flags = 0
if ignorecase: if ignorecase:
flags |= re.I flags |= re.I