From 0397cba0aba0d0d30bdbbcb70ef4f628d14b4d44 Mon Sep 17 00:00:00 2001 From: Chris Burr Date: Thu, 18 Apr 2024 07:15:31 +0200 Subject: [PATCH 1/2] fix: Drop imp to support Python 3.12 --- Pilot/pilotTools.py | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/Pilot/pilotTools.py b/Pilot/pilotTools.py index 6c3fc88e..bb99cab0 100644 --- a/Pilot/pilotTools.py +++ b/Pilot/pilotTools.py @@ -5,7 +5,6 @@ import fcntl import getopt -import imp import json import os import re @@ -31,6 +30,33 @@ from urllib2 import HTTPError, URLError, urlopen +try: + import importlib.util + from importlib import import_module +except ImportError: + def import_module(module): + import imp + + impData = imp.find_module(module) + return imp.load_module(module, *impData) + + + def load_module_from_path(module_name, path_to_module): + import imp + fp, pathname, description = imp.find_module(module_name, [path_to_module]) + try: + module = imp.load_module('module_name', fp, pathname, description) + finally: + if fp: + fp.close() + +else: + def load_module_from_path(module_name, path_to_module): + spec = importlib.util.spec_from_file_location(module_name, path_to_module) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + try: from cStringIO import StringIO except ImportError: @@ -370,12 +396,9 @@ def __recurseImport(self, modName, parentModule=None, hideExceptions=False): modName = modName.split(".") try: if parentModule: - impData = imp.find_module(modName[0], parentModule.__path__) + module = load_module_from_path(modName[0], parentModule.__path__) else: - impData = imp.find_module(modName[0]) - impModule = imp.load_module(modName[0], *impData) - if impData[0]: - impData[0].close() + module = import_module(modName[0]) except ImportError as excp: if str(excp).find("No module named %s" % modName[0]) == 0: return None, None @@ -415,8 +438,7 @@ def getCommand(params, commandName): # Look for commands in the modules in the current directory first for module in modules: try: - impData = imp.find_module(module) - commandModule = imp.load_module(module, *impData) + commandModule = import_module(module) commandObject = getattr(commandModule, commandName) except Exception: pass From 746a6ba7cfacc21c4fafbd6cefe5707ed47b2a66 Mon Sep 17 00:00:00 2001 From: Chris Burr Date: Thu, 18 Apr 2024 07:18:00 +0200 Subject: [PATCH 2/2] Update pilotTools.py --- Pilot/pilotTools.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Pilot/pilotTools.py b/Pilot/pilotTools.py index bb99cab0..8bf48043 100644 --- a/Pilot/pilotTools.py +++ b/Pilot/pilotTools.py @@ -45,7 +45,7 @@ def load_module_from_path(module_name, path_to_module): import imp fp, pathname, description = imp.find_module(module_name, [path_to_module]) try: - module = imp.load_module('module_name', fp, pathname, description) + return imp.load_module(module_name, fp, pathname, description) finally: if fp: fp.close() @@ -396,9 +396,9 @@ def __recurseImport(self, modName, parentModule=None, hideExceptions=False): modName = modName.split(".") try: if parentModule: - module = load_module_from_path(modName[0], parentModule.__path__) + impModule = load_module_from_path(modName[0], parentModule.__path__) else: - module = import_module(modName[0]) + impModule = import_module(modName[0]) except ImportError as excp: if str(excp).find("No module named %s" % modName[0]) == 0: return None, None