Skip to content
Browse files

Merge branch 'fix-del-method-exit-test' of http://github.com/takowl/i…

…python into takowl-fix-del-method-exit-test

Fix errors with __del__ methods being called on exit.

This branch reintroduces a recent fix that got accidentally
overwritten, as well as adding a more stringent form of the test so
that we don't miss it again in the future (the old test only printed a
message but didn't fail, so it was easy to miss).

Closes gh-172 (pull request).
  • Loading branch information...
2 parents 35e407a + fab7319 commit 5c6a34778a567b30e51fc1fd46cac64e0c1e5a94 @takluyver takluyver committed with fperez Oct 22, 2010
Showing with 19 additions and 3 deletions.
  1. +17 −2 IPython/core/interactiveshell.py
  2. +2 −1 IPython/core/tests/test_run.py
View
19 IPython/core/interactiveshell.py
@@ -819,8 +819,11 @@ def init_create_namespaces(self, user_ns=None, user_global_ns=None):
# Similarly, track all namespaces where references can be held and that
# we can safely clear (so it can NOT include builtin). This one can be
- # a simple list.
- self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
+ # a simple list. Note that the main execution namespaces, user_ns and
+ # user_global_ns, can NOT be listed here, as clearing them blindly
+ # causes errors in object __del__ methods. Instead, the reset() method
+ # clears them manually and carefully.
+ self.ns_refs_table = [ self.user_ns_hidden,
self.internal_ns, self._main_ns_cache ]
def make_user_namespaces(self, user_ns=None, user_global_ns=None):
@@ -985,6 +988,18 @@ def reset(self):
# Restore the user namespaces to minimal usability
for ns in self.ns_refs_table:
ns.clear()
+
+ # The main execution namespaces must be cleared very carefully,
+ # 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]
+
+ # Restore the user namespaces to minimal usability
self.init_user_ns()
# Restore the default and user aliases
View
3 IPython/core/tests/test_run.py
@@ -88,12 +88,13 @@ def doctest_reset_del():
In [2]: class A(object):
...: def __del__(self):
- ...: str("Hi")
+ ...: print str("Hi")
...:
In [3]: a = A()
In [4]: get_ipython().reset()
+ Hi
In [5]: 1+1
Out[5]: 2

0 comments on commit 5c6a347

Please sign in to comment.
Something went wrong with that request. Please try again.