|
| 1 | +import importlib.machinery |
1 | 2 | import os
|
2 | 3 | import importlib
|
3 | 4 | import warnings
|
|
6 | 7 | from labscript_utils import dedent
|
7 | 8 | from labscript_utils.labconfig import LabConfig
|
8 | 9 |
|
9 |
| -# deal with removal of imp from python 3.12 |
10 |
| -try: |
11 |
| - import _imp as imp |
12 |
| -except ImportError: |
13 |
| - import imp |
14 | 10 |
|
15 | 11 | """This file contains the machinery for registering and looking up what BLACS tab and
|
16 | 12 | 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
|
253 | 249 |
|
254 | 250 | def populate_registry():
|
255 | 251 | """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 |
257 | 253 | register_classes() to inform us of what BLACS tabs and runviewer classes correspond
|
258 | 254 | 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. |
263 | 256 | for devices_dir in LABSCRIPT_DEVICES_DIRS:
|
264 | 257 | for folder, _, filenames in os.walk(devices_dir):
|
265 | 258 | if 'register_classes.py' in filenames:
|
266 | 259 | # The module name is the path to the file, relative to the labscript suite
|
267 | 260 | # 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 |
0 commit comments