# HG changeset patch
# User Simon King <simon.king@unijena.de>
# Date 1363337373 3600
# Node ID a0ca4ca2e910564a8da9e9548f256c1d96abed2c
# Parent 54866941f4d98e5a7be6ce829da3e0f719334dc3
#14278: Provide a way to remove all instances of a CachedRepresentation subclass from the cache
diff git a/doc/en/thematic_tutorials/coercion_and_categories.rst b/doc/en/thematic_tutorials/coercion_and_categories.rst
a

b


404  404  
405  405  sage: import inspect 
406  406  sage: len([s for s in dir(MS1) if inspect.ismethod(getattr(MS1,s,None))]) 
407   51 
 407  52 
408  408  sage: len([s for s in dir(MS2) if inspect.ismethod(getattr(MS2,s,None))]) 
409   73 
 409  74 
410  410  sage: MS1.__class__ is MS2.__class__ 
411  411  True 
412  412  
diff git a/sage/structure/unique_representation.py b/sage/structure/unique_representation.py
a

b


450  450  instance._reduction = (cls, args, options) 
451  451  return instance 
452  452  
 453  @classmethod 
 454  def _clear_cache_(cls): 
 455  """ 
 456  Remove all instances of this class from the cache. 
 457  
 458  EXAMPLES: 
 459  
 460  If ``cls`` overloads :meth:`~sage.structure.unique_representation.CachedRepresentation.__classcall__`, 
 461  clearing the cache still works, because ``cls.mro()`` 
 462  is searched until a ``__classcall__`` with an attribute 
 463  ``cache`` is found:: 
 464  
 465  sage: class A(UniqueRepresentation): 
 466  ... def __init__(self, x): 
 467  ... pass 
 468  sage: class B(A): 
 469  ... @staticmethod 
 470  ... def __classcall__(cls, *args, **kwds): 
 471  ... return super(B,cls).__classcall__(cls,*args,**kwds) 
 472  sage: class C(B): pass 
 473  sage: a = A(1) 
 474  sage: b = B(2) 
 475  sage: c = C(3) 
 476  sage: a is A(1) 
 477  True 
 478  sage: b is B(2) 
 479  True 
 480  sage: c is C(3) 
 481  True 
 482  sage: B._clear_cache_() 
 483  
 484  Now, all instances of (subclasses of) ``B`` have disappeared 
 485  from the cache:: 
 486  
 487  sage: a is A(1) 
 488  True 
 489  sage: b is B(2) 
 490  False 
 491  sage: c is C(3) 
 492  False 
 493  
 494  """ 
 495  del_list = [] 
 496  cache = None 
 497  for C in cls.mro(): 
 498  try: 
 499  cache = C.__classcall__.cache 
 500  except AttributeError: 
 501  pass 
 502  for k in cache.iterkeys(): 
 503  if issubclass(k[0][0],cls): 
 504  del_list.append(k) 
 505  for k in del_list: 
 506  del cache[k] 
 507  
453  508  def __reduce__(self): 
454  509  """ 
455  510  Returns the argument that have been passed to :meth:`__new__<object.__new__>` 