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