Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ The released versions correspond to PyPI releases.
* changed the default for `FakeFilesystem.shuffle_listdir_results` to `True` to reflect
the real filesystem behavior

### Fixes
* fixed a deadlock in `shutil.copytree` if copying using an `shutil` function as
`copy_function` argument (see [#1235](../../issues/1235))

## [Version 5.10.0](https://pypi.python.org/pypi/pyfakefs/5.10.0) (2025-10-11)
Adds official support for Python 3.14. Last minor version before the 6.0 release.

Expand Down
12 changes: 9 additions & 3 deletions pyfakefs/fake_filesystem_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import os
import shutil
import sys
from threading import Lock
from threading import RLock
from collections.abc import Callable


Expand All @@ -44,7 +44,7 @@ class FakeShutilModule:
the `fs` fixture, the `patchfs` decorator, or directly the `Patcher`.
"""

module_lock = Lock()
module_lock = RLock()

use_copy_file_range = (
hasattr(shutil, "_USE_CP_COPY_FILE_RANGE") and shutil._USE_CP_COPY_FILE_RANGE # type: ignore[attr-defined]
Expand All @@ -71,9 +71,12 @@ def __init__(self, filesystem):
"""
self.filesystem = filesystem
self.shutil_module = shutil
self._in_get_attribute = False
self._patch_level = 0

def _start_patching_global_vars(self):
self._patch_level += 1
if self._patch_level > 1:
return # nested call - already patched
if self.has_fcopy_file:
self.shutil_module._HAS_FCOPYFILE = False
if self.use_copy_file_range:
Expand All @@ -89,6 +92,9 @@ def _start_patching_global_vars(self):
self.shutil_module._use_fd_functions = False

def _stop_patching_global_vars(self):
self._patch_level -= 1
if self._patch_level > 0:
return # nested call - remains patched
if self.has_fcopy_file:
self.shutil_module._HAS_FCOPYFILE = True
if self.use_copy_file_range:
Expand Down
12 changes: 12 additions & 0 deletions pyfakefs/tests/fake_filesystem_shutil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ def test_copy(self):
self.assertTrue(os.path.exists(dst_file))
self.assertEqual(os.stat(src_file).st_mode, os.stat(dst_file).st_mode)

def test_copytree_with_copy_function(self):
# regression test for #1235 (deadlock)
source_dir = Path(self.make_path("source_dir"))
target_dir = Path(self.make_path("target_dir"))
test_contents = "Test contents"
source_file = source_dir / "test.txt"
target_file = target_dir / "test.txt"
self.create_file(source_file, contents=test_contents)

shutil.copytree(source_dir, target_dir, copy_function=shutil.copy2)
assert target_file.read_text() == test_contents

def test_permission_error_message(self):
self.check_posix_only()
dst_dir = Path(self.make_path("home1"))
Expand Down
Loading