From b95e3d18a7e0c93ffac24917e928a05f26542d87 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Thu, 10 Sep 2015 08:57:53 +0300 Subject: [PATCH] Python 3: use the right PyYAML SafeRepresenter for unicode PyYAML has a SafeRepresenter in lib/... that defines def represent_unicode(self, data): return self.represent_scalar(u'tag:yaml.org,2002:str', data) and a different SafeRepresenter in lib3/... that defines def represent_str(self, data): return self.represent_scalar('tag:yaml.org,2002:str', data) so the right thing to do on Python 3 is to use represent_str. (AnsibleUnicode is a subclass of six.text_type, i.e. 'str' on Python 3.) --- lib/ansible/parsing/yaml/dumper.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/ansible/parsing/yaml/dumper.py b/lib/ansible/parsing/yaml/dumper.py index dc498acd066..3c7bd3120be 100644 --- a/lib/ansible/parsing/yaml/dumper.py +++ b/lib/ansible/parsing/yaml/dumper.py @@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type import yaml +from six import PY3 from ansible.parsing.yaml.objects import AnsibleUnicode @@ -30,8 +31,13 @@ class AnsibleDumper(yaml.SafeDumper): ''' pass +if PY3: + represent_unicode = yaml.representer.SafeRepresenter.represent_str +else: + represent_unicode = yaml.representer.SafeRepresenter.represent_unicode + AnsibleDumper.add_representer( AnsibleUnicode, - yaml.representer.SafeRepresenter.represent_unicode + represent_unicode, )