Merge pull request #14197 from bcoca/safer_data_load

load now does not modify the incomming data
This commit is contained in:
Brian Coca 2016-02-02 00:02:23 -05:00
commit eafc31f3f8

View file

@ -20,7 +20,6 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import copy import copy
import json
import os import os
import stat import stat
import subprocess import subprocess
@ -74,23 +73,17 @@ class DataLoader():
a JSON or YAML string. a JSON or YAML string.
''' '''
try: # YAML parser will take JSON as it is a subset.
# we first try to load this data as JSON
return json.loads(data)
except:
# if loading JSON failed for any reason, we go ahead
# and try to parse it as YAML instead
if isinstance(data, AnsibleUnicode): if isinstance(data, AnsibleUnicode):
# The PyYAML's libyaml bindings use PyUnicode_CheckExact so # The PyYAML's libyaml bindings use PyUnicode_CheckExact so
# they are unable to cope with our subclass. # they are unable to cope with our subclass.
# Unwrap and re-wrap the unicode so we can keep track of line # Unwrap and re-wrap the unicode so we can keep track of line
# numbers # numbers
new_data = text_type(data) in_data = text_type(data)
else: else:
new_data = data in_data = data
try: try:
new_data = self._safe_load(new_data, file_name=file_name) new_data = self._safe_load(in_data, file_name=file_name)
except YAMLError as yaml_exc: except YAMLError as yaml_exc:
self._handle_error(yaml_exc, file_name, show_content) self._handle_error(yaml_exc, file_name, show_content)