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

ConstructorError exception while processing yaml files with tagged values #271

Open
sambowry opened this issue Mar 11, 2025 · 0 comments
Open

Comments

@sambowry
Copy link

sambowry commented Mar 11, 2025

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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant