Skip to content

Commit 3587f5b

Browse files
authored
Merge pull request ipython#12718 from Carreau/reset--hard
Mare reset -f more agressive and cull sys.modules;
2 parents 2abc37a + b1ef396 commit 3587f5b

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

IPython/core/interactiveshell.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ def all_ns_refs(self):
14101410
return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
14111411
[m.__dict__ for m in self._main_mod_cache.values()]
14121412

1413-
def reset(self, new_session=True):
1413+
def reset(self, new_session=True, aggressive=False):
14141414
"""Clear all internal namespaces, and attempt to release references to
14151415
user objects.
14161416
@@ -1447,6 +1447,15 @@ def reset(self, new_session=True):
14471447

14481448
# Restore the user namespaces to minimal usability
14491449
self.init_user_ns()
1450+
if aggressive and not hasattr(self, "_sys_modules_keys"):
1451+
print("Cannot restore sys.module, no snapshot")
1452+
elif aggressive:
1453+
print("culling sys module...")
1454+
current_keys = set(sys.modules.keys())
1455+
for k in current_keys - self._sys_modules_keys:
1456+
if k.startswith("multiprocessing"):
1457+
continue
1458+
del sys.modules[k]
14501459

14511460
# Restore the default and user aliases
14521461
self.alias_manager.clear_aliases()

IPython/core/magics/namespace.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@ def reset(self, parameter_s=''):
491491
we do a 'hard' reset, giving you a new session and removing all
492492
references to objects from the current session.
493493
494+
--aggressive: Try to aggressively remove modules from sys.modules ; this
495+
may allow you to reimport Python modules that have been updated and
496+
pick up changes, but can have unattended consequences.
497+
494498
in : reset input history
495499
496500
out : reset output history
@@ -533,8 +537,8 @@ def reset(self, parameter_s=''):
533537
such as the ipython notebook interface, will reset the namespace
534538
without confirmation.
535539
"""
536-
opts, args = self.parse_options(parameter_s,'sf', mode='list')
537-
if 'f' in opts:
540+
opts, args = self.parse_options(parameter_s, "sf", "aggressive", mode="list")
541+
if "f" in opts:
538542
ans = True
539543
else:
540544
try:
@@ -552,7 +556,7 @@ def reset(self, parameter_s=''):
552556
for i in self.who_ls():
553557
del(user_ns[i])
554558
elif len(args) == 0: # Hard reset
555-
self.shell.reset(new_session = False)
559+
self.shell.reset(new_session=False, aggressive=("aggressive" in opts))
556560

557561
# reset in/out/dhist/array: previously extensinions/clearcmd.py
558562
ip = self.shell

IPython/core/shellapp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ def init_code(self):
331331
# flush output, so itwon't be attached to the first cell
332332
sys.stdout.flush()
333333
sys.stderr.flush()
334+
self.shell._sys_modules_keys = set(sys.modules.keys())
334335

335336
def _run_exec_lines(self):
336337
"""Run lines of code in IPythonApp.exec_lines in the user's namespace."""

0 commit comments

Comments
 (0)