| 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 |
|
| 13 |
import weakref |
| 14 |
|
| 15 |
class A(object): |
| 16 |
pass |
| 17 |
|
| 18 |
def callback(x): |
| 19 |
del lst[:] |
| 20 |
|
| 21 |
|
| 22 |
keepalive = [] |
| 23 |
|
| 24 |
for 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[:]) |