File do-not-monkey-patch-yaml-bsc-1177474.patch of Package salt
From 8eaeb751d4077d6514577b53a9dbe23df231018e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?=
<psuarezhernandez@suse.com>
Date: Mon, 8 Mar 2021 12:35:14 +0000
Subject: [PATCH] Do not monkey patch yaml (bsc#1177474)
Add changelog file
Add suggestions by pre-commit
Add unit test to check for monkey patching
---
changelog/57995.fixed | 1 +
salt/utils/yamlloader.py | 28 ++++++++++------------------
tests/unit/utils/test_yamlloader.py | 6 +++++-
3 files changed, 16 insertions(+), 19 deletions(-)
create mode 100644 changelog/57995.fixed
diff --git a/changelog/57995.fixed b/changelog/57995.fixed
new file mode 100644
index 0000000000..78f2cd1fa4
--- /dev/null
+++ b/changelog/57995.fixed
@@ -0,0 +1 @@
+Do not monkey patch yaml loaders: Prevent breaking Ansible filter modules
diff --git a/salt/utils/yamlloader.py b/salt/utils/yamlloader.py
index e9d80fc4ad..f98fdcb0e9 100644
--- a/salt/utils/yamlloader.py
+++ b/salt/utils/yamlloader.py
@@ -1,10 +1,7 @@
-# -*- coding: utf-8 -*-
"""
Custom YAML loading in Salt
"""
-# Import python libs
-from __future__ import absolute_import, print_function, unicode_literals
import warnings
@@ -13,13 +10,8 @@ import yaml # pylint: disable=blacklisted-import
from yaml.constructor import ConstructorError
from yaml.nodes import MappingNode, SequenceNode
-try:
- yaml.Loader = yaml.CLoader
- yaml.Dumper = yaml.CDumper
- yaml.SafeLoader = yaml.CSafeLoader
- yaml.SafeDumper = yaml.CSafeDumper
-except Exception: # pylint: disable=broad-except
- pass
+# prefer C bindings over python when available
+BaseLoader = getattr(yaml, "CSafeLoader", yaml.SafeLoader)
__all__ = ["SaltYamlSafeLoader", "load", "safe_load"]
@@ -35,7 +27,7 @@ warnings.simplefilter("always", category=DuplicateKeyWarning)
# with code integrated from https://gist.github.com/844388
-class SaltYamlSafeLoader(yaml.SafeLoader):
+class SaltYamlSafeLoader(BaseLoader):
"""
Create a custom YAML loader that uses the custom constructor. This allows
for the YAML loading defaults to be manipulated based on needs within salt
@@ -43,7 +35,7 @@ class SaltYamlSafeLoader(yaml.SafeLoader):
"""
def __init__(self, stream, dictclass=dict):
- super(SaltYamlSafeLoader, self).__init__(stream)
+ super().__init__(stream)
if dictclass is not dict:
# then assume ordered dict and use it for both !map and !omap
self.add_constructor("tag:yaml.org,2002:map", type(self).construct_yaml_map)
@@ -74,7 +66,7 @@ class SaltYamlSafeLoader(yaml.SafeLoader):
raise ConstructorError(
None,
None,
- "expected a mapping node, but found {0}".format(node.id),
+ "expected a mapping node, but found {}".format(node.id),
node.start_mark,
)
@@ -90,7 +82,7 @@ class SaltYamlSafeLoader(yaml.SafeLoader):
raise ConstructorError(
context,
node.start_mark,
- "found unacceptable key {0}".format(key_node.value),
+ "found unacceptable key {}".format(key_node.value),
key_node.start_mark,
)
value = self.construct_object(value_node, deep=deep)
@@ -98,7 +90,7 @@ class SaltYamlSafeLoader(yaml.SafeLoader):
raise ConstructorError(
context,
node.start_mark,
- "found conflicting ID '{0}'".format(key),
+ "found conflicting ID '{}'".format(key),
key_node.start_mark,
)
mapping[key] = value
@@ -118,7 +110,7 @@ class SaltYamlSafeLoader(yaml.SafeLoader):
# an empty string. Change it to '0'.
if node.value == "":
node.value = "0"
- return super(SaltYamlSafeLoader, self).construct_scalar(node)
+ return super().construct_scalar(node)
def construct_yaml_str(self, node):
value = self.construct_scalar(node)
@@ -142,7 +134,7 @@ class SaltYamlSafeLoader(yaml.SafeLoader):
raise ConstructorError(
"while constructing a mapping",
node.start_mark,
- "expected a mapping for merging, but found {0}".format(
+ "expected a mapping for merging, but found {}".format(
subnode.id
),
subnode.start_mark,
@@ -156,7 +148,7 @@ class SaltYamlSafeLoader(yaml.SafeLoader):
raise ConstructorError(
"while constructing a mapping",
node.start_mark,
- "expected a mapping or list of mappings for merging, but found {0}".format(
+ "expected a mapping or list of mappings for merging, but found {}".format(
value_node.id
),
value_node.start_mark,
diff --git a/tests/unit/utils/test_yamlloader.py b/tests/unit/utils/test_yamlloader.py
index a1e17af760..3f2e4403ba 100644
--- a/tests/unit/utils/test_yamlloader.py
+++ b/tests/unit/utils/test_yamlloader.py
@@ -13,7 +13,7 @@ import salt.utils.files
# Import 3rd-party libs
from salt.ext import six
-from salt.utils.yamlloader import SaltYamlSafeLoader
+from salt.utils.yamlloader import SaltYamlSafeLoader, yaml
from tests.support.mock import mock_open, patch
# Import Salt Testing Libs
@@ -177,3 +177,7 @@ class YamlLoaderTestCase(TestCase):
),
{"foo": {"b": {"foo": "bar", "one": 1, "list": [1, "two", 3]}}},
)
+
+ def test_not_yaml_monkey_patching(self):
+ if hasattr(yaml, "CSafeLoader"):
+ assert yaml.SafeLoader != yaml.CSafeLoader
--
2.30.1