classification
Title: Windows does not raise IsADirectoryError
Type: behavior Stage:
Components: IO, Windows Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Jairo Llopis, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2021-02-01 20:16 by Jairo Llopis, last changed 2021-02-01 21:33 by eryksun.

Messages (2)
msg386103 - (view) Author: Jairo Llopis (Jairo Llopis) Date: 2021-02-01 20:16
Trying to read a directory on Linux raises the expected exception:

Python 3.9.1 (default, Jan 20 2021, 00:00:00) 
[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pathlib
>>> pathlib.Path(".venv").read_text()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.9/pathlib.py", line 1255, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
  File "/usr/lib64/python3.9/pathlib.py", line 1241, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
IsADirectoryError: [Errno 21] Is a directory: '.venv'


Doing the same on Windows raises a misleading permission error instead:

>>> import pathlib
>>> pathlib.Path(".venv").read_text()
Traceback (most recent call last):   
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\lib\pathlib.py", line 1255, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\lib\pathlib.py", line 1241, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\lib\pathlib.py", line 1109, in _opener
    return self._accessor.open(self, flags, mode)
PermissionError: [Errno 13] Permission denied: '.venv'
msg386117 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-02-01 21:33
When an open attempts to access a directory as data, the error code from WinAPI CreateFileW is ERROR_ACCESS_DENIED (5), which maps to C EACCES and thus raises Python PermissionError. 

Python's io.FileIO() type could handle an EACCES error in Windows by checking for a directory. There's a race condition to consider in which an inaccessible file is replaced by a directory, but that seems to be a trivial concern.

io.FileIO() could also use the undocumented _O_OBTAIN_DIR (0x2000) open flag. This opens with backup semantics, which allows opening directories. Then, as already implemented in POSIX, fail with EISDIR if a directory is opened. For example:

    >>> fd = os.open('C:/Temp', 0x2000)
    >>> stat.S_ISDIR(os.fstat(fd).st_mode)
    True
History
Date User Action Args
2021-02-01 21:33:03eryksunsetnosy: + eryksun
messages: + msg386117

components: + IO
type: behavior
2021-02-01 20:16:33Jairo Llopiscreate