PageRenderTime 466ms CodeModel.GetById 119ms app.highlight 2ms RepoModel.GetById 237ms app.codeStats 0ms

/Lib/test/crashers/mutation_inside_cyclegc.py

http://unladen-swallow.googlecode.com/
Python | 31 lines | 14 code | 9 blank | 8 comment | 3 complexity | 016c8b2f1ec4bbe2fc3967ed2ff993aa MD5 | raw file
 1
 2# The cycle GC collector can be executed when any GC-tracked object is
 3# allocated, e.g. during a call to PyList_New(), PyDict_New(), ...
 4# Moreover, it can invoke arbitrary Python code via a weakref callback.
 5# This means that there are many places in the source where an arbitrary
 6# mutation could unexpectedly occur.
 7
 8# The example below shows list_slice() not expecting the call to
 9# PyList_New to mutate the input list.  (Of course there are many
10# more examples like this one.)
11
12
13import weakref
14
15class A(object):
16    pass
17
18def callback(x):
19    del lst[:]
20
21
22keepalive = []
23
24for i in range(100):
25    lst = [str(i)]
26    a = A()
27    a.cycle = a
28    keepalive.append(weakref.ref(a, callback))
29    del a
30    while lst:
31        keepalive.append(lst[:])