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

A more flexible fix for custom tag constructors #279

Merged
merged 1 commit into from Mar 31, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/yaml/__init__.py
Expand Up @@ -371,7 +371,12 @@ class YAMLObjectMetaclass(type):
def __init__(cls, name, bases, kwds):
super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds)
if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None:
cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)
if isinstance(cls.yaml_loader, list):
for loader in cls.yaml_loader:
loader.add_constructor(cls.yaml_tag, cls.from_yaml)
else:
cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)

cls.yaml_dumper.add_representer(cls, cls.to_yaml)

class YAMLObject(object):
Expand All @@ -383,7 +388,7 @@ class YAMLObject(object):
__metaclass__ = YAMLObjectMetaclass
__slots__ = () # no direct instantiation, so allow immutable subclasses

yaml_loader = Loader
yaml_loader = [Loader, FullLoader, UnsafeLoader]
yaml_dumper = Dumper

yaml_tag = None
Expand Down
9 changes: 7 additions & 2 deletions lib3/yaml/__init__.py
Expand Up @@ -368,7 +368,12 @@ class YAMLObjectMetaclass(type):
def __init__(cls, name, bases, kwds):
super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds)
if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None:
cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)
if isinstance(cls.yaml_loader, list):
for loader in cls.yaml_loader:
loader.add_constructor(cls.yaml_tag, cls.from_yaml)
else:
cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)

cls.yaml_dumper.add_representer(cls, cls.to_yaml)

class YAMLObject(metaclass=YAMLObjectMetaclass):
Expand All @@ -379,7 +384,7 @@ class YAMLObject(metaclass=YAMLObjectMetaclass):

__slots__ = () # no direct instantiation, so allow immutable subclasses

yaml_loader = Loader
yaml_loader = [Loader, FullLoader, UnsafeLoader]
yaml_dumper = Dumper

yaml_tag = None
Expand Down