From 7e7daef35ffb98ad1303c69ba6fb614f67871ee9 Mon Sep 17 00:00:00 2001 From: Jeroen Demeyer Date: Thu, 12 Jul 2018 14:01:53 +0200 Subject: [PATCH] Move Set_PythonType to a new file --- src/doc/en/reference/sets/index.rst | 1 + src/sage/categories/homset.py | 5 +- src/sage/categories/map.pyx | 2 +- src/sage/combinat/words/alphabet.py | 2 +- src/sage/rings/complex_double.pyx | 4 +- src/sage/rings/finite_rings/integer_mod.pyx | 2 +- src/sage/rings/integer.pyx | 4 +- src/sage/rings/rational.pyx | 4 +- src/sage/rings/real_double.pyx | 2 +- src/sage/rings/real_lazy.pyx | 2 +- src/sage/sets/finite_enumerated_set.py | 2 +- src/sage/sets/pythonclass.pxd | 8 + src/sage/sets/pythonclass.pyx | 247 ++++++++++++++++++++ src/sage/structure/coerce.pyx | 3 +- src/sage/structure/coerce_actions.pyx | 2 +- src/sage/structure/coerce_maps.pyx | 2 +- src/sage/structure/parent.pxd | 7 +- src/sage/structure/parent.pyx | 247 +------------------- src/sage/structure/parent_old.pyx | 2 +- src/sage/symbolic/ring.pyx | 2 +- 20 files changed, 284 insertions(+), 266 deletions(-) create mode 100644 src/sage/sets/pythonclass.pxd create mode 100644 src/sage/sets/pythonclass.pyx diff --git a/src/doc/en/reference/sets/index.rst b/src/doc/en/reference/sets/index.rst index 7389338f9ad..8631e55850a 100644 --- a/src/doc/en/reference/sets/index.rst +++ b/src/doc/en/reference/sets/index.rst @@ -18,6 +18,7 @@ Set Constructions sage/sets/finite_set_maps sage/sets/finite_set_map_cy sage/sets/totally_ordered_finite_set + sage/sets/pythonclass Sets of Numbers --------------- diff --git a/src/sage/categories/homset.py b/src/sage/categories/homset.py index ab59d28d64b..fe23ead6bda 100644 --- a/src/sage/categories/homset.py +++ b/src/sage/categories/homset.py @@ -273,8 +273,9 @@ def Hom(X, Y, category=None, check=True): Facade parents over plain Python types are supported:: - sage: R = sage.structure.parent.Set_PythonType(int) - sage: S = sage.structure.parent.Set_PythonType(float) + sage: from sage.sets.pythonclass import Set_PythonType + sage: R = Set_PythonType(int) + sage: S = Set_PythonType(float) sage: Hom(R, S) Set of Morphisms from Set of Python objects of class 'int' to Set of Python objects of class 'float' in Category of sets diff --git a/src/sage/categories/map.pyx b/src/sage/categories/map.pyx index b6a443cc38f..4ec1ddc7acd 100644 --- a/src/sage/categories/map.pyx +++ b/src/sage/categories/map.pyx @@ -25,7 +25,7 @@ from . import homset import weakref from sage.ext.stdsage cimport HAS_DICTIONARY from sage.arith.power cimport generic_power -from sage.structure.parent cimport Set_PythonType +from sage.sets.pythonclass cimport Set_PythonType from sage.misc.constant_function import ConstantFunction from sage.misc.superseded import deprecated_function_alias from sage.structure.element cimport parent diff --git a/src/sage/combinat/words/alphabet.py b/src/sage/combinat/words/alphabet.py index 08013eed9a6..70f0a3528bf 100644 --- a/src/sage/combinat/words/alphabet.py +++ b/src/sage/combinat/words/alphabet.py @@ -265,7 +265,7 @@ def build_alphabet(data=None, names=None, name=None): # Alphabet(**nothing**) if data is None: # name is also None - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType return Set_PythonType(object) raise ValueError("unable to construct an alphabet from the given parameters") diff --git a/src/sage/rings/complex_double.pyx b/src/sage/rings/complex_double.pyx index 8e57f0f1dbe..20ad8c66006 100644 --- a/src/sage/rings/complex_double.pyx +++ b/src/sage/rings/complex_double.pyx @@ -2426,7 +2426,7 @@ cdef class FloatToCDF(Morphism): """ from sage.categories.homset import Hom if isinstance(R, type): - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType R = Set_PythonType(R) Morphism.__init__(self, Hom(R, CDF)) @@ -2476,7 +2476,7 @@ cdef class ComplexToCDF(Morphism): def __init__(self, R): from sage.categories.homset import Hom if isinstance(R, type): - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType R = Set_PythonType(R) Morphism.__init__(self, Hom(R, CDF)) diff --git a/src/sage/rings/finite_rings/integer_mod.pyx b/src/sage/rings/finite_rings/integer_mod.pyx index aa0cbfb86ea..b8c8a76ae87 100644 --- a/src/sage/rings/finite_rings/integer_mod.pyx +++ b/src/sage/rings/finite_rings/integer_mod.pyx @@ -4370,7 +4370,7 @@ cdef class Int_to_IntegerMod(IntegerMod_hom): """ def __init__(self, R): import sage.categories.homset - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType IntegerMod_hom.__init__(self, sage.categories.homset.Hom(Set_PythonType(int), R)) cpdef Element _call_(self, x): diff --git a/src/sage/rings/integer.pyx b/src/sage/rings/integer.pyx index a095d00b3b0..8f24646a9df 100644 --- a/src/sage/rings/integer.pyx +++ b/src/sage/rings/integer.pyx @@ -7080,7 +7080,7 @@ cdef class int_to_Z(Morphism): Set of Morphisms from Set of Python objects of class 'int' to Integer Ring in Category of sets """ import sage.categories.homset - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType Morphism.__init__(self, sage.categories.homset.Hom(Set_PythonType(int), integer_ring.ZZ)) cpdef Element _call_(self, a): @@ -7126,7 +7126,7 @@ cdef class long_to_Z(Morphism): """ def __init__(self): import sage.categories.homset - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType Morphism.__init__(self, sage.categories.homset.Hom(Set_PythonType(long), integer_ring.ZZ)) cpdef Element _call_(self, a): diff --git a/src/sage/rings/rational.pyx b/src/sage/rings/rational.pyx index 2d541b4fc6e..7faff252568 100644 --- a/src/sage/rings/rational.pyx +++ b/src/sage/rings/rational.pyx @@ -4221,7 +4221,7 @@ cdef class int_to_Q(Morphism): """ from . import rational_field import sage.categories.homset - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType Morphism.__init__(self, sage.categories.homset.Hom(Set_PythonType(int), rational_field.QQ)) cpdef Element _call_(self, a): @@ -4281,7 +4281,7 @@ cdef class long_to_Q(Morphism): """ from . import rational_field import sage.categories.homset - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType Morphism.__init__(self, sage.categories.homset.Hom( Set_PythonType(long), rational_field.QQ)) diff --git a/src/sage/rings/real_double.pyx b/src/sage/rings/real_double.pyx index 4de636a6a38..09c76cfdf16 100644 --- a/src/sage/rings/real_double.pyx +++ b/src/sage/rings/real_double.pyx @@ -2703,7 +2703,7 @@ cdef class ToRDF(Morphism): """ from sage.categories.homset import Hom if isinstance(R, type): - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType R = Set_PythonType(R) Morphism.__init__(self, Hom(R, RDF)) diff --git a/src/sage/rings/real_lazy.pyx b/src/sage/rings/real_lazy.pyx index 71cb68246bb..6c13be234e1 100644 --- a/src/sage/rings/real_lazy.pyx +++ b/src/sage/rings/real_lazy.pyx @@ -158,7 +158,7 @@ cdef class LazyField(Field): """ if isinstance(R, type): if R in [int, long]: - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType return LazyWrapperMorphism(Set_PythonType(R), self) elif R.is_exact(): ivf = self.interval_field() diff --git a/src/sage/sets/finite_enumerated_set.py b/src/sage/sets/finite_enumerated_set.py index ba5b1b26224..d695ca463bd 100644 --- a/src/sage/sets/finite_enumerated_set.py +++ b/src/sage/sets/finite_enumerated_set.py @@ -342,7 +342,7 @@ def __call__(self, el): 2 sage: phi.register_as_conversion() - sage: from sage.structure.parent import Set_PythonType_class + sage: from sage.sets.pythonclass import Set_PythonType_class sage: psi = Hom(Set_PythonType_class(str), F, Sets())(lambda s: ZZ(len(s))) sage: psi.register_as_conversion() sage: psi('a') diff --git a/src/sage/sets/pythonclass.pxd b/src/sage/sets/pythonclass.pxd new file mode 100644 index 00000000000..4346d918386 --- /dev/null +++ b/src/sage/sets/pythonclass.pxd @@ -0,0 +1,8 @@ +from sage.structure.parent cimport Set_generic + + +cdef class Set_PythonType_class(Set_generic): + cdef type _type + + +cpdef Set_PythonType(typ) diff --git a/src/sage/sets/pythonclass.pyx b/src/sage/sets/pythonclass.pyx new file mode 100644 index 00000000000..a2be8670aeb --- /dev/null +++ b/src/sage/sets/pythonclass.pyx @@ -0,0 +1,247 @@ +""" +Set of all objects of a given Python class +""" + +#***************************************************************************** +# Copyright (C) 2018 Jeroen Demeyer +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** + +from cpython.object cimport Py_EQ, Py_NE +from cpython.version cimport PY_MAJOR_VERSION +from sage.structure.richcmp cimport rich_to_bool +from sage.categories.sets_cat import Sets + + +cdef dict _type_set_cache = {} + +cpdef Set_PythonType(typ): + """ + Return the (unique) Parent that represents the set of Python objects + of a specified type. + + EXAMPLES:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: Set_PythonType(list) + Set of Python objects of class 'list' + sage: Set_PythonType(list) is Set_PythonType(list) + True + sage: S = Set_PythonType(tuple) + sage: S([1,2,3]) + (1, 2, 3) + + S is a parent which models the set of all lists:: + + sage: S.category() + Category of sets + """ + try: + return _type_set_cache[typ] + except KeyError: + _type_set_cache[typ] = theSet = Set_PythonType_class(typ) + return theSet + + +cdef class Set_PythonType_class(Set_generic): + r""" + The set of Python objects of a given class. + + The elements of this set are not instances of + :class:`~sage.structure.element.Element`; they are instances of + the given class. + + INPUT: + + - ``typ`` -- a Python (new-style) class + + EXAMPLES:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: S = Set_PythonType(int); S + Set of Python objects of class 'int' + sage: int('1') in S + True + sage: Integer('1') in S + False + + sage: Set_PythonType(2) + Traceback (most recent call last): + ... + TypeError: must be initialized with a class, not 2 + """ + + def __init__(self, typ): + """ + EXAMPLES:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: Set_PythonType(float).category() + Category of sets + """ + if not isinstance(typ, type): + raise TypeError(f"must be initialized with a class, not {typ!r}") + super().__init__(category=Sets()) + self._type = typ + + def _element_constructor_(self, *args, **kwds): + """ + Construct an instance of the class. + + EXAMPLES:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: S = Set_PythonType(complex) + sage: S._element_constructor_(5) + (5+0j) + sage: S._element_constructor_(1, 5/2) + (1+2.5j) + """ + return self._type(*args, **kwds) + + def __reduce__(self): + r""" + Pickling support + + TESTS:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: S = Set_PythonType(object) + sage: loads(dumps(S)) + Set of Python objects of class 'object' + """ + return type(self), (self._type,) + + def __call__(self, x): + """ + Construct a new instance from ``x``. If ``x`` is already an + instance of the correct class, directly return ``x`` itself. + + EXAMPLES:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: S = Set_PythonType(float) + sage: S(5) + 5.0 + sage: S(9/3) + 3.0 + sage: S(1/3) + 0.333333333333333... + sage: a = float(3); S(a) is a + True + """ + if isinstance(x, self._type): + return x + return self._type(x) + + def __hash__(self): + """ + TESTS:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: S = Set_PythonType(int) + sage: hash(S) == -hash(int) + True + """ + return -hash(self._type) + + def __richcmp__(self, other, int op): + """ + Two Python class sets are considered the same if they contain + the same class. + + EXAMPLES:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: S = Set_PythonType(int) + sage: T = Set_PythonType(int) + sage: U = type(S)(int) # bypass caching + sage: S is T + True + sage: S == T + True + sage: S is U + False + sage: S == U + True + sage: S == Set_PythonType(float) + False + sage: S == int + False + """ + if not (op == Py_EQ or op == Py_NE): + return NotImplemented + if self is other: + return rich_to_bool(op, 0) + if not isinstance(other, Set_PythonType_class): + return rich_to_bool(op, 1) + s = (self)._type + o = (other)._type + return rich_to_bool(op, s is not o) + + def __contains__(self, x): + """ + Only things of the right class (or subclasses thereof) are + considered to belong to the set. + + EXAMPLES:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: S = Set_PythonType(tuple) + sage: (1,2,3) in S + True + sage: () in S + True + sage: [1,2] in S + False + """ + return isinstance(x, self._type) + + def _repr_(self): + """ + EXAMPLES:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: Set_PythonType(tuple) + Set of Python objects of class 'tuple' + sage: Set_PythonType(Integer) + Set of Python objects of class 'Integer' + sage: Set_PythonType(Parent) + Set of Python objects of class 'Parent' + """ + return f"Set of Python objects of class '{self._type.__name__}'" + + def object(self): + """ + EXAMPLES:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: Set_PythonType(tuple).object() + <... 'tuple'> + """ + return self._type + + def cardinality(self): + """ + EXAMPLES:: + + sage: from sage.sets.pythonclass import Set_PythonType + sage: S = Set_PythonType(bool) + sage: S.cardinality() + 2 + sage: S = Set_PythonType(int) + sage: S.cardinality() + +Infinity + """ + if self._type is bool: + from sage.rings.integer import Integer + return Integer(2) + else: + # Probably infinite + import sage.rings.infinity + return sage.rings.infinity.infinity diff --git a/src/sage/structure/coerce.pyx b/src/sage/structure/coerce.pyx index 13acfc1058a..73d84830b4e 100644 --- a/src/sage/structure/coerce.pyx +++ b/src/sage/structure/coerce.pyx @@ -94,7 +94,7 @@ except ImportError: from .richcmp cimport rich_to_bool, revop from .sage_object cimport SageObject -from .parent cimport Set_PythonType, Parent_richcmp_element_without_coercion +from .parent cimport Parent_richcmp_element_without_coercion from .element cimport bin_op_exception, parent, Element from .coerce_actions import LeftModuleAction, RightModuleAction from .coerce_exceptions import CoercionException @@ -102,6 +102,7 @@ from sage.rings.integer_fake cimport is_Integer from sage.categories.map cimport Map from sage.categories.morphism import IdentityMorphism from sage.categories.action cimport Action, InverseAction, PrecomposedAction +from sage.sets.pythonclass cimport Set_PythonType import traceback diff --git a/src/sage/structure/coerce_actions.pyx b/src/sage/structure/coerce_actions.pyx index 8f9901682b5..7c43c9ada1a 100644 --- a/src/sage/structure/coerce_actions.pyx +++ b/src/sage/structure/coerce_actions.pyx @@ -667,7 +667,7 @@ cdef class IntegerAction(Action): """ def __init__(self, Z, S, is_left, op): if isinstance(Z, type): - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType Z = Set_PythonType(Z) super().__init__(Z, S, is_left, op) diff --git a/src/sage/structure/coerce_maps.pyx b/src/sage/structure/coerce_maps.pyx index 4d3a60c6f7e..b717e67186c 100644 --- a/src/sage/structure/coerce_maps.pyx +++ b/src/sage/structure/coerce_maps.pyx @@ -6,9 +6,9 @@ from __future__ import print_function, absolute_import import re import types -from .parent import Set_PythonType from sage.structure.parent cimport Parent from sage.structure.element cimport Element +from sage.sets.pythonclass cimport Set_PythonType cdef object BuiltinMethodType = type(repr) diff --git a/src/sage/structure/parent.pxd b/src/sage/structure/parent.pxd index 304a77d7ea1..c28722233ac 100644 --- a/src/sage/structure/parent.pxd +++ b/src/sage/structure/parent.pxd @@ -98,10 +98,13 @@ cdef class Parent(sage.structure.category_object.CategoryObject): # An optional single Morphism that describes a canonical coercion out of self cdef _embedding + +cdef class Set_generic(Parent): + pass + + # Flags for Parent.flags cdef enum: # If this flag is set, call __richcmp__ on elements without # coercion. This allows a completely custom comparison function. Parent_richcmp_element_without_coercion = 1 - -cpdef Parent Set_PythonType(theType) diff --git a/src/sage/structure/parent.pyx b/src/sage/structure/parent.pyx index 49a8c7b43d0..f78c733aca7 100644 --- a/src/sage/structure/parent.pyx +++ b/src/sage/structure/parent.pyx @@ -129,6 +129,7 @@ from sage.categories.sets_cat import Sets, EmptySetError from sage.misc.lazy_format import LazyFormat from .coerce_maps cimport (NamedConvertMap, DefaultConvertMap, DefaultConvertMap_unique, CallableConvertMap) +from sage.sets.pythonclass cimport Set_PythonType_class, Set_PythonType cdef _record_exception(): @@ -2785,8 +2786,7 @@ cdef class Parent(sage.structure.category_object.CategoryObject): # Set base class -- ############################################################################ - -cdef class Set_generic(Parent): # Cannot use Parent because Element._parent is Parent +cdef class Set_generic(Parent): """ Abstract base class for sets. @@ -2822,249 +2822,6 @@ cdef class Set_generic(Parent): # Cannot use Parent because Element._parent is P return not (self.is_finite() and len(self) == 0) -cdef _type_set_cache = {} - -cpdef Parent Set_PythonType(theType): - """ - Return the (unique) Parent that represents the set of Python objects - of a specified type. - - EXAMPLES:: - - sage: from sage.structure.parent import Set_PythonType - sage: Set_PythonType(list) - Set of Python objects of class 'list' - sage: Set_PythonType(list) is Set_PythonType(list) - True - sage: S = Set_PythonType(tuple) - sage: S([1,2,3]) - (1, 2, 3) - - S is a parent which models the set of all lists: - sage: S.category() - Category of sets - - EXAMPLES:: - - sage: R = sage.structure.parent.Set_PythonType(int) - sage: S = sage.structure.parent.Set_PythonType(float) - sage: Hom(R, S) - Set of Morphisms from Set of Python objects of class 'int' to Set of Python objects of class 'float' in Category of sets - - """ - try: - return _type_set_cache[theType] - except KeyError: - _type_set_cache[theType] = theSet = Set_PythonType_class(theType) - return theSet - - -cdef class Set_PythonType_class(Set_generic): - r""" - The set of Python objects of a given class. - - The elements of this set are not instances of - :class:`~sage.structure.element.Element`; they are instances of - the given class. - - INPUT: - - - ``theType`` -- a Python (new-style) class - - EXAMPLES:: - - sage: S = sage.structure.parent.Set_PythonType(int) - sage: S - Set of Python objects of class 'int' - sage: int('1') in S - True - sage: Integer('1') in S - False - - sage: sage.structure.parent.Set_PythonType(2) - Traceback (most recent call last): - ... - TypeError: must be initialized with a class, not 2 - """ - cdef type _type - - def __init__(self, theType): - """ - EXAMPLES:: - - sage: S = sage.structure.parent.Set_PythonType(float) - sage: S.category() - Category of sets - """ - if not isinstance(theType, type): - raise TypeError(f"must be initialized with a class, not {theType!r}") - super().__init__(category=Sets()) - self._type = theType - - def _element_constructor_(self, *args, **kwds): - """ - Construct an instance of the class. - - EXAMPLES:: - - sage: S = sage.structure.parent.Set_PythonType(complex) - sage: S._element_constructor_(5) - (5+0j) - sage: S._element_constructor_(1, 5/2) - (1+2.5j) - """ - return self._type(*args, **kwds) - - def __reduce__(self): - r""" - Pickling support - - TESTS:: - - sage: S = sage.structure.parent.Set_PythonType(object) - sage: loads(dumps(S)) - Set of Python objects of class 'object' - """ - return Set_PythonType, (self._type,) - - def __call__(self, x): - """ - Construct a new instance from ``x``. If ``x`` is already an - instance of the correct class, directly return ``x`` itself. - - EXAMPLES:: - - sage: S = sage.structure.parent.Set_PythonType(float) - sage: S(5) - 5.0 - sage: S(9/3) - 3.0 - sage: S(1/3) - 0.333333333333333... - sage: a = float(3); S(a) is a - True - """ - if isinstance(x, self._type): - return x - return self._type(x) - - def __hash__(self): - """ - TESTS:: - - sage: S = sage.structure.parent.Set_PythonType(int) - sage: hash(S) == -hash(int) - True - """ - return -hash(self._type) - - def __richcmp__(self, other, int op): - """ - Two Python class sets are considered the same if they contain - the same class. - - EXAMPLES:: - - sage: from sage.structure.parent import Set_PythonType - sage: S = Set_PythonType(int) - sage: T = Set_PythonType(int) - sage: U = type(S)(int) # bypass caching - sage: S is T - True - sage: S == T - True - sage: S is U - False - sage: S == U - True - sage: S == Set_PythonType(float) - False - sage: S == int - False - """ - if not (op == Py_EQ or op == Py_NE): - return NotImplemented - if self is other: - return rich_to_bool(op, 0) - if not isinstance(other, Set_PythonType_class): - return rich_to_bool(op, 1) - s = (self)._type - o = (other)._type - return rich_to_bool(op, s is not o) - - def __contains__(self, x): - """ - Only things of the right class (or subclasses thereof) are - considered to belong to the set. - - EXAMPLES:: - - sage: S = sage.structure.parent.Set_PythonType(tuple) - sage: (1,2,3) in S - True - sage: () in S - True - sage: [1,2] in S - False - """ - return isinstance(x, self._type) - - def _repr_(self): - """ - EXAMPLES:: - - sage: sage.structure.parent.Set_PythonType(tuple) - Set of Python objects of class 'tuple' - sage: sage.structure.parent.Set_PythonType(Integer) - Set of Python objects of class 'Integer' - sage: sage.structure.parent.Set_PythonType(Parent) - Set of Python objects of class 'Parent' - """ - return f"Set of Python objects of class '{self._type.__name__}'" - - def object(self): - """ - EXAMPLES:: - - sage: S = sage.structure.parent.Set_PythonType(tuple) - sage: S.object() - <... 'tuple'> - """ - return self._type - - def cardinality(self): - """ - EXAMPLES:: - - sage: S = sage.structure.parent.Set_PythonType(bool) - sage: S.cardinality() - 2 - sage: S = sage.structure.parent.Set_PythonType(int) - sage: S.cardinality() - 4294967296 # 32-bit - 18446744073709551616 # 64-bit - sage: S = sage.structure.parent.Set_PythonType(float) - sage: S.cardinality() - 18437736874454810627 - sage: S = sage.structure.parent.Set_PythonType(long) - sage: S.cardinality() - +Infinity - """ - from sage.rings.integer import Integer - two = Integer(2) - if self._type is bool: - return two - elif self._type is int: - import sys - return two * sys.maxsize + 2 - elif self._type is float: - return 2 * two**52 * (two**11 - 1) + 3 # all NaN's are the same from Python's point of view - else: - # probably - import sage.rings.infinity - return sage.rings.infinity.infinity - - # These functions are to guarantee that user defined _lmul_, _rmul_, # _act_on_, _acted_upon_ do not in turn call __mul__ on their # arguments, leading to an infinite loop. diff --git a/src/sage/structure/parent_old.pyx b/src/sage/structure/parent_old.pyx index a00b6c06987..c122e323c8e 100644 --- a/src/sage/structure/parent_old.pyx +++ b/src/sage/structure/parent_old.pyx @@ -31,9 +31,9 @@ from __future__ import absolute_import, print_function cimport sage.structure.sage_object as sage_object import operator -from .parent import Set_PythonType, Set_PythonType_class from .coerce cimport py_scalar_parent from sage.ext.stdsage cimport HAS_DICTIONARY +from sage.sets.pythonclass cimport Set_PythonType, Set_PythonType_class from cpython.object cimport * from cpython.bool cimport * diff --git a/src/sage/symbolic/ring.pyx b/src/sage/symbolic/ring.pyx index 2bfb3aa13f1..5b37859c934 100644 --- a/src/sage/symbolic/ring.pyx +++ b/src/sage/symbolic/ring.pyx @@ -1219,7 +1219,7 @@ cdef class UnderscoreSageMorphism(Morphism): Symbolic Ring """ import sage.categories.homset - from sage.structure.parent import Set_PythonType + from sage.sets.pythonclass import Set_PythonType Morphism.__init__(self, sage.categories.homset.Hom(Set_PythonType(t), R)) cpdef Element _call_(self, a):