Reading in safe mode after round trip does not work if value contains colon
ruamel.yaml is a YAML 1.2 parser/emitter for Python
Brought to you by:
anthon
Steps to reproduce:
1 2 3 4 5 6 7 8 9 10 11 12 | import io import ruamel.yaml my_config = '{"url": "http://example.com"}' # note the ':' in the value yaml = ruamel.yaml.YAML(typ="rt") config = yaml.load(my_config) out_io = io.StringIO() yaml.dump(config, out_io) yaml = ruamel.yaml.YAML(typ="safe") in_io = io.StringIO(out_io.getvalue()) yaml.load(in_io) |
Error:
Traceback (most recent call last): File "foo.py", line 12, in <module> yaml.load(in_io) File "ruamel/yaml/main.py", line 325, in load return constructor.get_single_data() File "ruamel/yaml/constructor.py", line 106, in get_single_data node = self.composer.get_single_node() File "_ruamel_yaml.pyx", line 703, in _ruamel_yaml.CParser.get_single_node File "_ruamel_yaml.pyx", line 904, in _ruamel_yaml.CParser._parse_next_event ruamel.yaml.scanner.ScannerError: while scanning a plain scalar in "<file>", line 1, column 7 found unexpected ':' in "<file>", line 1, column 11
Workaround is to use type="rt"
in the last call to YAML.__init__
(originally posted on 2018-10-13 at 12:48:35 by Dimitri Merejkowsky <dmerejkowsky@bitbucket>)
None
(originally posted on 2018-10-13 at 12:48:51 by Dimitri Merejkowsky <dmerejkowsky@bitbucket>)
This is because the C code, coming from libyaml has errors in the scanner. Instead of using
type="rt"
, you can dotype='safe', pure=True
. That will get you the Python primitives, instead of theCommentedMap
etc, you get with round-trip-mode.(originally posted on 2018-10-13 at 13:06:39)
Oh. Did not know about
pure=True
.Turns out it solves an other problem I was having.
Thanks !
I guess we can close this issue, and perhaps talk about this is the documentation (if necessary)
(originally posted on 2018-10-13 at 13:52:26 by Dimitri Merejkowsky <dmerejkowsky@bitbucket>)
Well it needs to be solved. But I am probably not going to patch the libyaml much further, I rather start with code that also speeds up round-trip mode (libyaml strips the comments at a very low level).
I am glad I included the
pure=True
suggestion, I was just wondering why you were doing safe-loading (speed is of course much lower not using the C library).(originally posted on 2018-10-13 at 17:03:56)