Skip to content

Commit b0882cd

Browse files
authored
Merge pull request #105 from dihm/imp_hotfix
Imp hotfix
2 parents 851f354 + 2ab6077 commit b0882cd

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

labscript_utils/device_registry/_device_registry.py

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib.machinery
12
import os
23
import importlib
34
import warnings
@@ -6,11 +7,6 @@
67
from labscript_utils import dedent
78
from labscript_utils.labconfig import LabConfig
89

9-
# deal with removal of imp from python 3.12
10-
try:
11-
import _imp as imp
12-
except ImportError:
13-
import imp
1410

1511
"""This file contains the machinery for registering and looking up what BLACS tab and
1612
runviewer parser classes belong to a particular labscript device. "labscript device"
@@ -253,20 +249,19 @@ def register_classes(labscript_device_name, BLACS_tab=None, runviewer_parser=Non
253249

254250
def populate_registry():
255251
"""Walk the labscript_devices folder looking for files called register_classes.py,
256-
and run them (i.e. import them). These files are expected to make calls to
252+
and run them. These files are expected to make calls to
257253
register_classes() to inform us of what BLACS tabs and runviewer classes correspond
258254
to their labscript device classes."""
259-
# We import the register_classes modules as a direct submodule of labscript_devices.
260-
# But they cannot all have the same name, so we import them as
261-
# labscript_devices._register_classes_script_<num> with increasing number.
262-
module_num = 0
255+
# We execute the register_classes modules as a direct submodule of labscript_devices.
263256
for devices_dir in LABSCRIPT_DEVICES_DIRS:
264257
for folder, _, filenames in os.walk(devices_dir):
265258
if 'register_classes.py' in filenames:
266259
# The module name is the path to the file, relative to the labscript suite
267260
# install directory:
268-
# Open the file using the import machinery, and import it as module_name.
269-
fp, pathname, desc = imp.find_module('register_classes', [folder])
270-
module_name = 'labscript_devices._register_classes_%d' % module_num
271-
_ = imp.load_module(module_name, fp, pathname, desc)
272-
module_num += 1
261+
# Open the file using the import machinery, and run it
262+
spec = importlib.machinery.PathFinder.find_spec('register_classes', [folder])
263+
mod = importlib.util.module_from_spec(spec)
264+
spec.loader.exec_module(mod)
265+
# fully importing module would require adding to sys.modules
266+
# and each import would need to have unique names
267+
# but we just need to run the registering code, not actually import the module

labscript_utils/modulewatcher.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
# deal with removal of imp from python 3.12
2121
try:
22-
import _imp as imp
23-
except ImportError:
2422
import imp
23+
except ImportError:
24+
import _imp as imp
2525

2626

2727
# Directories in which the standard library and installed packages may be located.

0 commit comments

Comments
 (0)