From eb72ef050d94dd32cbb1499243af5de4e9eec22c Mon Sep 17 00:00:00 2001 From: Dima Pasechnik Date: Tue, 29 Nov 2022 21:06:01 +0000 Subject: [PATCH] avoid is_GapElement --- src/sage/rings/finite_rings/element_givaro.pyx | 8 ++++++-- src/sage/rings/finite_rings/element_ntl_gf2e.pyx | 10 +++++++--- src/sage/rings/finite_rings/element_pari_ffelt.pyx | 8 ++++++-- src/sage/rings/finite_rings/integer_mod_ring.py | 13 +++++++++++-- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/sage/rings/finite_rings/element_givaro.pyx b/src/sage/rings/finite_rings/element_givaro.pyx index 42f93cceb75..790121a62dc 100644 --- a/src/sage/rings/finite_rings/element_givaro.pyx +++ b/src/sage/rings/finite_rings/element_givaro.pyx @@ -72,7 +72,11 @@ from cypari2.stack cimport clear_stack from sage.structure.parent cimport Parent -from sage.interfaces.gap import is_GapElement + +try: + from sage.interfaces.gap import GapElement +except ImportError: + GapElement = () cdef object is_IntegerMod cdef object Integer @@ -435,7 +439,7 @@ cdef class Cache_givaro(Cache_base): elif isinstance(e, sage.libs.gap.element.GapElement_FiniteField): return e.sage(ring=self.parent) - elif is_GapElement(e): + elif isinstance(e, GapElement): from sage.libs.gap.libgap import libgap return libgap(e).sage(ring=self.parent) diff --git a/src/sage/rings/finite_rings/element_ntl_gf2e.pyx b/src/sage/rings/finite_rings/element_ntl_gf2e.pyx index ebe16dcdcd8..fefd492f952 100644 --- a/src/sage/rings/finite_rings/element_ntl_gf2e.pyx +++ b/src/sage/rings/finite_rings/element_ntl_gf2e.pyx @@ -52,11 +52,15 @@ from sage.arith.long cimport pyobject_to_long from .element_pari_ffelt import FiniteFieldElement_pari_ffelt from .finite_field_ntl_gf2e import FiniteField_ntl_gf2e +from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing + from sage.libs.gap.element import GapElement_FiniteField -from sage.interfaces.gap import is_GapElement +try: + from sage.interfaces.gap import GapElement +except ImportError: + GapElement = () -from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing cdef object is_IntegerMod cdef object IntegerModRing_generic @@ -369,7 +373,7 @@ cdef class Cache_ntl_gf2e(Cache_base): elif isinstance(e, GapElement_FiniteField): return e.sage(ring=self._parent) - elif is_GapElement(e): + elif isinstance(e, GapElement): from sage.libs.gap.libgap import libgap return libgap(e).sage(ring=self._parent) diff --git a/src/sage/rings/finite_rings/element_pari_ffelt.pyx b/src/sage/rings/finite_rings/element_pari_ffelt.pyx index 0ee972a7b61..d427f1a5282 100644 --- a/src/sage/rings/finite_rings/element_pari_ffelt.pyx +++ b/src/sage/rings/finite_rings/element_pari_ffelt.pyx @@ -29,7 +29,6 @@ from .element_base cimport FinitePolyExtElement from .integer_mod import IntegerMod_abstract import sage.rings.integer -from sage.interfaces.gap import is_GapElement from sage.modules.free_module_element import FreeModuleElement from sage.rings.integer cimport Integer from sage.rings.polynomial.polynomial_element import Polynomial @@ -39,6 +38,11 @@ from sage.structure.element cimport Element, ModuleElement, RingElement from sage.structure.richcmp cimport rich_to_bool +try: + from sage.interfaces.gap import GapElement +except ImportError: + GapElement = () + cdef GEN _INT_to_FFELT(GEN g, GEN x) except NULL: """ Convert the t_INT `x` to an element of the field of definition of @@ -504,7 +508,7 @@ cdef class FiniteFieldElement_pari_ffelt(FinitePolyExtElement): elif isinstance(x, str): self.construct_from(self._parent.polynomial_ring()(x)) - elif is_GapElement(x): + elif isinstance(x, GapElement): try: from sage.libs.gap.libgap import libgap self.construct_from(libgap(x).sage(ring=self._parent)) diff --git a/src/sage/rings/finite_rings/integer_mod_ring.py b/src/sage/rings/finite_rings/integer_mod_ring.py index 7049f2d6b11..01eab795111 100644 --- a/src/sage/rings/finite_rings/integer_mod_ring.py +++ b/src/sage/rings/finite_rings/integer_mod_ring.py @@ -77,7 +77,10 @@ from sage.structure.factory import UniqueFactory from sage.structure.richcmp import richcmp, richcmp_method -from sage.interfaces.gap import is_GapElement +try: + from sage.interfaces.gap import GapElement +except ImportError: + GapElement = () class IntegerModFactory(UniqueFactory): r""" @@ -1180,13 +1183,19 @@ def _element_constructor_(self, x): 4 sage: libgap(a.sage()) == a True + + better syntax for libgap interface:: + + sage: a = libgap.Z(13)^2 + sage: libgap(a.sage()) == a + True """ try: return integer_mod.IntegerMod(self, x) except (NotImplementedError, PariError): raise TypeError("error coercing to finite field") except TypeError: - if is_GapElement(x): + if isinstance(x, GapElement): from sage.libs.gap.libgap import libgap return libgap(x).sage() raise # Continue up with the original TypeError