Skip to content

ConstructorError exception while processing yaml files with tagged values #271

@sambowry

Description

@sambowry

I have some yaml files which contains local tags. I got ConstructorError exceptions when i wanted to validate them.

The solution can be to handle all local tags by an "unknown" contructor:

def _pyyaml(f):
    import yaml

    def unknown(loader, suffix, node):
        if isinstance(node, yaml.ScalarNode):
            constructor = loader.__class__.construct_scalar
        elif isinstance(node, yaml.SequenceNode):
            constructor = loader.__class__.construct_sequence
        elif isinstance(node, yaml.MappingNode):
            constructor = loader.__class__.construct_mapping

        data = constructor(loader, node)
        return data

    try:
        Loader = yaml.CSafeLoader
    except AttributeError:  # System does not have libyaml
        Loader = yaml.SafeLoader

    yaml.add_multi_constructor('!', unknown, Loader=Loader)

    return list(yaml.load_all(f, Loader=Loader))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions