From 5834d5e7cf1d8332f4621b0de5fdf40c93456dae Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Tue, 15 May 2018 16:38:36 -0500 Subject: [PATCH] Avoid globally modifying yaml library The heat utils set up the yaml parser to always return unicode strings, but are currently doing it in a way that infects all uses of the library. Make a subclass of the loader and run add_constructor on that so that the new constructor is confined to the specific loader. Change-Id: I49e97b5e2ae9b7862915ff83263718cf6cad32b8 Story: 2002040 --- openstack/cloud/_heat/template_format.py | 15 +++++++-------- .../notes/fix-yaml-load-3e6bd852afe549b4.yaml | 5 +++++ 2 files changed, 12 insertions(+), 8 deletions(-) create mode 100644 releasenotes/notes/fix-yaml-load-3e6bd852afe549b4.yaml diff --git a/openstack/cloud/_heat/template_format.py b/openstack/cloud/_heat/template_format.py index 4bb6098dc..cf37ee528 100644 --- a/openstack/cloud/_heat/template_format.py +++ b/openstack/cloud/_heat/template_format.py @@ -18,23 +18,22 @@ if hasattr(yaml, 'CSafeLoader'): else: yaml_loader = yaml.SafeLoader -if hasattr(yaml, 'CSafeDumper'): - yaml_dumper = yaml.CSafeDumper -else: - yaml_dumper = yaml.SafeDumper + +class HeatYamlLoader(yaml_loader): + pass def _construct_yaml_str(self, node): # Override the default string handling function # to always return unicode objects return self.construct_scalar(node) -yaml_loader.add_constructor(u'tag:yaml.org,2002:str', _construct_yaml_str) +HeatYamlLoader.add_constructor(u'tag:yaml.org,2002:str', _construct_yaml_str) # Unquoted dates like 2013-05-23 in yaml files get loaded as objects of type # datetime.data which causes problems in API layer when being processed by # openstack.common.jsonutils. Therefore, make unicode string out of timestamps # until jsonutils can handle dates. -yaml_loader.add_constructor(u'tag:yaml.org,2002:timestamp', - _construct_yaml_str) +HeatYamlLoader.add_constructor( + u'tag:yaml.org,2002:timestamp', _construct_yaml_str) def parse(tmpl_str): @@ -49,7 +48,7 @@ def parse(tmpl_str): tpl = json.loads(tmpl_str) else: try: - tpl = yaml.load(tmpl_str, Loader=yaml_loader) + tpl = yaml.load(tmpl_str, Loader=HeatYamlLoader) except yaml.YAMLError: # NOTE(prazumovsky): we need to return more informative error for # user, so use SafeLoader, which return error message with template diff --git a/releasenotes/notes/fix-yaml-load-3e6bd852afe549b4.yaml b/releasenotes/notes/fix-yaml-load-3e6bd852afe549b4.yaml new file mode 100644 index 000000000..ac34188cc --- /dev/null +++ b/releasenotes/notes/fix-yaml-load-3e6bd852afe549b4.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fixed an issue where importing openstacksdk changed the behavior of + ``yaml.load`` globally.