From 37aa2ca2986a9f530d947bdf8f93cf37a39e0a4e Mon Sep 17 00:00:00 2001 From: David Meyer Date: Thu, 4 Apr 2024 14:09:32 -0400 Subject: [PATCH 1/2] Swap over of `imp` import check to default to older behavior if available. --- labscript_utils/device_registry/_device_registry.py | 4 ++-- labscript_utils/modulewatcher.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/labscript_utils/device_registry/_device_registry.py b/labscript_utils/device_registry/_device_registry.py index 0c64cb2..7c0a6dc 100644 --- a/labscript_utils/device_registry/_device_registry.py +++ b/labscript_utils/device_registry/_device_registry.py @@ -8,9 +8,9 @@ # deal with removal of imp from python 3.12 try: - import _imp as imp -except ImportError: import imp +except ImportError: + import _imp as imp """This file contains the machinery for registering and looking up what BLACS tab and runviewer parser classes belong to a particular labscript device. "labscript device" diff --git a/labscript_utils/modulewatcher.py b/labscript_utils/modulewatcher.py index d9029dd..c87cfc0 100644 --- a/labscript_utils/modulewatcher.py +++ b/labscript_utils/modulewatcher.py @@ -19,9 +19,9 @@ # deal with removal of imp from python 3.12 try: - import _imp as imp -except ImportError: import imp +except ImportError: + import _imp as imp # Directories in which the standard library and installed packages may be located. From 2ab607796df39b83fe622c4dccff6d52d008dfae Mon Sep 17 00:00:00 2001 From: David Meyer Date: Thu, 4 Apr 2024 14:17:04 -0400 Subject: [PATCH 2/2] Remove old method of importing `register_classes.py` to remove `imp` dependency. --- .../device_registry/_device_registry.py | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/labscript_utils/device_registry/_device_registry.py b/labscript_utils/device_registry/_device_registry.py index 7c0a6dc..e5a122a 100644 --- a/labscript_utils/device_registry/_device_registry.py +++ b/labscript_utils/device_registry/_device_registry.py @@ -1,3 +1,4 @@ +import importlib.machinery import os import importlib import warnings @@ -6,11 +7,6 @@ from labscript_utils import dedent from labscript_utils.labconfig import LabConfig -# deal with removal of imp from python 3.12 -try: - import imp -except ImportError: - import _imp as imp """This file contains the machinery for registering and looking up what BLACS tab and 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 def populate_registry(): """Walk the labscript_devices folder looking for files called register_classes.py, - and run them (i.e. import them). These files are expected to make calls to + and run them. These files are expected to make calls to register_classes() to inform us of what BLACS tabs and runviewer classes correspond to their labscript device classes.""" - # We import the register_classes modules as a direct submodule of labscript_devices. - # But they cannot all have the same name, so we import them as - # labscript_devices._register_classes_script_ with increasing number. - module_num = 0 + # We execute the register_classes modules as a direct submodule of labscript_devices. for devices_dir in LABSCRIPT_DEVICES_DIRS: for folder, _, filenames in os.walk(devices_dir): if 'register_classes.py' in filenames: # The module name is the path to the file, relative to the labscript suite # install directory: - # Open the file using the import machinery, and import it as module_name. - fp, pathname, desc = imp.find_module('register_classes', [folder]) - module_name = 'labscript_devices._register_classes_%d' % module_num - _ = imp.load_module(module_name, fp, pathname, desc) - module_num += 1 + # Open the file using the import machinery, and run it + spec = importlib.machinery.PathFinder.find_spec('register_classes', [folder]) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + # fully importing module would require adding to sys.modules + # and each import would need to have unique names + # but we just need to run the registering code, not actually import the module pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy