diff options
author | andrei kulakov <andrei.avk@gmail.com> | 2021-09-21 21:53:07 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-21 21:53:07 (GMT) |
commit | b7eac52b466f697d3e89f47508e0df0196a98970 (patch) | |
tree | f399750eb74741f8bf7d3e1035e000e83728dbe0 /Lib/shutil.py | |
parent | 86f28372b17c8c56539e9543bea9f125ab11b8aa (diff) | |
download | cpython-b7eac52b466f697d3e89f47508e0df0196a98970.zip cpython-b7eac52b466f697d3e89f47508e0df0196a98970.tar.gz cpython-b7eac52b466f697d3e89f47508e0df0196a98970.tar.bz2 |
bpo-45234: Fix FileNotFound exception raised instead of IsADirectoryError in shutil.copyfile() (GH-28421)
This was a regression from fixing BPO-43219.
Diffstat (limited to 'Lib/shutil.py')
-rw-r--r-- | Lib/shutil.py | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py index 273a7d2..e544498 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -253,36 +253,37 @@ def copyfile(src, dst, *, follow_symlinks=True): if not follow_symlinks and _islink(src): os.symlink(os.readlink(src), dst) else: - 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) + with open(src, 'rb') as fsrc: + try: + with 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) - - # 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 + + copyfileobj(fsrc, fdst) + + # Issue 43219, raise a less confusing exception + except IsADirectoryError as e: + if not os.path.exists(dst): + raise FileNotFoundError(f'Directory does not exist: {dst}') from e + else: + raise return dst |