Ticket #715: trac_715-reviewer.patch
File trac_715-reviewer.patch, 11.7 KB (added by , 9 years ago) |
---|
-
sage/categories/action.pyx
# HG changeset patch # User Jean-Pierre Flori <jean-pierre.flor@ssi.gouv.fr> # Date 1331128122 -3600 # Node ID 36c211dc61a6bf79dee93a227a37f23e9dd7de23 # Parent d3c9efea86f7440332b9c43d9dc3e4dafd38a73d #715: Reviewer patch -- minor typos and additional doc diff --git a/sage/categories/action.pyx b/sage/categories/action.pyx
a b 1 1 r""" 2 Group, ring, etc. actions on objects. 2 Group, ring, etc. actions on objects. 3 3 4 The terminology and notation used is suggestive of groups 5 acting on sets, but this framework can be used for modules, 6 algebras, etc. 4 The terminology and notation used is suggestive of groups acting on sets, 5 but this framework can be used for modules, algebras, etc. 7 6 8 A group action $G \times S \rightarrow S$ is a functor from $G$ to Sets. 7 A group action $G \times S \rightarrow S$ is a functor from $G$ to Sets. 9 8 10 AUTHORS: 11 -- Robert Bradshaw: initial version 9 .. WARNING:: 10 11 An :class:`Action` object only keeps a weak reference to the underlying set 12 which is acted upon. This decision was made in :trac:`715` in order to 13 allow garbage collection within the coercion framework (this is where 14 actions are mainly used) and avoid memory leaks. 15 16 :: 17 18 sage: from sage.categories.action import Action 19 sage: class P: pass 20 sage: A = Action(P(),P()) 21 sage: import gc 22 sage: _ = gc.collect() 23 sage: A 24 Traceback (most recent call last): 25 ... 26 RuntimeError: This action acted on a set that became garbage collected 27 28 To avoid garbage collection of the underlying set, it is sufficient to 29 create a strong reference to it before the action is created. 30 31 :: 32 33 sage: _ = gc.collect() 34 sage: from sage.categories.action import Action 35 sage: class P: pass 36 sage: q = P() 37 sage: A = Action(P(),q) 38 sage: gc.collect() 39 0 40 sage: A 41 Left action by <__main__.P instance at ...> on <__main__.P instance at ...> 42 43 AUTHOR: 44 45 - Robert Bradshaw: initial version 12 46 """ 13 47 14 48 #***************************************************************************** … … 130 164 sage: A.left_domain() is R 131 165 True 132 166 133 By trac ticket #715, there is only a weak reference to the underlying134 set.Hence, the underlying set may be garbage collected, even when the167 By :trac:`715`, there is only a weak reference to the underlying set. 168 Hence, the underlying set may be garbage collected, even when the 135 169 action is still alive. This may result in a runtime error, as follows:: 136 170 137 171 sage: from sage.categories.action import Action … … 143 177 Left action by <__main__.P instance at ...> on <__main__.P instance at ...> 144 178 sage: del q 145 179 sage: import gc 146 sage: n= gc.collect()180 sage: _ = gc.collect() 147 181 sage: A 148 182 Traceback (most recent call last): 149 183 ... -
sage/matrix/action.pyx
diff --git a/sage/matrix/action.pyx b/sage/matrix/action.pyx
a b 1 1 """ 2 These are the actions used by the coercion model for matrix and vector multiplications. 2 These are the actions used by the coercion model for matrix and vector 3 multiplications. 3 4 4 AUTHORS: 5 -- Robert Bradshaw (2007-09): Initial version. 5 .. WARNING:: 6 7 The class :class:`MatrixMulAction` and its descendants extends the class 8 :class:`Action`. As a cosnequence objects from these classes only keep weak 9 references to the underlying sets which are acted upon. This decision was 10 made in :trac:`715` in order to allow garbage collection within the coercion 11 framework, where actions are mainly used, and avoid memory leaks. 12 13 To ensure that the underlying set of such an object does not get garbage 14 collected, it is sufficient to explicitely create a strong reference to it 15 before creating the action. 16 17 :: 18 19 sage: MSQ = MatrixSpace(QQ, 2) 20 sage: MSZ = MatrixSpace(ZZ['x'], 2) 21 sage: A = MSQ.get_action(MSZ) 22 sage: A 23 Left action by Full MatrixSpace of 2 by 2 dense matrices over Rational Field on Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Integer Ring 24 sage: import gc 25 sage: _ = gc.collect() 26 sage: A 27 Left action by Full MatrixSpace of 2 by 2 dense matrices over Rational Field on Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Integer Ring 28 29 .. NOTE:: 30 31 The :func:`MatrixSpace` function caches the objects it creates. Therefore, 32 the underlying set ``MSZ`` in the above example will not be garbage 33 collected, even if it is not strongly ref'ed. Nonetheless, there is no 34 guarantee that the set that is acted upon will always be cached in such a 35 way, so that following the above example is good practice. 36 37 AUTHOR: 38 39 - Robert Bradshaw (2007-09): Initial version. 6 40 """ 7 41 8 42 #***************************************************************************** … … 40 74 """ 41 75 EXAMPLES: 42 76 43 By trac ticket #715, there only is a weak reference on the underlying set,44 so that it can be garbage collected if only the action itself is explicitly45 referred to. Hence, we first assign the involved matrix spaces to a46 variable::77 By :trac:`715`, there only is a weak reference on the underlying set, 78 so that it can be garbage collected if only the action itself is 79 explicitly referred to. Hence, we first assign the involved matrix 80 spaces to a variable:: 47 81 48 82 sage: MSQ = MatrixSpace(QQ, 2) 49 83 sage: MSZ = MatrixSpace(ZZ['x'], 2) … … 55 89 Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Integer Ring 56 90 sage: A.codomain() 57 91 Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field 92 93 .. NOTE:: 94 95 The :func:`MatrixSpace` function caches the object it creates. 96 Therefore, the underlying set ``MSZ`` in the above example will not 97 be garbage collected, even if it is not strongly ref'ed. 98 Nonetheless, there is no guarantee that the set that is acted upon 99 will always be cached in such a way, so that following the above 100 example is good practice. 101 58 102 """ 59 103 return self.underlying_set() 60 104 … … 64 108 """ 65 109 EXAMPLES: 66 110 67 By trac ticket #715, there only is a weak reference on the underlying set,68 so that it can be garbage collected if only the action itself is explicitly69 referred to. Hence, we first assign the involved matrix spaces to a70 variable::111 By :trac:`715`, there only is a weak reference on the underlying set, 112 so that it can be garbage collected if only the action itself is 113 explicitly referred to. Hence, we first assign the involved matrix 114 spaces to a variable:: 71 115 72 116 sage: R.<x> = ZZ[] 73 117 sage: MSR = MatrixSpace(R, 3, 3) … … 81 125 [ 0 x] 82 126 [2*x 3*x] 83 127 [4*x 5*x] 128 129 .. NOTE:: 130 131 The :func:`MatrixSpace` function caches the object it creates. 132 Therefore, the underlying set ``MSZ`` in the above example will not 133 be garbage collected, even if it is not strongly ref'ed. 134 Nonetheless, there is no guarantee that the set that is acted upon 135 will always be cached in such a way, so that following the above 136 example is good practice. 137 84 138 """ 85 139 if not is_MatrixSpace(S): 86 140 raise TypeError, "Not a matrix space: %s" % S … … 90 144 """ 91 145 EXAMPLES: 92 146 93 By trac ticket #715, there only is a weak reference on the underlying set,94 so that it can be garbage collected if only the action itself is explicitly95 referred to. Hence, we first assign the involved matrix spaces to a96 variable::147 By :trac:`715`, there only is a weak reference on the underlying set, 148 so that it can be garbage collected if only the action itself is 149 explicitly referred to. Hence, we first assign the involved matrix 150 spaces to a variable:: 97 151 98 152 sage: from sage.matrix.action import MatrixMatrixAction 99 153 sage: R.<x> = ZZ[] … … 103 157 Left action by Full MatrixSpace of 3 by 3 dense matrices over Univariate Polynomial Ring in x over Integer Ring on Full MatrixSpace of 3 by 2 dense matrices over Rational Field 104 158 sage: A.codomain() 105 159 Full MatrixSpace of 3 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field 160 161 .. NOTE:: 162 163 The :func:`MatrixSpace` function caches the object it creates. 164 Therefore, the underlying set ``MSZ`` in the above example will not 165 be garbage collected, even if it is not strongly ref'ed. 166 Nonetheless, there is no guarantee that the set that is acted upon 167 will always be cached in such a way, so that following the above 168 example is good practice. 169 106 170 """ 107 171 if self.G.ncols() != self.underlying_set().nrows(): 108 172 raise TypeError, "incompatible dimensions %s, %s" % (self.G.ncols(), self.underlying_set().nrows()) -
sage/structure/coerce_dict.pyx
diff --git a/sage/structure/coerce_dict.pyx b/sage/structure/coerce_dict.pyx
a b 24 24 """ 25 25 Erases items from a :class:`TripleDict` when a weak reference becomes invalid. 26 26 27 This is of internal use only. Instances of this class will be passed as a callback28 function when creating a weak reference.27 This is of internal use only. Instances of this class will be passed as a 28 callback function when creating a weak reference. 29 29 30 30 EXAMPLES:: 31 31 … … 46 46 47 47 AUTHOR: 48 48 49 Simon King (2012-01)49 - Simon King (2012-01) 50 50 """ 51 51 52 52 def __init__(self, D): … … 64 64 65 65 """ 66 66 self.D = D 67 67 68 def __call__(self, r): 68 69 """ 69 70 INPUT: 70 71 71 72 A weak reference with key. 72 73 73 When this is called with a weak reference ``r``, then each item containing ``r``74 is removed from the associated :class:`TripleDict`. Normally, this only happens75 when a weak reference becomes invalid.74 When this is called with a weak reference ``r``, then each item 75 containing ``r`` is removed from the associated :class:`TripleDict`. 76 Normally, this only happens when a weak reference becomes invalid. 76 77 77 78 EXAMPLES:: 78 79 … … 195 196 Note that this kind of dictionary is also used for caching actions 196 197 and coerce maps. In previous versions of Sage, the cache was by 197 198 strong references and resulted in a memory leak in the following 198 example. However, this leak was fixed by trac ticket #715, using199 weak references::199 example. However, this leak was fixed by trac ticket :trac:`715`, 200 using weak references:: 200 201 201 202 sage: K = GF(1<<55,'t') 202 203 sage: for i in range(50): … … 211 212 sage: len(LE) # indirect doctest 212 213 1 213 214 214 AUTHOR ::215 AUTHORS: 215 216 216 217 - Robert Bradshaw, 2007-08 218 217 219 - Simon King, 2012-01 218 220 """ 219 221 … … 261 263 """ 262 264 The distribution of items in buckets. 263 265 264 OUTPUT: :266 OUTPUT: 265 267 266 268 - (min, avg, max) 267 269 … … 300 302 301 303 OUTPUT: 302 304 303 A list of how many items are in each bucket. 305 A list of how many items are in each bucket. 304 306 305 307 EXAMPLES:: 306 308