Ticket #11521: trac_11521_callback.patch

File trac_11521_callback.patch, 2.4 KB (added by SimonKing, 7 years ago)

Use a callback, to make sure that items in the homset cache are deleted if the homset is garbage collected

  • sage/categories/homset.py

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.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  
    7373# Use the weak "triple" dictionary
    7474# introduced in trac ticket #715
    7575
    76 import weakref
     76from weakref import KeyedRef
    7777from sage.structure.coerce_dict import TripleDict
    7878_cache = TripleDict(53)
    7979
     
    209209    # However it breaks somehow the coercion (see e.g. sage -t sage.rings.real_mpfr)
    210210    # To be investigated.
    211211    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)
    214213    try:
    215214        H = _cache[key]()
    216215    except KeyError:
    217216        H = None
    218     if H:
     217    if H is not None:
    219218        # Are domain or codomain breaking the unique parent condition?
    220219        if H.domain() is X and H.codomain() is Y:
    221220            return H
     
    238237    else:
    239238        raise TypeError, "Argument category (= %s) must be a category."%category
    240239    # 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)
    243241    try:
    244242        H = _cache[key]()
    245243    except KeyError:
    246244        H = None
    247     if H:
     245    if H is not None:
    248246        # Are domain or codomain breaking the unique parent condition?
    249247        if H.domain() is X and H.codomain() is Y:
    250248            return H
     
    260258    H = category.hom_category().parent_class(X, Y, category = category)
    261259           
    262260    ##_cache[key] = weakref.ref(H)
    263     _cache[key] = weakref.ref(H)
     261    _cache[key] = KeyedRef(H, _cache.eraser, (id(X),id(Y),id(category)))
    264262    return H
    265263
    266264def hom(X, Y, f):
  • sage/structure/coerce_dict.pxd

    diff --git a/sage/structure/coerce_dict.pxd b/sage/structure/coerce_dict.pxd
    a b  
    33    cdef buckets
    44    cdef dict _refcache
    55    cdef double threshold
    6     cdef TripleDictEraser eraser
     6    cdef public TripleDictEraser eraser
    77    cdef get(self, object k1, object k2, object k3)
    88    cdef set(self, object k1, object k2, object k3, value)
    99