Skip to content

Commit 3132aad

Browse files
author
Sylvain MARIE
committed
PyYaml 5.1 added some breaking changes. We now comply with them while still supporting the old versions. Fixed #8
1 parent 94d94bc commit 3132aad

6 files changed

Lines changed: 67 additions & 27 deletions

File tree

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ matrix:
77
include:
88
# - python: 2.6
99
- python: 2.7
10+
PYYAML_VERSION: ""
1011
# - python: 3.3
1112
# - python: 3.4
1213
- python: 3.5
14+
PYYAML_VERSION: "<5"
1315
- python: 3.6
16+
PYYAML_VERSION: ""
1417
- python: 3.7
18+
PYYAML_VERSION: ""
1519
dist: xenial
1620
sudo: true
1721

ci_tools/requirements-pip.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ pytest-runner
44
pandoc
55
pypandoc
66

7+
# --- to install
8+
pyyaml$PYYAML_VERSION
9+
710
# --- to run the tests
811
pytest #$PYTEST_VERSION
912
pytest-logging # ==2015.11.4

yamlable/main.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,17 @@ def encode_yamlable(dumper,
262262
return dumper.represent_mapping(yaml_tag, new_data, flow_style=None)
263263

264264

265-
def register_yamlable_codec(loaders=(Loader, SafeLoader), dumpers=(Dumper, SafeDumper)):
265+
try: # PyYaml 5.1+
266+
from yaml import FullLoader, UnsafeLoader
267+
ALL_PYYAML_LOADERS = (Loader, SafeLoader, FullLoader, UnsafeLoader)
268+
except ImportError:
269+
ALL_PYYAML_LOADERS = (Loader, SafeLoader)
270+
271+
272+
ALL_PYYAML_DUMPERS = (Dumper, SafeDumper)
273+
274+
275+
def register_yamlable_codec(loaders=ALL_PYYAML_LOADERS, dumpers=ALL_PYYAML_DUMPERS):
266276
# type: (...) -> None
267277
"""
268278
Registers the yamlable encoder and decoder with all pyYaml loaders and dumpers.
@@ -472,17 +482,17 @@ def to_yaml_dict(cls, obj):
472482
"""
473483

474484
@classmethod
475-
def register_with_pyyaml(cls, loaders={Loader, SafeLoader}, dumpers={Dumper, SafeDumper}):
485+
def register_with_pyyaml(cls, loaders=ALL_PYYAML_LOADERS, dumpers=ALL_PYYAML_DUMPERS):
476486
# type: (...) -> None
477487
"""
478488
Registers this codec with PyYaml, on the provided loaders and dumpers (default: all PyYaml loaders and dumpers).
479489
- The encoding part is registered for the object types listed in cls.get_known_types(), in order
480490
- The decoding part is registered for the yaml prefix in cls.get_yaml_prefix()
481491
482492
:param loaders: the PyYaml loaders to register this codec with. By default all pyyaml loaders are considered
483-
(Loader, SafeLoader)
493+
(Loader, SafeLoader...)
484494
:param dumpers: the PyYaml dumpers to register this codec with. By default all pyyaml loaders are considered
485-
(Dumper, SafeDumper)
495+
(Dumper, SafeDumper...)
486496
:return:
487497
"""
488498
for loader in loaders:

yamlable/tests/test_yamlable.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ def __from_yaml_dict__(cls, # type: Type[Y]
8181
f = Foo(1, 'hello') # note:
8282

8383
# dump
84-
y = f.dumps_yaml()
85-
assert y == "!yamlable/yaml.tests.Foo {a: 1, b: hello}\n"
84+
y = f.dumps_yaml(default_flow_style=False)
85+
assert y == """!yamlable/yaml.tests.Foo
86+
a: 1
87+
b: hello
88+
"""
8689

8790
# dump io
8891
class MemorizingStringIO(StringIO):
@@ -93,11 +96,11 @@ def close(self):
9396
StringIO.close(self)
9497

9598
s = MemorizingStringIO()
96-
f.dump_yaml(s)
99+
f.dump_yaml(s, default_flow_style=False)
97100
assert s.value == y
98101

99102
# dump pyyaml
100-
assert dump(f) == y
103+
assert dump(f, default_flow_style=False) == y
101104

102105
# load
103106
assert f == Foo.loads_yaml(y)
@@ -147,8 +150,11 @@ def from_yaml_dict(cls, # type: Type[Y]
147150
f = FooLegacy(1, 'hello')
148151

149152
# dump
150-
y = f.dumps_yaml()
151-
assert y == "!yamlable/yaml.tests.FooLegacy {a: 1, b: hello}\n"
153+
y = f.dumps_yaml(default_flow_style=False)
154+
assert y == """!yamlable/yaml.tests.FooLegacy
155+
a: 1
156+
b: hello
157+
"""
152158

153159
# dump io
154160
class MemorizingStringIO(StringIO):
@@ -159,11 +165,11 @@ def close(self):
159165
StringIO.close(self)
160166

161167
s = MemorizingStringIO()
162-
f.dump_yaml(s)
168+
f.dump_yaml(s, default_flow_style=False)
163169
assert s.value == y
164170

165171
# dump pyyaml
166-
assert dump(f) == y
172+
assert dump(f, default_flow_style=False) == y
167173

168174
# load
169175
assert f == FooLegacy.loads_yaml(y)
@@ -228,10 +234,13 @@ def __init__(self, a, b):
228234
self.b = b
229235

230236
f = Foo_Default(1, 'hello')
231-
s = '!yamlable/yaml.tests.Foo_Default {a: 1, b: hello}\n'
232-
assert dump(f) == s
237+
s = """!yamlable/yaml.tests.Foo_Default
238+
a: 1
239+
b: hello
240+
"""
241+
assert dump(f, default_flow_style=False) == s
233242

234-
assert dump(load(dump(load(s)))) == s
243+
assert dump(load(dump(load(s))), default_flow_style=False) == s
235244

236245

237246
def test_help_yaml_info():
@@ -297,8 +306,11 @@ def __eq__(self, other):
297306
f = FooValid(1, 'hello') # note:
298307

299308
# dump
300-
y = f.dumps_yaml()
301-
assert y == "!yamlable/yaml.tests.FooValid {a: 1, b: hello}\n"
309+
y = f.dumps_yaml(default_flow_style=False)
310+
assert y == """!yamlable/yaml.tests.FooValid
311+
a: 1
312+
b: hello
313+
"""
302314

303315
# dump io
304316
class MemorizingStringIO(StringIO):
@@ -310,11 +322,11 @@ def close(self):
310322
StringIO.close(self)
311323

312324
s = MemorizingStringIO()
313-
f.dump_yaml(s)
325+
f.dump_yaml(s, default_flow_style=False)
314326
assert s.value == y
315327

316328
# dump pyyaml
317-
assert dump(f) == y
329+
assert dump(f, default_flow_style=False) == y
318330

319331
# load
320332
assert f == FooValid.loads_yaml(y)

yamlable/tests/test_yamlcodec.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,19 @@ def to_yaml_dict(cls, obj):
6969

7070
# instantiate
7171
f = Foo(1, 'hello')
72-
fy = "!mycodec/yaml.tests.Foo {a: 1, b: hello}\n"
72+
fy = """!mycodec/yaml.tests.Foo
73+
a: 1
74+
b: hello
75+
"""
7376

7477
b = Bar('what?')
75-
by = "!mycodec/yaml.tests.Bar {c: 'what?'}\n"
78+
by = """!mycodec/yaml.tests.Bar
79+
c: what?
80+
"""
7681

7782
# dump pyyaml
78-
assert dump(f) == fy
79-
assert dump(b) == by
83+
assert dump(f, default_flow_style=False) == fy
84+
assert dump(b, default_flow_style=False) == by
8085

8186
# load pyyaml
8287
assert f == load(fy)

yamlable/tests/test_yamlobjects.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ def __from_yaml_dict__(cls, # type: Type[Y]
4949
f = Foo(1, 'hello')
5050

5151
# dump to yaml
52-
o = f.dumps_yaml(safe=False)
53-
assert o == "!foo {a: 1, b: hello}\n"
52+
o = f.dumps_yaml(safe=False, default_flow_style=False)
53+
assert o == """!foo
54+
a: 1
55+
b: hello
56+
"""
5457

5558
# load from yaml
5659
g = Foo.loads_yaml(o)
@@ -104,8 +107,11 @@ def __eq__(self, other):
104107
f = FooValid(1, 'hello')
105108

106109
# dump to yaml
107-
o = f.dumps_yaml(safe=False)
108-
assert o == "!foo {a: 1, b: hello}\n"
110+
o = f.dumps_yaml(safe=False, default_flow_style=False)
111+
assert o == """!foo
112+
a: 1
113+
b: hello
114+
"""
109115

110116
# load from yaml
111117
g = FooValid.loads_yaml(o)

0 commit comments

Comments
 (0)