Skip to content

Commit

Permalink
Trac #34547: Interfaces: use more lazy imports, restore top-level fun…
Browse files Browse the repository at this point in the history
…ctions maxima_console etc.

Various files in `sage/interfaces/` have this pattern:
{{{
class Octave(...):
    ....

octave = Octave()
}}}
When combined with `from .octave import Octave, octave` in
`sage/interfaces/all.py`, this means that an instance of `Octave` is
created when Sage starts up. I think we should avoid this with lazy
imports.

URL: https://trac.sagemath.org/34547
Reported by: jhpalmieri
Ticket author(s): John Palmieri
Reviewer(s): Matthias Koeppe
  • Loading branch information
Release Manager committed Jan 2, 2023
2 parents 5905da7 + 09f5d92 commit b0cc282
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 83 deletions.
12 changes: 12 additions & 0 deletions src/sage/all_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@
from sage.all import *
from sage.calculus.predefined import x

from sage.misc.lazy_import import lazy_import

for pkg in ['axiom', 'fricas', 'gap' , 'gap3', 'giac', 'gp',
'gnuplot', 'kash', 'magma', 'macaulay2', 'maple',
'mathematica', 'mathics', 'matlab',
'mupad', 'mwrank', 'octave', 'qepcad', 'singular',
'sage0', 'lie', 'r']:
lazy_import(f'sage.interfaces.{pkg}', f'{pkg}_console')
del pkg

lazy_import('sage.interfaces.maxima_abstract', 'maxima_console')

sage.misc.session.init()
107 changes: 36 additions & 71 deletions src/sage/interfaces/all.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,45 @@
# interfaces to other interpreters

from sage.misc.lazy_import import lazy_import

from .frobby import frobby
from .four_ti_2 import four_ti_2
from .axiom import Axiom, axiom
from .fricas import FriCAS, fricas

from .sage0 import sage0, sage0_version, Sage
from .gap import gap, gap_reset_workspace, Gap
from .gap3 import gap3, gap3_version, Gap3
lazy_import('sage.interfaces.genus2reduction', ['genus2reduction', 'Genus2reduction'])
from .gfan import gfan, Gfan
from .giac import giac, Giac
from .gp import gp, gp_version, Gp
from .gnuplot import gnuplot
from .kash import kash, kash_version, Kash
from .lisp import lisp, Lisp
from .magma import magma, Magma
from .magma_free import magma_free
from .macaulay2 import macaulay2, Macaulay2
from .maple import maple, Maple
from .maxima import maxima, Maxima
# import problems
#from maxima_lib import maxima_lib
from .mathematica import mathematica, Mathematica
from .mathics import mathics, Mathics
from .matlab import matlab, matlab_version, Matlab
from .mupad import mupad, Mupad # NOT functional yet
from .mwrank import mwrank, Mwrank
from .octave import octave, Octave
from .polymake import polymake
from .qepcad import qepcad, qepcad_version, qepcad_formula
from .qsieve import qsieve
from .maxima import maxima, Maxima
from .singular import singular, singular_version, Singular
from .sage0 import sage0 as sage0, sage0_version, Sage
from .scilab import scilab
from .tachyon import tachyon_rt
from .psage import PSage
from .ecm import ECM, ecm
from .povray import povray
from .lie import lie, LiE
from .r import r, R, r_version
from .read_data import read_data

interfaces = ['gap', 'gap3', 'giac', 'gp', 'mathematica', 'gnuplot', \
'kash', 'magma', 'macaulay2', 'maple', 'maxima', \
'mathematica', 'mwrank', 'octave', 'r', \
'singular', 'sage0', 'sage']
from .magma import magma, Magma
from .polymake import polymake

from sage.misc.lazy_import import lazy_import

try:
from sage.repl.rich_output.display_manager import get_display_manager as _get_display_manager
except ImportError:
pass
else:
if _get_display_manager().is_in_terminal():
from .axiom import axiom_console
from .fricas import fricas_console
from .gap import gap_console
from .gap3 import gap3_console
from .giac import giac_console
from .gp import gp_console
from .gnuplot import gnuplot_console
from .kash import kash_console
from .lisp import lisp_console
from .magma import magma_console
from .macaulay2 import macaulay2_console
from .maple import maple_console
from .maxima_abstract import maxima_console
from .mathematica import mathematica_console
from .mathics import mathics_console
from .matlab import matlab_console
from .mupad import mupad_console
from .mwrank import mwrank_console
from .octave import octave_console
from .qepcad import qepcad_console
from .singular import singular_console
from .sage0 import sage0_console
from .lie import lie_console
from .r import r_console
lazy_import('sage.interfaces.axiom', ['Axiom', 'axiom'])
lazy_import('sage.interfaces.ecm', ['ECM', 'ecm'])
lazy_import('sage.interfaces.four_ti_2', 'four_ti_2')
lazy_import('sage.interfaces.fricas', ['FriCAS', 'fricas'])
lazy_import('sage.interfaces.frobby', 'frobby')
lazy_import('sage.interfaces.gap3', ['gap3', 'gap3_version', 'Gap3'])
lazy_import('sage.interfaces.genus2reduction', ['genus2reduction', 'Genus2reduction'])
lazy_import('sage.interfaces.gfan', ['gfan', 'Gfan'])
lazy_import('sage.interfaces.giac', ['giac', 'Giac'])
lazy_import('sage.interfaces.gnuplot', 'gnuplot')
lazy_import('sage.interfaces.kash', ['kash', 'kash_version', 'Kash'])
lazy_import('sage.interfaces.lie', ['lie', 'LiE'])
lazy_import('sage.interfaces.lisp', ['lisp', 'Lisp'])
lazy_import('sage.interfaces.macaulay2', ['macaulay2', 'Macaulay2'])
lazy_import('sage.interfaces.magma_free', 'magma_free')
lazy_import('sage.interfaces.maple', ['maple', 'Maple'])
lazy_import('sage.interfaces.mathematica', ['mathematica', 'Mathematica'])
lazy_import('sage.interfaces.mathics', ['mathics', 'Mathics'])
lazy_import('sage.interfaces.matlab', ['matlab', 'matlab_version', 'Matlab'])
lazy_import('sage.interfaces.mupad', ['mupad', 'Mupad']) # NOT functional yet
lazy_import('sage.interfaces.mwrank', ['mwrank', 'Mwrank'])
lazy_import('sage.interfaces.octave', ['octave', 'Octave'])
lazy_import('sage.interfaces.povray', 'povray')
lazy_import('sage.interfaces.psage', 'PSage')
lazy_import('sage.interfaces.qepcad', ['qepcad', 'qepcad_version', 'qepcad_formula'])
lazy_import('sage.interfaces.qsieve', 'qsieve')
lazy_import('sage.interfaces.r', ['r', 'R', 'r_version'])
lazy_import('sage.interfaces.read_data', 'read_data')
lazy_import('sage.interfaces.scilab', 'scilab')
lazy_import('sage.interfaces.tachyon', 'tachyon_rt')
2 changes: 1 addition & 1 deletion src/sage/interfaces/axiom.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def __reduce__(self):
"""
EXAMPLES::
sage: axiom.__reduce__()
sage: Axiom().__reduce__()
(<function reduce_load_Axiom at 0x...>, ())
sage: f, args = _
sage: f(*args)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/giac.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def __reduce__(self):
"""
EXAMPLES::
sage: giac.__reduce__()
sage: Giac().__reduce__()
(<function reduce_load_Giac at 0x...>, ())
sage: f, args = _
sage: f(*args)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/kash.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ def _function_call_string(self, function, args, kwds):
EXAMPLES::
sage: kash._function_call_string('Expand', ['x', 'y'], ['Prec:=10'])
sage: Kash()._function_call_string('Expand', ['x', 'y'], ['Prec:=10'])
'Expand(x,y,rec(Prec:=10))'
"""
if not kwds:
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/lie.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def __reduce__(self):
"""
EXAMPLES::
sage: lie.__reduce__()
sage: LiE().__reduce__()
(<function reduce_load_lie at 0x...>, ())
"""
return reduce_load_lie, tuple([])
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/lisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def __reduce__(self):
"""
EXAMPLES::
sage: lisp.__reduce__()
sage: Lisp().__reduce__()
(<function reduce_load_Lisp at 0x...>, ())
"""
return reduce_load_Lisp, tuple([])
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/macaulay2.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def __reduce__(self):
EXAMPLES::
sage: rlm2, t = macaulay2.__reduce__()
sage: rlm2, t = Macaulay2().__reduce__()
sage: rlm2(*t)
Macaulay2
"""
Expand Down
1 change: 1 addition & 0 deletions src/sage/interfaces/magma.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ def _preparse(self, s):
EXAMPLES::
sage: magma = Magma()
sage: magma._preparse_colon_equals = False
sage: magma._preparse('a = 5')
'a = 5'
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/maple.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def __reduce__(self):
"""
EXAMPLES::
sage: maple.__reduce__()
sage: Maple().__reduce__()
(<function reduce_load_Maple at 0x...>, ())
sage: f, args = _
sage: f(*args)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/mupad.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __reduce__(self):
"""
EXAMPLES::
sage: mupad.__reduce__()
sage: Mupad().__reduce__()
(<function reduce_load_mupad at 0x...>, ())
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/mwrank.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def __reduce__(self):
"""
EXAMPLES::
sage: mwrank.__reduce__()
sage: Mwrank().__reduce__()
(<function _reduce_load_mwrank at 0x...>, ())
"""

Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/octave.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def __reduce__(self):
"""
EXAMPLES::
sage: octave.__reduce__()
sage: Octave().__reduce__()
(<function reduce_load_Octave at 0x...>, ())
"""
return reduce_load_Octave, tuple([])
Expand Down
2 changes: 0 additions & 2 deletions src/sage/rings/finite_rings/integer_mod.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ from sage.libs.pari.all import pari, PariError
import sage.rings.integer_ring as integer_ring
import sage.rings.rational_field

import sage.interfaces.all

import sage.rings.integer
cimport sage.rings.integer
from sage.rings.integer cimport Integer
Expand Down

0 comments on commit b0cc282

Please sign in to comment.