Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions IPython/lib/deepreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ def import_submodule(mod, subname, fullname):
# else: mod.__name__ + "." + subname == fullname

global found_now
if fullname in found_now and fullname in sys.modules:
# Never reload the standard library: it cannot have changed, and re-executing
# it swaps out objects like os.environ and enum.Enum that others hold on to.
if fullname in sys.modules and (
fullname in found_now or fullname.partition(".")[0] in sys.stdlib_module_names
):
m = sys.modules[fullname]
else:
print('Reloading', fullname)
Expand Down Expand Up @@ -297,7 +301,8 @@ def reload(
takes a list of modules to exclude from reloading. The default exclude
list contains modules listed in sys.builtin_module_names with additional
sys, os.path, builtins and __main__, to prevent, e.g., resetting
display, exception, and io hooks.
display, exception, and io hooks. Standard-library modules
(``sys.stdlib_module_names``) are never reloaded.
"""
global found_now
for i in exclude:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_deepreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

import os
import sys
import types
from pathlib import Path
Expand Down Expand Up @@ -102,6 +103,24 @@ def test_reload_types_module_is_noop():
assert dreload(types) is types


def test_deepreload_skips_stdlib():
with TemporaryDirectory() as tmpdir:
with prepended_to_syspath(tmpdir):
(Path(tmpdir) / "uses_stdlib.py").write_text(
"import os\nimport enum\nimport json.decoder\n", encoding="utf-8"
)
import uses_stdlib

environ = os.environ
Enum = sys.modules["enum"].Enum
JSONDecoder = sys.modules["json.decoder"].JSONDecoder
dreload(uses_stdlib)
assert os.environ is environ
assert sys.modules["enum"].Enum is Enum
assert sys.modules["json.decoder"].JSONDecoder is JSONDecoder
del sys.modules["uses_stdlib"]


class TestGetParent:
def test_no_level(self):
assert deepreload.get_parent({"__package__": "os"}, 0) == (None, "")
Expand Down
Loading