From 84cc48f91bacc83d00e65e1402e3478ae7dc9311 Mon Sep 17 00:00:00 2001 From: Wim Glenn Date: Mon, 23 Apr 2018 15:07:35 -0500 Subject: [PATCH] make sure dump and safe_dump match --- oyaml.py | 1 + setup.py | 2 +- test_oyaml.py | 9 +++++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/oyaml.py b/oyaml.py index dfc7977..0fdf8e4 100644 --- a/oyaml.py +++ b/oyaml.py @@ -18,6 +18,7 @@ def map_constructor(loader, 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) diff --git a/setup.py b/setup.py index 5aa46b7..0d8f6fb 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='oyaml', - version='0.3', + version='0.4', description='Ordered YAML: drop-in replacement for PyYAML which preserves dict ordering', long_description=open('README.rst').read(), author='Wim Glenn', diff --git a/test_oyaml.py b/test_oyaml.py index 80368c6..69ff6d1 100644 --- a/test_oyaml.py +++ b/test_oyaml.py @@ -3,7 +3,6 @@ from types import GeneratorType import pytest -from yaml.representer import RepresenterError import oyaml as yaml @@ -23,6 +22,12 @@ def test_dump_all(): assert yaml.dump_all(documents=[data, {}]) == '{x: 1, z: 3, y: 2}\n--- {}\n' +def test_dump_and_safe_dump_match(): + mydict = {'x': 1, 'z': 2, 'y': 3} + # don't know if mydict is ordered in the implementation or not (but don't care) + assert yaml.dump(mydict) == yaml.safe_dump(mydict) + + def test_safe_dump_all(): assert yaml.safe_dump_all(documents=[data, {}]) == '{x: 1, z: 3, y: 2}\n--- {}\n' @@ -60,7 +65,7 @@ class MyOrderedDict(OrderedDict): data = MyOrderedDict([('x', 1), ('y', 2)]) assert '!!python/object/apply:test_oyaml.MyOrderedDict' in yaml.dump(data) - with pytest.raises(RepresenterError) as cm: + 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)])"