/Lib/test/crashers/mutation_inside_cyclegc.py

http://unladen-swallow.googlecode.com/ · Python · 31 lines · 14 code · 9 blank · 8 comment · 2 complexity · 016c8b2f1ec4bbe2fc3967ed2ff993aa MD5 · raw file

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