Skip to content

Commit 119822c

Browse files
committed
Use a stack to implement _WritablePath._copy_from()
1 parent bf8da86 commit 119822c

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Lib/pathlib/types.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -415,18 +415,21 @@ def _copy_from(self, source, follow_symlinks=True):
415415
"""
416416
Recursively copy the given path to this path.
417417
"""
418-
if not follow_symlinks and source.info.is_symlink():
419-
self.symlink_to(str(source.readlink()), source.info.is_dir())
420-
elif source.info.is_dir():
421-
children = source.iterdir()
422-
self.mkdir()
423-
for child in children:
424-
self.joinpath(child.name)._copy_from(child, follow_symlinks)
425-
else:
426-
ensure_different_files(source, self)
427-
with magic_open(source, 'rb') as source_f:
428-
with magic_open(self, 'wb') as target_f:
429-
copyfileobj(source_f, target_f)
418+
stack = [(source, self)]
419+
while stack:
420+
src, dst = stack.pop()
421+
if not follow_symlinks and src.info.is_symlink():
422+
dst.symlink_to(str(src.readlink()), src.info.is_dir())
423+
elif src.info.is_dir():
424+
children = src.iterdir()
425+
dst.mkdir()
426+
for child in children:
427+
stack.append((child, dst.joinpath(child.name)))
428+
else:
429+
ensure_different_files(src, dst)
430+
with magic_open(src, 'rb') as source_f:
431+
with magic_open(dst, 'wb') as target_f:
432+
copyfileobj(source_f, target_f)
430433

431434

432435
_JoinablePath.register(PurePath)

0 commit comments

Comments
 (0)