Emily Morehouse <em...@cuttlesoft.com> added the comment:

Thanks for the bug report!

shutil.move should certainly accept a path object, as shutil.copy does, though 
it should be noted that in your example, 'path' could become out of date as it 
does not refresh the path information. For example, with shutil.move fixed:

    >>> import os, pathlib, shutil
    >>> os.mkdir('test1')
    >>>
    >>> os.mkdir('test2')
    >>> path = pathlib.Path('test1')
    >>> path.absolute()
    PosixPath('/Users/e/Development/OSS/cpython/test1')
    >>> shutil.move(path, 'test2')
    'test2/test1'
    >>> path.absolute()
    PosixPath('/Users/e/Development/OSS/cpython/test2')

test1 is now actually at '/Users/e/Development/OSS/cpython/test2/test1'


For the fix:
I did a bit of digging and the error comes from a helper method _basename that 
uses rstrip to remove a trailing separator, hence the error as rstrip doesn't 
exist for a path object (and I don't think it makes sense that it should, 
though that was one solution). Removing the trailing separator is, however, 
very important in determining the full destination path.

After trying a few different approaches, I think the simplest way is to cast 
the src to a string before finding its appropriate basename. I also added some 
comments to make it more clear why _basename is used over os.path.basename to 
hopefully save someone else time in the future.

A more robust option would be to explicitly handle Path objects or to handle 
exceptions for any dst that cannot be cast to a string. However, the current 
patch fixes the issue without introducing new problems.

----------
nosy: +emilyemorehouse

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue32689>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to