Skip to content

Commit fb97b62

Browse files
committed
Reload modules loaded dynamically during test
- modules loaded dynamically are now reloaded instead of deleted; deleting them caused some unexpected behavior in django - if reloading failed delete the module as last ressort
1 parent 51d26de commit fb97b62

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The released versions correspond to PyPI releases.
55

66
### Fixes
77
* fixes handling of unhashable modules which cannot be cached (see [#923](../../issues/923))
8+
* reload modules loaded by the dynamic patcher instead of removing them - sometimes they may
9+
not be reloaded automatically (see [#932](../../issues/932))
810

911
## [Version 5.3.2](https://pypi.python.org/pypi/pyfakefs/5.3.2) (2023-11-30)
1012
Bugfix release.

pyfakefs/fake_filesystem_unittest.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def patchfs(
104104
use_known_patches: bool = True,
105105
patch_open_code: PatchMode = PatchMode.OFF,
106106
patch_default_args: bool = False,
107-
use_cache: bool = True
107+
use_cache: bool = True,
108108
) -> Callable:
109109
"""Convenience decorator to use patcher with additional parameters in a
110110
test function.
@@ -1091,12 +1091,14 @@ def cleanup(self) -> None:
10911091
reloaded_module_names = [
10921092
module.__name__ for module in self._patcher.modules_to_reload
10931093
]
1094-
# Dereference all modules loaded during the test so they will reload on
1095-
# the next use, ensuring that no faked modules are referenced after the
1096-
# test.
1094+
# Reload all modules loaded during the test, ensuring that
1095+
# no faked modules are referenced after the test.
10971096
for name in self._loaded_module_names:
10981097
if name in sys.modules and name not in reloaded_module_names:
1099-
del sys.modules[name]
1098+
try:
1099+
reload(sys.modules[name])
1100+
except Exception:
1101+
del sys.modules[name]
11001102

11011103
def needs_patch(self, name: str) -> bool:
11021104
"""Check if the module with the given name shall be replaced."""

0 commit comments

Comments
 (0)