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