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

Commit caa380b

Browse files
committed
Do not unpack hashable entries of tuples when looking for a cache key for unhashable elements in sage.misc.cachefunc
1 parent 877302e commit caa380b

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/sage/misc/cachefunc.pyx

+17-6
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def _cache_key(o):
474474
This function is intended for objects which are not hashable such as
475475
`p`-adic numbers. The difference from calling an object's ``_cache_key``
476476
method directly, is that it also works for tuples and unpacks them
477-
recursively.
477+
recursively (if necessary, i.e., if they are not hashable).
478478
479479
EXAMPLES::
480480
@@ -492,17 +492,28 @@ def _cache_key(o):
492492
sage: _cache_key(o)
493493
(1, 2, (3, (((1,),), 0, 20)))
494494
495+
Note that tuples are only partially unpacked if some of its entries are
496+
hashable::
497+
498+
sage: o = (matrix([1, 2, 3]), a)
499+
sage: _cache_key(o)
500+
([1 2 3], (((1,),), 0, 20))
501+
495502
.. SEEALSO::
496503
497504
:meth:`sage.structure.sage_object.SageObject._cache_key`
498505
499506
"""
500-
if isinstance(o, sage.structure.sage_object.SageObject):
501-
o = o._cache_key()
502-
if isinstance(o,tuple):
503-
return tuple(_cache_key(item) for item in o)
504-
else:
507+
try:
508+
hash(o)
505509
return o
510+
except TypeError:
511+
if isinstance(o, sage.structure.sage_object.SageObject):
512+
o = o._cache_key()
513+
if isinstance(o,tuple):
514+
return tuple(_cache_key(item) for item in o)
515+
else:
516+
return o
506517

507518
cdef class CachedFunction(object):
508519
"""

0 commit comments

Comments
 (0)