1. Kirill Simonov
  2. pyyaml
  3. Issues
Issue #20 new

CDumper behaves different from Dumper when dumping sequences of objects, unlike stated in the documentation

Jannis Pohlmann
created an issue

http://pyyaml.org/wiki/PyYAMLDocumentation states that CDumper is a libyaml-based implementation of Dumper (just like CBaseDumper is a C implementation of BaseDumper etc.).

However, the behaviour of CDumper is different from Dumper when representing sequences of Python objects. Here is my test code:

#!/usr/bin/env python

import yaml

class Foo(yaml.YAMLObject):

    yaml_tag = u'!Foo'

    def __init__(self, value):
        self.value = value

    def to_yaml(cls, dumper, obj):
        return dumper.represent_scalar(u'tag:yaml.org,2002:str', obj.value)

foos = [Foo('first'), Foo('second')]

print 'Dumper:'
print yaml.dump(foos, Dumper=yaml.Dumper)

print 'CDumper:'
print yaml.dump(foos, Dumper=yaml.CDumper)

And here is the output:

[first, second]

- !!python/object:__main__.Foo {value: first}
- !!python/object:__main__.Foo {value: second}

I would expect the output to be

[first, second]

in both cases.

Comments (1)

  1. Jannis Pohlmann reporter

    I just realised that it is independent of whether or not the objects are in a sequence. The CDumper doesn't represent individual Foo object as strings either.

    This may not be a bug in the code. But if it isn't then the documentation should probably be fixed by adding a note saying that the CDumper does not support this feature.

  2. Log in to comment