This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
weak_dict.pyx
1292 lines (1064 loc) · 41.1 KB
/
weak_dict.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Fast and safe weak value dictionary
AUTHORS:
- Simon King (2013-10)
- Nils Bruin (2013-10)
- Julian Rueth (2014-03-16): improved handling of unhashable objects
Python's :mod:`weakref` module provides
:class:`~weakref.WeakValueDictionary`. This behaves similar to a dictionary,
but it does not prevent its values from garbage collection. Hence, it stores
the values by weak references with callback functions: The callback function
deletes a key-value pair from the dictionary, as soon as the value becomes
subject to garbage collection.
However, a problem arises if hash and comparison of the key depend on the
value that is being garbage collected::
sage: import weakref
sage: class Vals(object): pass
sage: class Keys:
....: def __init__(self, val):
....: self.val = weakref.ref(val)
....: def __hash__(self):
....: return hash(self.val())
....: def __eq__(self, other):
....: return self.val() == other.val()
sage: ValList = [Vals() for _ in range(10)]
sage: D = weakref.WeakValueDictionary()
sage: for v in ValList:
....: D[Keys(v)] = v
sage: len(D)
10
sage: del ValList, v
Exception KeyError: (<__main__.Keys instance at ...>,) in <function remove at ...> ignored
Exception KeyError: (<__main__.Keys instance at ...>,) in <function remove at ...> ignored
Exception KeyError: (<__main__.Keys instance at ...>,) in <function remove at ...> ignored
Exception KeyError: (<__main__.Keys instance at ...>,) in <function remove at ...> ignored
...
sage: len(D) > 1
True
Hence, there are scary error messages, and moreover the defunct items have not
been removed from the dictionary.
Therefore, Sage provides an alternative implementation
:class:`sage.misc.weak_dict.WeakValueDictionary`, using a callback that
removes the defunct item not based on hash and equality check of the key (this
is what fails in the example above), but based on comparison by identity. This
is possible, since references with callback function are distinct even if they
point to the same object. Hence, even if the same object ``O`` occurs as value
for several keys, each reference to ``O`` corresponds to a unique key. We see
no error messages, and the items get correctly removed::
sage: ValList = [Vals() for _ in range(10)]
sage: import sage.misc.weak_dict
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: for v in ValList:
....: D[Keys(v)] = v
sage: len(D)
10
sage: del ValList
sage: len(D)
1
sage: del v
sage: len(D)
0
Another problem arises when iterating over the items of a dictionary: If
garbage collection occurs during iteration, then the content of the dictionary
changes, and the iteration breaks for :class:`weakref.WeakValueDictionary`::
sage: class Cycle:
....: def __init__(self):
....: self.selfref = self
sage: C = [Cycle() for n in range(10)]
sage: D = weakref.WeakValueDictionary(enumerate(C))
sage: import gc
sage: gc.disable()
sage: del C[:5]
sage: len(D)
10
sage: for k in D.iterkeys():
....: gc.enable()
....: _ = gc.collect()
Traceback (most recent call last):
...
RuntimeError: dictionary changed size during iteration
With :class:`~sage.misc.weak_dict.WeakValueDictionary`, the behaviour is
safer. Note that iteration over a WeakValueDictionary is non-deterministic,
since the lifetime of values (and hence the presence of keys) in the dictionary
may depend on when garbage collection occurs. The method implemented here
will at least postpone dictionary mutations due to garbage collection callbacks.
This means that as long as there is at least one iterator active on a dictionary,
none of its keys will be deallocated (which could have side-effects).
Which entries are returned is of course still dependent on when garbage
collection occurs. Note that when a key gets returned as "present" in the
dictionary, there is no guarantee one can actually retrieve its value: it may
have been garbage collected in the mean time.
Note that Sage's weak value dictionary is actually an instance of
:class:`dict`, in contrast to :mod:`weakref`'s weak value dictionary::
sage: issubclass(weakref.WeakValueDictionary, dict)
False
sage: issubclass(sage.misc.weak_dict.WeakValueDictionary, dict)
True
See :trac:`13394` for a discussion of some of the design considerations.
"""
########################################################################
# Copyright (C) 2013 Simon King <simon.king@uni-jena.de>
# Nils Bruin <nbruin@sfu.ca>
# Julian Rueth <julian.rueth@fsfe.org>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# http://www.gnu.org/licenses/
########################################################################
import weakref
from weakref import KeyedRef
from copy import deepcopy
from cpython.dict cimport *
from cpython.weakref cimport *
from cpython.list cimport *
from cpython cimport Py_XINCREF, Py_XDECREF
cdef extern from "Python.h":
ctypedef struct PyDictEntry:
Py_ssize_t me_hash
PyObject* me_key
PyObject* me_value
ctypedef struct PyDictObject:
Py_ssize_t ma_fill
Py_ssize_t ma_used
Py_ssize_t ma_mask
PyDictEntry* ma_table
PyDictEntry* (*ma_lookup)(PyDictObject *mp, PyObject *key, long hash)
PyObject* Py_None
#we need this redefinition because we want to be able to call
#PyWeakref_GetObject with borrowed references. This is the recommended
#strategy according to Cython/Includes/cpython/__init__.pxd
PyObject* PyWeakref_GetObject(PyObject * wr)
int PyList_SetItem(object list, Py_ssize_t index,PyObject * item) except -1
#this one's just missing.
long PyObject_Hash(object obj)
cdef PyObject* PyDict_GetItemWithError(dict op, object key) except? NULL:
cdef PyDictEntry* ep
cdef PyDictObject* mp = <PyDictObject*><void *>op
ep = mp.ma_lookup(mp, <PyObject*><void*>key, PyObject_Hash(key))
if ep:
return ep.me_value
else:
return NULL
#this routine extracts the "dummy" sentinel value that is used in dicts to mark
#"freed" slots. We need that to delete things ourselves.
cdef PyObject* init_dummy() except NULL:
cdef dict D = dict()
cdef PyDictObject* mp = <PyDictObject *><void *>D
cdef size_t mask
cdef PyDictEntry* ep0 = mp.ma_table
cdef PyDictEntry* ep
cdef size_t i
global dummy
D[0]=0; del D[0] #ensure that there is a "deleted" entry in the dict
mask = mp.ma_mask
#since our entry had hash 0, we should really succeed on the first iteration
for i in range(mask+1):
ep = &(ep0[i])
if ep.me_key != NULL:
return ep.me_key
raise RuntimeError("Problem initializing dummy")
#note that dummy here is a borrowed reference. That's not a problem because
#we're never giving it up and dictobject.c is also holding a permanent reference
#to this object
cdef PyObject* dummy = init_dummy()
#this routine looks for the first entry in dict D with given hash of the
#key and given (identical!) value and deletes that entry.
cdef del_dictitem_by_exact_value(PyDictObject *mp, PyObject *value, long hash):
"""
This is used in callbacks for the weak values of :class:`WeakValueDictionary`.
INPUT:
- ``PyDictObject *mp`` -- pointer to a dict
- ``PyObject *value`` -- pointer to a value of the dictionary
- ``long hash`` -- hash of the key by which the value is stored in the dict
The hash bucket determined by the given hash is searched for the item
containing the given value. If this item can't be found, the function is
silently returning. Otherwise, the item is removed from the dict.
TESTS:
The following is an indirect doctest, as discussed on :trac:`13394`.
::
sage: from sage.misc.weak_dict import WeakValueDictionary
sage: V = [set(range(n)) for n in range(5)]
sage: D = WeakValueDictionary(enumerate(V))
The line ``V[k] = None`` triggers execution of the callback functions of
the dict values. However, the actual deletion is postponed till after the
iteration over the dictionary has finished. Hence, when the callbacks are
executed, the values which the callback belongs to has already been
overridded by a new value. Therefore, the callback does not delete the
item::
sage: for k in D.iterkeys(): # indirect doctest
....: V[k] = None
....: D[k] = ZZ
sage: len(D)
5
sage: D[1]
Integer Ring
TESTS:
The following shows that the deletion of deeply nested structures does not
result in an error, by :trac:`15506`::
sage: class A: pass
sage: a = A(); prev = a
sage: M = WeakValueDictionary()
sage: for i in range(10^3+10): newA = A(); M[newA] = prev; prev = newA
sage: del a
"""
cdef size_t i
cdef size_t perturb
cdef size_t mask = <size_t> mp.ma_mask
cdef PyDictEntry* ep0 = mp.ma_table
cdef PyDictEntry* ep
i = hash & mask
ep = &(ep0[i])
if ep.me_key == NULL:
# key not found
return
perturb = hash
while (<PyObject *>(ep.me_value) != value or ep.me_hash != hash):
i = (i << 2) + i + perturb +1
ep = &ep0[i & mask]
if ep.me_key == NULL:
# key not found
return
perturb = perturb >> 5 #this is the value of PERTURB_SHIFT
T=PyList_New(2)
PyList_SetItem(T,0,ep.me_key)
if dummy == NULL:
raise RuntimeError("dummy needs to be initialized")
Py_XINCREF(dummy)
ep.me_key = dummy
PyList_SetItem(T,1,ep.me_value)
ep.me_value = NULL
mp.ma_used -= 1
#We have transferred the to-be-deleted references to the list T
#we now delete the list so that the actual decref happens through a
#deallocation routine that uses the Python Trashcan macros to
#avoid stack overflow in deleting deep structures.
del T
def test_del_dictitem_by_exact_value(D, value, h):
"""
This function helps testing some cdef function used to delete dictionary items.
INPUT:
- ``D`` -- a Python ``<dict>``.
- ``value`` -- an object that is value ``D``.
- ``h`` -- the hash of the key under which to find ``value`` in ``D``.
The underlying cdef function deletes an item from ``D`` that is in the
hash bucket determined by ``h`` and whose value is identic with
``value``. Of course, this only makes sense if the pairs ``(h, value)``
corresponding to items in ``D`` are pair-wise distinct.
If a matching item can not be found, the function does nothing and
silently returns.
TESTS:
See :trac:`13394` for a discussion.
::
sage: from sage.misc.weak_dict import test_del_dictitem_by_exact_value
sage: B=1000
sage: L=list(range(B))
sage: D1=dict()
sage: D2=dict()
sage: for i in range(100000): # long time
....: ki=L[floor(random()*B)]
....: vi=L[floor(random()*B)]
....: D1[ki]=vi
....: D2[ki]=vi
....: ko=L[floor(random()*B)]
....: if ko in D1:
....: vo=D1[ko]
....: del D1[ko]
....: test_del_dictitem_by_exact_value(D2,vo,hash(ko))
....: assert D1 == D2
No action is taken if the item prescribed by key hash and value does not
exist in the dictionary::
sage: D = {1: ZZ}
sage: test_del_dictitem_by_exact_value(D, ZZ, 2)
sage: D
{1: Integer Ring}
sage: test_del_dictitem_by_exact_value(D, QQ, 1)
sage: D
{1: Integer Ring}
"""
return del_dictitem_by_exact_value(<PyDictObject *>D, <PyObject *>value, h)
cdef class WeakValueDictEraser:
"""
Erases items from a :class:`sage.misc.weak_dict.WeakValueDictionary` when
a weak reference becomes invalid.
This is of internal use only. Instances of this class will be passed as a
callback function when creating a weak reference.
EXAMPLES::
sage: from sage.misc.weak_dict import WeakValueDictionary
sage: v = frozenset([1])
sage: D = WeakValueDictionary({1 : v})
sage: len(D)
1
sage: del v
sage: len(D)
0
AUTHOR:
- Nils Bruin (2013-11)
"""
cdef D
def __init__(self, D):
"""
INPUT:
A :class:`sage.misc.weak_dict.WeakValueDictionary`.
EXAMPLES::
sage: v = frozenset([1])
sage: D = sage.misc.weak_dict.WeakValueDictionary({ 1 : v })
sage: len(D)
1
sage: del v
sage: len(D) #indirect doctest
0
"""
self.D = PyWeakref_NewRef(D,None)
def __call__(self, r):
"""
INPUT:
A weak reference with key.
When this is called with a weak reference ``r``, then an entry from the
dictionary pointed to by ``self.D`` is removed that has ``r`` as a value
identically, stored under a key with hash ``r.key``. If no such key
exists, or if the dictionary itself doesn't exist any more, then nothing
happens.
If the dictionary has an iterator active on it then the object is
queued for removal when all iterators have concluded.
EXAMPLES::
sage: v = frozenset([1])
sage: D = sage.misc.weak_dict.WeakValueDictionary({ 1 : v })
sage: len(D)
1
sage: del v
sage: len(D) #indirect doctest
0
"""
cdef WeakValueDictionary D = <object> PyWeakref_GetObject(<PyObject*> self.D)
if D is None:
return
#The situation is the following:
#in the underlying dictionary, we have stored a KeyedRef r
#under a key k. The attribute r.key is the hash of k.
if D._guard_level:
D._pending_removals.append(r)
else:
del_dictitem_by_exact_value(<PyDictObject *>D, <PyObject *>r, r.key)
cdef class WeakValueDictionary(dict):
"""
IMPLEMENTATION:
The :class:`WeakValueDictionary` inherits from :class:`dict`. In its
implementation, it stores weakrefs to the actual values under the keys.
All access routines are wrapped to transparently place and remove these
weakrefs.
NOTE:
In contrast to :class:`weakref.WeakValueDictionary` in Python's
:mod:`weakref` module, the callback does not need to assume that the
dictionary key is a valid Python object when it is called. There is no
need to compute the hash or compare the dictionary keys. This is why
the example below would not work with
:class:`weakref.WeakValueDictionary`, but does work with
:class:`sage.misc.weak_dict.WeakValueDictionary`.
EXAMPLES::
sage: import weakref
sage: class Vals(object): pass
sage: class Keys:
....: def __init__(self, val):
....: self.val = weakref.ref(val)
....: def __hash__(self):
....: return hash(self.val())
....: def __eq__(self, other):
....: return self.val() == other.val()
sage: ValList = [Vals() for _ in range(10)]
sage: import sage.misc.weak_dict
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: for v in ValList:
....: D[Keys(v)] = v
sage: len(D)
10
sage: del ValList
sage: len(D)
1
sage: del v
sage: len(D)
0
TESTS:
The following reflects the behaviour of the callback on weak dict values,
as discussed on :trac:`13394`. ::
sage: from sage.misc.weak_dict import WeakValueDictionary
sage: V = [set(range(n)) for n in range(5)]
sage: D = WeakValueDictionary(enumerate(V))
The line ``V[k] = None`` triggers execution of the callback functions of
the dictionary values. However, the actual deletion is postponed till
after the iteration over the dictionary has finished. Hence, when the
callbacks are executed, the values which the callback belongs to has
already been overridded by a new value. Therefore, the callback does not
delete the item::
sage: for k in D.iterkeys(): # indirect doctest
....: V[k] = None
....: D[k] = ZZ
sage: len(D)
5
sage: D[1]
Integer Ring
The following is a stress test for weak value dictionaries::
sage: class C(object):
....: def __init__(self, n):
....: self.n = n
....: def __cmp__(self, other):
....: return cmp(type(self),type(other)) or cmp(self.n, other.n)
sage: B = 100
sage: L = [None]*B
sage: D1 = WeakValueDictionary()
sage: D2 = WeakValueDictionary()
sage: for i in range(10000):
....: ki = floor(random()*B)
....: vi = C(floor(random()*B))
....: D1[ki] = vi
....: D2[ki] = vi
....: L[ki] = vi
....: del vi
....: ko = floor(random()*B)
....: if ko in D1:
....: del D1[ko]
....: L[ko] = None
....: assert D1 == D2
"""
cdef __weakref__
cdef callback
cdef int _guard_level
cdef list _pending_removals
def __init__(self, data=()):
"""
Create a :class:`WeakValueDictionary` with given initial data.
INPUT:
Optional iterable of key-value pairs
EXAMPLES::
sage: L = [(p,GF(p)) for p in prime_range(10)]
sage: import sage.misc.weak_dict
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: len(D)
0
sage: D = sage.misc.weak_dict.WeakValueDictionary(L)
sage: len(D) == len(L)
True
"""
dict.__init__(self)
self.callback = WeakValueDictEraser(self)
self._guard_level = 0
self._pending_removals = []
try:
data=data.iteritems()
except AttributeError:
pass
for k,v in data:
self[k] = v
def __copy__(self):
"""
Return a copy of this weak dictionary.
EXAMPLES::
sage: import sage.misc.weak_dict
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: D[1] = QQ
sage: D[2] = ZZ
sage: D[None] = CC
sage: E = copy(D) # indirect doctest
sage: set(E.items()) == set(D.items())
True
"""
return WeakValueDictionary(self.iteritems())
def __deepcopy__(self, memo):
"""
Return a copy of this dictionary using copies of the keys.
NOTE:
The values of the dictionary are not copied, since we can not copy the
external strong references to the values, which are decisive for
garbage collection.
EXAMPLES::
sage: class C(object): pass
sage: V = [C(),C()]
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: D[C()] = V[0]
sage: D[C()] = V[1]
sage: E = deepcopy(D) # indirect doctest
The keys are copied (in this silly example, the copies of the keys are
actually not equal to the original keys)::
sage: set(E.keys()) == set(D.keys())
False
However, the values are not copied::
sage: set(E.values()) == set(D.values()) == set(V)
True
"""
out = WeakValueDictionary()
for k,v in self.iteritems():
out[deepcopy(k, memo)] = v
return out
def __repr__(self):
"""
EXAMPLES::
sage: import sage.misc.weak_dict
sage: repr(sage.misc.weak_dict.WeakValueDictionary([(1,ZZ),(2,QQ)])) # indirect doctest
'<WeakValueDictionary at 0x...>'
"""
return "<WeakValueDictionary at 0x%x>" % id(self)
def __str__(self):
"""
EXAMPLES::
sage: import sage.misc.weak_dict
sage: str(sage.misc.weak_dict.WeakValueDictionary([(1,ZZ),(2,QQ)])) # indirect doctest
'<WeakValueDictionary at 0x...>'
"""
return "<WeakValueDictionary at 0x%x>" % id(self)
def setdefault(self, k, default=None):
"""
Return the stored value for a given key; return and store a default
value if no previous value is stored.
EXAMPLES::
sage: import sage.misc.weak_dict
sage: L = [(p,GF(p)) for p in prime_range(10)]
sage: D = sage.misc.weak_dict.WeakValueDictionary(L)
sage: len(D)
4
The value for an existing key is returned and not overridden::
sage: D.setdefault(5, ZZ)
Finite Field of size 5
sage: D[5]
Finite Field of size 5
For a non-existing key, the default value is stored and returned::
sage: 4 in D
False
sage: D.setdefault(4, ZZ)
Integer Ring
sage: 4 in D
True
sage: D[4]
Integer Ring
sage: len(D)
5
TESTS:
Check that :trac:`15956` has been fixed, i.e., a ``TypeError`` is
raised for unhashable objects::
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: D.setdefault(matrix([]),ZZ)
Traceback (most recent call last):
...
TypeError: mutable matrices are unhashable
"""
cdef PyObject* wr = PyDict_GetItemWithError(self, k)
if wr != NULL:
out = PyWeakref_GetObject(wr)
if out != Py_None:
return <object>out
PyDict_SetItem(self,k,KeyedRef(default,self.callback,PyObject_Hash(k)))
return default
def __setitem__(self, k, v):
"""
EXAMPLES::
sage: import sage.misc.weak_dict
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: ZZ in D
False
One can set new items::
sage: D[ZZ] = QQ # indirect doctest
sage: D[ZZ]
Rational Field
sage: len(D)
1
sage: ZZ in D
True
One can also override existing items::
sage: D[ZZ] = RLF
sage: ZZ in D
True
sage: D[ZZ]
Real Lazy Field
sage: len(D)
1
TESTS:
One may wonder whether it causes problems when garbage collection for
a previously existing item happens *after* overriding the item. The
example shows that it is not a problem::
sage: class Cycle:
....: def __init__(self):
....: self.selfref = self
sage: L = [Cycle() for _ in range(5)]
sage: D = sage.misc.weak_dict.WeakValueDictionary(enumerate(L))
sage: len(D)
5
sage: import gc
sage: gc.disable()
sage: del L
sage: len(D)
5
sage: D[2] = ZZ
sage: len(D)
5
sage: gc.enable()
sage: _ = gc.collect()
sage: len(D)
1
sage: D.items()
[(2, Integer Ring)]
Check that :trac:`15956` has been fixed, i.e., a ``TypeError`` is
raised for unhashable objects::
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: D[matrix([])] = ZZ
Traceback (most recent call last):
...
TypeError: mutable matrices are unhashable
"""
PyDict_SetItem(self,k,KeyedRef(v,self.callback,PyObject_Hash(k)))
#def __delitem__(self, k):
#we don't really have to override this method.
def pop(self, k):
"""
Return the value for a given key, and delete it from the dictionary.
EXAMPLES::
sage: import sage.misc.weak_dict
sage: L = [GF(p) for p in prime_range(10^3)]
sage: D = sage.misc.weak_dict.WeakValueDictionary(enumerate(L))
sage: 20 in D
True
sage: D.pop(20)
Finite Field of size 73
sage: 20 in D
False
sage: D.pop(20)
Traceback (most recent call last):
...
KeyError: 20
TESTS:
Check that :trac:`15956` has been fixed, i.e., a ``TypeError`` is
raised for unhashable objects::
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: D.pop(matrix([]))
Traceback (most recent call last):
...
TypeError: mutable matrices are unhashable
"""
cdef PyObject* wr = PyDict_GetItemWithError(self, k)
if wr == NULL:
raise KeyError(k)
cdef PyObject* outref = PyWeakref_GetObject(wr)
if outref == Py_None:
raise KeyError(k)
#we turn the output into a new reference before deleting the item,
#because the deletion can cause any kind of havoc.
out = <object>outref
del self[k]
return out
def popitem(self):
"""
Return and delete some item from the dictionary.
EXAMPLES::
sage: import sage.misc.weak_dict
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: D[1] = ZZ
The dictionary only contains a single item, hence, it is clear which
one will be returned::
sage: D.popitem()
(1, Integer Ring)
Now, the dictionary is empty, and hence the next attempt to pop an
item will fail with a ``KeyError``::
sage: D.popitem()
Traceback (most recent call last):
...
KeyError: 'popitem(): weak value dictionary is empty'
"""
for k,v in self.iteritems():
del self[k]
return k, v
raise KeyError('popitem(): weak value dictionary is empty')
def get(self, k, d=None):
"""
Return the stored value for a key, or a default value for unkown keys.
The default value defaults to None.
EXAMPLES::
sage: import sage.misc.weak_dict
sage: L = [GF(p) for p in prime_range(10^3)]
sage: D = sage.misc.weak_dict.WeakValueDictionary(enumerate(L))
sage: 100 in D
True
sage: 200 in D
False
sage: D.get(100, "not found")
Finite Field of size 547
sage: D.get(200, "not found")
'not found'
sage: D.get(200) is None
True
TESTS:
Check that :trac:`15956` has been fixed, i.e., a ``TypeError`` is
raised for unhashable objects::
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: D.get(matrix([]))
Traceback (most recent call last):
...
TypeError: mutable matrices are unhashable
"""
cdef PyObject * wr = PyDict_GetItemWithError(self, k)
if wr == NULL:
return d
out = PyWeakref_GetObject(wr)
if out == Py_None:
return d
else:
return <object>out
def __getitem__(self, k):
"""
TESTS::
sage: import sage.misc.weak_dict
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: D[ZZ] = QQ
sage: D[QQ]
Traceback (most recent call last):
...
KeyError: Rational Field
sage: D[ZZ] # indirect doctest
Rational Field
As usual, the dictionary keys are compared by ``==`` and not by
identity::
sage: D[10] = ZZ
sage: D[int(10)]
Integer Ring
Check that :trac:`15956` has been fixed, i.e., a ``TypeError`` is
raised for unhashable objects::
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: D[matrix([])]
Traceback (most recent call last):
...
TypeError: mutable matrices are unhashable
"""
cdef PyObject* wr = PyDict_GetItemWithError(self, k)
if wr == NULL:
raise KeyError(k)
out = PyWeakref_GetObject(wr)
if out == Py_None:
raise KeyError(k)
return <object>out
def __contains__(self, k):
"""
Containment in the set of keys.
TESTS::
sage: import sage.misc.weak_dict
sage: class Vals(object): pass
sage: L = [Vals() for _ in range(10)]
sage: D = sage.misc.weak_dict.WeakValueDictionary(enumerate(L))
sage: 3 in D # indirect doctest
True
As usual, keys are compared by equality and not by identity::
sage: int(3) in D
True
This is a weak value dictionary. Hence, the existence of the
dictionary does not prevent the values from garbage collection,
thereby removing the corresponding key-value pairs::
sage: del L[3]
sage: 3 in D
False
Check that :trac:`15956` has been fixed, i.e., a ``TypeError`` is
raised for unhashable objects::
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: matrix([]) in D
Traceback (most recent call last):
...
TypeError: mutable matrices are unhashable
"""
cdef PyObject* wr = PyDict_GetItemWithError(self, k)
if wr==NULL:
return False
if PyWeakref_GetObject(wr)==Py_None:
return False
else:
return True
#def __len__(self):
#since GC is not deterministic, neither is the length of a WeakValueDictionary,
#so we might as well just return the normal dictionary length.
def iterkeys(self):
"""
Iterate over the keys of this dictionary.
.. WARNING::
Iteration is unsafe, if the length of the dictionary changes
during the iteration! This can also happen by garbage collection.
EXAMPLES::
sage: import sage.misc.weak_dict
sage: class Vals(object): pass
sage: L = [Vals() for _ in range(10)]
sage: D = sage.misc.weak_dict.WeakValueDictionary(enumerate(L))
sage: del L[4]
One item got deleted from the list ``L`` and hence the corresponding
item in the dictionary got deleted as well. Therefore, the
corresponding key 4 is missing in the list of keys::
sage: list(sorted(D.iterkeys()))
[0, 1, 2, 3, 5, 6, 7, 8, 9]
"""
cdef PyObject *key, *wr
cdef Py_ssize_t pos = 0
try:
self._enter_iter()
while PyDict_Next(self, &pos, &key, &wr):
#this check doesn't really say anything: by the time
#the key makes it to the customer, it may have already turned
#invalid. It's a cheap check, though.
if PyWeakref_GetObject(wr)!=Py_None:
yield <object>key
finally:
self._exit_iter()
def __iter__(self):
"""
Iterate over the keys of this dictionary.
.. WARNING::
Iteration is unsafe, if the length of the dictionary changes
during the iteration! This can also happen by garbage collection.
EXAMPLES::
sage: import sage.misc.weak_dict
sage: class Vals(object): pass
sage: L = [Vals() for _ in range(10)]
sage: D = sage.misc.weak_dict.WeakValueDictionary(enumerate(L))
sage: del L[4]
One item got deleted from the list ``L`` and hence the corresponding
item in the dictionary got deleted as well. Therefore, the
corresponding key 4 is missing in the list of keys::