From c1c229c6d4cf74d022c56a17061ab57918822f88 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Fri, 10 Aug 2018 11:13:29 -0500 Subject: [PATCH] Remove use of simplejson throughout code base (#43548) * Remove use of simplejson throughout code base. Fixes #42761 * Address failing tests * Remove simplejson from contrib and other outlying files * Add changelog fragment for simplejson removal --- changelogs/fragments/remove-simplejson.yaml | 2 ++ contrib/inventory/abiquo.py | 5 +--- contrib/inventory/apache-libcloud.py | 5 +--- contrib/inventory/brook.py | 5 +--- contrib/inventory/cloudforms.py | 5 +--- contrib/inventory/cloudstack.py | 7 +---- contrib/inventory/cobbler.py | 5 +--- contrib/inventory/collins.py | 5 +--- contrib/inventory/consul_io.py | 5 +--- contrib/inventory/digital_ocean.py | 5 +--- contrib/inventory/ec2.py | 5 +--- contrib/inventory/fleet.py | 5 +--- contrib/inventory/gce.py | 5 +--- contrib/inventory/landscape.py | 5 +--- contrib/inventory/linode.py | 5 +--- contrib/inventory/openshift.py | 5 +--- contrib/inventory/openstack_inventory.py | 5 +--- contrib/inventory/ovirt.py | 6 +---- contrib/inventory/ovirt4.py | 5 +--- contrib/inventory/packet_net.py | 5 +--- contrib/inventory/proxmox.py | 5 +--- contrib/inventory/rax.py | 5 +--- contrib/inventory/rudder.py | 5 +--- contrib/inventory/scaleway.py | 5 +--- contrib/inventory/serf.py | 5 +--- contrib/inventory/softlayer.py | 5 +--- contrib/inventory/spacewalk.py | 5 +--- contrib/inventory/ssh_config.py | 5 +--- contrib/inventory/stacki.py | 5 +--- contrib/inventory/vagrant.py | 5 +--- contrib/inventory/vbox.py | 5 +--- contrib/inventory/zabbix.py | 5 +--- docs/docsite/rst/user_guide/intro_bsd.rst | 3 ++- hacking/test-module | 5 +--- lib/ansible/module_utils/basic.py | 16 ++---------- lib/ansible/module_utils/ipa.py | 5 +--- lib/ansible/module_utils/json_utils.py | 6 ++--- lib/ansible/module_utils/lxd.py | 5 +--- lib/ansible/module_utils/netapp.py | 5 +--- .../module_utils/network/f5/icontrol.py | 5 +--- lib/ansible/modules/commands/raw.py | 12 +++------ .../modules/monitoring/logicmonitor.py | 26 +------------------ .../modules/monitoring/logicmonitor_facts.py | 25 +----------------- .../modules/monitoring/sensu_silence.py | 6 +---- .../modules/monitoring/sensu_subscription.py | 7 ++--- lib/ansible/modules/packaging/language/npm.py | 9 +------ lib/ansible/modules/system/service.py | 6 +---- .../modules/utilities/logic/async_wrapper.py | 5 +--- lib/ansible/parsing/utils/jsonify.py | 5 +--- lib/ansible/plugins/action/__init__.py | 4 +-- lib/ansible/plugins/action/fetch.py | 2 +- lib/ansible/plugins/filter/core.py | 24 ++++++----------- lib/ansible/plugins/lookup/consul_kv.py | 5 +--- packaging/port/sysutils/ansible/Makefile | 3 +-- test/units/module_utils/basic/test_imports.py | 9 +++---- 55 files changed, 73 insertions(+), 285 deletions(-) create mode 100644 changelogs/fragments/remove-simplejson.yaml diff --git a/changelogs/fragments/remove-simplejson.yaml b/changelogs/fragments/remove-simplejson.yaml new file mode 100644 index 0000000000..58003403f3 --- /dev/null +++ b/changelogs/fragments/remove-simplejson.yaml @@ -0,0 +1,2 @@ +major_changes: +- Remove support for simplejson (https://github.com/ansible/ansible/issues/42761) diff --git a/contrib/inventory/abiquo.py b/contrib/inventory/abiquo.py index ce5fa1f3df..621ef96620 100755 --- a/contrib/inventory/abiquo.py +++ b/contrib/inventory/abiquo.py @@ -46,10 +46,7 @@ import sys import time import ConfigParser -try: - import json -except ImportError: - import simplejson as json +import json from ansible.module_utils.urls import open_url diff --git a/contrib/inventory/apache-libcloud.py b/contrib/inventory/apache-libcloud.py index 067076fdf5..5d3fe9a2e0 100755 --- a/contrib/inventory/apache-libcloud.py +++ b/contrib/inventory/apache-libcloud.py @@ -42,10 +42,7 @@ from libcloud.compute.types import Provider from libcloud.compute.providers import get_driver import libcloud.security as sec -try: - import json -except ImportError: - import simplejson as json +import json class LibcloudInventory(object): diff --git a/contrib/inventory/brook.py b/contrib/inventory/brook.py index 378e713fac..dc983761c0 100755 --- a/contrib/inventory/brook.py +++ b/contrib/inventory/brook.py @@ -84,10 +84,7 @@ try: except ImportError: from configparser import ConfigParser -try: - import json -except ImportError: - import simplejson as json +import json try: import libbrook diff --git a/contrib/inventory/cloudforms.py b/contrib/inventory/cloudforms.py index 64c300cab8..51c080d396 100755 --- a/contrib/inventory/cloudforms.py +++ b/contrib/inventory/cloudforms.py @@ -31,10 +31,7 @@ from requests.auth import HTTPBasicAuth import warnings from ansible.errors import AnsibleError -try: - import json -except ImportError: - import simplejson as json +import json class CloudFormsInventory(object): diff --git a/contrib/inventory/cloudstack.py b/contrib/inventory/cloudstack.py index 0d5d67bb14..36ad84c520 100755 --- a/contrib/inventory/cloudstack.py +++ b/contrib/inventory/cloudstack.py @@ -74,12 +74,7 @@ from __future__ import print_function import sys import argparse - -try: - import json -except: - import simplejson as json - +import json try: from cs import CloudStack, CloudStackException, read_config diff --git a/contrib/inventory/cobbler.py b/contrib/inventory/cobbler.py index 87e855faa5..eff9318967 100755 --- a/contrib/inventory/cobbler.py +++ b/contrib/inventory/cobbler.py @@ -65,10 +65,7 @@ import re from time import time import xmlrpclib -try: - import json -except ImportError: - import simplejson as json +import json from six import iteritems diff --git a/contrib/inventory/collins.py b/contrib/inventory/collins.py index a6af0d7302..8c7b36c9bc 100755 --- a/contrib/inventory/collins.py +++ b/contrib/inventory/collins.py @@ -75,10 +75,7 @@ import sys from time import time import traceback -try: - import json -except ImportError: - import simplejson as json +import json from six import iteritems from six.moves.urllib.parse import urlencode diff --git a/contrib/inventory/consul_io.py b/contrib/inventory/consul_io.py index 57bad6f934..17defb435e 100755 --- a/contrib/inventory/consul_io.py +++ b/contrib/inventory/consul_io.py @@ -191,10 +191,7 @@ if os.getenv('ANSIBLE_INVENTORY_CONSUL_IO_LOG_ENABLED'): setup_logging() -try: - import json -except ImportError: - import simplejson as json +import json try: import consul diff --git a/contrib/inventory/digital_ocean.py b/contrib/inventory/digital_ocean.py index f067876f49..eb573677a9 100755 --- a/contrib/inventory/digital_ocean.py +++ b/contrib/inventory/digital_ocean.py @@ -149,10 +149,7 @@ try: except ImportError: import configparser as ConfigParser -try: - import json -except ImportError: - import simplejson as json +import json class DoManager: diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index 479bdf1e4e..45619c2ac3 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -179,10 +179,7 @@ except ImportError: from six.moves import configparser from collections import defaultdict -try: - import json -except ImportError: - import simplejson as json +import json DEFAULTS = { 'all_elasticache_clusters': 'False', diff --git a/contrib/inventory/fleet.py b/contrib/inventory/fleet.py index e749aa6905..dd0d4f7168 100755 --- a/contrib/inventory/fleet.py +++ b/contrib/inventory/fleet.py @@ -29,10 +29,7 @@ import subprocess import re import string from optparse import OptionParser -try: - import json -except: - import simplejson as json +import json # Options # ------------------------------ diff --git a/contrib/inventory/gce.py b/contrib/inventory/gce.py index 5c5773e0aa..d0285ecf17 100755 --- a/contrib/inventory/gce.py +++ b/contrib/inventory/gce.py @@ -101,10 +101,7 @@ else: import logging logging.getLogger('libcloud.common.google').addHandler(logging.NullHandler()) -try: - import json -except ImportError: - import simplejson as json +import json try: from libcloud.compute.types import Provider diff --git a/contrib/inventory/landscape.py b/contrib/inventory/landscape.py index 4b53171c34..9aa660bef8 100755 --- a/contrib/inventory/landscape.py +++ b/contrib/inventory/landscape.py @@ -40,10 +40,7 @@ import sys from landscape_api.base import API, HTTPError -try: - import json -except ImportError: - import simplejson as json +import json _key = 'landscape' diff --git a/contrib/inventory/linode.py b/contrib/inventory/linode.py index 87198dddd2..5af8d25923 100755 --- a/contrib/inventory/linode.py +++ b/contrib/inventory/linode.py @@ -81,10 +81,7 @@ import sys import argparse from time import time -try: - import json -except ImportError: - import simplejson as json +import json try: from chube import load_chube_config diff --git a/contrib/inventory/openshift.py b/contrib/inventory/openshift.py index 479b8085d6..e6ba8ecfa2 100755 --- a/contrib/inventory/openshift.py +++ b/contrib/inventory/openshift.py @@ -28,10 +28,7 @@ version_added: None author: Michael Scherer ''' -try: - import json -except ImportError: - import simplejson as json +import json import os import os.path import sys diff --git a/contrib/inventory/openstack_inventory.py b/contrib/inventory/openstack_inventory.py index 81237a7bf2..ab2d96cb8b 100755 --- a/contrib/inventory/openstack_inventory.py +++ b/contrib/inventory/openstack_inventory.py @@ -59,10 +59,7 @@ import time from distutils.version import StrictVersion from io import StringIO -try: - import json -except: - import simplejson as json +import json import openstack as sdk from openstack.cloud import inventory as sdk_inventory diff --git a/contrib/inventory/ovirt.py b/contrib/inventory/ovirt.py index 9ac85aae79..90729558ab 100755 --- a/contrib/inventory/ovirt.py +++ b/contrib/inventory/ovirt.py @@ -71,11 +71,7 @@ import argparse import ConfigParser from collections import defaultdict -try: - import json -except ImportError: - # noinspection PyUnresolvedReferences,PyPackageRequirements - import simplejson as json +import json try: # noinspection PyUnresolvedReferences diff --git a/contrib/inventory/ovirt4.py b/contrib/inventory/ovirt4.py index 33fb5b3a8a..040b33255a 100755 --- a/contrib/inventory/ovirt4.py +++ b/contrib/inventory/ovirt4.py @@ -70,10 +70,7 @@ try: except ImportError: import configparser -try: - import json -except ImportError: - import simplejson as json +import json try: import ovirtsdk4 as sdk diff --git a/contrib/inventory/packet_net.py b/contrib/inventory/packet_net.py index 0b1a1f5730..d919a67d80 100755 --- a/contrib/inventory/packet_net.py +++ b/contrib/inventory/packet_net.py @@ -55,10 +55,7 @@ except ImportError as e: import traceback -try: - import json -except ImportError: - import simplejson as json +import json ini_section = 'packet' diff --git a/contrib/inventory/proxmox.py b/contrib/inventory/proxmox.py index afa7032ad2..351e55da09 100755 --- a/contrib/inventory/proxmox.py +++ b/contrib/inventory/proxmox.py @@ -25,10 +25,7 @@ # # { "groups": ["utility", "databases"], "a": false, "b": true } -try: - import json -except ImportError: - import simplejson as json +import json import os import sys from optparse import OptionParser diff --git a/contrib/inventory/rax.py b/contrib/inventory/rax.py index 1e66d28b14..96a32c1fbb 100755 --- a/contrib/inventory/rax.py +++ b/contrib/inventory/rax.py @@ -155,10 +155,7 @@ import ConfigParser from six import iteritems -try: - import json -except ImportError: - import simplejson as json +import json try: import pyrax diff --git a/contrib/inventory/rudder.py b/contrib/inventory/rudder.py index 4e2a119152..54bb616840 100755 --- a/contrib/inventory/rudder.py +++ b/contrib/inventory/rudder.py @@ -59,10 +59,7 @@ from time import time from ansible.module_utils.six.moves import configparser from ansible.module_utils.six.moves.urllib.parse import urlparse -try: - import json -except ImportError: - import simplejson as json +import json class RudderInventory(object): diff --git a/contrib/inventory/scaleway.py b/contrib/inventory/scaleway.py index bf55a0e093..756af89931 100755 --- a/contrib/inventory/scaleway.py +++ b/contrib/inventory/scaleway.py @@ -47,10 +47,7 @@ import sys import time import traceback -try: - import json -except ImportError: - import simplejson as json +import json EMPTY_GROUP = { 'children': [], diff --git a/contrib/inventory/serf.py b/contrib/inventory/serf.py index 5bdcad7f76..8a24027dd4 100755 --- a/contrib/inventory/serf.py +++ b/contrib/inventory/serf.py @@ -38,10 +38,7 @@ import sys # https://pypi.org/project/serfclient/ from serfclient import SerfClient, EnvironmentConfig -try: - import json -except ImportError: - import simplejson as json +import json _key = 'serf' diff --git a/contrib/inventory/softlayer.py b/contrib/inventory/softlayer.py index d53b6fa26d..b87d874cb0 100755 --- a/contrib/inventory/softlayer.py +++ b/contrib/inventory/softlayer.py @@ -36,10 +36,7 @@ import SoftLayer import re import argparse import itertools -try: - import json -except: - import simplejson as json +import json class SoftLayerInventory(object): diff --git a/contrib/inventory/spacewalk.py b/contrib/inventory/spacewalk.py index 770c488bb2..3d6d00b8f5 100755 --- a/contrib/inventory/spacewalk.py +++ b/contrib/inventory/spacewalk.py @@ -48,13 +48,10 @@ import time from optparse import OptionParser import subprocess import ConfigParser +import json from six import iteritems -try: - import json -except: - import simplejson as json base_dir = os.path.dirname(os.path.realpath(__file__)) default_ini_file = os.path.join(base_dir, "spacewalk.ini") diff --git a/contrib/inventory/ssh_config.py b/contrib/inventory/ssh_config.py index e7195f6b1e..03c4aa3d7f 100755 --- a/contrib/inventory/ssh_config.py +++ b/contrib/inventory/ssh_config.py @@ -45,10 +45,7 @@ import os.path import sys from collections import MutableSequence -try: - import json -except ImportError: - import simplejson as json +import json import paramiko diff --git a/contrib/inventory/stacki.py b/contrib/inventory/stacki.py index 824ecaf5e7..7af106ec9b 100755 --- a/contrib/inventory/stacki.py +++ b/contrib/inventory/stacki.py @@ -49,10 +49,7 @@ import sys import yaml from distutils.version import StrictVersion -try: - import json -except: - import simplejson as json +import json try: import requests diff --git a/contrib/inventory/vagrant.py b/contrib/inventory/vagrant.py index 072340015e..d235ed4df8 100755 --- a/contrib/inventory/vagrant.py +++ b/contrib/inventory/vagrant.py @@ -40,10 +40,7 @@ import re from paramiko import SSHConfig from optparse import OptionParser from collections import defaultdict -try: - import json -except Exception: - import simplejson as json +import json from ansible.module_utils._text import to_text from ansible.module_utils.six.moves import StringIO diff --git a/contrib/inventory/vbox.py b/contrib/inventory/vbox.py index 61ecf2826a..47fe2f6923 100755 --- a/contrib/inventory/vbox.py +++ b/contrib/inventory/vbox.py @@ -18,10 +18,7 @@ import sys from subprocess import Popen, PIPE -try: - import json -except ImportError: - import simplejson as json +import json class SetEncoder(json.JSONEncoder): diff --git a/contrib/inventory/zabbix.py b/contrib/inventory/zabbix.py index 0f57bff14f..86769ea778 100755 --- a/contrib/inventory/zabbix.py +++ b/contrib/inventory/zabbix.py @@ -49,10 +49,7 @@ except: file=sys.stderr) sys.exit(1) -try: - import json -except: - import simplejson as json +import json class ZabbixInventory(object): diff --git a/docs/docsite/rst/user_guide/intro_bsd.rst b/docs/docsite/rst/user_guide/intro_bsd.rst index 1b4ea0b339..d93cc35047 100644 --- a/docs/docsite/rst/user_guide/intro_bsd.rst +++ b/docs/docsite/rst/user_guide/intro_bsd.rst @@ -16,7 +16,8 @@ versions of sshpass do not deal particularly well with BSD login prompts, so whe [freebsd] mybsdhost1 ansible_connection=paramiko -Ansible is agentless by default, however certain software is required on the target machines. Using Python 2.4 on the agents requires an additional py-simplejson package/library to be installed, however this library is already included in Python 2.5 and above. +Ansible is agentless by default, however certain software is required on the target machines. + Operating without Python is possible with the ``raw`` module. Although this module can be used to bootstrap Ansible and install Python on BSD variants (see below), it is very limited and the use of Python is required to make full use of Ansible's features. .. _bootstrap_bsd: diff --git a/hacking/test-module b/hacking/test-module index b0764982a4..bd9d96a474 100755 --- a/hacking/test-module +++ b/hacking/test-module @@ -44,10 +44,7 @@ import ansible.constants as C from ansible.module_utils._text import to_native, to_text from ansible.template import Templar -try: - import json -except ImportError: - import simplejson as json +import json def parse(): diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index ac96d0a9a7..b87f7e587d 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -109,26 +109,14 @@ NoneType = type(None) try: import json # Detect the python-json library which is incompatible - # Look for simplejson if that's the case try: if not isinstance(json.loads, types.FunctionType) or not isinstance(json.dumps, types.FunctionType): raise ImportError except AttributeError: raise ImportError except ImportError: - try: - import simplejson as json - except ImportError: - print('\n{"msg": "Error: ansible requires the stdlib json or simplejson module, neither was found!", "failed": true}') - sys.exit(1) - except SyntaxError: - print('\n{"msg": "SyntaxError: probably due to installed simplejson being for a different python version", "failed": true}') - sys.exit(1) - else: - sj_version = json.__version__.split('.') - if sj_version < ['1', '6']: - # Version 1.5 released 2007-01-18 does not have the encoding parameter which we need - print('\n{"msg": "Error: Ansible requires the stdlib json or simplejson >= 1.6. Neither was found!", "failed": true}') + print('\n{"msg": "Error: ansible requires the stdlib json and was not found!", "failed": true}') + sys.exit(1) AVAILABLE_HASH_ALGORITHMS = dict() try: diff --git a/lib/ansible/module_utils/ipa.py b/lib/ansible/module_utils/ipa.py index da4a6d4806..245e0cec6e 100644 --- a/lib/ansible/module_utils/ipa.py +++ b/lib/ansible/module_utils/ipa.py @@ -27,10 +27,7 @@ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -try: - import json -except ImportError: - import simplejson as json +import json import re from ansible.module_utils._text import to_bytes, to_native, to_text diff --git a/lib/ansible/module_utils/json_utils.py b/lib/ansible/module_utils/json_utils.py index 181e955d54..571fe5b020 100644 --- a/lib/ansible/module_utils/json_utils.py +++ b/lib/ansible/module_utils/json_utils.py @@ -23,10 +23,8 @@ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # -try: - import json -except ImportError: - import simplejson as json + +import json # NB: a copy of this function exists in ../../modules/core/async_wrapper.py. Ensure any diff --git a/lib/ansible/module_utils/lxd.py b/lib/ansible/module_utils/lxd.py index f9ec1fd4e2..c53c3a76ab 100644 --- a/lib/ansible/module_utils/lxd.py +++ b/lib/ansible/module_utils/lxd.py @@ -39,10 +39,7 @@ from ansible.module_utils._text import to_text HTTPConnection = http_client.HTTPConnection HTTPSConnection = http_client.HTTPSConnection -try: - import json -except ImportError: - import simplejson as json +import json class UnixHTTPConnection(HTTPConnection): diff --git a/lib/ansible/module_utils/netapp.py b/lib/ansible/module_utils/netapp.py index ac0c85e8e1..40bd6bb1ad 100644 --- a/lib/ansible/module_utils/netapp.py +++ b/lib/ansible/module_utils/netapp.py @@ -27,10 +27,7 @@ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -try: - import json -except ImportError: - import simplejson as json +import json from ansible.module_utils.six.moves.urllib.error import HTTPError from ansible.module_utils.urls import open_url diff --git a/lib/ansible/module_utils/network/f5/icontrol.py b/lib/ansible/module_utils/network/f5/icontrol.py index 3a795cfd60..f40bfd60d9 100644 --- a/lib/ansible/module_utils/network/f5/icontrol.py +++ b/lib/ansible/module_utils/network/f5/icontrol.py @@ -19,10 +19,7 @@ from ansible.module_utils.urls import urllib_error from ansible.module_utils._text import to_native from ansible.module_utils.six import PY3 -try: - import json as _json -except ImportError: - import simplejson as _json +import json as _json try: from library.module_utils.network.f5.common import F5ModuleError diff --git a/lib/ansible/modules/commands/raw.py b/lib/ansible/modules/commands/raw.py index 0b4785403b..74cc676398 100644 --- a/lib/ansible/modules/commands/raw.py +++ b/lib/ansible/modules/commands/raw.py @@ -30,10 +30,9 @@ options: version_added: "1.0" description: - Executes a low-down and dirty SSH command, not going through the module - subsystem. This is useful and should only be done in two cases. The - first case is installing C(python-simplejson) on older (Python 2.4 and - before) hosts that need it as a dependency to run modules, since nearly - all core modules require it. Another is speaking to any devices such as + subsystem. This is useful and should only be done in a few cases. A common + case is installing C(python) on a system without python installed by default. + Another is speaking to any devices such as routers that do not have any Python installed. In any other case, using the M(shell) or M(command) module is much more appropriate. Arguments given to C(raw) are run directly through the configured remote shell. @@ -58,9 +57,6 @@ author: ''' EXAMPLES = ''' -- name: Bootstrap a legacy python 2.4 host - raw: yum -y install python-simplejson - - name: Bootstrap a host without python2 installed raw: dnf install -y python2 python2-dnf libselinux-python @@ -70,5 +66,5 @@ EXAMPLES = ''' executable: /bin/bash - name: safely use templated variables. Always use quote filter to avoid injection issues. - raw: "{{package_mgr|quote}} {{pkg_flags|quote}} install {{python_simplejson|quote}}" + raw: "{{package_mgr|quote}} {{pkg_flags|quote}} install {{python|quote}}" ''' diff --git a/lib/ansible/modules/monitoring/logicmonitor.py b/lib/ansible/modules/monitoring/logicmonitor.py index 3151e46e39..3047847b65 100644 --- a/lib/ansible/modules/monitoring/logicmonitor.py +++ b/lib/ansible/modules/monitoring/logicmonitor.py @@ -497,6 +497,7 @@ EXAMPLES = ''' ''' import datetime +import json import os import platform import socket @@ -508,28 +509,6 @@ from ansible.module_utils.six.moves.urllib.parse import urlencode from ansible.module_utils.urls import open_url -HAS_LIB_JSON = True -try: - import json - # Detect the python-json library which is incompatible - # Look for simplejson if that's the case - try: - if ( - not isinstance(json.loads, types.FunctionType) or - not isinstance(json.dumps, types.FunctionType) - ): - raise ImportError - except AttributeError: - raise ImportError -except ImportError: - try: - import simplejson as json - except ImportError: - HAS_LIB_JSON = False - except SyntaxError: - HAS_LIB_JSON = False - - class LogicMonitor(object): def __init__(self, module, **params): @@ -2148,9 +2127,6 @@ def main(): supports_check_mode=True ) - if HAS_LIB_JSON is not True: - module.fail_json(msg="Unable to load JSON library") - selector(module) diff --git a/lib/ansible/modules/monitoring/logicmonitor_facts.py b/lib/ansible/modules/monitoring/logicmonitor_facts.py index d69ae23cd0..5811f011d0 100644 --- a/lib/ansible/modules/monitoring/logicmonitor_facts.py +++ b/lib/ansible/modules/monitoring/logicmonitor_facts.py @@ -113,30 +113,10 @@ RETURN = ''' ... ''' +import json import socket import types -HAS_LIB_JSON = True -try: - import json - # Detect the python-json library which is incompatible - # Look for simplejson if that's the case - try: - if ( - not isinstance(json.loads, types.FunctionType) or - not isinstance(json.dumps, types.FunctionType) - ): - raise ImportError - except AttributeError: - raise ImportError -except ImportError: - try: - import simplejson as json - except ImportError: - HAS_LIB_JSON = False - except SyntaxError: - HAS_LIB_JSON = False - from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.six.moves.urllib.parse import urlencode from ansible.module_utils._text import to_native @@ -577,9 +557,6 @@ def main(): supports_check_mode=True ) - if HAS_LIB_JSON is not True: - module.fail_json(msg="Unable to load JSON library") - selector(module) diff --git a/lib/ansible/modules/monitoring/sensu_silence.py b/lib/ansible/modules/monitoring/sensu_silence.py index 2879b904cc..ccabb68f7f 100644 --- a/lib/ansible/modules/monitoring/sensu_silence.py +++ b/lib/ansible/modules/monitoring/sensu_silence.py @@ -95,11 +95,7 @@ EXAMPLES = ''' RETURN = ''' ''' - -try: - import json -except ImportError: - import simplejson as json +import json from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.urls import fetch_url diff --git a/lib/ansible/modules/monitoring/sensu_subscription.py b/lib/ansible/modules/monitoring/sensu_subscription.py index f10ff238cc..81bc48904a 100644 --- a/lib/ansible/modules/monitoring/sensu_subscription.py +++ b/lib/ansible/modules/monitoring/sensu_subscription.py @@ -64,6 +64,8 @@ EXAMPLES = ''' - name: unsubscribe from common checks sensu_subscription: name=common state=absent ''' + +import json import traceback from ansible.module_utils.basic import AnsibleModule @@ -74,11 +76,6 @@ def sensu_subscription(module, path, name, state='present', backup=False): changed = False reasons = [] - try: - import json - except ImportError: - import simplejson as json - try: config = json.load(open(path)) except IOError as e: diff --git a/lib/ansible/modules/packaging/language/npm.py b/lib/ansible/modules/packaging/language/npm.py index 2f871b2f97..e87e1ee3db 100644 --- a/lib/ansible/modules/packaging/language/npm.py +++ b/lib/ansible/modules/packaging/language/npm.py @@ -123,14 +123,7 @@ import re from ansible.module_utils.basic import AnsibleModule -try: - import json -except ImportError: - try: - import simplejson as json - except ImportError: - # Let snippet from module_utils/basic.py return a proper error in this case - pass +import json class Npm(object): diff --git a/lib/ansible/modules/system/service.py b/lib/ansible/modules/system/service.py index 6061806345..dce8c7f2fd 100644 --- a/lib/ansible/modules/system/service.py +++ b/lib/ansible/modules/system/service.py @@ -114,6 +114,7 @@ EXAMPLES = ''' ''' import glob +import json import os import platform import re @@ -124,11 +125,6 @@ import subprocess import tempfile import time -try: - import json -except ImportError: - import simplejson as json - # The distutils module is not shipped with SUNWPython on Solaris. # It's in the SUNWPython-devel package which also contains development files # that don't belong on production boxes. Since our Solaris code doesn't diff --git a/lib/ansible/modules/utilities/logic/async_wrapper.py b/lib/ansible/modules/utilities/logic/async_wrapper.py index bbe4c28f63..c1e331bc5f 100644 --- a/lib/ansible/modules/utilities/logic/async_wrapper.py +++ b/lib/ansible/modules/utilities/logic/async_wrapper.py @@ -8,10 +8,7 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -try: - import json -except ImportError: - import simplejson as json +import json import shlex import shutil import os diff --git a/lib/ansible/parsing/utils/jsonify.py b/lib/ansible/parsing/utils/jsonify.py index 53885f1ec0..19ebc5653b 100644 --- a/lib/ansible/parsing/utils/jsonify.py +++ b/lib/ansible/parsing/utils/jsonify.py @@ -19,10 +19,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -try: - import json -except ImportError: - import simplejson as json +import json def jsonify(result, format=False): diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 41ac119d95..162c544118 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -575,8 +575,8 @@ class ActionBase(with_metaclass(ABCMeta, object)): x = "2" # cannot read file elif errormsg.endswith(u'MODULE FAILURE'): x = "4" # python not found or module uncaught exception - elif 'json' in errormsg or 'simplejson' in errormsg: - x = "5" # json or simplejson modules needed + elif 'json' in errormsg: + x = "5" # json module needed finally: return x # pylint: disable=lost-exception diff --git a/lib/ansible/plugins/action/fetch.py b/lib/ansible/plugins/action/fetch.py index fbaf9926c5..475c9df9f6 100644 --- a/lib/ansible/plugins/action/fetch.py +++ b/lib/ansible/plugins/action/fetch.py @@ -159,7 +159,7 @@ class ActionModule(ActionBase): elif remote_checksum == '4': result['msg'] = "python isn't present on the system. Unable to compute checksum" elif remote_checksum == '5': - result['msg'] = "stdlib json or simplejson was not found on the remote machine. Only the raw module can work without those installed" + result['msg'] = "stdlib json was not found on the remote machine. Only the raw module can work without those installed" # Historically, these don't fail because you may want to transfer # a log file that possibly MAY exist but keep going to fetch other # log files. Today, this is better achieved by adding diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 7c1f5561af..70d5b8cf52 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -58,6 +58,12 @@ from ansible.utils.hashing import md5s, checksum_s from ansible.utils.unicode import unicode_wrap from ansible.utils.vars import merge_hash +try: + from __main__ import display +except ImportError: + from ansible.utils.display import Display + display = Display() + UUID_NAMESPACE_ANSIBLE = uuid.UUID('361E6D51-FAEC-444A-9079-341386DA8E2E') @@ -80,25 +86,11 @@ def to_json(a, *args, **kw): def to_nice_json(a, indent=4, *args, **kw): '''Make verbose, human readable JSON''' - # python-2.6's json encoder is buggy (can't encode hostvars) - if sys.version_info < (2, 7): - try: - import simplejson - except ImportError: - pass - else: - try: - major = int(simplejson.__version__.split('.')[0]) - except Exception: - pass - else: - if major >= 2: - return simplejson.dumps(a, default=AnsibleJSONEncoder.default, indent=indent, sort_keys=True, *args, **kw) - try: return json.dumps(a, indent=indent, sort_keys=True, cls=AnsibleJSONEncoder, *args, **kw) - except Exception: + except Exception as e: # Fallback to the to_json filter + display.warning(u'Unable to convert data using to_nice_json, falling back to to_json: %s' % to_text(e)) return to_json(a, *args, **kw) diff --git a/lib/ansible/plugins/lookup/consul_kv.py b/lib/ansible/plugins/lookup/consul_kv.py index 8d6edc2342..60f98c2dc6 100644 --- a/lib/ansible/plugins/lookup/consul_kv.py +++ b/lib/ansible/plugins/lookup/consul_kv.py @@ -67,10 +67,7 @@ from ansible.module_utils.six.moves.urllib.parse import urlparse from ansible.errors import AnsibleError, AnsibleAssertionError from ansible.plugins.lookup import LookupBase -try: - import json -except ImportError: - import simplejson as json +import json try: import consul diff --git a/packaging/port/sysutils/ansible/Makefile b/packaging/port/sysutils/ansible/Makefile index 0ad6201999..70ccb77254 100644 --- a/packaging/port/sysutils/ansible/Makefile +++ b/packaging/port/sysutils/ansible/Makefile @@ -13,11 +13,10 @@ COMMENT= Ansible ssh based config management framework LICENSE= GPLv3 LICENSE_FILE= ${WRKSRC}/COPYING -RUN_DEPENDS= python>2.5:${PORTSDIR}/lang/python \ +RUN_DEPENDS= python>2.7:${PORTSDIR}/lang/python \ ${PORTSDIR}/devel/py-Jinja2 \ ${PORTSDIR}/devel/py-yaml \ ${PORTSDIR}/security/py-paramiko \ - ${PORTSDIR}/devel/py-simplejson \ ${PORTSDIR}/security/py-pycrypto OPTIONS_DEFINE= EXAMPLES diff --git a/test/units/module_utils/basic/test_imports.py b/test/units/module_utils/basic/test_imports.py index 9f12fa6f37..0a0f8333f6 100644 --- a/test/units/module_utils/basic/test_imports.py +++ b/test/units/module_utils/basic/test_imports.py @@ -66,18 +66,15 @@ class TestImports(ModuleTestCase): def _mock_import(name, *args, **kwargs): if name == 'json': raise ImportError - elif name == 'simplejson': - sj = MagicMock() - sj.__version__ = '3.10.0' - return sj return realimport(name, *args, **kwargs) self.clear_modules(['json', 'ansible.module_utils.basic']) - mod = builtins.__import__('ansible.module_utils.basic') + builtins.__import__('ansible.module_utils.basic') self.clear_modules(['json', 'ansible.module_utils.basic']) mock_import.side_effect = _mock_import - mod = builtins.__import__('ansible.module_utils.basic') + with self.assertRaises(SystemExit): + builtins.__import__('ansible.module_utils.basic') # FIXME: doesn't work yet # @patch.object(builtins, 'bytes')