Browse Source

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
changes/23/637023/1
Tobias Henkel 3 years ago
parent
commit
1a5a9863bb
No known key found for this signature in database GPG Key ID: 3750DEC158E5FA2
  1. 9
      zuul/lib/yamlutil.py

9
zuul/lib/yamlutil.py

@ -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)

Loading…
Cancel
Save