Skip to content

_OsPathBackend.copy() raises PermissionError instead of IsADirectoryError on Windows for a directory source #818

Open

Description

@MohammedAlkindi

_OsPathBackend.copy() has no check for a directory source:

def copy(self, path: PathLike, dst: PathLike, overwrite: bool) -> None:
  if not overwrite and self.exists(dst):
    raise FileExistsError(f'{dst} already exists. Cannot copy {path}.')
  shutil.copyfile(path, dst)

Its sibling _FileSystemSpecBackend.copy() does, at backend.py:563-567:

if self.isdir(path) and not is_dir_dst:
  raise IsADirectoryError(f'Cannot copy to {dst}. Path {path} is a directory')

On POSIX the missing check does not matter, because open() on a directory raises IsADirectoryError natively and shutil.copyfile propagates it. On Windows, opening a directory is "Access is denied":

# Windows 11, CPython 3.13.13
shutil.copyfile(srcdir, dst)
# PermissionError: [Errno 13] Permission denied: '...\\srcdir'
# isinstance(e, IsADirectoryError) -> False

So caller code written against the contract the fsspec backend states, and against the behaviour the os backend shows on Linux, does not catch the Windows exception. PermissionError and IsADirectoryError are siblings under OSError, neither inherits from the other, so an except IsADirectoryError clause silently misses it and the error surfaces somewhere unrelated.

test_backend[_test_copy] and test_backend[_test_copy_with_overwrite] already assert the intended type and report this. Neither has run on Windows, because the module fails collection there on import grp.

I have no Linux or macOS machine, so the POSIX half above is read from the documented open() behaviour rather than measured.

Activity

  1. itzzdev09 commented on Sep 16, 2026

    @itzzdev09

    I'd like to work on this. Straightforward fix: add the same isdir check _FileSystemSpecBackend.copy() already has, so _OsPathBackend.copy() raises IsADirectoryError consistently on all platforms instead of relying on shutil.copyfile's platform-dependent behavior. Will add a Windows-specific regression test alongside it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions