Skip to content

Commit

Permalink
support PyYAML 4.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Jun 27, 2018
1 parent 155ec0c commit b057a59
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ python:
- "pypy"
- "pypy3.5"

env:
- PYYAML_VERSION="3.12"
- PYYAML_VERSION="4.1"

matrix:
fast_finish: true
allow_failures:
- python: "nightly"

install:
- pip install pyyaml~=$PYYAML_VERSION
- pip install --editable .
- pip install pytest-cov coveralls

Expand Down
17 changes: 12 additions & 5 deletions oyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ def map_constructor(loader, node):
loader.flatten_mapping(node)
return OrderedDict(loader.construct_pairs(node))


pyyaml.add_representer(dict, map_representer)
pyyaml.add_representer(OrderedDict, map_representer)
pyyaml.add_representer(dict, map_representer, Dumper=pyyaml.dumper.SafeDumper)
pyyaml.add_representer(OrderedDict, map_representer, Dumper=pyyaml.dumper.SafeDumper)
if pyyaml.safe_dump is pyyaml.dump:
# PyYAML >= 4
SafeDumper = pyyaml.dumper.Dumper
DangerDumper = pyyaml.dumper.DangerDumper
else:
SafeDumper = pyyaml.dumper.SafeDumper
DangerDumper = pyyaml.dumper.Dumper

pyyaml.add_representer(dict, map_representer, Dumper=SafeDumper)
pyyaml.add_representer(OrderedDict, map_representer, Dumper=SafeDumper)
pyyaml.add_representer(dict, map_representer, Dumper=DangerDumper)
pyyaml.add_representer(OrderedDict, map_representer, Dumper=DangerDumper)


if sys.version_info < (3, 7):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='oyaml',
version='0.4',
version='0.5',
description='Ordered YAML: drop-in replacement for PyYAML which preserves dict ordering',
long_description=open('README.rst').read(),
author='Wim Glenn',
Expand Down
16 changes: 13 additions & 3 deletions test_oyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,28 @@ def test_loads_to_std_dict():
assert isinstance(loaded, dict)


def test_subclass_dump():
class MyOrderedDict(OrderedDict):
pass

class MyOrderedDict(OrderedDict):
pass

@pytest.mark.skipif(yaml.pyyaml.__version__ >= '4', reason="requires PyYAML version < 4")
def test_subclass_dump_pyyaml3():
data = MyOrderedDict([('x', 1), ('y', 2)])
assert '!!python/object/apply:test_oyaml.MyOrderedDict' in yaml.dump(data)
with pytest.raises(yaml.pyyaml.representer.RepresenterError) as cm:
yaml.safe_dump(data)
assert str(cm.value) == "cannot represent an object: MyOrderedDict([('x', 1), ('y', 2)])"


@pytest.mark.skipif(yaml.pyyaml.__version__ < '4', reason="requires PyYAML version >= 4")
def test_subclass_dump_pyyaml4():
data = MyOrderedDict([('x', 1), ('y', 2)])
assert '!!python/object/apply:test_oyaml.MyOrderedDict' in yaml.danger_dump(data)
with pytest.raises(yaml.pyyaml.representer.RepresenterError) as cm:
yaml.dump(data)
assert str(cm.value) == "('cannot represent an object', MyOrderedDict([('x', 1), ('y', 2)]))"


def test_anchors_and_references():
text = '''
defaults:
Expand Down

0 comments on commit b057a59

Please sign in to comment.