Re: Ruby and Python YAML 1.{0,1} Compatibility

Anders Arpteg <[email protected]>
Newsgroups gmane.text.yaml.general
Message-ID <[email protected]>
Just a small update. I found a really small and nice solution to the
problem. The following module will make python able to parse ruby yaml
objects better, and solves all my problems. Hope that someone else also
finds use for it. Just use ruby_yaml.safe_load(xxx) instead of yaml and it
will work much better. Here is ruby_yaml.py:

from yaml import *

# Add constructors for special ruby yaml objects
for loader in (Loader, SafeLoader):
    add_constructor(u'!binary',
constructor.SafeConstructor.construct_yaml_binary, loader)
    add_constructor(u'!map:HashWithIndifferentAccess',
constructor.SafeConstructor.construct_mapping, loader)

Sincerely,
Anders

On Tue, Jul 27, 2010 at 11:26, Dr AA <[email protected]> wrote:

> Hi,
>
> We are having problem with the Ruby and the Python implementation of
> YAML. Ruby seems to be YAML 1.0 and Python shall be YAML 1.1
> compatible. Hopefully, one would wish that the Python implementation
> was backward compatible and able to parse Ruby encoded data. However,
> that seems not to be the case. See example below.
>
> I have tried with the PySyck module that is YAML 1.0 compatible, and
> it can then parse the Ruby YAML. However, that project seems abandoned
> and seems to have problems with certain python versions. Anyone with
> similar problems, and possible solutions?
>
> This cause serious problems for us and it seems very strange that YAML
> is not even backwards compatible. Perhaps we have to switch to JSON or
> some other format to overcome this problem, but hopefully have I
> overlooked some issue/solution that someone in this mailing list can
> help me with.
>
> Thanks in advance!
> Anders
>
> Ruby 1.9.0:
>
> >> require 'yaml'
> => []
> >> YAML.dump('å')
> => "--- !binary |\nw6U=\n\n"
>
> Python 2.6.5:
>
> >>> import yaml
> >>> yaml.safe_load("--- !binary |\nw6U=\n\n")
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "/usr/lib/python2.6/dist-packages/yaml/__init__.py", line 75,
> in safe_load
>    return load(stream, SafeLoader)
>  File "/usr/lib/python2.6/dist-packages/yaml/__init__.py", line 58,
> in load
>    return loader.get_single_data()
>  File "/usr/lib/python2.6/dist-packages/yaml/constructor.py", line
> 42, in get_single_data
>    node = self.get_single_node()
>  File "/usr/lib/python2.6/dist-packages/yaml/composer.py", line 39,
> in get_single_node
>    if not self.check_event(StreamEndEvent):
>  File "/usr/lib/python2.6/dist-packages/yaml/parser.py", line 93, in
> check_event
>    self.current_event = self.state()
>  File "/usr/lib/python2.6/dist-packages/yaml/parser.py", line 169, in
> parse_document_start
>    self.peek_token().start_mark)
> yaml.parser.ParserError: expected '<document start>', but found
> '<scalar>'
>  in "<string>", line 2, column 1:
>    w6U=
>    ^
> >>>
>
>

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share 
of $1 Million in cash or HP Products. Visit us here for more details:
http://ad.doubleclick.net/clk;226879339;13503038;l?
http://clk.atdmt.com/CRS/go/247765532/direct/01/

_______________________________________________
Yaml-core mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/yaml-core
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.