Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 6cbd1fd

Browse files
committed
Trac #31182: doctests added for __setstate__ and __getstate__
1 parent d6d6ba4 commit 6cbd1fd

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/sage/structure/mutability.pyx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,30 @@ cdef class Mutability:
121121
def __getstate__(self):
122122
r"""
123123
Get the current state of ``self`` including the mutability status.
124+
125+
TESTS::
126+
127+
sage: class A(SageObject, Mutability):
128+
....: def __init__(self, val):
129+
....: self._val = val
130+
....: def change(self, val):
131+
....: self._require_mutable()
132+
....: self._val = val
133+
....: def __hash__(self):
134+
....: self._require_immutable()
135+
....: return hash(self._val)
136+
sage: a = A(4)
137+
sage: a.__dict__
138+
{'_val': 4}
139+
sage: a.__getstate__()
140+
{'_is_immutable': False, '_val': 4}
141+
sage: a.__reduce__() # indirect doctest
142+
(<function _reconstructor at ...>,
143+
(<class '__main__.A'>,
144+
<class 'sage.structure.sage_object.SageObject'>,
145+
<sage.structure.sage_object.SageObject object at ...>),
146+
{'_is_immutable': False, '_val': 4})
147+
124148
"""
125149
state = getattr(self, '__dict__', {})
126150
state['_is_immutable'] = self._is_immutable
@@ -130,6 +154,30 @@ cdef class Mutability:
130154
r"""
131155
Set the state of ``self`` from the dictionary ``state`` including the
132156
mutability status.
157+
158+
TESTS::
159+
160+
sage: class A(SageObject, Mutability):
161+
....: def __init__(self, val):
162+
....: self._val = val
163+
....: def change(self, val):
164+
....: self._require_mutable()
165+
....: self._val = val
166+
....: def __hash__(self):
167+
....: self._require_immutable()
168+
....: return hash(self._val)
169+
sage: a = A(4)
170+
sage: a.is_immutable()
171+
False
172+
sage: d = a.__getstate__(); d
173+
{'_is_immutable': False, '_val': 4}
174+
sage: d['_is_immutable'] = True
175+
sage: a.__setstate__(d)
176+
sage: a.is_immutable()
177+
True
178+
sage: a.__getstate__()
179+
{'_is_immutable': True, '_val': 4}
180+
133181
"""
134182
if hasattr(self, '__dict__'):
135183
self.__dict__ = state

0 commit comments

Comments
 (0)