Ticket #13904: test.sage

File test.sage, 1.7 KB (added by nbruin, 8 years ago)

Script that tries to cause a double dealloc but fails because python is mature

Line 
1cython("""
2import gc, sys, weakref
3
4cdef int counter = 0
5cdef int next_counter():
6    global counter
7    counter += 1
8    return counter
9
10cdef class Collector:
11    # Indirectly trigger garbage collection in SimpleGarbage deallocation.
12    # The __dealloc__ method of SimpleGarbage won't trigger the bug as the
13    # refcount is artifitially inflated for the durration of that function.
14    def __dealloc__(self):
15        print "Collector.__dealloc__"
16        print "collect", gc.collect()
17
18cdef class SimpleGarbage:
19    cdef Collector c # to particpate in garbage collection
20    cdef int index
21    cdef bint deallocated
22    cdef object __weakref__
23    def __cinit__(self):
24        self.index = next_counter()
25        #self.c = Collector()
26        print self, "__cinit__"
27    def __dealloc__(self):
28        print self, "__dealloc__"
29        if self.deallocated:
30            print "Double dealloc!"
31        self.deallocated = True
32        gc.collect()
33    def __str__(self):
34        return "SimpleGarbage(%s)" % self.index
35    def __repr__(self):
36        return "SimpleGarbage(%s)" % self.index
37
38cdef class SimpleRef:
39    cdef object ref
40    def __init__(self,ref):
41        print "initializing SimpleRef with ref=%s"%ref
42        self.ref=ref
43    def __dealloc__(self):
44        print "dealloc of SimpleRef with ref=%s"%self.ref
45""")
46
47from sage.structure.coerce_dict import TripleDict
48import gc
49
50print "making tripledict"
51D=TripleDict(53)
52print "making simple garbage"
53s1=SimpleGarbage()
54s2=SimpleGarbage()
55print "putting entry in dictionary"
56D[SimpleRef(s1),s1,None]=1
57D[s2,SimpleRef(s2),None]=2
58print "deleting s"
59del s1,s2
60print "deleting tripledict"
61del D
62print "garbage collecting"
63gc.collect()