-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
os.remove is documented to raise IsADirectoryError but macOS raises PermissionError #95815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
In Windows, calling import ctypes
import os
import tempfile
RtlGetLastNtStatus = ctypes.WinDLL('ntdll').RtlGetLastNtStatus
STATUS_FILE_IS_A_DIRECTORY = ctypes.c_long(0xC000_00BA).value
def test():
path = tempfile.mkdtemp()
try:
os.remove(path)
except PermissionError as e:
if RtlGetLastNtStatus() != STATUS_FILE_IS_A_DIRECTORY:
raise
raise IsADirectoryError(errno.EISDIR,
os.strerror(errno.EISDIR),
e.filename,
e.winerror) >>> try: test()
... except OSError as ex: e = ex
...
>>> e
IsADirectoryError(13, 'Is a directory')
>>> e.__context__
PermissionError(13, 'Access is denied')
>>> os.rmdir(e.filename) |
The behaviour on macOS matches the documentation of the underlying os, the manpage for unlink(2) and unlinkat(2) mention:
IMHO we shouldn't try to work around this, but should mention this in the documentation. |
The current text was introduced in #14262, replacing OSError in the text. In this case the new more specific exception is incorrect. |
os.remove can raise PermissionError instead of IsADirectoryError, when the object to be removed is a directory (in particular on macOS). This reverts a change done in python#14262.
I've added a PR that reverts the text back to mentioning OSError instead of IsADirectoryError, but I'm not convinced yet that this is the best way to change the documentation. An alternative is to document both exceptions. I'm -1 on changing the implementation to raise the documented exception, that's not possible to do without a race condition because there are other reasons for getting EPERM (e.g. the folder in which the to-be-removed object is located is not writable). |
…99571) os.remove can raise PermissionError instead of IsADirectoryError, when the object to be removed is a directory (in particular on macOS). This reverts a change done in pythonGH-14262. (cherry picked from commit 1cae31d) Co-authored-by: Ronald Oussoren <[email protected]>
…99571) os.remove can raise PermissionError instead of IsADirectoryError, when the object to be removed is a directory (in particular on macOS). This reverts a change done in pythonGH-14262. (cherry picked from commit 1cae31d) Co-authored-by: Ronald Oussoren <[email protected]>
#99641) GH-95815: Document less specific error for os.remove (GH-99571) os.remove can raise PermissionError instead of IsADirectoryError, when the object to be removed is a directory (in particular on macOS). This reverts a change done in GH-14262. (cherry picked from commit 1cae31d) Co-authored-by: Ronald Oussoren <[email protected]> Co-authored-by: Ronald Oussoren <[email protected]>
#99639) GH-95815: Document less specific error for os.remove (GH-99571) os.remove can raise PermissionError instead of IsADirectoryError, when the object to be removed is a directory (in particular on macOS). This reverts a change done in GH-14262. (cherry picked from commit 1cae31d) Co-authored-by: Ronald Oussoren <[email protected]>
Documentation
In the documentation for
os.remove
it states:This is the case on Linux:
However on macOS this results in a
PermissionError
:Linked PRs
The text was updated successfully, but these errors were encountered: