Skip to content

replace loadable_module_extension() by importlib.machinery.EXTENSION_SUFFIXES #33636

@tornaria

Description

@tornaria

After #33626, loadable_module_extension() is only used twice in sage.misc.cython.cython().

For these I have this tentative change:

--- a/src/sage/misc/cython.py
+++ b/src/sage/misc/cython.py
@@ -240,13 +240,20 @@ def cython(filename, verbose=0, compile_message=False,
         # There is already a module here. Maybe we do not have to rebuild?
         # Find the name.
         if use_cache:
-            from sage.misc.sageinspect import loadable_module_extension
-            prev_so = [F for F in os.listdir(target_dir) if F.endswith(loadable_module_extension())]
-            if len(prev_so) > 0:
-                prev_so = prev_so[0]     # should have length 1 because of deletes below
-                if os.path.getmtime(filename) <= os.path.getmtime('%s/%s' % (target_dir, prev_so)):
+            from importlib.machinery import EXTENSION_SUFFIXES
+            for f in os.listdir(target_dir):
+                for suffix in EXTENSION_SUFFIXES:
+                    if f.endswith(suffix):
+                        # use the first matching extension
+                        prev_file = os.path.join(target_dir, f)
+                        prev_name = f[:-len(suffix)]
+                        break
+                else:
+                    # no match, try next file
+                    continue
+                if os.path.getmtime(filename) <= os.path.getmtime(prev_file):
                     # We do not have to rebuild.
-                    return prev_so[:-len(loadable_module_extension())], target_dir
+                    return prev_name, target_dir
 
         # Delete all ordinary files in target_dir
         for F in os.listdir(target_dir):
@@ -410,9 +417,11 @@ def cython(filename, verbose=0, compile_message=False,
 
     if create_local_so_file:
         # Copy module to current directory
-        from sage.misc.sageinspect import loadable_module_extension
-        shutil.copy(os.path.join(target_dir, name + loadable_module_extension()),
-                    os.curdir)
+        from importlib.machinery import EXTENSION_SUFFIXES
+        for ext in EXTENSION_SUFFIXES:
+            path = os.path.join(target_dir, name + ext)
+            if os.path.exists(path):
+                shutil.copy(path, os.curdir)
 
     return name, target_dir
 

Notes:

  • The option use_cache=True is only used in sage.repl.load.load_cython() and I'm not sure there's any doctest that will run this code.
  • The option create_local_so_file=True is not used anywhere, and it's not doctested either.
  • If both options are given together and the compiled *.so file is found in cache, it will not save a copy in the current directory (seems to be a bug).

Depends on #33626

Component: refactoring

Author: Gonzalo Tornaría

Branch/Commit: 780be6a

Reviewer: Matthias Koeppe

Issue created by migration from https://trac.sagemath.org/ticket/33636

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions