Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Drop imp to support Python 3.12 #237

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions Pilot/pilotTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import fcntl
import getopt
import imp
import json
import os
import re
Expand All @@ -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:
return 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:
Expand Down Expand Up @@ -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__)
impModule = 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()
impModule = import_module(modName[0])
except ImportError as excp:
if str(excp).find("No module named %s" % modName[0]) == 0:
return None, None
Expand Down Expand Up @@ -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
Expand Down
Loading