Skip to content

Commit c0bf7e0

Browse files
committed
leveleditor: Fix use of removed imp module (untested)
1 parent c1a2458 commit c0bf7e0

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

direct/src/leveleditor/FileMgr.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
import imp
2+
import importlib.util
33

44

55
class FileMgr:
@@ -32,21 +32,22 @@ def saveToFile(self, fileName):
3232
f.write(data)
3333
f.write('\n')
3434
f.close()
35-
self.editor.updateStatusReadout('Sucessfully saved to %s'%fileName)
35+
self.editor.updateStatusReadout(f'Sucessfully saved to {fileName}')
3636
self.editor.fNeedToSave = False
3737
except IOError:
38-
print('failed to save %s'%fileName)
38+
print(f'failed to save {fileName}')
3939
if f:
4040
f.close()
4141

4242
def loadFromFile(self, fileName):
4343
dirname, moduleName = os.path.split(fileName)
4444
if moduleName.endswith('.py'):
4545
moduleName = moduleName[:-3]
46-
file, pathname, description = imp.find_module(moduleName, [dirname])
46+
spec = importlib.util.spec_from_file_location(moduleName, fileName)
4747
try:
48-
module = imp.load_module(moduleName, file, pathname, description)
49-
self.editor.updateStatusReadout('Sucessfully opened file %s'%fileName)
48+
module = importlib.util.module_from_spec(spec)
49+
spec.loader.exec_module(module)
50+
self.editor.updateStatusReadout(f'Sucessfully opened file {fileName}')
5051
self.editor.fNeedToSave = False
5152
except Exception:
52-
print('failed to load %s'%fileName)
53+
print(f'failed to load {fileName}')

direct/src/leveleditor/LevelLoaderBase.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import imp
1+
import importlib.util
22

33

44
class LevelLoaderBase:
@@ -30,10 +30,15 @@ def loadFromFile(self, fileName, filePath=None):
3030

3131
if fileName.endswith('.py'):
3232
fileName = fileName[:-3]
33-
file, pathname, description = imp.find_module(fileName, [filePath])
33+
3434
try:
35-
module = imp.load_module(fileName, file, pathname, description)
35+
spec = importlib.util.spec_from_file_location(fileName, filePath)
36+
if spec is None or spec.loader is None:
37+
raise ImportError
38+
39+
module = importlib.util.module_from_spec(spec)
40+
spec.loader.exec_module(module)
3641
return True
3742
except Exception:
38-
print('failed to load %s'%fileName)
43+
print(f'failed to load {fileName}')
3944
return None

direct/src/leveleditor/ProtoObjs.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Palette for Prototyping
33
"""
44
import os
5-
import imp
5+
import importlib.util
66

77

88
class ProtoObjs:
@@ -15,8 +15,13 @@ def __init__(self, name):
1515
def populate(self):
1616
moduleName = self.name
1717
try:
18-
file, pathname, description = imp.find_module(moduleName, [self.dirname])
19-
module = imp.load_module(moduleName, file, pathname, description)
18+
pathname = self.dirname + self.filename
19+
spec = importlib.util.spec_from_file_location(moduleName, pathname)
20+
if spec is None or spec.loader is None:
21+
raise ImportError
22+
23+
module = importlib.util.module_from_spec(spec)
24+
spec.loader.exec_module(module)
2025
self.data = module.protoData
2126
except Exception:
2227
print(f"{self.name} doesn't exist")

direct/src/leveleditor/ProtoPaletteBase.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""
22
Palette for Prototyping
33
"""
4-
import imp
4+
import importlib.util
5+
import os, sys
56

67
from .ObjectPaletteBase import ObjectBase, ObjectPaletteBase
78

@@ -23,8 +24,15 @@ def addItems(self):
2324
def populate(self):
2425
moduleName = 'protoPaletteData'
2526
try:
26-
file, pathname, description = imp.find_module(moduleName, [self.dirname])
27-
module = imp.load_module(moduleName, file, pathname, description)
27+
pathname = os.path.join(self.dirname, moduleName + ".py")
28+
spec = importlib.util.spec_from_file_location(moduleName, pathname)
29+
if spec is None or spec.loader is None:
30+
raise ImportError
31+
32+
module = importlib.util.module_from_spec(spec)
33+
sys.modules[module_name] = module
34+
spec.loader.exec_module(module)
35+
2836
self.data = module.protoData
2937
self.dataStruct = module.protoDataStruct
3038
except:

0 commit comments

Comments
 (0)