Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix del method exit test #172

Closed
wants to merge 2 commits into from

1 participant

@takluyver
Owner

This fixes a recent test that __del__ methods, called on exit, are able to access built-in functions such as str(), so that the test will fail if that code is broken. It then reapplies Fernando's fix for the bug, which was removed by another commit in the meantime.

@takluyver
Owner

Merge branch 'fix-del-method-exit-test' of http://github.com/takowl/ipython 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).

Closed by 5c6a347 (pull request).

@markvoorhies markvoorhies referenced this pull request from a commit in markvoorhies/ipython
@takluyver takluyver 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).
5c6a347
@fperez fperez referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@damianavila damianavila referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@mattvonrocketstein mattvonrocketstein referenced this pull request from a commit in mattvonrocketstein/ipython
@takluyver takluyver 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).
2636437
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.