Skip to content

Commit 55ca839

Browse files
authored
Merge pull request #1465 from xael-fry/1457_python-imp-module
1457 python imp module
2 parents f832ef8 + 6211a2f commit 55ca839

File tree

5 files changed

+172
-136
lines changed

5 files changed

+172
-136
lines changed

.github/workflows/build-test.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ jobs:
2424
name: Check / Tests -> JDK-${{ matrix.jdk }}/${{ matrix.os }}
2525
steps:
2626
- name: Checkout
27-
uses: actions/checkout@v2
27+
uses: actions/checkout@v4
2828
with:
2929
# we don't know what commit the last tag was it's safer to get entire repo so previousStableVersion resolves
3030
fetch-depth: 0
3131

3232
- name: Set up python 3
33-
uses: actions/setup-python@v3
33+
uses: actions/setup-python@v4
3434
with:
3535
python-version: '3.x'
3636
architecture: 'x64'
@@ -39,7 +39,7 @@ jobs:
3939
- run: pip install -r python/requirements.txt
4040

4141
- name: Set up JDK ${{ matrix.jdk }}
42-
uses: actions/setup-java@v2
42+
uses: actions/setup-java@v3
4343
with:
4444
java-version: ${{ matrix.jdk }}
4545
distribution: 'adopt'
@@ -55,19 +55,19 @@ jobs:
5555
name: BUILD ${{ github.sha }}
5656
steps:
5757
- name: Checkout
58-
uses: actions/checkout@v2
58+
uses: actions/checkout@v4
5959
with:
6060
# we don't know what commit the last tag was it's safer to get entire repo so previousStableVersion resolves
6161
fetch-depth: 0
6262

6363
- name: Set up python 3
64-
uses: actions/setup-python@v3
64+
uses: actions/setup-python@v4
6565
with:
6666
python-version: '3.x'
6767
architecture: 'x64'
6868

6969
- name: Set up JDK 17
70-
uses: actions/setup-java@v2
70+
uses: actions/setup-java@v3
7171
with:
7272
java-version: 17
7373
distribution: 'adopt'
@@ -77,7 +77,7 @@ jobs:
7777
run: ant artifact
7878

7979
- name: ziping artifact
80-
uses: actions/upload-artifact@v2
80+
uses: actions/upload-artifact@v4
8181
with:
8282
name: play-${{ github.sha }}
8383
if-no-files-found: error

framework/pym/play/cmdloader.py

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import print_function
2-
import imp
2+
import importlib.util
3+
import importlib.machinery
34
import os
45
import warnings
56
import traceback
@@ -22,8 +23,9 @@ def load_core(self):
2223
for filename in os.listdir(self.path):
2324
if filename != "__init__.py" and filename.endswith(".py"):
2425
try:
25-
name = filename.replace(".py", "")
26-
mod = load_python_module(name, self.path)
26+
module_name = filename.replace(".py", "")
27+
module_path = os.path.join(self.path, filename)
28+
mod = load_python_module(module_name, module_path)
2729
self._load_cmd_from(mod)
2830
except Exception as e:
2931
print (e)
@@ -35,7 +37,8 @@ def load_play_module(self, modname):
3537
if os.path.exists(commands):
3638
try:
3739
leafname = os.path.basename(modname).split('.')[0]
38-
mod = imp.load_source(leafname, os.path.join(modname, "commands.py"))
40+
# print(f"Loading commands for module \"{modname}\"")
41+
mod = load_source(leafname, os.path.join(modname, "commands.py"))
3942
self._load_cmd_from(mod)
4043
except Exception as e:
4144
print('~')
@@ -55,12 +58,26 @@ def _load_cmd_from(self, mod):
5558
if 'MODULE' in dir(mod):
5659
self.modules[mod.MODULE] = mod
5760

61+
5862
def load_python_module(name, location):
59-
mod_desc = imp.find_module(name, [location])
60-
mod_file = mod_desc[0]
61-
try:
62-
return imp.load_module(name, mod_desc[0], mod_desc[1], mod_desc[2])
63-
finally:
64-
if mod_file != None and not mod_file.closed:
65-
mod_file.close()
63+
# print(f"Loading module \"{name}\" at location \"{location}\"")
64+
spec = importlib.util.spec_from_file_location(name, location)
65+
if spec is None:
66+
raise ImportError(f"Could not find module {name} at {location}")
67+
68+
mod = importlib.util.module_from_spec(spec)
69+
spec.loader.exec_module(mod)
70+
71+
return mod
72+
6673

74+
# Obtained from https://docs.python.org/dev/whatsnew/3.12.html#imp
75+
def load_source(modname, filename):
76+
loader = importlib.machinery.SourceFileLoader(modname, filename)
77+
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
78+
module = importlib.util.module_from_spec(spec)
79+
# The module is always executed and not cached in sys.modules.
80+
# Uncomment the following line to cache the module.
81+
# sys.modules[module.__name__] = module
82+
loader.exec_module(module)
83+
return module

framework/pym/play/commands/modulesrepo.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import urllib.request, urllib.error, urllib.parse
1313
import shutil
1414
import string
15-
import imp
15+
import importlib.util
1616
import time
1717
import urllib.request, urllib.parse, urllib.error
1818
import yaml
@@ -40,8 +40,16 @@
4040

4141
def load_module(name):
4242
base = os.path.normpath(os.path.dirname(os.path.realpath(sys.argv[0])))
43-
mod_desc = imp.find_module(name, [os.path.join(base, 'framework/pym')])
44-
return imp.load_module(name, mod_desc[0], mod_desc[1], mod_desc[2])
43+
module_path = os.path.join(base, 'framework/pym', name, '__init__.py')
44+
45+
spec = importlib.util.spec_from_file_location(name, module_path)
46+
if spec is None:
47+
raise ImportError(f"Could not find module \"{name}\" at \"{module_path}\"")
48+
49+
mod = importlib.util.module_from_spec(spec)
50+
spec.loader.exec_module(mod)
51+
52+
return mod
4553

4654
json = load_module('simplejson')
4755

samples-and-tests/i-am-a-developer/test_jvm_version_flag.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def testWithFlag(self, mock):
3333
play_app.java_cmd([])
3434

3535
step('Assert getJavaVersion was not called')
36-
self.assert_(not mock.called)
36+
mock.assert_(not mock.called)
3737

3838
@mock.patch('play.application.getJavaVersion', return_value='')
3939
def testWithoutFlag(self, mock):
@@ -43,7 +43,7 @@ def testWithoutFlag(self, mock):
4343
play_app.java_cmd([])
4444

4545
step('Assert getJavaVersion was called once')
46-
self.assert_(mock.called)
46+
mock.assert_(mock.called)
4747

4848

4949
if __name__ == '__main__':

0 commit comments

Comments
 (0)