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

Commit

Permalink
Trac #31182: doctests added for __setstate__ and __getstate__
Browse files Browse the repository at this point in the history
  • Loading branch information
mjungmath committed Jan 18, 2021
1 parent d6d6ba4 commit 6cbd1fd
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/sage/structure/mutability.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ cdef class Mutability:
def __getstate__(self):
r"""
Get the current state of ``self`` including the mutability status.
TESTS::
sage: class A(SageObject, Mutability):
....: def __init__(self, val):
....: self._val = val
....: def change(self, val):
....: self._require_mutable()
....: self._val = val
....: def __hash__(self):
....: self._require_immutable()
....: return hash(self._val)
sage: a = A(4)
sage: a.__dict__
{'_val': 4}
sage: a.__getstate__()
{'_is_immutable': False, '_val': 4}
sage: a.__reduce__() # indirect doctest
(<function _reconstructor at ...>,
(<class '__main__.A'>,
<class 'sage.structure.sage_object.SageObject'>,
<sage.structure.sage_object.SageObject object at ...>),
{'_is_immutable': False, '_val': 4})
"""
state = getattr(self, '__dict__', {})
state['_is_immutable'] = self._is_immutable
Expand All @@ -130,6 +154,30 @@ cdef class Mutability:
r"""
Set the state of ``self`` from the dictionary ``state`` including the
mutability status.
TESTS::
sage: class A(SageObject, Mutability):
....: def __init__(self, val):
....: self._val = val
....: def change(self, val):
....: self._require_mutable()
....: self._val = val
....: def __hash__(self):
....: self._require_immutable()
....: return hash(self._val)
sage: a = A(4)
sage: a.is_immutable()
False
sage: d = a.__getstate__(); d
{'_is_immutable': False, '_val': 4}
sage: d['_is_immutable'] = True
sage: a.__setstate__(d)
sage: a.is_immutable()
True
sage: a.__getstate__()
{'_is_immutable': True, '_val': 4}
"""
if hasattr(self, '__dict__'):
self.__dict__ = state
Expand Down

0 comments on commit 6cbd1fd

Please sign in to comment.