From 263d8f14af736fbd4a6f58e4829e19cf70d1692b 2010-10-10 18:40:09 From: Fernando Perez Date: 2010-10-10 18:40:09 Subject: [PATCH] Complete fix for __del__ errors with .reset(), add unit test. --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index fa9ce3c..bb857dc 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -983,11 +983,12 @@ class InteractiveShell(Configurable, Magic): ns.clear() # The main execution namespaces must be cleared very carefully, - # skipping the deletion of the __builtin__ key, because doing so would - # cause errors in many object's __del__ methods. + # skipping the deletion of the builtin-related keys, because doing so + # would cause errors in many object's __del__ methods. for ns in [self.user_ns, self.user_global_ns]: drop_keys = set(ns.keys()) drop_keys.discard('__builtin__') + drop_keys.discard('__builtins__') for k in drop_keys: del ns[k] diff --git a/IPython/core/tests/test_run.py b/IPython/core/tests/test_run.py index 12f634f..b4728c3 100644 --- a/IPython/core/tests/test_run.py +++ b/IPython/core/tests/test_run.py @@ -83,6 +83,22 @@ def doctest_run_builtins(): ....: """ +def doctest_reset_del(): + """Test that resetting doesn't cause errors in __del__ methods. + + In [2]: class A(object): + ...: def __del__(self): + ...: str("Hi") + ...: + + In [3]: a = A() + + In [4]: get_ipython().reset() + + In [5]: 1+1 + Out[5]: 2 + """ + # For some tests, it will be handy to organize them in a class with a common # setup that makes a temp file