Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve contextual error when patching the loader #18

Merged
merged 3 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")