Remove obsolete Python 2.x compat code.

pull/77224/head
Matt Clay 2 years ago
parent 0c4c18bc04
commit 3a3c496ade

@ -50,13 +50,8 @@ from ansible.executor import action_write_locks
from ansible.utils.display import Display
from collections import namedtuple
try:
import importlib.util
import importlib.machinery
imp = None
except ImportError:
import imp
import importlib.util
import importlib.machinery
# if we're on a Python that doesn't have FNFError, redefine it as IOError (since that's what we'll see)
try:
@ -812,34 +807,14 @@ class LegacyModuleUtilLocator(ModuleUtilLocatorBase):
paths = [os.path.join(p, *rel_name_parts[:-1]) for p in
self._mu_paths] # extend the MU paths with the relative bit
if imp is None: # python3 find module
# find_spec needs the full module name
self._info = info = importlib.machinery.PathFinder.find_spec('.'.join(name_parts), paths)
if info is not None and os.path.splitext(info.origin)[1] in importlib.machinery.SOURCE_SUFFIXES:
self.is_package = info.origin.endswith('/__init__.py')
path = info.origin
else:
return False
self.source_code = _slurp(path)
else: # python2 find module
try:
# imp just wants the leaf module/package name being searched for
info = imp.find_module(name_parts[-1], paths)
except ImportError:
return False
if info[2][2] == imp.PY_SOURCE:
fd = info[0]
elif info[2][2] == imp.PKG_DIRECTORY:
self.is_package = True
fd = open(os.path.join(info[1], '__init__.py'))
else:
return False
try:
self.source_code = fd.read()
finally:
fd.close()
# find_spec needs the full module name
self._info = info = importlib.machinery.PathFinder.find_spec('.'.join(name_parts), paths)
if info is not None and os.path.splitext(info.origin)[1] in importlib.machinery.SOURCE_SUFFIXES:
self.is_package = info.origin.endswith('/__init__.py')
path = info.origin
else:
return False
self.source_code = _slurp(path)
return True

@ -38,11 +38,7 @@ except ImportError:
SpecifierSet = None
Version = None
try:
import importlib.util
imp = None
except ImportError:
import imp
import importlib.util
display = Display()
@ -788,15 +784,10 @@ class PluginLoader:
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
if imp is None:
spec = importlib.util.spec_from_file_location(to_native(full_name), to_native(path))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
sys.modules[full_name] = module
else:
with open(to_bytes(path), 'rb') as module_file:
# to_native is used here because imp.load_source's path is for tracebacks and python's traceback formatting uses native strings
module = imp.load_source(to_native(full_name), to_native(path), module_file)
spec = importlib.util.spec_from_file_location(to_native(full_name), to_native(path))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
sys.modules[full_name] = module
return module
def _update_object(self, obj, name, path, redirected_names=None):

@ -23,15 +23,11 @@ import os
from hashlib import sha1
# Backwards compat only
try:
from hashlib import md5 as _md5
except ImportError:
try:
from md5 import md5 as _md5
except ImportError:
# Assume we're running in FIPS mode here
_md5 = None
# Assume we're running in FIPS mode here
_md5 = None
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_bytes

@ -14,8 +14,4 @@ import multiprocessing
#
# This exists in utils to allow it to be easily imported into various places
# without causing circular import or dependency problems
try:
context = multiprocessing.get_context('fork')
except AttributeError:
# Py2 has no context functionality, and only supports fork
context = multiprocessing
context = multiprocessing.get_context('fork')

Loading…
Cancel
Save