Make UnsafeTag self registering

YAMLObject classes can self-register themselves in the yaml dumper and
loader via metaclass magic. Use it so we don't execute statements
during import.

Change-Id: I31f097701f863e3f2298028d76054e80dfc2d23e
This commit is contained in:
Tobias Henkel 2019-02-14 19:19:29 +01:00
parent c7ec1490b0
commit 1a5a9863bb
No known key found for this signature in database
GPG Key ID: 03750DEC158E5FA2
1 changed files with 2 additions and 7 deletions

View File

@ -27,6 +27,8 @@ except ImportError:
class UnsafeTag(yaml.YAMLObject):
yaml_tag = u'!unsafe'
yaml_dumper = yaml.SafeDumper
yaml_loader = yaml.SafeLoader
def __init__(self, unsafe_var):
self.unsafe_var = unsafe_var
@ -43,13 +45,6 @@ class UnsafeTag(yaml.YAMLObject):
return self.unsafe_var
# Just calling SafeLoader and SafeDumper without yaml module prefix
# does not work and cyaml is using yaml.SafeConstructor yaml.SafeRepresenter
# underneath so this just fine for both
yaml.SafeLoader.add_constructor(UnsafeTag.yaml_tag, UnsafeTag.from_yaml)
yaml.SafeDumper.add_multi_representer(UnsafeTag, UnsafeTag.to_yaml)
def safe_load(stream, *args, **kwargs):
return yaml.load(stream, *args, Loader=SafeLoader, **kwargs)