Ticket #13370: trac13370_deprecate_is_field.patch

File trac13370_deprecate_is_field.patch, 22.2 KB (added by SimonKing, 7 years ago)

Deprecate is_Field(R) in favour of R in Fields().

  • sage/algebras/quatalg/quaternion_algebra.py

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1345034465 -7200
    # Node ID 228d96516a200ef4fc0c3c4943c58fe3e04da78b
    # Parent  40849f3d0dcba6dd4ef6737dd1d0d0f51e1389f4
    #13370: Remove is_Field from the Sage tree, and deprecate it. This fixes a memleak. Rel #11310 and #13089.
    
    diff --git a/sage/algebras/quatalg/quaternion_algebra.py b/sage/algebras/quatalg/quaternion_algebra.py
    a b  
    4343from sage.rings.rational import Rational
    4444from sage.rings.finite_rings.constructor import GF
    4545
    46 from sage.rings.ring import Algebra, is_Field
     46from sage.rings.ring import Algebra
    4747from sage.rings.ideal import Ideal_fractional
    4848from sage.rings.rational_field import is_RationalField, QQ
    4949from sage.rings.infinity import infinity
     
    6565
    6666from sage.misc.cachefunc import cached_method
    6767
     68from sage.categories.fields import Fields
     69_Fields = Fields()
     70
    6871########################################################
    6972# Constructor
    7073########################################################
     
    220223    # QuaternionAlgebra(K, a, b)
    221224    else:
    222225        K = arg0
    223         if not is_Field(K):
     226        if K not in _Fields:
    224227            raise TypeError("base ring of quaternion algebra must be a field")
    225228        a = K(arg1)
    226229        b = K(arg2)
     
    589592        ParentWithGens.__init__(self, base_ring, names=names)
    590593        self._a = a
    591594        self._b = b
    592         if not is_Field(base_ring):
     595        if base_ring not in _Fields:
    593596            raise TypeError("base ring of quaternion algebra must be a field")
    594597        if is_RationalField(base_ring) and a.denominator() == 1 and b.denominator() == 1:
    595598            element_constructor = quaternion_algebra_element.QuaternionAlgebraElement_rational_field
  • sage/categories/fields.py

    diff --git a/sage/categories/fields.py b/sage/categories/fields.py
    a b  
    1919
    2020from sage.misc.cachefunc import cached_method
    2121from sage.misc.lazy_attribute import lazy_class_attribute
    22 from sage.rings.field import is_Field
     22import sage.rings.ring
    2323
    2424class Fields(Category_singleton):
    2525    """
     
    8888            sage: GR.category = lambda : Fields()
    8989            sage: GR in Fields()
    9090            True
     91
     92        The following tests against a memory leak fixed in :trac:`13370`::
     93
     94            sage: import gc
     95            sage: _ = gc.collect()
     96            sage: n = len([X for X in gc.get_objects() if isinstance(X, sage.rings.finite_rings.integer_mod_ring.IntegerModRing_generic)])
     97            sage: for i in prime_range(100):
     98            ...     R = ZZ.quotient(i)
     99            ...     t = R in Fields()
     100            sage: _ = gc.collect()
     101            sage: len([X for X in gc.get_objects() if isinstance(X, sage.rings.finite_rings.integer_mod_ring.IntegerModRing_generic)]) - n
     102            1
     103
    91104        """
    92105        try:
    93             return self._contains_helper(x) or is_Field(x)
     106            return self._contains_helper(x) or sage.rings.ring._is_Field(x)
    94107        except StandardError:
    95108            return False
    96109
  • sage/categories/finite_fields.py

    diff --git a/sage/categories/finite_fields.py b/sage/categories/finite_fields.py
    a b  
    1313
    1414from sage.misc.cachefunc import cached_method
    1515from sage.categories.category import Category
    16 from sage.rings.field import is_Field
     16from sage.categories.fields import Fields
     17_Fields = Fields()
    1718
    1819class FiniteFields(Category):
    1920    """
     
    6061            sage: IntegerModRing(4) in FiniteFields()
    6162            False
    6263        """
    63         return is_Field(x) and x.is_finite()
     64        return x in _Fields and x.is_finite()
    6465
    6566    # As is, this does no more than the usual __call__ of Category, but for the error message
    6667    def _call_(self, x):
  • sage/categories/pushout.py

    diff --git a/sage/categories/pushout.py b/sage/categories/pushout.py
    a b  
    23552355        I = self.I
    23562356        from sage.all import QQ
    23572357        if not I.is_zero():
    2358             from sage.rings.field import is_Field
    2359             if is_Field(R):
     2358            from sage.categories.fields import Fields
     2359            if R in Fields():
    23602360                from sage.all import Integers
    23612361                return Integers(1)
    23622362        if I.ring() != R:
  • sage/geometry/polyhedron/parent.py

    diff --git a/sage/geometry/polyhedron/parent.py b/sage/geometry/polyhedron/parent.py
    a b  
    1414from sage.structure.unique_representation import UniqueRepresentation
    1515from sage.modules.free_module import is_FreeModule
    1616from sage.misc.cachefunc import cached_method
    17 from sage.rings.all import ZZ, QQ, RDF, is_Field, is_CommutativeRing
     17from sage.rings.all import ZZ, QQ, RDF, is_CommutativeRing
     18from sage.categories.fields import Fields
     19_Fields = Fields()
    1820
    1921from sage.geometry.polyhedron.base import Polyhedron_base, is_Polyhedron
    2022from representation import Inequality, Equation, Vertex, Ray, Line
     
    295297            sage: Polyhedra(QQ, 4).ambient_space()
    296298            Vector space of dimension 4 over Rational Field
    297299        """
    298         if is_Field(self.base_ring()):
     300        if self.base_ring() in _Fields:
    299301            from sage.modules.free_module import VectorSpace
    300302            return VectorSpace(self.base_ring(), self.ambient_dim())
    301303        else:
     
    319321            sage: Polyhedra(ZZ, 2).Hrepresentation_space()
    320322            Ambient free module of rank 3 over the principal ideal domain Integer Ring
    321323        """
    322         if is_Field(self.base_ring()):
     324        if self.base_ring() in _Fields:
    323325            from sage.modules.free_module import VectorSpace
    324326            return VectorSpace(self.base_ring(), self.ambient_dim()+1)
    325327        else:
  • sage/matrix/matrix_misc.py

    diff --git a/sage/matrix/matrix_misc.py b/sage/matrix/matrix_misc.py
    a b  
    1818#                  http://www.gnu.org/licenses/
    1919#*****************************************************************************
    2020
     21from sage.categories.fields import Fields
     22_Fields = Fields()
     23
    2124def row_iterator(A):
    2225    for i in xrange(A.nrows()):
    2326        yield A.row(i)
     
    7578    # determine whether M has polynomial or rational function coefficients
    7679    R0 = M.base_ring()
    7780
    78     from sage.rings.ring import is_Field
    79 
    8081    #Compute the base polynomial ring
    81 
    82     if is_Field(R0):
     82    if R0 in _Fields:
    8383        R = R0.base()
    8484    else:
    8585        R = R0
     
    9494    from sage.rings.arith import lcm
    9595    from sage.matrix.constructor import matrix
    9696    from sage.misc.functional import numerator
    97     if is_Field(R0):
     97    if R0 in _Fields:
    9898        den = lcm([a.denominator() for a in M.list()])
    9999        num = matrix([(lambda x : map(numerator,  x))(v) for v in map(list,(M*den).rows())])
    100100    else:
  • sage/modular/abvar/finite_subgroup.py

    diff --git a/sage/modular/abvar/finite_subgroup.py b/sage/modular/abvar/finite_subgroup.py
    a b  
    9797from sage.modules.free_module import is_FreeModule
    9898from sage.structure.element   import ModuleElement
    9999from sage.structure.sequence  import Sequence
    100 from sage.rings.all           import gcd, lcm, QQ, ZZ, QQbar, is_Field, Integer, composite_field
     100from sage.rings.all           import gcd, lcm, QQ, ZZ, QQbar, Integer, composite_field
    101101from sage.misc.misc           import prod
    102102
    103103import abvar as abelian_variety
     104from sage.categories.fields import Fields
     105_Fields = Fields()
    104106
    105107class FiniteSubgroup(Module_old):
    106108    def __init__(self, abvar, field_of_definition=QQ):
     
    130132            sage: isinstance(G, FiniteSubgroup)
    131133            True
    132134        """
    133         if not is_Field(field_of_definition):
     135        if field_of_definition not in _Fields:
    134136            raise TypeError, "field_of_definition must be a field"
    135137        if not abelian_variety.is_ModularAbelianVariety(abvar):
    136138            raise TypeError, "abvar must be a modular abelian variety"
  • sage/rings/all.py

    diff --git a/sage/rings/all.py b/sage/rings/all.py
    a b  
    2424from dedekind_domain import DedekindDomain, is_DedekindDomain
    2525from principal_ideal_domain import PrincipalIdealDomain, is_PrincipalIdealDomain
    2626from euclidean_domain import EuclideanDomain, is_EuclideanDomain
    27 from field import Field, is_Field, is_PrimeField
     27from field import Field, is_PrimeField
    2828
    2929from commutative_algebra_element import CommutativeAlgebraElement, is_CommutativeAlgebraElement
    3030
  • sage/rings/field.py

    diff --git a/sage/rings/field.py b/sage/rings/field.py
    a b  
    1717#                  http://www.gnu.org/licenses/
    1818#*****************************************************************************
    1919
    20 from sage.rings.ring import Field, is_Field
     20from sage.rings.ring import Field
    2121
    2222def is_PrimeField(R):
    2323    """
  • sage/rings/misc.py

    diff --git a/sage/rings/misc.py b/sage/rings/misc.py
    a b  
    55###########################################################################
    66
    77from sage.structure.sequence import Sequence
    8 from sage.rings.ring import is_Field
     8from sage.categories.fields import Fields
     9_Fields = Fields()
    910
    1011def composite_field(K, L):
    1112    """
     
    3132        ValueError: unable to find a common field
    3233    """
    3334    C = Sequence([K(0), L(0)]).universe()
    34     if not is_Field(C):
     35    if C not in _Fields:
    3536        raise ValueError, "unable to find a common field"
    3637    return C
  • sage/rings/power_series_ring_element.pyx

    diff --git a/sage/rings/power_series_ring_element.pyx b/sage/rings/power_series_ring_element.pyx
    a b  
    116116from sage.misc.functional import sqrt, log
    117117from sage.rings.arith import integer_ceil as ceil
    118118
    119 from sage.rings.ring import is_Field
     119from sage.categories.fields import Fields
     120_Fields = Fields()
    120121
    121122from sage.misc.derivative import multi_derivative
    122123
     
    10881089            return False
    10891090        elif not self[val].is_square():
    10901091            return False
    1091         elif is_Field(self.base_ring()):
     1092        elif self.base_ring() in _Fields:
    10921093            return True
    10931094        else:
    10941095            try:
     
    13121313            raise ValueError, "Square root not defined for power series of odd valuation."
    13131314        elif not self[val].is_square():
    13141315            raise ValueError, "Square root does not live in this ring."
    1315         elif is_Field(self.base_ring()):
     1316        elif self.base_ring() in _Fields:
    13161317            return self.sqrt()
    13171318        else:
    13181319            try:
  • sage/rings/ring.pxd

    diff --git a/sage/rings/ring.pxd b/sage/rings/ring.pxd
    a b  
    11from sage.structure.parent_gens cimport ParentWithGens
    22
     3cpdef bint _is_Field(x)
     4
    35cdef class Ring(ParentWithGens):
    46    cdef public object _zero_element
    57    cdef public object _one_element
  • sage/rings/ring.pyx

    diff --git a/sage/rings/ring.pyx b/sage/rings/ring.pyx
    a b  
    7373from types import GeneratorType
    7474
    7575from sage.misc.lazy_attribute import lazy_class_attribute
     76from sage.misc.superseded import deprecation
    7677from sage.structure.parent_gens cimport ParentWithGens
    7778from sage.structure.parent cimport Parent
    7879from sage.structure.category_object import check_default_category
     
    20082009       """
    20092010        raise NotImplementedError
    20102011
    2011 cdef dict _is_Field_cache = {}
    2012 
    2013 def is_Field(x):
     2012cpdef bint _is_Field(x):
    20142013    """
    20152014    Return True if x is a field.
    20162015
    20172016    EXAMPLES::
    20182017
    2019         sage: from sage.rings.ring import is_Field
    2020         sage: is_Field(QQ)
     2018        sage: from sage.rings.ring import _is_Field
     2019        sage: _is_Field(QQ)
    20212020        True
    2022         sage: is_Field(ZZ)
     2021        sage: _is_Field(ZZ)
    20232022        False
    2024         sage: is_Field(pAdicField(2))
     2023        sage: _is_Field(pAdicField(2))
    20252024        True
    2026         sage: is_Field(5)
     2025        sage: _is_Field(5)
    20272026        False
     2027
     2028    NOTE:
     2029
     2030    ``_is_Field(R)`` is of internal use. It is better (and faster) to
     2031    use ``R in Fields()`` instead.
    20282032    """
    2029     try:
    2030         result = _is_Field_cache[x]
    2031     except (TypeError,KeyError):
    2032         pass
    2033     # The cached result is not immediately returned, since otherwise
    2034     # a non-unique parent's category would be refined only once.
     2033    # The result is not immediately returned, since we want to refine
     2034    # x's category, so that calling x in Fields() will be faster next time.
    20352035    try:
    20362036        result = isinstance(x, Field) or x.is_field()
    20372037    except AttributeError:
    20382038        result = False
    2039     _is_Field_cache[x] = result
    20402039    if result:
    20412040        x._refine_category_(_Fields)
    20422041    return result
    20432042
     2043def is_Field(x):
     2044    """
     2045    Deprecated test of an object being a field.
     2046
     2047    NOTE:
     2048
     2049    For testing whether ``R`` is a field, use ``R in Fields()``,
     2050    not ``is_Field(R)``. See :trac:`13370`.
     2051
     2052    TESTS::
     2053
     2054        sage: from sage.rings.ring import is_Field
     2055        sage: is_Field(ZZ)
     2056        doctest:...: DeprecationWarning: use 'R in Fields()', not 'is_Field(R)'
     2057        See http://trac.sagemath.org/13370 for details.
     2058        False
     2059        sage: is_Field(ZZ.quotient(5))
     2060        True
     2061
     2062    """
     2063    deprecation(13370, "use 'R in Fields()', not 'is_Field(R)'")
     2064    return _is_Field(x)
     2065
    20442066# This imports is_Field, so must be executed after is_Field is defined.
    20452067from sage.categories.algebras import Algebras
    20462068from sage.categories.commutative_algebras import CommutativeAlgebras
  • sage/schemes/elliptic_curves/constructor.py

    diff --git a/sage/schemes/elliptic_curves/constructor.py b/sage/schemes/elliptic_curves/constructor.py
    a b  
    2525
    2626
    2727import sage.rings.all as rings
     28from sage.categories.fields import Fields
     29_Fields = Fields()
    2830
    2931from sage.structure.sequence import Sequence
    3032from sage.structure.element import parent
     
    242244        <class 'sage.schemes.elliptic_curves.ell_field.EllipticCurve_field_with_category'>
    243245        sage: E.category()
    244246        Category of schemes over Symbolic Ring
    245         sage: is_field(SR)
     247        sage: SR in Fields()
    246248        True
    247249
    248250        sage: F = FractionField(PolynomialRing(QQ,'t'))
     
    339341            return ell_padic_field.EllipticCurve_padic_field(x, y)
    340342        elif rings.is_NumberField(x):
    341343            return ell_number_field.EllipticCurve_number_field(x, y)
    342         elif rings.is_Field(x):
     344        elif x in _Fields:
    343345            return ell_field.EllipticCurve_field(x, y)
    344346        return ell_generic.EllipticCurve_generic(x, y)
    345347
     
    372374    elif rings.is_FiniteField(R) or (rings.is_IntegerModRing(R) and R.characteristic().is_prime()):
    373375        return ell_finite_field.EllipticCurve_finite_field(x, y)
    374376
    375     elif rings.is_Field(R):
     377    elif R in _Fields:
    376378        return ell_field.EllipticCurve_field(x, y)
    377379
    378380    return ell_generic.EllipticCurve_generic(x, y)
     
    395397        K = c4.parent()
    396398    except AttributeError:
    397399        K = rings.RationalField()
    398     if not rings.is_Field(K):
     400    if K not in _Fields:
    399401        K = K.fraction_field()
    400402    return EllipticCurve([-K(c4)/K(48), -K(c6)/K(864)])
    401403
     
    456458        K = j.parent()
    457459    except AttributeError:
    458460        K = rings.RationalField()
    459     if not rings.is_Field(K):
     461    if K not in _Fields:
    460462        K = K.fraction_field()
    461463
    462464    char=K.characteristic()
  • sage/schemes/generic/projective_space.py

    diff --git a/sage/schemes/generic/projective_space.py b/sage/schemes/generic/projective_space.py
    a b  
    6060#*****************************************************************************
    6161
    6262from sage.rings.all import (PolynomialRing,
    63                             is_Field,
    6463                            is_FiniteField,
    6564                            is_RationalField,
    6665                            is_Ring,
     
    6968                            Integer,
    7069                            ZZ)
    7170
     71from sage.categories.fields import Fields
     72_Fields = Fields()
     73
    7274from sage.misc.all import (latex,
    7375                           prod)
    7476from sage.structure.parent_gens import normalize_names
     
    157159        n, R = R, n
    158160    if R is None:
    159161        R = ZZ  # default is the integers
    160     if is_Field(R):
     162    if R in _Fields:
    161163        if is_FiniteField(R):
    162164            return ProjectiveSpace_finite_field(n, R, names)
    163165        if is_RationalField(R):
  • sage/schemes/jacobians/abstract_jacobian.py

    diff --git a/sage/schemes/jacobians/abstract_jacobian.py b/sage/schemes/jacobians/abstract_jacobian.py
    a b  
    88#                  http://www.gnu.org/licenses/
    99#*******************************************************************************
    1010
    11 from sage.rings.all import is_Field
     11from sage.categories.fields import Fields
     12_Fields = Fields()
    1213from sage.schemes.generic.scheme import Scheme, is_Scheme
    1314
    1415def is_Jacobian(J):
     
    9697        """
    9798        if not is_Scheme(C):
    9899            raise TypeError, "Argument (=%s) must be a scheme."%C
    99         if not is_Field(C.base_ring()):
     100        if C.base_ring() not in _Fields:
    100101            raise TypeError, "C (=%s) must be defined over a field."%C
    101102        if C.dimension() != 1:
    102103            raise ValueError, "C (=%s) must have dimension 1."%C
     
    227228            Jacobian of Hyperelliptic Curve over Number Field in a with defining
    228229            polynomial x^2 + 1 defined by y^2 = x^3 - 10*x + 9
    229230        """
    230         if not is_Field(R):
     231        if R not in _Fields:
    231232            raise ValueError('Not a field: '+str(R))
    232233        if self.base_ring() is R:
    233234            return self
  • sage/schemes/plane_conics/con_field.py

    diff --git a/sage/schemes/plane_conics/con_field.py b/sage/schemes/plane_conics/con_field.py
    a b  
    2525#*****************************************************************************
    2626
    2727from sage.rings.all import (PolynomialRing,
    28                             is_ComplexField, is_RealField,
    29                             is_Field)
     28                            is_ComplexField, is_RealField)
    3029from sage.modules.free_module_element import vector
    3130from sage.structure.sequence import Sequence
    3231from sage.structure.element import is_Vector
     
    3635
    3736from sage.schemes.plane_curves.projective_curve import ProjectiveCurve_generic
    3837
     38from sage.categories.fields import Fields
     39_Fields = Fields()
     40
    3941class ProjectiveConic_field(ProjectiveCurve_generic):
    4042    r"""
    4143    Create a projective plane conic curve over a field.
     
    107109            sage: d.rational_point(algorithm = 'rnfisnorm')
    108110            (i : 1 : 0)
    109111        """
    110         if is_Field(S):
     112        if S in _Fields:
    111113            B = self.base_ring()
    112114            if B == S:
    113115                return self
  • sage/schemes/toric/fano_variety.py

    diff --git a/sage/schemes/toric/fano_variety.py b/sage/schemes/toric/fano_variety.py
    a b  
    156156from sage.geometry.all import Cone, FaceFan, Fan, LatticePolytope
    157157from sage.misc.all import latex, prod
    158158from sage.rings.all import (PolynomialRing, QQ,
    159                             is_FractionField, is_Field,
     159                            is_FractionField,
    160160                            is_MPolynomialRing, is_PolynomialRing)
    161161from sage.schemes.generic.algebraic_scheme import AlgebraicScheme_subscheme_toric
    162162from sage.schemes.toric.variety import (
    163163                                            ToricVariety_field,
    164164                                            normalize_names)
    165165from sage.symbolic.all import SR
     166from sage.categories.fields import Fields
     167_Fields = Fields()
    166168
    167169
    168170# Default coefficient for anticanonical hypersurfaces
     
    628630    # Check/normalize base_field
    629631    if base_field is None:
    630632        base_field = QQ
    631     elif not is_Field(base_field):
     633    elif base_field not in _Fields:
    632634        raise TypeError("need a field to construct a Fano toric variety!"
    633635                        "\n Got %s" % base_field)
    634636    fan._is_complete = True     # At this point it must be for sure
  • sage/schemes/toric/library.py

    diff --git a/sage/schemes/toric/library.py b/sage/schemes/toric/library.py
    a b  
    4242from sage.geometry.fan import Fan
    4343from sage.geometry.toric_lattice import ToricLattice
    4444from sage.geometry.lattice_polytope import LatticePolytope
    45 from sage.rings.all import ZZ, QQ, is_Field, gcd
     45from sage.rings.all import ZZ, QQ, gcd
    4646from sage.schemes.toric.variety import (DEFAULT_PREFIX,
    4747                                        ToricVariety,
    4848                                        normalize_names)
    4949from sage.schemes.toric.fano_variety import CPRFanoToricVariety
    50 
     50from sage.categories.fields import Fields
     51_Fields = Fields()
    5152
    5253
    5354
     
    13501351        for key in kw:
    13511352            if key == 'K':
    13521353                K = kw['K']
    1353                 if not is_Field(K):
     1354                if K not in _Fields:
    13541355                    raise TypeError("K (=%r) must be a field" % K)
    13551356            elif key == 'names':
    13561357                names = kw['names']
  • sage/schemes/toric/variety.py

    diff --git a/sage/schemes/toric/variety.py b/sage/schemes/toric/variety.py
    a b  
    328328from sage.misc.all import latex, prod, uniq, cached_method
    329329from sage.structure.unique_representation import UniqueRepresentation
    330330from sage.modules.free_module_element import vector
    331 from sage.rings.all import PolynomialRing, ZZ, QQ, is_Field
     331from sage.rings.all import PolynomialRing, ZZ, QQ
    332332from sage.rings.quotient_ring_element import QuotientRingElement
    333333from sage.rings.quotient_ring import QuotientRing_generic
    334334from sage.schemes.generic.affine_space import AffineSpace
    335335from sage.schemes.generic.ambient_space import AmbientSpace
    336336from sage.schemes.toric.homset import SchemeHomset_points_toric_field
    337 
     337from sage.categories.fields import Fields
     338_Fields = Fields()
    338339
    339340
    340341# Default prefix for indexed coordinates
     
    464465        if coordinate_names is not None:
    465466            raise ValueError('You must not specify both coordinate_names and names!')
    466467        coordinate_names = names
    467     if not is_Field(base_field):
     468    if base_field not in _Fields:
    468469        raise TypeError("need a field to construct a toric variety!\n Got %s"
    469470                        % base_field)
    470471    return ToricVariety_field(fan, coordinate_names, coordinate_indices,