Skip to content

Commit

Permalink
preserve contextual error when patching the loader (#18)
Browse files Browse the repository at this point in the history
resolves #17
  • Loading branch information
wimglenn committed Mar 28, 2019
1 parent 425fd97 commit 6350d9d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion oyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ def map_representer(dumper, data):

def map_constructor(loader, node):
loader.flatten_mapping(node)
return OrderedDict(loader.construct_pairs(node))
pairs = loader.construct_pairs(node)
try:
return OrderedDict(pairs)
except TypeError:
loader.construct_mapping(node) # trigger any contextual error
raise


if pyyaml.safe_dump is pyyaml.dump:
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.8",
version="0.9",
description="Ordered YAML: drop-in replacement for PyYAML which preserves dict ordering",
long_description=open("README.rst").read(),
author="Wim Glenn",
Expand Down
6 changes: 6 additions & 0 deletions test_oyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from types import GeneratorType

import pytest
from yaml.constructor import ConstructorError
from yaml.representer import RepresenterError

import oyaml as yaml
Expand Down Expand Up @@ -186,3 +187,8 @@ def test_merge():
assert map2 == expected
assert map3 == expected
assert map4 == expected


def test_unhashable_error_context():
with pytest.raises(ConstructorError, match=r".*line.*column.*"):
yaml.safe_load("{foo: bar}: baz")

0 comments on commit 6350d9d

Please sign in to comment.