Skip to content

Commit cc5645d

Browse files
bpo-32689: Updates shutil.move function to allow for Path objects to be used as source argument by casting to string before performing rstrip. Added documentation on why rstrip needs to be used in the helper _basename method.
1 parent 059f58c commit cc5645d

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

Lib/shutil.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,13 @@ def onerror(*args):
513513
def _basename(path):
514514
# A basename() variant which first strips the trailing slash, if present.
515515
# Thus we always get the last component of the path, even for directories.
516+
# e.g.
517+
# >>> os.path.basename('/bar/foo')
518+
# 'foo'
519+
# >>> os.path.basename('/bar/foo/')
520+
# ''
521+
# >>> _basename('/bar/foo/')
522+
# 'foo'
516523
sep = os.path.sep + (os.path.altsep or '')
517524
return os.path.basename(path.rstrip(sep))
518525

@@ -550,7 +557,12 @@ def move(src, dst, copy_function=copy2):
550557
os.rename(src, dst)
551558
return
552559

553-
real_dst = os.path.join(dst, _basename(src))
560+
# Using _basename instead of os.path.basename is important, as we must
561+
# ignore any trailing slash to avoid the basename returning ''
562+
# Forcing src to a string allows flexibility of objects being passed in,
563+
# including Path objects
564+
real_dst = os.path.join(dst, _basename(str(src)))
565+
554566
if os.path.exists(real_dst):
555567
raise Error("Destination path '%s' already exists" % real_dst)
556568
try:

0 commit comments

Comments
 (0)