r"""1Base class for old-style parent objects with a base ring2"""3###############################################################################4# Sage: System for Algebra and Geometry Experimentation5# Copyright (C) 2006 William Stein <[email protected]>6# Distributed under the terms of the GNU General Public License (GPL)7# The full text of the GPL is available at:8# http://www.gnu.org/licenses/9###############################################################################1011include "sage/ext/stdsage.pxi"1213cimport parent1415from coerce_exceptions import CoercionException1617cdef inline check_old_coerce(parent.Parent p):18if p._element_constructor is not None:19raise RuntimeError, "%s still using old coercion framework" % p202122# TODO: Unpickled parents with base sometimes have their base set to None.23# This causes a segfault in the module arithmetic architecture.24#25# sage: H = HomsetWithBase(QQ, RR, base=ZZ); H26# sage: H0 = loads(dumps(H))27# sage: H.base_ring(), H0.base_ring()28# (Integer Ring, None)29#30# Perhaps the code below would help (why was it commented out?).3132## def make_parent_with_base_v0(_class, _dict, base, has_coerce_map_from):33## """34## This should work for any Python class deriving from this, as long35## as it doesn't implement some screwy __new__() method.36## """37## new_object = _class.__new__(_class)38## if base is None:39## (<ParentWithBase>new_object)._base = new_object40## else:41## (<ParentWithBase>new_object)._base = base42## (<ParentWithBase>new_object)._has_coerce_map_from = has_coerce_map_from43## if not _dict is None:44## new_object.__dict__ = _dict45## return new_object4647def is_ParentWithBase(x):48"""49Return True if x is a parent object with base.50"""51return bool(PY_TYPE_CHECK(x, ParentWithBase))5253cdef class ParentWithBase(parent_old.Parent):54"""55This class is being deprecated, see parent.Parent for the new model.56"""57def __init__(self, base, coerce_from=[], actions=[], embeddings=[], category=None):58# TODO: SymbolicExpressionRing has base RR, which makes this bad59# print type(self), "base", base, coerce_from60# if base != self and not base in coerce_from:61# coerce_from.append(base)62parent_old.Parent.__init__(self, coerce_from=coerce_from, actions=actions, embeddings=embeddings, category=category)63self._base = base6465def _richcmp(left, right, int op):66check_old_coerce(left)67return (<parent_old.Parent>left)._richcmp(right, op) # the cdef method686970cdef _coerce_c_impl(self,x):71check_old_coerce(self)72if not self._base is self:73return self._coerce_try(x,(self._base))74else:75raise TypeError, "No canonical coercion found."7677## def x__reduce__(self):78## if HAS_DICTIONARY(self):79## _dict = self.__dict__80## else:81## _dict = None82## if self._base is self:83## return (make_parent_with_base_v0, (self.__class__, _dict, None, self._has_coerce_map_from))84## else:85## return (make_parent_with_base_v0, (self.__class__, _dict, self._base, self._has_coerce_map_from))8687# Derived class *must* define base_extend.88def base_extend(self, X):89check_old_coerce(self)90raise CoercionException, "BUG: the base_extend method must be defined for '%s' (class '%s')"%(91self, type(self))929394