ipaclient_test: Fix Python2 decode use with Python3

This is a remain of the Python2 version and has been fixed.

Fixed: #86 (AttributeError: 'str' object has no attribute 'decode')
This commit is contained in:
Thomas Woerner
2019-06-12 11:00:59 +02:00
parent 6d328caa59
commit 3de056bc60

View File

@@ -469,23 +469,30 @@ def main():
# validate zonemgr # validate zonemgr
if options.zonemgr: if options.zonemgr:
try: if six.PY3:
# IDNA support requires unicode
encoding = getattr(sys.stdin, 'encoding', None)
if encoding is None:
encoding = 'utf-8'
value = options.zonemgr.decode(encoding)
with redirect_stdout(ansible_log): with redirect_stdout(ansible_log):
bindinstance.validate_zonemgr_str(value) bindinstance.validate_zonemgr_str(options.zonemgr)
except ValueError as e: else:
# FIXME we can do this in better way try:
# https://fedorahosted.org/freeipa/ticket/4804 # IDNA support requires unicode
# decode to proper stderr encoding encoding = getattr(sys.stdin, 'encoding', None)
stderr_encoding = getattr(sys.stderr, 'encoding', None) if encoding is None:
if stderr_encoding is None: encoding = 'utf-8'
stderr_encoding = 'utf-8' if not isinstance(value, unicode):
error = unicode(e).encode(stderr_encoding) value = options.zonemgr.decode(encoding)
ansible_module.fail_json(msg=error) else:
value = options.zonemgr
with redirect_stdout(ansible_log):
bindinstance.validate_zonemgr_str(value)
except ValueError as e:
# FIXME we can do this in better way
# https://fedorahosted.org/freeipa/ticket/4804
# decode to proper stderr encoding
stderr_encoding = getattr(sys.stderr, 'encoding', None)
if stderr_encoding is None:
stderr_encoding = 'utf-8'
error = unicode(e).encode(stderr_encoding)
ansible_module.fail_json(msg=error)
# external cert file paths are absolute # external cert file paths are absolute
for path in options.external_cert_files: for path in options.external_cert_files: