From ca826508d992f99ab6cba75d03a34b804448dfef Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Thu, 15 Oct 2015 09:38:14 +0300 Subject: [PATCH 1/2] Python 3: fix AnsibleError formatting If you convert the error string to bytes and embed it inside another error string, you get Prefix: b'Embedded\nerror\nstring' which is not what we want. But we also don't want Unicode in error messages causing unexpected UnicodeEncodeErrors when on Python 2. So let's convert the error message into the native string type (bytes on Python 2, unicode on Python 3). --- lib/ansible/errors/__init__.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/ansible/errors/__init__.py b/lib/ansible/errors/__init__.py index f46c4f34c9..c88f2bb864 100644 --- a/lib/ansible/errors/__init__.py +++ b/lib/ansible/errors/__init__.py @@ -19,11 +19,18 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import os - from ansible.errors.yaml_strings import * from ansible.utils.unicode import to_unicode, to_bytes + +if str is bytes: + # Python 2 + to_str = to_bytes +else: + # Python 3 + to_str = to_unicode + + class AnsibleError(Exception): ''' This is the base class for all errors raised from Ansible code, @@ -49,7 +56,7 @@ class AnsibleError(Exception): if obj and isinstance(obj, AnsibleBaseYAMLObject): extended_error = self._get_extended_error() if extended_error: - self.message = 'ERROR! %s\n\n%s' % (message, to_bytes(extended_error)) + self.message = 'ERROR! %s\n\n%s' % (message, to_str(extended_error)) else: self.message = 'ERROR! %s' % message From ab569cea2253fd70bc8b3f0a2540ac0d075e8c4a Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Thu, 15 Oct 2015 10:27:38 +0300 Subject: [PATCH 2/2] Move to_str alias into ansible.utils.unicode @abadger suggested on IRC that it's useful enough to go into ansible.utils.unicode. --- lib/ansible/errors/__init__.py | 10 +--------- lib/ansible/utils/unicode.py | 7 +++++++ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/ansible/errors/__init__.py b/lib/ansible/errors/__init__.py index c88f2bb864..d8f6a61c97 100644 --- a/lib/ansible/errors/__init__.py +++ b/lib/ansible/errors/__init__.py @@ -20,15 +20,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type from ansible.errors.yaml_strings import * -from ansible.utils.unicode import to_unicode, to_bytes - - -if str is bytes: - # Python 2 - to_str = to_bytes -else: - # Python 3 - to_str = to_unicode +from ansible.utils.unicode import to_unicode, to_str class AnsibleError(Exception): diff --git a/lib/ansible/utils/unicode.py b/lib/ansible/utils/unicode.py index a63c1960e1..4d69c707ce 100644 --- a/lib/ansible/utils/unicode.py +++ b/lib/ansible/utils/unicode.py @@ -251,3 +251,10 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None): # ensure that a filter will return unicode values. def unicode_wrap(func, *args, **kwargs): return to_unicode(func(*args, **kwargs), nonstring='passthru') + + +# Alias for converting to native strings. +if PY3: + to_str = to_unicode +else: + to_str = to_bytes