changeset 8082:a4127d7afaa9

fix: remove references to imp in untouched code CI runs flake8 to check for syntax errors and it threw errors on old imp.XYZ code that was not actually called. It looks like the code was only called when resolving modules like ssl, _ssl, importlib, importlib.resources .. rather than files/directory paths. In Roundup's use case it only runs over files/directories.
author John Rouillard <rouilj@ieee.org>
date Sat, 13 Jul 2024 21:15:50 -0400
parents 95f91b6f0386
children 60e92a540ca7
files roundup/pygettext.py
diffstat 1 files changed, 7 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/roundup/pygettext.py	Sat Jul 13 18:52:55 2024 -0400
+++ b/roundup/pygettext.py	Sat Jul 13 21:15:50 2024 -0400
@@ -269,35 +269,13 @@
     a package. Return None if the name is not found, or is a builtin or
     extension module.
     """
-    # split off top-most name
-    parts = dotted_name.split('.', 1)
-
-    if len(parts) > 1:
-        # we have a dotted path, import top-level package
-        try:
-            file, pathname, description = importlib.find_module(parts[0], pathlist)
-            if file: file.close()
-        except ImportError:
-            return None
+    pathname = None
+    r =  importlib.util.find_spec(dotted_name, pathlist)
 
-        # check if it's indeed a package
-        if description[2] == imp.PKG_DIRECTORY:
-            # recursively handle the remaining name parts
-            pathname = _get_modpkg_path(parts[1], [pathname])
-        else:
-            pathname = None
-    else:
-        # plain name
-        try:
-            file, pathname, description = imp.find_module(
-                dotted_name, pathlist)
-            if file:
-                file.close()
-            if description[2] not in [imp.PY_SOURCE, imp.PKG_DIRECTORY]:
-                pathname = None
-        except ImportError:
-            pathname = None
-
+    if r.loader.is_package(dotted_name):
+        pathname = r.submodule_search_locations[0]
+    elif issubclass(r.loader.__class__,(importlib.abc.SourceLoader)):
+        pathname = r.origin
     return pathname
 
 
@@ -325,8 +303,7 @@
         # get extension for python source files
         if '_py_ext' not in globals():
             global _py_ext
-            _py_ext = [triple[0] for triple in imp.get_suffixes()
-                       if triple[2] == imp.PY_SOURCE][0]
+            _py_ext = importlib.machinery.SOURCE_SUFFIXES
         for root, dirs, files in os.walk(name):
             # don't recurse into CVS directories
             if 'CVS' in dirs:

Roundup Issue Tracker: http://roundup-tracker.org/