Ticket #11521: weak_ref_to_functor.patch

File weak_ref_to_functor.patch, 3.9 KB (added by SimonKing, 9 years ago)

Experimental patch using weak references for actions

  • sage/categories/functor.pxd

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1325178279 -3600
    # Node ID 37e9463019952555098db659704012eb0af9dc3a
    # Parent  d74c47d472c9c71e24c3ad7de50727c282ef60b1
    [mq]: weak_ref_to_functor.patch
    
    diff --git a/sage/categories/functor.pxd b/sage/categories/functor.pxd
    a b  
    11from sage.structure.sage_object cimport SageObject
    22
    33cdef class Functor(SageObject):
     4    cdef __weakref__
    45    cdef object __domain
    5     cdef object __codomain
    6  No newline at end of file
     6    cdef object __codomain
  • sage/structure/coerce.pyx

    diff --git a/sage/structure/coerce.pyx b/sage/structure/coerce.pyx
    a b  
    8888from parent import Set_PythonType
    8989from coerce_exceptions import CoercionException
    9090
    91 import sys, traceback
     91import sys, traceback, weakref
    9292
    9393from coerce_actions import LeftModuleAction, RightModuleAction, IntegerMulAction
    9494
     
    12051205
    12061206        """
    12071207        try:
    1208             return self._action_maps.get(R, S, op)
     1208            out = self._action_maps.get(R, S, op)()
     1209            if out is None:
     1210                action = self.discover_action(R, S, op)
     1211                action = self.verify_action(action, R, S, op)
     1212                if action is not None:
     1213                    self._action_maps.set(R, S, op, weakref.ref(action))
     1214                return action
     1215            return out
    12091216        except KeyError:
    12101217            action = self.discover_action(R, S, op)
    12111218            action = self.verify_action(action, R, S, op)
    1212             self._action_maps.set(R, S, op, action)
     1219            if action is not None:
     1220                self._action_maps.set(R, S, op, weakref.ref(action))
    12131221            return action
    12141222           
    12151223    cpdef verify_action(self, action, R, S, op, bint fix=True):
  • sage/structure/parent.pyx

    diff --git a/sage/structure/parent.pyx b/sage/structure/parent.pyx
    a b  
    802802            self._coerce_from_list = []
    803803            self._coerce_from_hash = {}
    804804            self._action_list = []
    805             self._action_hash = {}
     805            self._action_hash = weakref.WeakValueDictionary()
    806806            self._convert_from_list = []
    807807            self._convert_from_hash = {}
    808808            self._embedding = None
     
    17561756        from sage.categories.action import Action
    17571757        if isinstance(action, Action):
    17581758            if action.actor() is self:
    1759                 self._action_list.append(action)
     1759                self._action_list.append(weakref.ref(action))
    17601760                self._action_hash[action.domain(), action.operation(), action.is_left()] = action
    17611761            elif action.domain() is self:
    1762                 self._action_list.append(action)
     1762                self._action_list.append(weakref.ref(action))
    17631763                self._action_hash[action.actor(), action.operation(), not action.is_left()] = action
    17641764            else:
    17651765                raise ValueError, "Action must involve self"
     
    23112311            # We do NOT add to the list, as this would lead to errors as in
    23122312            # the example above.
    23132313
    2314         self._action_hash[S, op, self_on_left] = action
     2314            # Since weak references are used, we couldn't store
     2315            # the result "None"
     2316            self._action_hash[S, op, self_on_left] = action
    23152317        return action
    23162318       
    23172319
     
    23232325        from sage.categories.homset import Hom
    23242326        from coerce_actions import LeftModuleAction, RightModuleAction
    23252327        cdef Parent R
    2326         for action in self._action_list:
     2328        for ref in self._action_list:
     2329            action = ref()
     2330            if action is None:
     2331                continue
    23272332            if PY_TYPE_CHECK(action, Action) and action.operation() is op:
    23282333                if self_on_left:
    23292334                    if action.left_domain() is not self: continue