Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use packaged json module for ThreadsafeProxy #55660

Merged
merged 3 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions salt/modules/azurearm_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@

# Python libs
from __future__ import absolute_import
from json import loads, dumps
import logging

# Salt Libs
import salt.utils.json

# Azure libs
HAS_LIBS = False
try:
Expand Down Expand Up @@ -1061,7 +1063,7 @@ def policy_definition_create_or_update(name, policy_rule, **kwargs): # pylint:
polconn = __utils__['azurearm.get_client']('policy', **kwargs)

# Convert OrderedDict to dict
prop_kwargs = {'policy_rule': loads(dumps(policy_rule))}
prop_kwargs = {'policy_rule': salt.utils.json.loads(salt.utils.json.dumps(policy_rule))}

policy_kwargs = kwargs.copy()
policy_kwargs.update(prop_kwargs)
Expand Down
7 changes: 4 additions & 3 deletions salt/modules/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

# Import python libs
from __future__ import absolute_import, print_function, unicode_literals
from json import JSONEncoder, loads

# Import Salt libs
import salt.ext.six as six
import salt.utils.json


try:
Expand Down Expand Up @@ -64,7 +65,7 @@ def _get_connection(**kwargs):
return pymssql.connect(**connection_args)


class _MssqlEncoder(JSONEncoder):
class _MssqlEncoder(salt.utils.json.JSONEncoder):
# E0202: 68:_MssqlEncoder.default: An attribute inherited from JSONEncoder hide this method
def default(self, o): # pylint: disable=E0202
return six.text_type(o)
Expand All @@ -84,7 +85,7 @@ def tsql_query(query, **kwargs):
cur = _get_connection(**kwargs).cursor()
cur.execute(query)
# Making sure the result is JSON serializable
return loads(_MssqlEncoder().encode({'resultset': cur.fetchall()}))['resultset']
return salt.utils.json.loads(_MssqlEncoder().encode({'resultset': cur.fetchall()}))['resultset']
except Exception as err:
# Trying to look like the output of cur.fetchall()
return (('Could not run the query', ), (six.text_type(err), ))
Expand Down
4 changes: 2 additions & 2 deletions salt/modules/saltcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@
import logging
import os
import time
from json import loads, dumps

# Import Salt libs
import salt.utils.files
import salt.utils.json
import salt.utils.path
import salt.utils.yaml
import salt.client
Expand Down Expand Up @@ -699,7 +699,7 @@ def load_file_salt_rendered(self, filepath):
# use the salt renderer module to interpret jinja and etc
tests = _render_file(filepath)
# use json as a convenient way to convert the OrderedDicts from salt renderer
mydict = loads(dumps(tests))
mydict = salt.utils.json.loads(salt.utils.json.dumps(tests))
for key, value in mydict.items():
self.test_dict[key] = value
return
Expand Down
4 changes: 2 additions & 2 deletions salt/netapi/rest_tornado/saltnado.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
)

salt.utils.zeromq.install_zmq()
json = salt.utils.json.import_json()
_json = salt.utils.json.import_json()
log = logging.getLogger(__name__)


Expand All @@ -233,7 +233,7 @@ def _json_dumps(obj, **kwargs):
salt.utils.json.import_json(). This ensures that we properly encode any
strings in the object before we perform the serialization.
'''
return salt.utils.json.dumps(obj, _json_module=json, **kwargs)
return salt.utils.json.dumps(obj, _json_module=_json, **kwargs)

# The clients rest_cherrypi supports. We want to mimic the interface, but not
# necessarily use the same API under the hood
Expand Down
5 changes: 2 additions & 3 deletions salt/states/netsnmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
import logging
log = logging.getLogger(__name__)

from json import loads, dumps

# salt lib
from salt.ext import six
# import NAPALM utils
import salt.utils.json
import salt.utils.napalm

# ----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -69,7 +68,7 @@ def _ordered_dict_to_dict(config):
Forced the datatype to dict, in case OrderedDict is used.
'''

return loads(dumps(config))
return salt.utils.json.loads(salt.utils.json.dumps(config))


def _expand_config(config, defaults):
Expand Down
4 changes: 2 additions & 2 deletions salt/states/netusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

# Python std lib
from copy import deepcopy
from json import loads, dumps

# salt lib
from salt.ext import six
# import NAPALM utils
import salt.utils.json
import salt.utils.napalm

# ----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -69,7 +69,7 @@ def _ordered_dict_to_dict(probes):

'''.'''

return loads(dumps(probes))
return salt.utils.json.loads(salt.utils.json.dumps(probes))


def _expand_users(device_users, common_users):
Expand Down
4 changes: 2 additions & 2 deletions salt/states/probes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
log = logging.getLogger(__name__)

from copy import deepcopy
from json import loads, dumps

# salt modules
from salt.ext import six
import salt.utils.json
# import NAPALM utils
import salt.utils.napalm

Expand Down Expand Up @@ -205,7 +205,7 @@ def _ordered_dict_to_dict(probes):

'''Mandatory to be dict type in order to be used in the NAPALM Jinja template.'''

return loads(dumps(probes))
return salt.utils.json.loads(salt.utils.json.dumps(probes))


def _set_rpm_probes(probes):
Expand Down
4 changes: 2 additions & 2 deletions salt/states/zabbix_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

'''
from __future__ import absolute_import, print_function, unicode_literals
from json import loads, dumps
from copy import deepcopy
from salt.ext import six
import salt.utils.json


def __virtual__():
Expand Down Expand Up @@ -101,7 +101,7 @@ def _interface_format(interfaces_data):
return list()

interface_attrs = ('ip', 'dns', 'main', 'type', 'useip', 'port')
interfaces_json = loads(dumps(interfaces_data))
interfaces_json = salt.utils.json.loads(salt.utils.json.dumps(interfaces_data))
interfaces_dict = dict()

for interface in interfaces_json:
Expand Down
4 changes: 2 additions & 2 deletions salt/states/zabbix_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
from json import loads, dumps
from copy import deepcopy

# Import Salt libs
import salt.utils.json
from salt.ext import six
from salt.exceptions import SaltException

Expand Down Expand Up @@ -196,7 +196,7 @@ def _media_format(medias_data):
'''
if not medias_data:
return list()
medias_json = loads(dumps(medias_data))
medias_json = salt.utils.json.loads(salt.utils.json.dumps(medias_data))
medias_attr = ('active', 'mediatype', 'period', 'severity', 'sendto')
media_type = {'mail': 1, 'jabber': 2, 'sms': 3}
media_severities = ('D', 'H', 'A', 'W', 'I', 'N')
Expand Down
4 changes: 4 additions & 0 deletions salt/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
log = logging.getLogger(__name__)


# One to one mappings
JSONEncoder = json.JSONEncoder


def __split(raw):
'''
Performs a splitlines on the string. This function exists to make mocking
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/utils/test_thin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
patch)

import salt.exceptions
import salt.utils.json
from salt.utils import thin
from salt.utils import json
import salt.utils.stringutils
import salt.utils.platform
from salt.utils.stringutils import to_bytes as bts
Expand Down Expand Up @@ -211,7 +211,7 @@ def test_gte(self):

:return:
'''
assert json.loads(thin.gte()).get('foo') == 'bar'
assert salt.utils.json.loads(thin.gte()).get('foo') == 'bar'

def test_add_dep_path(self):
'''
Expand Down Expand Up @@ -243,12 +243,12 @@ def test_get_salt_call_script(self):
out = thin._get_salt_call('foo', 'bar', py26=[2, 6], py27=[2, 7], py34=[3, 4])
for line in salt.utils.stringutils.to_str(out).split(os.linesep):
if line.startswith('namespaces = {'):
data = json.loads(line.replace('namespaces = ', '').strip())
data = salt.utils.json.loads(line.replace('namespaces = ', '').strip())
assert data.get('py26') == [2, 6]
assert data.get('py27') == [2, 7]
assert data.get('py34') == [3, 4]
if line.startswith('syspaths = '):
data = json.loads(line.replace('syspaths = ', ''))
data = salt.utils.json.loads(line.replace('syspaths = ', ''))
assert data == ['foo', 'bar']

def test_get_ext_namespaces_empty(self):
Expand Down