File: 0000-deprecated-imp.patch

package info (click to toggle)
uranium 5.0.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,328 kB
  • sloc: python: 31,765; sh: 132; makefile: 12
file content (55 lines) | stat: -rw-r--r-- 2,060 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
commit e86d717035af317dab5d62851181873ec3c38ebe
Author: Remco Burema <r.burema@ultimaker.com>
Date:   Fri Oct 20 14:40:10 2023 +0200

    Replace deprecated imp module with importlib.
    
    done as part of Python 3.12 spike (CURA-11078)

--- a/UM/PluginRegistry.py
+++ b/UM/PluginRegistry.py
@@ -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
@@ -704,7 +706,10 @@
             except:
                 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
@@ -725,13 +730,12 @@
                 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