jaraco / cmdix

Fork of https://launchpad.net/pycoreutils
MIT License
27 stars 1 forks source link

cp SameFileError #13

Closed jaraco closed 1 year ago

jaraco commented 1 year ago

Attempting to copy a file to another location fails with a SameFileError:

 draft @ pwd
C:\Users\jaraco\draft
 draft @ cp z:\draft\mod.py .
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\jaraco\.local\bin\cp.exe\__main__.py", line 7, in <module>
  File "C:\Users\jaraco\.local\pipx\venvs\cmdix\Lib\site-packages\cmdix\__init__.py", line 127, in run
    return run_subcommand(commandname, argv)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jaraco\.local\pipx\venvs\cmdix\Lib\site-packages\cmdix\__init__.py", line 165, in run_subcommand
    args.func(args)
  File "C:\Users\jaraco\.local\pipx\venvs\cmdix\Lib\site-packages\cmdix\command\cp.py", line 59, in func
    handle(_copy, args, args.DIRECTORY.pop(), src)
  File "C:\Users\jaraco\.local\pipx\venvs\cmdix\Lib\site-packages\cmdix\command\cp.py", line 73, in handle
    _copy(src, dstfile)
  File "C:\Program Files\Python 3.12\Lib\shutil.py", line 423, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Program Files\Python 3.12\Lib\shutil.py", line 240, in copyfile
    raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
shutil.SameFileError: 'z:\\draft\\mod.py' and 'z:\\draft\\mod.py' are the same file
 draft @ cp z:\draft\mod.py c:\users\jaraco\draft
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\jaraco\.local\bin\cp.exe\__main__.py", line 7, in <module>
  File "C:\Users\jaraco\.local\pipx\venvs\cmdix\Lib\site-packages\cmdix\__init__.py", line 127, in run
    return run_subcommand(commandname, argv)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jaraco\.local\pipx\venvs\cmdix\Lib\site-packages\cmdix\__init__.py", line 165, in run_subcommand
    args.func(args)
  File "C:\Users\jaraco\.local\pipx\venvs\cmdix\Lib\site-packages\cmdix\command\cp.py", line 59, in func
    handle(_copy, args, args.DIRECTORY.pop(), src)
  File "C:\Users\jaraco\.local\pipx\venvs\cmdix\Lib\site-packages\cmdix\command\cp.py", line 73, in handle
    _copy(src, dstfile)
  File "C:\Program Files\Python 3.12\Lib\shutil.py", line 423, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Program Files\Python 3.12\Lib\shutil.py", line 240, in copyfile
    raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
shutil.SameFileError: 'z:\\draft\\mod.py' and 'z:\\draft\\mod.py' are the same file
jaraco commented 1 year ago

The issue is apparent in:

https://github.com/jaraco/cmdix/blob/9393b9d6fc8f900252da666a2cbf9a36d57a814a/cmdix/command/cp.py#L73

...where the source is joined to the destination, obviating the destination.