Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Map._extra_slots(): do not pass dict
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Dec 14, 2017
1 parent 8d9c008 commit 375e40f
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 275 deletions.
9 changes: 5 additions & 4 deletions src/sage/categories/action.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ cdef class ActionEndomorphism(Morphism):
self._action = action
self._g = g

cdef dict _extra_slots(self, dict _slots):
cdef dict _extra_slots(self):
"""
Helper for pickling and copying.
Expand All @@ -403,9 +403,10 @@ cdef class ActionEndomorphism(Morphism):
sage: psi(x) == phi(x)
True
"""
_slots['_action'] = self._action
_slots['_g'] = self._g
return Morphism._extra_slots(self, _slots)
slots = Morphism._extra_slots(self)
slots['_action'] = self._action
slots['_g'] = self._g
return slots

cdef _update_slots(self, dict _slots):
"""
Expand Down
4 changes: 2 additions & 2 deletions src/sage/categories/map.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ cdef class Map(Element):
cdef public int _coerce_cost # a rough measure of the cost of using this morphism in the coercion system.
# 10 by default, 100 if a DefaultCoercionMorphism, 10000 if inexact.

cdef _update_slots(self, dict _dict)
cdef dict _extra_slots(self, dict _dict)
cdef _update_slots(self, dict)
cdef dict _extra_slots(self)

# these methods require x is an element of domain, and returns an element with parent codomain
cpdef Element _call_(self, x)
Expand Down
81 changes: 34 additions & 47 deletions src/sage/categories/map.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ cdef class Map(Element):
# Element.__copy__ updates the __dict__, but not the slots.
# Let's do this now, but with strong references.
out._parent = self.parent() # self._parent might be None
out._update_slots(self._extra_slots({}))
out._update_slots(self._extra_slots())
return out

def parent(self):
Expand Down Expand Up @@ -364,15 +364,16 @@ cdef class Map(Element):
self.domain = ConstantFunction(D)
self._parent = homset.Hom(D, C, self._category_for)

cdef _update_slots(self, dict _slots):
cdef _update_slots(self, dict slots):
"""
Auxiliary method, used in pickling and copying.
Set various attributes of this map to implement unpickling.
INPUT:
- A dictionary of slots to be updated.
The dictionary must have the keys ``'_domain'`` and ``'_codomain'``,
and may have the key ``'_repr_type_str'``.
- ``slots`` -- A dictionary of slots to be updated.
The dictionary must have the keys ``'_domain'`` and
``'_codomain'``, and may have the keys ``'_repr_type_str'``
and ``'_is_coercion'``.
TESTS:
Expand All @@ -394,21 +395,14 @@ cdef class Map(Element):
# todo: the following can break during unpickling of complex
# objects with circular references! In that case, _slots might
# contain incomplete objects.
self.domain = ConstantFunction(_slots['_domain'])
self._codomain = _slots['_codomain']
self.domain = ConstantFunction(slots['_domain'])
self._codomain = slots['_codomain']
self.codomain = ConstantFunction(self._codomain)

# Several pickles exist without a _repr_type_str, so
# if there is none saved, we just set it to None.
if '_repr_type_str' in _slots:
self._repr_type_str = _slots['_repr_type_str']
else:
self._repr_type_str = None
# Same for _is_coercion
if '_is_coercion' in _slots:
self._is_coercion = _slots['_is_coercion']
else:
self._is_coercion = None
# Several pickles exist without the following, so these are
# optional
self._repr_type_str = slots.get('_repr_type_str')
self._is_coercion = slots.get('_is_coercion')

def _update_slots_test(self, _slots):
"""
Expand All @@ -430,40 +424,31 @@ cdef class Map(Element):
"""
self._update_slots(_slots)

cdef dict _extra_slots(self, dict _slots):
cdef dict _extra_slots(self):
"""
Auxiliary method, used in pickling.
INPUT:
A dictionary.
OUTPUT:
The given dictionary, that is updated by the slots '_domain', '_codomain' and '_repr_type_str'.
Return a dict with attributes to pickle and copy this map.
"""
_slots['_domain'] = self.domain()
_slots['_codomain'] = self._codomain
_slots['_is_coercion'] = self._is_coercion
_slots['_repr_type_str'] = self._repr_type_str
return _slots
return dict(
_domain=self.domain(),
_codomain=self._codomain,
_is_coercion=self._is_coercion,
_repr_type_str=self._repr_type_str)

def _extra_slots_test(self, _slots):
def _extra_slots_test(self):
"""
A Python method to test the cdef _extra_slots method.
TESTS::
sage: from sage.categories.map import Map
sage: f = Map(Hom(QQ, ZZ, Rings()))
sage: f._extra_slots_test({"bla": 1})
sage: f._extra_slots_test()
{'_codomain': Integer Ring,
'_domain': Rational Field,
'_is_coercion': False,
'_repr_type_str': None,
'bla': 1}
'_is_coercion': False,
'_repr_type_str': None}
"""
return self._extra_slots(_slots)
return self._extra_slots()

def __reduce__(self):
"""
Expand All @@ -483,7 +468,7 @@ cdef class Map(Element):
_dict = self.__dict__
else:
_dict = {}
return unpickle_map, (type(self), self.parent(), _dict, self._extra_slots({}))
return unpickle_map, (type(self), self.parent(), _dict, self._extra_slots())

def _repr_type(self):
"""
Expand Down Expand Up @@ -1350,7 +1335,7 @@ cdef class Section(Map):
Map.__init__(self, Hom(map.codomain(), map.domain(), SetsWithPartialMaps()))
self._inverse = map # TODO: Use this attribute somewhere!

cdef dict _extra_slots(self, dict _slots):
cdef dict _extra_slots(self):
"""
Helper for pickling and copying.
Expand All @@ -1365,8 +1350,9 @@ cdef class Section(Map):
From: Multivariate Polynomial Ring in x, y over Rational Field
To: Multivariate Polynomial Ring in x, y over Rational Field
"""
_slots['_inverse'] = self._inverse
return Map._extra_slots(self, _slots)
slots = Map._extra_slots(self)
slots['_inverse'] = self._inverse
return slots

cdef _update_slots(self, dict _slots):
"""
Expand Down Expand Up @@ -1576,7 +1562,7 @@ cdef class FormalCompositeMap(Map):
self.__list = _slots['__list']
Map._update_slots(self, _slots)

cdef dict _extra_slots(self, dict _slots):
cdef dict _extra_slots(self):
"""
Used in pickling and copying.
Expand All @@ -1592,8 +1578,9 @@ cdef class FormalCompositeMap(Map):
sage: m == loads(dumps(m)) # indirect doctest
True
"""
_slots['__list'] = self.__list
return Map._extra_slots(self, _slots)
slots = Map._extra_slots(self)
slots['__list'] = self.__list
return slots

def __richcmp__(self, other, int op):
"""
Expand Down
12 changes: 6 additions & 6 deletions src/sage/categories/morphism.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ cdef class SetMorphism(Morphism):
except Exception:
raise TypeError("Underlying map %s does not accept additional arguments" % type(self._function))

cdef dict _extra_slots(self, dict _slots):
cdef dict _extra_slots(self):
"""
INPUT:
Expand All @@ -584,16 +584,16 @@ cdef class SetMorphism(Morphism):
EXAMPLES::
sage: f = sage.categories.morphism.SetMorphism(Hom(ZZ,ZZ, Sets()), operator.__abs__)
sage: f._extra_slots_test({"bla":1})
sage: f._extra_slots_test()
{'_codomain': Integer Ring,
'_domain': Integer Ring,
'_function': <built-in function __abs__>,
'_is_coercion': False,
'_repr_type_str': None,
'bla': 1}
'_repr_type_str': None}
"""
_slots['_function'] = self._function
return Map._extra_slots(self, _slots)
slots = Map._extra_slots(self)
slots['_function'] = self._function
return slots

cdef _update_slots(self, dict _slots):
"""
Expand Down
7 changes: 4 additions & 3 deletions src/sage/rings/complex_number.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2521,7 +2521,7 @@ cdef class RRtoCC(Map):
self._zero = ComplexNumber(CC, 0)
self._repr_type_str = "Natural"

cdef dict _extra_slots(self, dict _slots):
cdef dict _extra_slots(self):
"""
A helper for pickling and copying.
Expand All @@ -2543,8 +2543,9 @@ cdef class RRtoCC(Map):
From: Real Field with 53 bits of precision
To: Complex Field with 53 bits of precision
"""
_slots['_zero'] = self._zero
return Map._extra_slots(self, _slots)
slots = Map._extra_slots(self)
slots['_zero'] = self._zero
return slots

cdef _update_slots(self, dict _slots):
"""
Expand Down
9 changes: 5 additions & 4 deletions src/sage/rings/finite_rings/integer_mod.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4117,7 +4117,7 @@ cdef class IntegerMod_hom(Morphism):
self.zero = C._element_constructor_(0)
self.modulus = C._pyx_order

cdef dict _extra_slots(self, dict _slots):
cdef dict _extra_slots(self):
"""
Helper for pickling and copying.
Expand All @@ -4139,9 +4139,10 @@ cdef class IntegerMod_hom(Morphism):
sage: psi(R15(7))
2
"""
_slots['zero'] = self.zero
_slots['modulus'] = self.modulus
return Morphism._extra_slots(self, _slots)
slots = Morphism._extra_slots(self)
slots['zero'] = self.zero
slots['modulus'] = self.modulus
return slots

cdef _update_slots(self, dict _slots):
"""
Expand Down
51 changes: 27 additions & 24 deletions src/sage/rings/finite_rings/residue_field.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ cdef class ReductionMap(Map):
self._repr_type_str = "Partially defined reduction"
Map.__init__(self, Hom(K, F, SetsWithPartialMaps()))

cdef dict _extra_slots(self, dict _slots):
cdef dict _extra_slots(self):
"""
Helper for copying and pickling.
Expand All @@ -855,14 +855,15 @@ cdef class ReductionMap(Map):
sage: r(2 + a) == cr(2 + a)
True
"""
_slots['_K'] = self._K
_slots['_F'] = self._F
_slots['_to_vs'] = self._to_vs
_slots['_PBinv'] = self._PBinv
_slots['_to_order'] = self._to_order
_slots['_PB'] = self._PB
_slots['_section'] = self._section
return Map._extra_slots(self, _slots)
slots = Map._extra_slots(self)
slots['_K'] = self._K
slots['_F'] = self._F
slots['_to_vs'] = self._to_vs
slots['_PBinv'] = self._PBinv
slots['_to_order'] = self._to_order
slots['_PB'] = self._PB
slots['_section'] = self._section
return slots

cdef _update_slots(self, dict _slots):
"""
Expand Down Expand Up @@ -1120,7 +1121,7 @@ cdef class ResidueFieldHomomorphism_global(RingHomomorphism):
self._repr_type_str = "Reduction"
RingHomomorphism.__init__(self, Hom(K,F))

cdef dict _extra_slots(self, dict _slots):
cdef dict _extra_slots(self):
"""
Helper for copying and pickling.
Expand All @@ -1140,14 +1141,15 @@ cdef class ResidueFieldHomomorphism_global(RingHomomorphism):
sage: psi(OK.an_element()) == phi(OK.an_element())
True
"""
_slots['_K'] = self._K
_slots['_F'] = self._F
_slots['_to_vs'] = self._to_vs
_slots['_PBinv'] = self._PBinv
_slots['_to_order'] = self._to_order
_slots['_PB'] = self._PB
_slots['_section'] = self._section
return RingHomomorphism._extra_slots(self, _slots)
slots = RingHomomorphism._extra_slots(self)
slots['_K'] = self._K
slots['_F'] = self._F
slots['_to_vs'] = self._to_vs
slots['_PBinv'] = self._PBinv
slots['_to_order'] = self._to_order
slots['_PB'] = self._PB
slots['_section'] = self._section
return slots

cdef _update_slots(self, dict _slots):
"""
Expand Down Expand Up @@ -1357,7 +1359,7 @@ cdef class LiftingMap(Section):
self._PB = PB
Section.__init__(self, reduction)

cdef dict _extra_slots(self, dict _slots):
cdef dict _extra_slots(self):
"""
Helper for copying and pickling.
Expand All @@ -1375,11 +1377,12 @@ cdef class LiftingMap(Section):
sage: phi(F.0) == psi(F.0)
True
"""
_slots['_K'] = self._K
_slots['_F'] = self._F
_slots['_to_order'] = self._to_order
_slots['_PB'] = self._PB
return Section._extra_slots(self, _slots)
slots = Section._extra_slots(self)
slots['_K'] = self._K
slots['_F'] = self._F
slots['_to_order'] = self._to_order
slots['_PB'] = self._PB
return slots

cdef _update_slots(self, dict _slots):
"""
Expand Down
Loading

0 comments on commit 375e40f

Please sign in to comment.