From: W. Trevor King Date: Sat, 28 Aug 2010 22:58:38 +0000 (-0400) Subject: Don't distinguish between ASCII unicode and strings in YAML output. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=608071d67afc1775941505c6684d31a279f041a0;p=hooke.git Don't distinguish between ASCII unicode and strings in YAML output. --- diff --git a/hooke/util/yaml.py b/hooke/util/yaml.py index b4fa7de..25d62fc 100644 --- a/hooke/util/yaml.py +++ b/hooke/util/yaml.py @@ -18,7 +18,7 @@ The default behavior is to crash. >>> yaml.Dumper.yaml_representers.pop(numpy.ndarray) # doctest: +ELLIPSIS ->>> print yaml.dump(a) +>>> print yaml.dump(a) # doctest: +REPORT_UDIFF !!python/object/apply:numpy.core.multiarray._reconstruct args: - !!python/name:numpy.ndarray '' @@ -27,7 +27,9 @@ args: state: !!python/tuple - 1 - !!python/tuple [3] -- null +- !!python/object/apply:numpy.dtype + args: [i4, 0, 1] + state: !!python/tuple [3, <, null, null, null, -1, -1, 0] - false - "\\x01\\0\\0\\0\\x02\\0\\0\\0\\x03\\0\\0\\0" @@ -44,6 +46,19 @@ Must be because of the other representers I've loaded since. Restore the representer for future tests. >>> yaml.add_representer(numpy.ndarray, none_representer) + +We also avoid !!python/unicode tags by sacrificing the string/unicode +distinction. + +>>> yaml.dump('ascii', allow_unicode=True) +'ascii\\n...\\n' +>>> yaml.dump(u'ascii', allow_unicode=True) +'ascii\\n...\\n' +>>> a = yaml.dump(u'Fran\\xe7ois', allow_unicode=True) +>>> a +'Fran\\xc3\\xa7ois\\n...\\n' +>>> unicode(a, 'utf-8') +u'Fran\\xe7ois\\n...\\n' """ from __future__ import absolute_import @@ -95,6 +110,9 @@ else: yaml.representer.SafeRepresenter.ignore_aliases = staticmethod( ignore_aliases) +def unicode_representer(dumper, data): + return dumper.represent_scalar(u'tag:yaml.org,2002:str', data) +yaml.add_representer(unicode, unicode_representer) def none_representer(dumper, data): return dumper.represent_none(None)