From a1d32e9f0eb135cda0b968f793b4080fd6336c41 Mon Sep 17 00:00:00 2001 From: likui Date: Mon, 19 Oct 2020 17:12:37 +0800 Subject: [PATCH] replace imp module The imp module is deprecated since version 3.4 [1] https://docs.python.org/3/library/imp.html Change-Id: I7d0f07c338693b7c76e16359a36bfcf579ed0ee6 --- tests/test_hook_chef.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/test_hook_chef.py b/tests/test_hook_chef.py index 8147925..8a2e8ee 100644 --- a/tests/test_hook_chef.py +++ b/tests/test_hook_chef.py @@ -12,11 +12,13 @@ # under the License. import copy -import imp +import importlib import json import logging import six import sys + +from multiprocessing import Lock from unittest import mock from tests import common @@ -24,6 +26,15 @@ from tests import common log = logging.getLogger('test_hook_chef') +def load_module(name, path): + module_spec = importlib.util.spec_from_file_location( + name, path + ) + module = importlib.util.module_from_spec(module_spec) + module_spec.loader.exec_module(module) + return module + + @mock.patch("os.chdir") @mock.patch("os.makedirs") @mock.patch('subprocess.Popen') @@ -67,11 +78,12 @@ class HookChefTest(common.RunScriptTest): sys.stdout = sys.__stdout__ def get_module(self): + lock = Lock() try: - imp.acquire_lock() - return imp.load_source("hook_chef", self.hook_path) + lock.acquire() + return load_module("hook_chef", self.hook_path) finally: - imp.release_lock() + lock.release() def test_hook(self, mock_popen, mock_mkdirs, mock_chdir): data = copy.deepcopy(self.data)