Replace python imp module with importlib

The imp module has been deprecated for quite some time. The importlib
module has been present since python 3.6 and replaces the imp module.
This is important because python3.12 removes the imp module entirely and
any code using imp needs to be rewritten to be python3.12 compatible.

The rewrite here comes from the suggestions in an upstream discussion
about this transition [0].


Change-Id: Iaa742c71bbb208225726c2b8fa5af111f48667c9
This commit is contained in:
Clark Boylan 2024-01-10 13:08:11 -08:00
parent 8a41a11885
commit 2846fe2e0d
1 changed files with 14 additions and 5 deletions

View File

@ -13,8 +13,9 @@
# You should have received a copy of the GNU General Public License
# along with this software. If not, see <>.
import imp
import importlib
import os
import sys
import ansible.plugins.action
@ -44,10 +45,18 @@ def _import_ansible_action_plugin(name):
# namespace, causing infinite recursion. So we supply an
# otherwise unused name for the module:
return imp.load_module(
'zuul.ansible.protected.action.' + name,
*imp.find_module(name, ansible.plugins.action.__path__))
# From
# for converting imp module loads to python3.12 compatible code.
spec = importlib.machinery.PathFinder.find_spec(
name, ansible.plugins.action.__path__)
if not spec:
raise Exception("Could not find %s at module path %s" %
(name, ansible.plugins.action.__path__))
mod = importlib.util.module_from_spec(spec)
sys.modules['zuul.ansible.protected.action.' + name] = mod
return mod
def _sanitize_filename(name):