Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/structure/parent_base.pyx
8814 views
1
r"""
2
Base class for old-style parent objects with a base ring
3
"""
4
###############################################################################
5
# Sage: System for Algebra and Geometry Experimentation
6
# Copyright (C) 2006 William Stein <[email protected]>
7
# Distributed under the terms of the GNU General Public License (GPL)
8
# The full text of the GPL is available at:
9
# http://www.gnu.org/licenses/
10
###############################################################################
11
12
include "sage/ext/stdsage.pxi"
13
14
cimport parent
15
16
from coerce_exceptions import CoercionException
17
18
cdef inline check_old_coerce(parent.Parent p):
19
if p._element_constructor is not None:
20
raise RuntimeError, "%s still using old coercion framework" % p
21
22
23
# TODO: Unpickled parents with base sometimes have their base set to None.
24
# This causes a segfault in the module arithmetic architecture.
25
#
26
# sage: H = HomsetWithBase(QQ, RR, base=ZZ); H
27
# sage: H0 = loads(dumps(H))
28
# sage: H.base_ring(), H0.base_ring()
29
# (Integer Ring, None)
30
#
31
# Perhaps the code below would help (why was it commented out?).
32
33
## def make_parent_with_base_v0(_class, _dict, base, has_coerce_map_from):
34
## """
35
## This should work for any Python class deriving from this, as long
36
## as it doesn't implement some screwy __new__() method.
37
## """
38
## new_object = _class.__new__(_class)
39
## if base is None:
40
## (<ParentWithBase>new_object)._base = new_object
41
## else:
42
## (<ParentWithBase>new_object)._base = base
43
## (<ParentWithBase>new_object)._has_coerce_map_from = has_coerce_map_from
44
## if not _dict is None:
45
## new_object.__dict__ = _dict
46
## return new_object
47
48
def is_ParentWithBase(x):
49
"""
50
Return True if x is a parent object with base.
51
"""
52
return bool(PY_TYPE_CHECK(x, ParentWithBase))
53
54
cdef class ParentWithBase(parent_old.Parent):
55
"""
56
This class is being deprecated, see parent.Parent for the new model.
57
"""
58
def __init__(self, base, coerce_from=[], actions=[], embeddings=[], category=None):
59
# TODO: SymbolicExpressionRing has base RR, which makes this bad
60
# print type(self), "base", base, coerce_from
61
# if base != self and not base in coerce_from:
62
# coerce_from.append(base)
63
parent_old.Parent.__init__(self, coerce_from=coerce_from, actions=actions, embeddings=embeddings, category=category)
64
self._base = base
65
66
def _richcmp(left, right, int op):
67
check_old_coerce(left)
68
return (<parent_old.Parent>left)._richcmp(right, op) # the cdef method
69
70
71
cdef _coerce_c_impl(self,x):
72
check_old_coerce(self)
73
if not self._base is self:
74
return self._coerce_try(x,(self._base))
75
else:
76
raise TypeError, "No canonical coercion found."
77
78
## def x__reduce__(self):
79
## if HAS_DICTIONARY(self):
80
## _dict = self.__dict__
81
## else:
82
## _dict = None
83
## if self._base is self:
84
## return (make_parent_with_base_v0, (self.__class__, _dict, None, self._has_coerce_map_from))
85
## else:
86
## return (make_parent_with_base_v0, (self.__class__, _dict, self._base, self._has_coerce_map_from))
87
88
# Derived class *must* define base_extend.
89
def base_extend(self, X):
90
check_old_coerce(self)
91
raise CoercionException, "BUG: the base_extend method must be defined for '%s' (class '%s')"%(
92
self, type(self))
93
94