Skip to content

Commit 07bd3f7

Browse files
committed
Fix documentation for cleanup handlers
- add convenience handler 'reload_cleanup_handler'
1 parent 3852d0f commit 07bd3f7

File tree

6 files changed

+66
-3
lines changed

6 files changed

+66
-3
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ The released versions correspond to PyPI releases.
1818
* fixed a problem with module and session scoped fixtures in Python 3.13
1919
(see [#1101](../../issues/1101))
2020
* fixed handling of `cwd` if set to a `pathlib.Path` (see [#1108](../../issues/1108))
21+
* fixed documentation for cleanup handlers, added convenience handler `reload_cleanup_handler`
22+
(see [#1105](../../issues/1105))
2123

2224
## [Version 5.7.3](https://pypi.python.org/pypi/pyfakefs/5.7.3) (2024-12-15)
2325
Fixes a regression in version 5.7.3.

docs/troubleshooting.rst

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,25 @@ be added to the patcher:
374374
375375
@pytest.fixture
376376
def my_fs():
377-
with Patcher():
378-
patcher["modulename"] = handler_no_cleanup
379-
yield
377+
with Patcher() as patcher:
378+
patcher.cleanup_handlers["modulename"] = handler_no_cleanup
379+
yield patcher.fs
380+
381+
382+
A specific problem are modules that use filesystem functions and are imported by other modules locally
383+
(e.g. inside a function). These kinds of modules are not correctly reset and need to be reloaded manually.
384+
For this case, the cleanup handler `reload_cleanup_handler` in `pyfakefs.helpers` can be used:
385+
386+
.. code:: python
387+
388+
from pyfakefs.helpers import reload_cleanup_handler
389+
390+
391+
@pytest.fixture
392+
def my_fs():
393+
with Patcher() as patcher:
394+
patcher.cleanup_handlers["modulename"] = reload_cleanup_handler
395+
yield patcher.fs
380396
381397
As this may not be trivial, we recommend to write an issue in ``pyfakefs`` with a reproducible example.
382398
We will analyze the problem, and if we find a solution we will either get this fixed in ``pyfakefs``

pyfakefs/helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"""Helper classes use for fake file system implementation."""
1414

1515
import ctypes
16+
import importlib
1617
import io
1718
import locale
1819
import os
@@ -539,3 +540,12 @@ def starts_with(path, string):
539540
):
540541
return True
541542
return False
543+
544+
545+
def reload_cleanup_handler(name):
546+
"""Cleanup handler that reloads the module with the given name.
547+
Maybe needed in cases where a module is imported locally.
548+
"""
549+
if name in sys.modules:
550+
importlib.reload(sys.modules[name])
551+
return True
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pathlib
2+
3+
4+
def use_pathlib(path: str):
5+
return pathlib.Path(path)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def load(path: str) -> str:
2+
from pyfakefs.pytest_tests import lib_using_pathlib
3+
4+
return lib_using_pathlib.use_pathlib(path)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
3+
from pyfakefs.fake_filesystem_unittest import Patcher
4+
from pyfakefs.fake_pathlib import FakePathlibModule
5+
from pyfakefs.helpers import reload_cleanup_handler
6+
from pyfakefs.pytest_tests import local_import
7+
8+
9+
@pytest.fixture
10+
def test_fs():
11+
with Patcher() as patcher:
12+
patcher.cleanup_handlers["pyfakefs.pytest_tests.lib_using_pathlib"] = (
13+
reload_cleanup_handler
14+
)
15+
yield patcher.fs
16+
17+
18+
class TestReloadCleanupHandler:
19+
def test1(self, test_fs):
20+
path = local_import.load("some_path")
21+
assert isinstance(path, FakePathlibModule.Path)
22+
23+
def test2(self):
24+
path = local_import.load("some_path")
25+
# will fail without reload handler
26+
assert not isinstance(path, FakePathlibModule.Path)

0 commit comments

Comments
 (0)