# HG changeset patch
# User Simon King <simon.king@unijena.de>
# Date 1345139157 7200
# Node ID 8a55ce250af80970145c467e6a35bf6794800218
# Parent 425204c38e8702f96ddf29a05e68a8803fcaf871
#11521: Use a callback function, so that an item in the homset cache is removed if the homset dies.
diff git a/sage/categories/homset.py b/sage/categories/homset.py
a

b


73  73  # Use the weak "triple" dictionary 
74  74  # introduced in trac ticket #715 
75  75  
76   import weakref 
 76  from weakref import KeyedRef 
77  77  from sage.structure.coerce_dict import TripleDict 
78  78  _cache = TripleDict(53) 
79  79  
… 
… 

209  209  # However it breaks somehow the coercion (see e.g. sage t sage.rings.real_mpfr) 
210  210  # To be investigated. 
211  211  global _cache 
212   cat_ref = weakref.ref(category) if category is not None else None 
213   key = (X,Y,cat_ref) 
 212  key = (X,Y,category) 
214  213  try: 
215  214  H = _cache[key]() 
216  215  except KeyError: 
217  216  H = None 
218   if H: 
 217  if H is not None: 
219  218  # Are domain or codomain breaking the unique parent condition? 
220  219  if H.domain() is X and H.codomain() is Y: 
221  220  return H 
… 
… 

238  237  else: 
239  238  raise TypeError, "Argument category (= %s) must be a category."%category 
240  239  # Now, as the category may have changed, we try to find the hom set in the cache, again: 
241   cat_ref = weakref.ref(category) if category is not None else None 
242   key = (X,Y,cat_ref) 
 240  key = (X,Y,category) 
243  241  try: 
244  242  H = _cache[key]() 
245  243  except KeyError: 
246  244  H = None 
247   if H: 
 245  if H is not None: 
248  246  # Are domain or codomain breaking the unique parent condition? 
249  247  if H.domain() is X and H.codomain() is Y: 
250  248  return H 
… 
… 

260  258  H = category.hom_category().parent_class(X, Y, category = category) 
261  259  
262  260  ##_cache[key] = weakref.ref(H) 
263   _cache[key] = weakref.ref(H) 
 261  _cache[key] = KeyedRef(H, _cache.eraser, (id(X),id(Y),id(category))) 
264  262  return H 
265  263  
266  264  def hom(X, Y, f): 
diff git a/sage/structure/coerce_dict.pxd b/sage/structure/coerce_dict.pxd
a

b


3  3  cdef buckets 
4  4  cdef dict _refcache 
5  5  cdef double threshold 
6   cdef TripleDictEraser eraser 
 6  cdef public TripleDictEraser eraser 
7  7  cdef get(self, object k1, object k2, object k3) 
8  8  cdef set(self, object k1, object k2, object k3, value) 
9  9  