diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-07-09 21:13:59 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-09 21:13:59 -0700 |
commit | c89f0b2587eb0b16175a0bbb12d0b86314ff9320 (patch) | |
tree | e2ee62219d8968030c2400dbf3e3907834e36804 /Lib/shutil.py | |
parent | 302df02789d041a09760f86295ea6b4dcd81aa1d (diff) | |
download | cpython-git-c89f0b2587eb0b16175a0bbb12d0b86314ff9320.tar.gz |
[3.9] bpo-43219: shutil.copyfile, raise a less confusing exception instead of IsADirectoryError (GH-27049) (GH-27082)
Fixes the misleading IsADirectoryError to be FileNotFoundError.
(cherry picked from commit 248173cc0483a9ad9261353302f1234cf9eb2ebe)
Co-authored-by: andrei kulakov <andrei.avk@gmail.com>
Automerge-Triggered-By: GH:gpshead
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 5cb796e800..08384cf92b 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -261,28 +261,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 |