Ticket #5970: weakref_poly_ring.patch

File weakref_poly_ring.patch, 2.5 KB (added by Martin Albrecht, 13 years ago)

slightly safer weakref patch

  • sage/rings/polynomial/polynomial_ring_constructor.py

    # HG changeset patch
    # User Martin Albrecht <malb@informatik.uni-bremen.de>
    # Date 1251243593 -3600
    # Node ID 6be4b277e4974b602be3fab42dd1a6809f1b6ce0
    # Parent  a7bcb6ef478e5d63e665a7f86dc9e89f4549fac7
    use weakrefs for cached polynomial rings where it is safe
    
    diff -r a7bcb6ef478e -r 6be4b277e497 sage/rings/polynomial/polynomial_ring_constructor.py
    a b  
    355355def _get_from_cache(key):
    356356    try:
    357357        if _cache.has_key(key):
    358             return _cache[key] #()
     358            r =  _cache[key]
     359            if isinstance(r,weakref.ReferenceType):
     360                return r()
     361            else:
     362                return r
    359363    except TypeError, msg:
    360         raise TypeError, 'key = %s\n%s'%(key,msg)       
     364        raise TypeError, 'key = %s\n%s'%(key,msg)
    361365    return None
    362366
    363 def _save_in_cache(key, R):
     367def _save_in_cache(key, R, weak=False):
    364368    try:
    365         #_cache[key] = weakref.ref(R)
    366          _cache[key] = R
     369        if weak:
     370            _cache[key] = weakref.ref(R)
     371        else:
     372            _cache[key] = R
    367373    except TypeError, msg:
    368374        raise TypeError, 'key = %s\n%s'%(key,msg)
    369375
     
    410416    if hasattr(R, '_implementation_names'):
    411417        for name in R._implementation_names:
    412418            real_key = key[0:3] + (name,)
    413             _save_in_cache(real_key, R)
     419            _save_in_cache(real_key, R, weak=False)
    414420    else:
    415         _save_in_cache(key, R)
     421        _save_in_cache(key, R,  weak=False)
    416422    return R
    417423
    418424def _multi_variate(base_ring, names, n, sparse, order):
     
    429435        return R
    430436
    431437    from sage.rings.polynomial.multi_polynomial_libsingular import MPolynomialRing_libsingular
     438
    432439    if m.integral_domain.is_IntegralDomain(base_ring):
    433440        if n < 1:
    434441            R = m.MPolynomialRing_polydict_domain(base_ring, n, names, order)
     
    443450        except ( TypeError, NotImplementedError ):
    444451            R = m.MPolynomialRing_polydict(base_ring, n, names, order)
    445452
    446     _save_in_cache(key, R)
     453    _save_in_cache(key, R, weak=True)
    447454    return R
    448455
    449456def BooleanPolynomialRing_constructor(n=None, names=None, order="lex"):
     
    520527    from sage.rings.polynomial.pbori import BooleanPolynomialRing
    521528    R = BooleanPolynomialRing(n, names, order)
    522529
    523     _save_in_cache(key, R)
     530    _save_in_cache(key, R, weak=True)
    524531    return R
    525532
    526533