Skip to content

Commit 5152325

Browse files
committed
c loaders/dumpers
1 parent 6350d9d commit 5152325

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ sudo: false
44

55
python:
66
- "2.7"
7-
- "3.4"
87
- "3.5"
98
- "3.6"
10-
- "3.7-dev"
9+
- "3.7"
10+
- "3.8"
1111
- "nightly"
1212
- "pypy"
1313
- "pypy3"
@@ -16,7 +16,7 @@ env:
1616
- PYYAML_VERSION="3.13"
1717
# - PYYAML_VERSION="4.1" # this was pulled from the index (!) ..wtf, Ingy?
1818
- PYYAML_VERSION="4.2b4"
19-
- PYYAML_VERSION="5.1"
19+
- PYYAML_VERSION="5.3"
2020

2121
matrix:
2222
fast_finish: true

oyaml.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,29 @@ def map_constructor(loader, node):
2525
raise
2626

2727

28-
if pyyaml.safe_dump is pyyaml.dump:
29-
# PyYAML v4.x
30-
SafeDumper = pyyaml.dumper.Dumper
31-
DangerDumper = pyyaml.dumper.DangerDumper
28+
_loaders = [getattr(pyyaml.loader, x) for x in pyyaml.loader.__all__]
29+
_dumpers = [getattr(pyyaml.dumper, x) for x in pyyaml.dumper.__all__]
30+
try:
31+
_cyaml = pyyaml.cyaml.__all__
32+
except AttributeError:
33+
pass
3234
else:
33-
SafeDumper = pyyaml.dumper.SafeDumper
34-
DangerDumper = pyyaml.dumper.Dumper
35-
36-
pyyaml.add_representer(dict, map_representer, Dumper=SafeDumper)
37-
pyyaml.add_representer(OrderedDict, map_representer, Dumper=SafeDumper)
38-
pyyaml.add_representer(dict, map_representer, Dumper=DangerDumper)
39-
pyyaml.add_representer(OrderedDict, map_representer, Dumper=DangerDumper)
35+
_loaders += [getattr(pyyaml.cyaml, x) for x in _cyaml if x.endswith("Loader")]
36+
_dumpers += [getattr(pyyaml.cyaml, x) for x in _cyaml if x.endswith("Dumper")]
4037

38+
Dumper = None
39+
for Dumper in _dumpers:
40+
pyyaml.add_representer(dict, map_representer, Dumper=Dumper)
41+
pyyaml.add_representer(OrderedDict, map_representer, Dumper=Dumper)
4142

4243
Loader = None
4344
if not _std_dict_is_order_preserving:
44-
for loader_name in pyyaml.loader.__all__:
45-
Loader = getattr(pyyaml.loader, loader_name)
45+
for Loader in _loaders:
4646
pyyaml.add_constructor("tag:yaml.org,2002:map", map_constructor, Loader=Loader)
4747

4848

4949
# Merge PyYAML namespace into ours.
5050
# This allows users a drop-in replacement:
5151
# import oyaml as yaml
52-
del map_constructor, map_representer, SafeDumper, DangerDumper, Loader
52+
del map_constructor, map_representer, Loader, Dumper
5353
from yaml import *

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="oyaml",
5-
version="0.9",
5+
version="1.0",
66
description="Ordered YAML: drop-in replacement for PyYAML which preserves dict ordering",
77
long_description=open("README.rst").read(),
88
author="Wim Glenn",

test_oyaml.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,17 @@ def test_merge():
192192
def test_unhashable_error_context():
193193
with pytest.raises(ConstructorError, match=r".*line.*column.*"):
194194
yaml.safe_load("{foo: bar}: baz")
195+
196+
197+
@pytest.mark.skipif(not hasattr(yaml, "CSafeLoader"), reason="requires cyaml loaders")
198+
def test_explicit_loader():
199+
data = yaml.load("{x: 1, z: 3, y: 2}", Loader=yaml.CSafeLoader)
200+
assert data == {"x": 1, "z": 3, "y": 2}
201+
assert list(data) == ["x", "z", "y"]
202+
203+
204+
@pytest.mark.skipif(not hasattr(yaml, "CSafeDumper"), reason="requires cyaml dumpers")
205+
def test_explicit_dumper():
206+
data = OrderedDict([("x", 1), ("z", 3), ("y", 2)])
207+
text = yaml.dump(data, Dumper=yaml.CSafeDumper, default_flow_style=None)
208+
assert text == "{x: 1, z: 3, y: 2}\n"

0 commit comments

Comments
 (0)