Skip to content

gh-132322: Ensure shutil functions return str when using pathlib #132820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 10 additions & 10 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def copyfile(src, dst, *, follow_symlinks=True):
if _HAS_FCOPYFILE:
try:
_fastcopy_fcopyfile(fsrc, fdst, posix._COPYFILE_DATA)
return dst
return os.fspath(dst)
except _GiveupOnFastCopy:
pass
# Linux / Android / Solaris
Expand All @@ -319,20 +319,20 @@ def copyfile(src, dst, *, follow_symlinks=True):
if _USE_CP_COPY_FILE_RANGE:
try:
_fastcopy_copy_file_range(fsrc, fdst)
return dst
return os.fspath(dst)
except _GiveupOnFastCopy:
pass
if _USE_CP_SENDFILE:
try:
_fastcopy_sendfile(fsrc, fdst)
return dst
return os.fspath(dst)
except _GiveupOnFastCopy:
pass
# Windows, see:
# https://github.com/python/cpython/pull/7160#discussion_r195405230
elif _WINDOWS and file_size > 0:
_copyfileobj_readinto(fsrc, fdst, min(file_size, COPY_BUFSIZE))
return dst
return os.fspath(dst)

copyfileobj(fsrc, fdst)

Expand All @@ -343,7 +343,7 @@ def copyfile(src, dst, *, follow_symlinks=True):
else:
raise

return dst
return os.fspath(dst)

def copymode(src, dst, *, follow_symlinks=True):
"""Copy mode bits from src to dst.
Expand Down Expand Up @@ -481,7 +481,7 @@ def copy(src, dst, *, follow_symlinks=True):
dst = os.path.join(dst, os.path.basename(src))
copyfile(src, dst, follow_symlinks=follow_symlinks)
copymode(src, dst, follow_symlinks=follow_symlinks)
return dst
return os.fspath(dst)

def copy2(src, dst, *, follow_symlinks=True):
"""Copy data and metadata. Return the file's destination.
Expand All @@ -505,7 +505,7 @@ def copy2(src, dst, *, follow_symlinks=True):
flags |= _winapi.COPY_FILE_COPY_SYMLINK
try:
_winapi.CopyFile2(src_, dst_, flags)
return dst
return os.fspath(dst)
except OSError as exc:
if (exc.winerror == _winapi.ERROR_PRIVILEGE_NOT_HELD
and not follow_symlinks):
Expand All @@ -521,7 +521,7 @@ def copy2(src, dst, *, follow_symlinks=True):

copyfile(src, dst, follow_symlinks=follow_symlinks)
copystat(src, dst, follow_symlinks=follow_symlinks)
return dst
return os.fspath(dst)

def ignore_patterns(*patterns):
"""Function that can be used as copytree() ignore parameter.
Expand Down Expand Up @@ -599,7 +599,7 @@ def _copytree(entries, src, dst, symlinks, ignore, copy_function,
errors.append((src, dst, str(why)))
if errors:
raise Error(errors)
return dst
return os.fspath(dst)

def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
ignore_dangling_symlinks=False, dirs_exist_ok=False):
Expand Down Expand Up @@ -930,7 +930,7 @@ def move(src, dst, copy_function=copy2):
else:
copy_function(src, real_dst)
os.unlink(src)
return real_dst
return os.fspath(real_dst)

def _destinsrc(src, dst):
src = os.path.abspath(src)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shutil.copy, copy2, copyfile, copytree, and move now consistently return str even when passed pathlib.Path arguments.
Loading