Skip to content

Commit

Permalink
Reload modules loaded dynamically during test
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mrbean-bremen committed Jan 12, 2024
1 parent 51d26de commit fb97b62
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The released versions correspond to PyPI releases.

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

## [Version 5.3.2](https://pypi.python.org/pypi/pyfakefs/5.3.2) (2023-11-30)
Bugfix release.
Expand Down
12 changes: 7 additions & 5 deletions pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def patchfs(
use_known_patches: bool = True,
patch_open_code: PatchMode = PatchMode.OFF,
patch_default_args: bool = False,
use_cache: bool = True
use_cache: bool = True,
) -> Callable:
"""Convenience decorator to use patcher with additional parameters in a
test function.
Expand Down Expand Up @@ -1091,12 +1091,14 @@ def cleanup(self) -> None:
reloaded_module_names = [
module.__name__ for module in self._patcher.modules_to_reload
]
# Dereference all modules loaded during the test so they will reload on
# the next use, ensuring that no faked modules are referenced after the
# test.
# Reload all modules loaded during the test, ensuring that
# no faked modules are referenced after the test.
for name in self._loaded_module_names:
if name in sys.modules and name not in reloaded_module_names:
del sys.modules[name]
try:
reload(sys.modules[name])
except Exception:
del sys.modules[name]

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

0 comments on commit fb97b62

Please sign in to comment.