diff options
author | andrei kulakov <andrei.avk@gmail.com> | 2021-07-10 03:47:41 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-10 03:47:41 (GMT) |
commit | 248173cc0483a9ad9261353302f1234cf9eb2ebe (patch) | |
tree | 1af4ee52325ed4a50696de8d68853cde4dc9464a /Lib/shutil.py | |
parent | f24777c2b329974b69d2a3bf5cfc37e0fcace36c (diff) | |
download | cpython-248173cc0483a9ad9261353302f1234cf9eb2ebe.zip cpython-248173cc0483a9ad9261353302f1234cf9eb2ebe.tar.gz cpython-248173cc0483a9ad9261353302f1234cf9eb2ebe.tar.bz2 |
bpo-43219: shutil.copyfile, raise a less confusing exception instead of IsADirectoryError (GH-27049)
Fixes the misleading IsADirectoryError to be FileNotFoundError.
Diffstat (limited to 'Lib/shutil.py')
-rw-r--r-- | Lib/shutil.py | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py index 1982b1c..2cb5ef8 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -253,28 +253,36 @@ def copyfile(src, dst, *, follow_symlinks=True): if not follow_symlinks and _islink(src): os.symlink(os.readlink(src), dst) else: - with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst: - # macOS - if _HAS_FCOPYFILE: - try: - _fastcopy_fcopyfile(fsrc, fdst, posix._COPYFILE_DATA) - return dst - except _GiveupOnFastCopy: - pass - # Linux - elif _USE_CP_SENDFILE: - try: - _fastcopy_sendfile(fsrc, fdst) + try: + with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst: + # macOS + if _HAS_FCOPYFILE: + try: + _fastcopy_fcopyfile(fsrc, fdst, posix._COPYFILE_DATA) + return dst + except _GiveupOnFastCopy: + pass + # Linux + elif _USE_CP_SENDFILE: + try: + _fastcopy_sendfile(fsrc, fdst) + return 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 - 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 - - copyfileobj(fsrc, fdst) + + copyfileobj(fsrc, fdst) + + # Issue 43219, raise a less confusing exception + except IsADirectoryError as e: + if os.path.exists(dst): + raise + else: + raise FileNotFoundError(f'Directory does not exist: {dst}') from e return dst |