Merge "Avoid globally modifying yaml library"

This commit is contained in:
Zuul 2018-05-26 03:14:12 +00:00 committed by Gerrit Code Review
commit 2a1eec9077
2 changed files with 12 additions and 8 deletions

View File

@ -18,23 +18,22 @@ if hasattr(yaml, 'CSafeLoader'):
else: else:
yaml_loader = yaml.SafeLoader yaml_loader = yaml.SafeLoader
if hasattr(yaml, 'CSafeDumper'):
yaml_dumper = yaml.CSafeDumper class HeatYamlLoader(yaml_loader):
else: pass
yaml_dumper = yaml.SafeDumper
def _construct_yaml_str(self, node): def _construct_yaml_str(self, node):
# Override the default string handling function # Override the default string handling function
# to always return unicode objects # to always return unicode objects
return self.construct_scalar(node) 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 # 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 # datetime.data which causes problems in API layer when being processed by
# openstack.common.jsonutils. Therefore, make unicode string out of timestamps # openstack.common.jsonutils. Therefore, make unicode string out of timestamps
# until jsonutils can handle dates. # until jsonutils can handle dates.
yaml_loader.add_constructor(u'tag:yaml.org,2002:timestamp', HeatYamlLoader.add_constructor(
_construct_yaml_str) u'tag:yaml.org,2002:timestamp', _construct_yaml_str)
def parse(tmpl_str): def parse(tmpl_str):
@ -49,7 +48,7 @@ def parse(tmpl_str):
tpl = json.loads(tmpl_str) tpl = json.loads(tmpl_str)
else: else:
try: try:
tpl = yaml.load(tmpl_str, Loader=yaml_loader) tpl = yaml.load(tmpl_str, Loader=HeatYamlLoader)
except yaml.YAMLError: except yaml.YAMLError:
# NOTE(prazumovsky): we need to return more informative error for # NOTE(prazumovsky): we need to return more informative error for
# user, so use SafeLoader, which return error message with template # user, so use SafeLoader, which return error message with template

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Fixed an issue where importing openstacksdk changed the behavior of
``yaml.load`` globally.