refbug.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Minimal script to reproduce our nasty reference counting bug.
  2. The problem is related to https://github.com/ipython/ipython/issues/141
  3. The original fix for that appeared to work, but John D. Hunter found a
  4. matplotlib example which, when run twice in a row, would break. The problem
  5. were references held by open figures to internals of Tkinter.
  6. This code reproduces the problem that John saw, without matplotlib.
  7. This script is meant to be called by other parts of the test suite that call it
  8. via %run as if it were executed interactively by the user. As of 2011-05-29,
  9. test_run.py calls it.
  10. """
  11. from __future__ import print_function
  12. #-----------------------------------------------------------------------------
  13. # Module imports
  14. #-----------------------------------------------------------------------------
  15. import sys
  16. from IPython import get_ipython
  17. #-----------------------------------------------------------------------------
  18. # Globals
  19. #-----------------------------------------------------------------------------
  20. # This needs to be here because nose and other test runners will import
  21. # this module. Importing this module has potential side effects that we
  22. # want to prevent.
  23. if __name__ == '__main__':
  24. ip = get_ipython()
  25. if not '_refbug_cache' in ip.user_ns:
  26. ip.user_ns['_refbug_cache'] = []
  27. aglobal = 'Hello'
  28. def f():
  29. return aglobal
  30. cache = ip.user_ns['_refbug_cache']
  31. cache.append(f)
  32. def call_f():
  33. for func in cache:
  34. print('lowercased:',func().lower())