From 7dae2c0527dbc5919b6c73637cce8e4b11c21260 Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Mon, 7 Dec 2020 22:07:04 +0100 Subject: [PATCH 01/17] lazy import polyhedra instances --- src/sage/geometry/polyhedron/parent.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/sage/geometry/polyhedron/parent.py b/src/sage/geometry/polyhedron/parent.py index 72160195d1c..a114ec993a1 100644 --- a/src/sage/geometry/polyhedron/parent.py +++ b/src/sage/geometry/polyhedron/parent.py @@ -22,6 +22,18 @@ from sage.geometry.polyhedron.base import is_Polyhedron from .representation import Inequality, Equation, Vertex, Ray, Line +from sage.misc.lazy_import import lazy_import + +lazy_import('sage.geometry.polyhedron.backend_cdd', ['Polyhedron_QQ_cdd', + 'Polyhedron_RDF_cdd']) +lazy_import('sage.geometry.polyhedron.backend_ppl', ['Polyhedron_ZZ_ppl', + 'Polyhedron_QQ_ppl']) +lazy_import('sage.geometry.polyhedron.backend_normaliz', ['Polyhedron_normaliz', + 'Polyhedron_ZZ_normaliz', + 'Polyhedron_QQ_normaliz']) +lazy_import('sage.geometry.polyhedron.backend_polymake', 'Polyhedron_polymake') +lazy_import('sage.geometry.polyhedron.backend_field', 'Polyhedron_field') + def Polyhedra(ambient_space_or_base_ring=None, ambient_dim=None, backend=None, *, ambient_space=None, base_ring=None): @@ -1066,11 +1078,6 @@ def _make_Line(self, polyhedron, data): -from sage.geometry.polyhedron.backend_cdd import Polyhedron_QQ_cdd, Polyhedron_RDF_cdd -from sage.geometry.polyhedron.backend_ppl import Polyhedron_ZZ_ppl, Polyhedron_QQ_ppl -from sage.geometry.polyhedron.backend_normaliz import Polyhedron_normaliz, Polyhedron_ZZ_normaliz, Polyhedron_QQ_normaliz -from sage.geometry.polyhedron.backend_polymake import Polyhedron_polymake -from sage.geometry.polyhedron.backend_field import Polyhedron_field class Polyhedra_ZZ_ppl(Polyhedra_base): Element = Polyhedron_ZZ_ppl From 736f7ae397965b2442aa7bf63e76cd260582170b Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Mon, 7 Dec 2020 22:16:18 +0100 Subject: [PATCH 02/17] lazy import ppl for cone --- src/sage/geometry/cone.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/sage/geometry/cone.py b/src/sage/geometry/cone.py index 33b28dbedec..e8d0dc49e23 100644 --- a/src/sage/geometry/cone.py +++ b/src/sage/geometry/cone.py @@ -212,9 +212,10 @@ from sage.rings.all import QQ, ZZ from sage.structure.all import SageObject, parent from sage.structure.richcmp import richcmp_method, richcmp -from ppl import (C_Polyhedron, Generator_System, Constraint_System, - Linear_Expression, ray as PPL_ray, point as PPL_point, - Poly_Con_Relation) +from sage.misc.lazy_import import lazy_import +lazy_import('ppl', ['C_Polyhedron', 'Generator_System', 'Constraint_System', + 'Linear_Expression', 'Poly_Con_Relation']) +lazy_import('ppl', ['ray', 'point'], as_=['PPL_ray', 'PPL_point']) from sage.geometry.integral_points import parallelotope_points From c8cb27a7b85d28b8f73560a5c257b2e2265629ac Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Mon, 7 Dec 2020 22:25:11 +0100 Subject: [PATCH 03/17] remove import of ppl through lattice polytope --- src/sage/geometry/lattice_polytope.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/sage/geometry/lattice_polytope.py b/src/sage/geometry/lattice_polytope.py index e26f2869970..1c27c8e413c 100644 --- a/src/sage/geometry/lattice_polytope.py +++ b/src/sage/geometry/lattice_polytope.py @@ -114,8 +114,9 @@ from sage.geometry.toric_lattice import ToricLattice, is_ToricLattice from sage.graphs.graph import DiGraph, Graph from sage.groups.perm_gps.permgroup_named import SymmetricGroup -from ppl import (C_Polyhedron, Generator_System, Linear_Expression, - point as PPL_point) +from sage.misc.lazy_import import lazy_import +lazy_import('ppl', ['C_Polyhedron', 'Generator_System', 'Linear_Expression']) +lazy_import('ppl', 'point', as_='PPL_point') from sage.matrix.constructor import matrix from sage.structure.element import is_Matrix from sage.misc.all import cached_method, flatten, tmp_filename From d8934c06ca64f8402153c7e038f987883455db4e Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Thu, 10 Dec 2020 10:07:22 +0100 Subject: [PATCH 04/17] document feature of lazy import --- src/sage/misc/lazy_import.pyx | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/sage/misc/lazy_import.pyx b/src/sage/misc/lazy_import.pyx index 9de1859fd3f..b685cb1728c 100644 --- a/src/sage/misc/lazy_import.pyx +++ b/src/sage/misc/lazy_import.pyx @@ -990,6 +990,9 @@ def lazy_import(module, names, as_=None, *, ``deprecation`` should be either a trac number (integer) or a pair ``(trac_number, message)`` + - ``feature`` -- a python module (optional), if it cannot be imported + an appropriate error is raised + .. SEEALSO:: :mod:`sage.misc.lazy_import`, :class:`LazyImport` EXAMPLES:: @@ -1052,6 +1055,24 @@ def lazy_import(module, names, as_=None, *, doctest:...: DeprecationWarning: This is an example. See http://trac.sagemath.org/14275 for details. 5-adic Field with capped relative precision 20 + + An example of and import relying on a feature:: + + sage: from sage.features import PythonModule + sage: lazy_import('ppl', 'equation', feature=PythonModule('ppl', spkg='pplpy')) + sage: equation + + sage: lazy_import('PyNormaliz', 'NmzListConeProperties', feature=PythonModule('PyNormaliz', spkg='pynormaliz')) # optional - pynormaliz + sage: NmzListConeProperties # optional - pynormaliz + + sage: lazy_import('foo', 'not_there', feature=PythonModule('foo', spkg='non-existing-package')) + sage: not_there + ) failed: sage.features.FeatureNotPresentError: foo is not available. + Importing not_there failed: No module named 'foo' + No equivalent system packages for ... are known to Sage. + To install foo using the Sage package manager, you can try to run: + !sage -i non-existing-package + No equivalent system packages for pip are known to Sage.> """ if as_ is None: as_ = names From 5e53333c7d9e74902bce58c1f548c8670c473ff3 Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Thu, 10 Dec 2020 11:09:06 +0100 Subject: [PATCH 05/17] Revert "lazy import polyhedra instances" This reverts commit 7dae2c0527dbc5919b6c73637cce8e4b11c21260. --- src/sage/geometry/polyhedron/parent.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/sage/geometry/polyhedron/parent.py b/src/sage/geometry/polyhedron/parent.py index a114ec993a1..72160195d1c 100644 --- a/src/sage/geometry/polyhedron/parent.py +++ b/src/sage/geometry/polyhedron/parent.py @@ -22,18 +22,6 @@ from sage.geometry.polyhedron.base import is_Polyhedron from .representation import Inequality, Equation, Vertex, Ray, Line -from sage.misc.lazy_import import lazy_import - -lazy_import('sage.geometry.polyhedron.backend_cdd', ['Polyhedron_QQ_cdd', - 'Polyhedron_RDF_cdd']) -lazy_import('sage.geometry.polyhedron.backend_ppl', ['Polyhedron_ZZ_ppl', - 'Polyhedron_QQ_ppl']) -lazy_import('sage.geometry.polyhedron.backend_normaliz', ['Polyhedron_normaliz', - 'Polyhedron_ZZ_normaliz', - 'Polyhedron_QQ_normaliz']) -lazy_import('sage.geometry.polyhedron.backend_polymake', 'Polyhedron_polymake') -lazy_import('sage.geometry.polyhedron.backend_field', 'Polyhedron_field') - def Polyhedra(ambient_space_or_base_ring=None, ambient_dim=None, backend=None, *, ambient_space=None, base_ring=None): @@ -1078,6 +1066,11 @@ def _make_Line(self, polyhedron, data): +from sage.geometry.polyhedron.backend_cdd import Polyhedron_QQ_cdd, Polyhedron_RDF_cdd +from sage.geometry.polyhedron.backend_ppl import Polyhedron_ZZ_ppl, Polyhedron_QQ_ppl +from sage.geometry.polyhedron.backend_normaliz import Polyhedron_normaliz, Polyhedron_ZZ_normaliz, Polyhedron_QQ_normaliz +from sage.geometry.polyhedron.backend_polymake import Polyhedron_polymake +from sage.geometry.polyhedron.backend_field import Polyhedron_field class Polyhedra_ZZ_ppl(Polyhedra_base): Element = Polyhedron_ZZ_ppl From aca03b0b439f33017c623a882a10d8e57215bf6f Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Thu, 10 Dec 2020 11:16:10 +0100 Subject: [PATCH 06/17] using feature of lazy import for backend normaliz --- .../geometry/polyhedron/backend_normaliz.py | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/sage/geometry/polyhedron/backend_normaliz.py b/src/sage/geometry/polyhedron/backend_normaliz.py index f47d07f493f..46699719a83 100644 --- a/src/sage/geometry/polyhedron/backend_normaliz.py +++ b/src/sage/geometry/polyhedron/backend_normaliz.py @@ -26,6 +26,9 @@ from sage.structure.element import Element from sage.misc.all import cached_method, prod from sage.features import PythonModule +from sage.misc.lazy_import import lazy_import +lazy_import('PyNormaliz', ['NmzResult', 'NmzCompute', 'NmzCone', 'NmzConeCopy'], + feature=PythonModule("PyNormaliz", spkg="pynormaliz")) from sage.rings.all import ZZ, QQ from sage.arith.functions import LCM_list @@ -273,11 +276,9 @@ def nfelem_handler(coords): # coords might be too short which is not accepted by Sage number field v = list(coords) + [0] * (self._normaliz_field.degree() - len(coords)) return self._normaliz_field(v) - - import PyNormaliz - return PyNormaliz.NmzResult(normaliz_cone, property, - RationalHandler=rational_handler, - NumberfieldElementHandler=nfelem_handler) + return NmzResult(normaliz_cone, property, + RationalHandler=rational_handler, + NumberfieldElementHandler=nfelem_handler) def _init_from_normaliz_cone(self, normaliz_cone, normaliz_field): """ @@ -416,9 +417,7 @@ def _cone_from_normaliz_data(self, data, verbose=False): if verbose: print("# Calling {}".format(_format_function_call('PyNormaliz.NmzCone', **data))) - PythonModule("PyNormaliz", spkg="pynormaliz").require() - import PyNormaliz - cone = PyNormaliz.NmzCone(**data) + cone = NmzCone(**data) assert cone, "{} did not return a cone".format(_format_function_call('PyNormaliz.NmzCone', **data)) return cone @@ -1117,11 +1116,9 @@ def _make_normaliz_cone(data, verbose=False): sage: NmzResult(nmz_cone, "ExtremeRays") # py3 # optional - pynormaliz [[1, 2, 0], [2, 1, 0]] """ - PythonModule("PyNormaliz", spkg="pynormaliz").require() - import PyNormaliz if verbose: print("# Calling PyNormaliz.NmzCone(**{})".format(data)) - cone = PyNormaliz.NmzCone(**data) + cone = NmzCone(**data) assert cone, "NmzCone(**{}) did not return a cone".format(data) return cone @@ -1253,8 +1250,7 @@ def __copy__(self): # Make a copy of the cone. cone = self._normaliz_cone - import PyNormaliz - conecopy = PyNormaliz.NmzConeCopy(cone) + conecopy = NmzConeCopy(cone) other._normaliz_cone = conecopy return other @@ -1803,9 +1799,7 @@ def _ehrhart_quasipolynomial_normaliz(self, variable='t'): """ cone = self._normaliz_cone # Normaliz needs to compute the EhrhartSeries first - PythonModule("PyNormaliz", spkg="pynormaliz").require() - import PyNormaliz - assert PyNormaliz.NmzCompute(cone, ["EhrhartSeries"]) + assert NmzCompute(cone, ["EhrhartSeries"]) e = self._nmz_result(cone, "EhrhartQuasiPolynomial") from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing From 8bd41ea2580fc7bf3c5d65ccd6cdcec68d9ef5c4 Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Thu, 10 Dec 2020 11:19:17 +0100 Subject: [PATCH 07/17] lazy import ppl in backend_ppl --- src/sage/geometry/polyhedron/backend_ppl.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/sage/geometry/polyhedron/backend_ppl.py b/src/sage/geometry/polyhedron/backend_ppl.py index 735079f4424..d83f2075373 100644 --- a/src/sage/geometry/polyhedron/backend_ppl.py +++ b/src/sage/geometry/polyhedron/backend_ppl.py @@ -6,11 +6,10 @@ from sage.rings.integer import Integer from sage.arith.functions import LCM_list from sage.misc.functional import denominator -from ppl import ( - C_Polyhedron, Constraint_System, Generator_System, - Linear_Expression, - line, ray, point -) +from sage.features import PythonModule +lazy_import('ppl', ['C_Polyhedron', 'Generator_System', 'Constraint_System', + 'Linear_Expression', 'line', 'ray', 'point'], + feature=PythonModule("ppl", spkg="pplpy")) from .base import Polyhedron_base from .base_QQ import Polyhedron_QQ from .base_ZZ import Polyhedron_ZZ From a4560302be654d73a3b02154fdea86254e2a8f97 Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Thu, 10 Dec 2020 11:21:37 +0100 Subject: [PATCH 08/17] add feature to cone --- src/sage/geometry/cone.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/sage/geometry/cone.py b/src/sage/geometry/cone.py index e8d0dc49e23..79c75ad7841 100644 --- a/src/sage/geometry/cone.py +++ b/src/sage/geometry/cone.py @@ -212,11 +212,15 @@ from sage.rings.all import QQ, ZZ from sage.structure.all import SageObject, parent from sage.structure.richcmp import richcmp_method, richcmp +from sage.geometry.integral_points import parallelotope_points + from sage.misc.lazy_import import lazy_import +from sage.features import PythonModule lazy_import('ppl', ['C_Polyhedron', 'Generator_System', 'Constraint_System', - 'Linear_Expression', 'Poly_Con_Relation']) -lazy_import('ppl', ['ray', 'point'], as_=['PPL_ray', 'PPL_point']) -from sage.geometry.integral_points import parallelotope_points + 'Linear_Expression', 'Poly_Con_Relation'], + feature=PythonModule("ppl", spkg="pplpy")) +lazy_import('ppl', ['ray', 'point'], as_=['PPL_ray', 'PPL_point'], + feature=PythonModule("ppl", spkg="pplpy")) def is_Cone(x): From 19bc7e49034f9f809fdb7df6d7ae622aae749490 Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Thu, 10 Dec 2020 11:22:35 +0100 Subject: [PATCH 09/17] add feature option to lattice polytope --- src/sage/geometry/lattice_polytope.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/sage/geometry/lattice_polytope.py b/src/sage/geometry/lattice_polytope.py index 1c27c8e413c..cc5d4303897 100644 --- a/src/sage/geometry/lattice_polytope.py +++ b/src/sage/geometry/lattice_polytope.py @@ -114,9 +114,14 @@ from sage.geometry.toric_lattice import ToricLattice, is_ToricLattice from sage.graphs.graph import DiGraph, Graph from sage.groups.perm_gps.permgroup_named import SymmetricGroup + from sage.misc.lazy_import import lazy_import -lazy_import('ppl', ['C_Polyhedron', 'Generator_System', 'Linear_Expression']) -lazy_import('ppl', 'point', as_='PPL_point') +from sage.features import PythonModule +lazy_import('ppl', ['C_Polyhedron', 'Generator_System', 'Linear_Expression'], + feature=PythonModule("ppl", spkg="pplpy")) +lazy_import('ppl', 'point', as_='PPL_point', + feature=PythonModule("ppl", spkg="pplpy")) + from sage.matrix.constructor import matrix from sage.structure.element import is_Matrix from sage.misc.all import cached_method, flatten, tmp_filename From 5feb98c3ebf754f6db2e26e1eced1621d8a6a163 Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Thu, 10 Dec 2020 11:24:58 +0100 Subject: [PATCH 10/17] missing import --- src/sage/geometry/polyhedron/backend_ppl.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/sage/geometry/polyhedron/backend_ppl.py b/src/sage/geometry/polyhedron/backend_ppl.py index d83f2075373..52d0f04a851 100644 --- a/src/sage/geometry/polyhedron/backend_ppl.py +++ b/src/sage/geometry/polyhedron/backend_ppl.py @@ -6,13 +6,15 @@ from sage.rings.integer import Integer from sage.arith.functions import LCM_list from sage.misc.functional import denominator +from .base import Polyhedron_base +from .base_QQ import Polyhedron_QQ +from .base_ZZ import Polyhedron_ZZ + +from sage.misc.lazy_import import lazy_import from sage.features import PythonModule lazy_import('ppl', ['C_Polyhedron', 'Generator_System', 'Constraint_System', 'Linear_Expression', 'line', 'ray', 'point'], feature=PythonModule("ppl", spkg="pplpy")) -from .base import Polyhedron_base -from .base_QQ import Polyhedron_QQ -from .base_ZZ import Polyhedron_ZZ ######################################################################### From d7a2de9bcb8d20df32fd0630e3ee3a9d5a049f74 Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Thu, 10 Dec 2020 17:45:49 +0100 Subject: [PATCH 11/17] make the doctest work outside of the distribution --- src/sage/geometry/polyhedron/backend_normaliz.py | 3 +-- src/sage/misc/lazy_import.pyx | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/sage/geometry/polyhedron/backend_normaliz.py b/src/sage/geometry/polyhedron/backend_normaliz.py index 46699719a83..c668c776d03 100644 --- a/src/sage/geometry/polyhedron/backend_normaliz.py +++ b/src/sage/geometry/polyhedron/backend_normaliz.py @@ -27,8 +27,7 @@ from sage.misc.all import cached_method, prod from sage.features import PythonModule from sage.misc.lazy_import import lazy_import -lazy_import('PyNormaliz', ['NmzResult', 'NmzCompute', 'NmzCone', 'NmzConeCopy'], - feature=PythonModule("PyNormaliz", spkg="pynormaliz")) +from PyNormaliz import NmzResult, NmzCompute, NmzCone, NmzConeCopy from sage.rings.all import ZZ, QQ from sage.arith.functions import LCM_list diff --git a/src/sage/misc/lazy_import.pyx b/src/sage/misc/lazy_import.pyx index b685cb1728c..512a05ed2fa 100644 --- a/src/sage/misc/lazy_import.pyx +++ b/src/sage/misc/lazy_import.pyx @@ -1070,9 +1070,7 @@ def lazy_import(module, names, as_=None, *, ) failed: sage.features.FeatureNotPresentError: foo is not available. Importing not_there failed: No module named 'foo' No equivalent system packages for ... are known to Sage. - To install foo using the Sage package manager, you can try to run: - !sage -i non-existing-package - No equivalent system packages for pip are known to Sage.> + ... """ if as_ is None: as_ = names From 96f151a9ea4f4fd10607153bee0fb8bf609ce7dd Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Thu, 10 Dec 2020 18:13:17 +0100 Subject: [PATCH 12/17] nicer representation of not available module --- src/sage/misc/lazy_import.pyx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/sage/misc/lazy_import.pyx b/src/sage/misc/lazy_import.pyx index 512a05ed2fa..b5d13b81964 100644 --- a/src/sage/misc/lazy_import.pyx +++ b/src/sage/misc/lazy_import.pyx @@ -372,7 +372,11 @@ cdef class LazyImport(object): sage: repr(lazy_ZZ) 'Integer Ring' """ - return repr(self.get_object()) + try: + obj = self.get_object() + return repr(obj) + except FeatureNotPresentError as e: + return str(e) def __str__(self): """ @@ -1056,7 +1060,7 @@ def lazy_import(module, names, as_=None, *, See http://trac.sagemath.org/14275 for details. 5-adic Field with capped relative precision 20 - An example of and import relying on a feature:: + An example of an import relying on a feature:: sage: from sage.features import PythonModule sage: lazy_import('ppl', 'equation', feature=PythonModule('ppl', spkg='pplpy')) @@ -1067,7 +1071,7 @@ def lazy_import(module, names, as_=None, *, sage: lazy_import('foo', 'not_there', feature=PythonModule('foo', spkg='non-existing-package')) sage: not_there - ) failed: sage.features.FeatureNotPresentError: foo is not available. + foo is not available. Importing not_there failed: No module named 'foo' No equivalent system packages for ... are known to Sage. ... From 044ae4389bec751bbfaf45a20284de6c02c6a284 Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Thu, 10 Dec 2020 19:28:43 +0100 Subject: [PATCH 13/17] lazy import representation acknowledges its type --- src/sage/misc/lazy_import.pyx | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/sage/misc/lazy_import.pyx b/src/sage/misc/lazy_import.pyx index b5d13b81964..dffb588eeed 100644 --- a/src/sage/misc/lazy_import.pyx +++ b/src/sage/misc/lazy_import.pyx @@ -33,7 +33,7 @@ that (s)he can remove the flag:: sage: ZZ Option ``at_startup=True`` for lazy import ZZ not needed anymore - Integer Ring + Lazy import of Integer Ring .. SEEALSO:: :func:`lazy_import`, :class:`LazyImport` @@ -211,7 +211,7 @@ cdef class LazyImport(object): sage: my_integer_ring = LazyImport('sage.rings.all', 'ZZ', at_startup=True) sage: my_integer_ring Option ``at_startup=True`` for lazy import ZZ not needed anymore - Integer Ring + Lazy import of Integer Ring """ if self._object is not None: return self._object @@ -368,15 +368,15 @@ cdef class LazyImport(object): sage: type(lazy_ZZ) sage: lazy_ZZ - Integer Ring + Lazy import of Integer Ring sage: repr(lazy_ZZ) - 'Integer Ring' + 'Lazy import of Integer Ring' """ try: obj = self.get_object() - return repr(obj) + return "Lazy import of " + repr(obj) except FeatureNotPresentError as e: - return str(e) + return "Failed lazy import:\n" + str(e) def __str__(self): """ @@ -1065,12 +1065,13 @@ def lazy_import(module, names, as_=None, *, sage: from sage.features import PythonModule sage: lazy_import('ppl', 'equation', feature=PythonModule('ppl', spkg='pplpy')) sage: equation - + Lazy import of sage: lazy_import('PyNormaliz', 'NmzListConeProperties', feature=PythonModule('PyNormaliz', spkg='pynormaliz')) # optional - pynormaliz sage: NmzListConeProperties # optional - pynormaliz - + Lazy import of sage: lazy_import('foo', 'not_there', feature=PythonModule('foo', spkg='non-existing-package')) sage: not_there + Failed lazy import: foo is not available. Importing not_there failed: No module named 'foo' No equivalent system packages for ... are known to Sage. From b3ec686f4e557bf7e2901ef489300d330b0436ec Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Thu, 10 Dec 2020 20:07:36 +0100 Subject: [PATCH 14/17] only be verbose on failure --- src/sage/misc/lazy_import.pyx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sage/misc/lazy_import.pyx b/src/sage/misc/lazy_import.pyx index dffb588eeed..47954b89109 100644 --- a/src/sage/misc/lazy_import.pyx +++ b/src/sage/misc/lazy_import.pyx @@ -33,7 +33,7 @@ that (s)he can remove the flag:: sage: ZZ Option ``at_startup=True`` for lazy import ZZ not needed anymore - Lazy import of Integer Ring + Integer Ring .. SEEALSO:: :func:`lazy_import`, :class:`LazyImport` @@ -211,7 +211,7 @@ cdef class LazyImport(object): sage: my_integer_ring = LazyImport('sage.rings.all', 'ZZ', at_startup=True) sage: my_integer_ring Option ``at_startup=True`` for lazy import ZZ not needed anymore - Lazy import of Integer Ring + Integer Ring """ if self._object is not None: return self._object @@ -368,13 +368,13 @@ cdef class LazyImport(object): sage: type(lazy_ZZ) sage: lazy_ZZ - Lazy import of Integer Ring + Integer Ring sage: repr(lazy_ZZ) - 'Lazy import of Integer Ring' + 'Integer Ring' """ try: obj = self.get_object() - return "Lazy import of " + repr(obj) + return repr(obj) except FeatureNotPresentError as e: return "Failed lazy import:\n" + str(e) @@ -1065,10 +1065,10 @@ def lazy_import(module, names, as_=None, *, sage: from sage.features import PythonModule sage: lazy_import('ppl', 'equation', feature=PythonModule('ppl', spkg='pplpy')) sage: equation - Lazy import of + sage: lazy_import('PyNormaliz', 'NmzListConeProperties', feature=PythonModule('PyNormaliz', spkg='pynormaliz')) # optional - pynormaliz sage: NmzListConeProperties # optional - pynormaliz - Lazy import of + sage: lazy_import('foo', 'not_there', feature=PythonModule('foo', spkg='non-existing-package')) sage: not_there Failed lazy import: From 3c7b704e2bbb421a074dfd6c3e977be25ba24d87 Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Fri, 11 Dec 2020 08:08:21 +0100 Subject: [PATCH 15/17] lazily import PyNormaliz --- src/sage/geometry/polyhedron/backend_normaliz.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sage/geometry/polyhedron/backend_normaliz.py b/src/sage/geometry/polyhedron/backend_normaliz.py index c668c776d03..1f6e3d62416 100644 --- a/src/sage/geometry/polyhedron/backend_normaliz.py +++ b/src/sage/geometry/polyhedron/backend_normaliz.py @@ -27,7 +27,8 @@ from sage.misc.all import cached_method, prod from sage.features import PythonModule from sage.misc.lazy_import import lazy_import -from PyNormaliz import NmzResult, NmzCompute, NmzCone, NmzConeCopy +lazy_import('PyNormaliz', ['NmzResult', 'NmzCompute', 'NmzCone', 'NmzConeCopy'], + feature=PythonModule("PyNormaliz", spkg="pynormaliz")) from sage.rings.all import ZZ, QQ from sage.arith.functions import LCM_list From 81a655efd3e477df5dddad31fc26c5e6ac23439c Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Sun, 13 Dec 2020 09:47:51 -0800 Subject: [PATCH 16/17] sage.features.package_systems: Ignore 'unknown' --- src/sage/features/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sage/features/__init__.py b/src/sage/features/__init__.py index 5fe9b87a0c1..459ef7261e1 100644 --- a/src/sage/features/__init__.py +++ b/src/sage/features/__init__.py @@ -374,7 +374,9 @@ def package_systems(): # to obtain system package advice. try: proc = run('sage-guess-package-system', shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True, check=True) - _cache_package_systems = [PackageSystem(proc.stdout.strip())] + system_name = proc.stdout.strip() + if system_name != 'unknown': + _cache_package_systems = [PackageSystem(system_name)] except CalledProcessError: pass more_package_systems = [SagePackageSystem(), PipPackageSystem()] From 17c4cdb90244a584e7ee5d98796877d06bfae009 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Sun, 13 Dec 2020 09:52:46 -0800 Subject: [PATCH 17/17] sage.misc.lazy_import: Add some more '...' to doctest --- src/sage/misc/lazy_import.pyx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/sage/misc/lazy_import.pyx b/src/sage/misc/lazy_import.pyx index 47954b89109..a62be123352 100644 --- a/src/sage/misc/lazy_import.pyx +++ b/src/sage/misc/lazy_import.pyx @@ -1073,9 +1073,8 @@ def lazy_import(module, names, as_=None, *, sage: not_there Failed lazy import: foo is not available. - Importing not_there failed: No module named 'foo' - No equivalent system packages for ... are known to Sage. - ... + Importing not_there failed: No module named 'foo'... + No equivalent system packages for ... are known to Sage... """ if as_ is None: as_ = names