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 ansible.utils.display import Display
from collections import namedtuple from collections import namedtuple
import importlib.util
try: import importlib.machinery
import importlib.util
import importlib.machinery
imp = None
except ImportError:
import imp
# if we're on a Python that doesn't have FNFError, redefine it as IOError (since that's what we'll see) # if we're on a Python that doesn't have FNFError, redefine it as IOError (since that's what we'll see)
try: try:
@ -812,34 +807,14 @@ class LegacyModuleUtilLocator(ModuleUtilLocatorBase):
paths = [os.path.join(p, *rel_name_parts[:-1]) for p in paths = [os.path.join(p, *rel_name_parts[:-1]) for p in
self._mu_paths] # extend the MU paths with the relative bit 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
# find_spec needs the full module name self._info = info = importlib.machinery.PathFinder.find_spec('.'.join(name_parts), paths)
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:
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')
self.is_package = info.origin.endswith('/__init__.py') path = info.origin
path = info.origin else:
else: return False
return False self.source_code = _slurp(path)
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()
return True return True

@ -38,11 +38,7 @@ except ImportError:
SpecifierSet = None SpecifierSet = None
Version = None Version = None
try: import importlib.util
import importlib.util
imp = None
except ImportError:
import imp
display = Display() display = Display()
@ -788,15 +784,10 @@ class PluginLoader:
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning) warnings.simplefilter("ignore", RuntimeWarning)
if imp is None: spec = importlib.util.spec_from_file_location(to_native(full_name), to_native(path))
spec = importlib.util.spec_from_file_location(to_native(full_name), to_native(path)) module = importlib.util.module_from_spec(spec)
module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module)
spec.loader.exec_module(module) sys.modules[full_name] = 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)
return module return module
def _update_object(self, obj, name, path, redirected_names=None): def _update_object(self, obj, name, path, redirected_names=None):

@ -23,15 +23,11 @@ import os
from hashlib import sha1 from hashlib import sha1
# Backwards compat only
try: try:
from hashlib import md5 as _md5 from hashlib import md5 as _md5
except ImportError: except ImportError:
try: # Assume we're running in FIPS mode here
from md5 import md5 as _md5 _md5 = None
except ImportError:
# Assume we're running in FIPS mode here
_md5 = None
from ansible.errors import AnsibleError from ansible.errors import AnsibleError
from ansible.module_utils._text import to_bytes 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 # This exists in utils to allow it to be easily imported into various places
# without causing circular import or dependency problems # without causing circular import or dependency problems
try: context = multiprocessing.get_context('fork')
context = multiprocessing.get_context('fork')
except AttributeError:
# Py2 has no context functionality, and only supports fork
context = multiprocessing

Loading…
Cancel
Save