py3: fixing zonemgr_callback

Since OptionParser behaves differently in Python 2/3,
zonemgr_callback now handles value as str in both version.

https://pagure.io/freeipa/issue/5990

Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
This commit is contained in:
Felipe Volpone 2017-07-27 08:24:44 -03:00 committed by Pavel Vomacka
parent 06fbf4b312
commit 75d26e1f01
2 changed files with 7 additions and 3 deletions

View File

@ -380,7 +380,7 @@ def validate_domain_name(domain_name, allow_underscore=False, allow_slash=False)
def validate_zonemgr(zonemgr):
assert isinstance(zonemgr, DNSName)
if any('@' in label for label in zonemgr.labels):
if any(b'@' in label for label in zonemgr.labels):
raise ValueError(_('too many \'@\' characters'))

View File

@ -423,7 +423,11 @@ def zonemgr_callback(option, opt_str, value, parser):
encoding = getattr(sys.stdin, 'encoding', None)
if encoding is None:
encoding = 'utf-8'
value = value.decode(encoding)
# value is of a string type in both py2 and py3
if not isinstance(value, unicode):
value = value.decode(encoding)
validate_zonemgr_str(value)
except ValueError as e:
# FIXME we can do this in better way
@ -433,7 +437,7 @@ def zonemgr_callback(option, opt_str, value, parser):
if stderr_encoding is None:
stderr_encoding = 'utf-8'
error = unicode(e).encode(stderr_encoding)
parser.error("invalid zonemgr: " + error)
parser.error(b"invalid zonemgr: " + error)
parser.values.zonemgr = value