Ticket #7585: 7585_8_parent_init.patch

File 7585_8_parent_init.patch, 4.0 KB (added by David Roe, 13 years ago)
  • sage/structure/parent.pyx

    # HG changeset patch
    # User David Roe <roed@math.harvard.edu>
    # Date 1260893171 18000
    # Node ID 45df5909ac43dbd9dbc5f9aef01d25abfb0d9c72
    # Parent  19e9e072f71a609c9737eac697c2ccbbc23f188f
    Makes ParentWithGens call the __init__ method on ParentWithBase.  Changes the init methods on parents so that if _element_constructor_ is implemented, then the parent's _element_constructor field is set to it at object creation time, rather than when __call__ is first called (this was causing some subtle bugs).
    
    diff -r 19e9e072f71a -r 45df5909ac43 sage/structure/parent.pyx
    a b  
    261261                print "Illegal keywords for %s: %s" % (type(self), kwds)
    262262        # TODO: many classes don't call this at all, but __new__ crashes Sage
    263263        if bad_parent_warnings:
    264             if element_constructor is None:
    265                 element_constructor = self._element_constructor_
    266             elif not callable(element_constructor):
     264            if element_constructor is not None and not callable(element_constructor):
    267265                print "coerce BUG: Bad element_constructor provided", type(self), type(element_constructor), element_constructor
    268266        if gens is not None:
    269267            self._populate_generators_(gens, names, normalize)
    270268        elif names is not None:
    271269            self._assign_names(names, normalize)
    272         self._element_constructor = element_constructor
    273         self._element_init_pass_parent = guess_pass_parent(self, element_constructor)
     270        if element_constructor is None:
     271            self._set_element_constructor()
     272        else:
     273            self._element_constructor = element_constructor
     274            self._element_init_pass_parent = guess_pass_parent(self, element_constructor)
    274275        self.init_coerce(False)
    275276
    276277        for cls in self.__class__.mro():
     
    339340        else:
    340341            return cls
    341342
     343    def _set_element_constructor(self):
     344        """
     345        This function is used in translating from the old to the new coercion model.
     346
     347        It is called from sage.structure.parent_old.Parent.__init__ when an old style parent provides a _element_constructor_ method.
     348
     349        It just asserts that this _element_constructor_ is callable and also sets self._element_init_pass_parent
     350
     351        EXAMPLES::
     352
     353            sage: k = GF(5); k._element_constructor # indirect doctest
     354            <bound method FiniteField_prime_modn._element_constructor_ of Finite Field of size 5>
     355        """
     356        if hasattr(self, '_element_constructor_'):
     357            assert callable(self._element_constructor_)
     358            self._element_constructor = self._element_constructor_
     359            self._element_init_pass_parent = guess_pass_parent(self, self._element_constructor)
     360
    342361    def category(self):
    343362        """
    344363        EXAMPLES::
  • sage/structure/parent_gens.pyx

    diff -r 19e9e072f71a -r 45df5909ac43 sage/structure/parent_gens.pyx
    a b  
    254254        self._assign_names(names=names, normalize=normalize)
    255255
    256256        # Why does not this call ParentWithBase.__init__ ?
    257         if category is not None:
    258             self._init_category_(category)
     257        parent_base.ParentWithBase.__init__(self, base, category=category)
     258        #if category is not None:
     259        #    self._init_category_(category)
    259260
    260261##     def x__reduce__(self):
    261262##         if self._base is self:
  • sage/structure/parent_old.pyx

    diff -r 19e9e072f71a -r 45df5909ac43 sage/structure/parent_old.pyx
    a b  
    7272            print mor
    7373            other.init_coerce() # TODO remove when we can
    7474            other._coerce_from_list.append(mor)
    75        
     75
     76        self._set_element_constructor()
     77
    7678        # old
    7779        self._has_coerce_map_from = {}
    7880        if category is not None: