Skip to content

Commit

Permalink
Replace deprecated imp module with importlib.
Browse files Browse the repository at this point in the history
done as part of Python 3.12 spike (CURA-11078)
  • Loading branch information
rburema committed Oct 20, 2023
1 parent 6745624 commit e86d717
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions UM/PluginRegistry.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Copyright (c) 2022 Ultimaker B.V.
# Copyright (c) 2023 UltiMaker
# Uranium is released under the terms of the LGPLv3 or higher.

import imp
import importlib.util
import importlib.machinery
import json
import os
import shutil # For deleting plugin directories;
import stat # For setting file permissions correctly;
import sys
import time
import types
import zipfile
Expand Down Expand Up @@ -762,7 +764,10 @@ def _findPlugin(self, plugin_id: str) -> Optional[types.ModuleType]:
except Exception:
pass
try:
file, path, desc = imp.find_module(plugin_id, [final_location])
spec = importlib.machinery.PathFinder().find_spec(plugin_id, [final_location])
if len(spec.submodule_search_locations) != 1:
raise IndexError(f"Attempt to load plugin '{plugin_id}' from {len(spec.submodule_search_locations)} locations.")
path = spec.submodule_search_locations[0]
except Exception:
Logger.logException("e", "Import error when importing %s", plugin_id)
return None
Expand All @@ -783,13 +788,12 @@ def _findPlugin(self, plugin_id: str) -> Optional[types.ModuleType]:
return None

try:
module = imp.load_module(plugin_id, file, path, desc) # type: ignore #MyPy gets the wrong output type from imp.find_module for some reason.
module = importlib.util.module_from_spec(spec)
sys.modules[plugin_id] = module
spec.loader.exec_module(module)
except Exception:
Logger.logException("e", "Import error loading module %s", plugin_id)
return None
finally:
if file:
os.close(file) #type: ignore #MyPy gets the wrong output type from imp.find_module for some reason.
self._found_plugins[plugin_id] = module
return module

Expand Down

0 comments on commit e86d717

Please sign in to comment.