Skip to content

Commit

Permalink
Trac #33636: replace loadable_module_extension() by importlib.machine…
Browse files Browse the repository at this point in the history
…ry.EXTENSION_SUFFIXES

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

For these I have this tentative change:
{{{#!diff
--- 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).

URL: https://trac.sagemath.org/33636
Reported by: tornaria
Ticket author(s): Gonzalo Tornaría
Reviewer(s): Matthias Koeppe
  • Loading branch information
Release Manager committed Jul 25, 2022
2 parents c744d7c + 780be6a commit 977e691
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/sage/misc/cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -409,9 +416,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

Expand Down
7 changes: 7 additions & 0 deletions src/sage/misc/sageinspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,20 @@ def loadable_module_extension():
It is '.dll' on cygwin, '.so' otherwise.
This function is deprecated.
EXAMPLES::
sage: from sage.misc.sageinspect import loadable_module_extension
sage: from importlib.machinery import EXTENSION_SUFFIXES
sage: loadable_module_extension() in EXTENSION_SUFFIXES
doctest:warning...
DeprecationWarning: loadable_module_extension is deprecated; use importlib.machinery.EXTENSION_SUFFIXES instead
See https://trac.sagemath.org/33636 for details.
True
"""
from sage.misc.superseded import deprecation
deprecation(33636, "loadable_module_extension is deprecated; use importlib.machinery.EXTENSION_SUFFIXES instead")
# Return the full platform-specific extension module suffix
return import_machinery.EXTENSION_SUFFIXES[0]

Expand Down

0 comments on commit 977e691

Please sign in to comment.