Skip to content

Commit c789ae6

Browse files
author
Release Manager
committed
sagemathgh-40874: Support methods with do_pickle=True for objects with unique representation behavior fixes sagemath#28096. <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> The doctest failure https://github.com/sagemath/sage/actions/runs/179402 71644/job/51014975901?pr=40874 is not related with this PR branch. What the PR branch does is explained clearly in sagemath#28096 (comment) ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#40874 Reported by: Kwankyu Lee Reviewer(s): Kwankyu Lee, Simon King, Vincent Macri
2 parents 5507b44 + f62defe commit c789ae6

File tree

5 files changed

+261
-81
lines changed

5 files changed

+261
-81
lines changed

src/sage/combinat/species/species.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def __ne__(self, other):
182182
"""
183183
return not (self == other)
184184

185-
def __getstate__(self):
185+
def _getstate_(self):
186186
r"""
187187
This is used during the pickling process and returns a dictionary
188188
of the data needed to create this object during the unpickling
@@ -194,7 +194,7 @@ def __getstate__(self):
194194
EXAMPLES::
195195
196196
sage: C = species.CharacteristicSpecies(5)
197-
sage: args, kwds = C.__getstate__()
197+
sage: args, kwds = C._getstate_()
198198
sage: args
199199
{0: 5}
200200
sage: sorted(kwds.items())
@@ -206,18 +206,18 @@ def __getstate__(self):
206206
except AttributeError:
207207
return ({}, kwds)
208208

209-
def __setstate__(self, state):
209+
def _setstate_(self, state):
210210
"""
211211
This is used during unpickling to recreate this object from the
212-
data provided by the ``__getstate__`` method.
212+
data provided by the ``_getstate_`` method.
213213
214214
TESTS::
215215
216216
sage: C2 = species.CharacteristicSpecies(2)
217217
sage: C4 = species.CharacteristicSpecies(4)
218218
sage: C2
219219
Characteristic species of order 2
220-
sage: C2.__setstate__(C4.__getstate__()); C2
220+
sage: C2._setstate_(C4._getstate_()); C2
221221
Characteristic species of order 4
222222
"""
223223
args_dict, kwds = state
@@ -235,7 +235,7 @@ def weighted(self, weight):
235235
sage: C.weighted(t)
236236
Cyclic permutation species with weight=t
237237
"""
238-
args_dict, kwds = self.__getstate__()
238+
args_dict, kwds = self._getstate_()
239239
kwds.update({'weight': weight})
240240
return self.__class__(*[args_dict[i] for i in range(len(args_dict))], **kwds)
241241

src/sage/misc/cachefunc.pyx

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
r"""
22
Cached Functions and Methods
33
4-
AUTHORS:
5-
6-
- William Stein: initial version, (inspired by conversation with Justin Walker)
7-
- Mike Hansen: added doctests and made it work with class methods.
8-
- Willem Jan Palenstijn: add CachedMethodCaller for binding cached methods to
9-
instances.
10-
- Tom Boothby: added DiskCachedFunction.
11-
- Simon King: improved performance, more doctests, cython version,
12-
CachedMethodCallerNoArgs, weak cached function, cached special methods.
13-
- Julian Rueth (2014-03-19, 2014-05-09, 2014-05-12): added ``key`` parameter, allow caching
14-
for unhashable elements, added ``do_pickle`` parameter
15-
164
EXAMPLES:
175
186
By :issue:`11115`, cached functions and methods are now also
@@ -429,8 +417,20 @@ Note that shallow copy of mutable objects may behave unexpectedly::
429417
2
430418
sage: b.f is a.f
431419
False
432-
"""
433420
421+
AUTHORS:
422+
423+
- William Stein: initial version, (inspired by conversation with Justin Walker)
424+
- Mike Hansen: added doctests and made it work with class methods
425+
- Willem Jan Palenstijn: added ``CachedMethodCaller`` for binding cached methods to
426+
instances
427+
- Tom Boothby: added ``DiskCachedFunction``
428+
- Simon King: improved performance, more doctests, cython version,
429+
``CachedMethodCallerNoArgs``, weak cached function, cached special methods
430+
- Julian Rueth (2014-03-19, 2014-05-09, 2014-05-12): added ``key`` parameter, allow caching
431+
for unhashable elements, added ``do_pickle`` parameter
432+
- Kwankyu Lee (2025-10): added ``is_pickled_with_cache`` method
433+
"""
434434
# ****************************************************************************
435435
# Copyright (C) 2008 William Stein <[email protected]>
436436
# Mike Hansen <[email protected]>
@@ -863,11 +863,31 @@ cdef class CachedFunction():
863863
"""
864864
return _cached_function_unpickle, (self.__cached_module__, self.__name__, self.cache)
865865

866-
#########
867-
# Introspection
866+
def is_pickled_with_cache(self):
867+
"""
868+
Return ``True`` if this cached function is pickled with the cache.
869+
870+
EXAMPLES::
871+
872+
sage: @cached_function
873+
....: def f(x):
874+
....: return x
875+
....:
876+
sage: f.is_pickled_with_cache()
877+
False
878+
sage: @cached_function(do_pickle=True)
879+
....: def f(x):
880+
....: return x
881+
....:
882+
sage: f.is_pickled_with_cache()
883+
True
884+
"""
885+
return self.do_pickle
886+
887+
# Introspection
868888
#
869-
# We provide some methods explicitly, and
870-
# forward other questions to the cached function.
889+
# We provide some methods explicitly, and forward
890+
# other questions to the cached function.
871891

872892
def _instancedoc_(self):
873893
"""
@@ -1650,7 +1670,7 @@ class CachedMethodPickle():
16501670
if isinstance(CM, CachedMethodCallerNoArgs):
16511671
CM.cache = self._cache
16521672
else:
1653-
for k, v in self._cache:
1673+
for k, v in self._cache.items():
16541674
CM.cache[k] = v
16551675
return CM(*args, **kwds)
16561676

@@ -1688,7 +1708,7 @@ class CachedMethodPickle():
16881708
if isinstance(CM, CachedMethodCallerNoArgs):
16891709
CM.cache = self._cache
16901710
else:
1691-
for k, v in self._cache:
1711+
for k, v in self._cache.items():
16921712
CM.cache[k] = v
16931713
return getattr(CM, s)
16941714

@@ -1802,10 +1822,9 @@ cdef class CachedMethodCaller(CachedFunction):
18021822
sage: J.groebner_basis
18031823
Cached version of <function ...groebner_basis at 0x...>
18041824
"""
1805-
if isinstance(self._cachedmethod, CachedInParentMethod) or hasattr(self._instance, self._cachedmethod._cache_name):
1806-
return CachedMethodPickle, (self._instance, self.__name__)
1807-
else:
1825+
if self.do_pickle:
18081826
return CachedMethodPickle, (self._instance, self.__name__, self.cache)
1827+
return CachedMethodPickle, (self._instance, self.__name__)
18091828

18101829
def _instance_call(self, *args, **kwds):
18111830
"""

src/sage/rings/function_field/function_field_polymod.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1845,7 +1845,7 @@ def exact_constant_field(self, name='t'):
18451845

18461846
return k_ext, embedding
18471847

1848-
@cached_method
1848+
@cached_method(do_pickle=True)
18491849
def genus(self) -> Integer:
18501850
"""
18511851
Return the genus of the function field.

0 commit comments

Comments
 (0)