[Re-send #1] Bug in pyyaml regarding representers and multi-line strings

Stuart Longland <[email protected]> Mon, 31 Jul 2017 09:10:47 +1000
Newsgroups gmane.text.yaml.general
Organization VRT Systems
Message-ID <[email protected]>
[Re-send #1, as I've received 0 replies either privately or via the
mailing list.  The problem persists.]

Hi all,

I've been scratching my head over this… I have some code that represents
multi-line strings as literal scalars in YAML (as IMO, they should be,
for readability).

If the string contains plain ASCII characters, all is well.  If however,
it contains some Unicode characters (in my case, it was "CO₂")… all hell
breaks loose and I wind up with a perfectly "valid", but completely
unreadable mess.

Attached is a script that reproduces the bug in both Python 2.7 and 3.4
with pyyaml-3.12.

Apologies if this is not the right place.  I tried logging in to the
Trac system on the pyyaml website, but there's no register link anywhere
and it won't accept Username: Anonymous, password: [email protected]
either, nor could I see any detail on how to register, or how to add a
ticket without logging in.

On the http://pyyaml.org front page, it says:
> YAML mailling list
> 
> There is no specific PyYAML mailling list, there is a general  yaml mailling list on sourceforge. 
and that led me here.  If there's a more appropriate place other than
/dev/null, I'm all ears.

Regards,
-- 
     _ ___             Stuart Longland - Systems Engineer
\  /|_) |                           T: +61 7 3535 9619
 \/ | \ |     38b Douglas Street    F: +61 7 3535 9699
   SYSTEMS    Milton QLD 4064       http://www.vrt.com.au

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

_______________________________________________
Yaml-core mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/yaml-core
pyyaml.py (text/x-python, 1.2 KB)
#!/usr/bin/env python
# vim: set sw=4 ts=4 et fileencoding=utf-8:

from __future__ import unicode_literals
try:
    from yaml import CDumper as YamlDumper
except ImportError:
    from yaml import Dumper as YamlDumper
from six import text_type
from yaml import dump, add_representer

# String that breaks YAML
break_string=u'''
This is a multi-line
  string that has some characters
that break pyyaml, such as these:

    ¹₂³₄⁵₆⁷₈
'''

# Configure YAML with a representation for multi-line strings so that we get
# sensible output that we can read.
# Credit:
# https://stackoverflow.com/questions/6432605/any-yaml-libraries-in-python-that-support-dumping-of-long-strings-as-block-liter
class MultilineString(text_type):
    """
    Wrapper class for yaml
    """
    def __repr__(self):
        return '%s(%s)' % (self.__class__.__name__, super(MultilineString, self).__repr__())

def multiline_string_representer(dumper, data):
    return dumper.represent_scalar(
            tag='tag:yaml.org,2002:str', value=data, style='|')

add_representer(MultilineString, multiline_string_representer)

print (dump(MultilineString(break_string), Dumper=YamlDumper, default_flow_style=False))