This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add DirectoryNotEmptyError subclass of OSError
Type: Stage: patch review
Components: Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, eryksun, nanjekyejoannah, pitrou, vstinner
Priority: normal Keywords: patch

Created on 2018-02-15 03:11 by barry, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 15496 open nanjekyejoannah, 2019-08-26 02:16
Messages (6)
msg312192 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2018-02-15 03:11
I just ran across errno 39 (Directory not empty) when using Path.rename(newdir) when newdir already existed and was not empty.  I realized that OSError exceptions don't have a DirectoryNotEmptyError subclass.  Maybe we should add one?

Probably not important or common enough to add for 3.7 given we're in beta now.
msg350778 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-08-29 13:35
> Maybe we should add one?

I don't think that we need to add an *builtin* exception for every single possible errno. On Linux with Python 3.7, I count 133 different error numbers:

>>> len([name for name in dir(errno) if name.startswith("E")])
133

PEP 3151 explains the rationale for added OSError subclasses.

I think that we should focus on the most common ones, especially the ones which are available on all platforms.

I have no opinion about "errno 39 (Directory not empty)". Is it a "common" error?
msg350783 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-08-29 14:37
> I have no opinion about "errno 39 (Directory not empty)". 
> Is it a "common" error?

Python's code base never specifically handles ENOTEMPTY. On the other hand, in terms of basic operations on files and directories, I think implementing DirectoryNotEmptyError follows naturally from the existing set: FileNotFoundError, FileExistsError, NotADirectoryError, and IsADirectoryError.
msg350785 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2019-08-29 14:41
According to POSIX, the only system calls that can fail with ENOTEMPTY are rmdir(), rename() and unlinkat().
msg352083 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-09-12 09:49
Copy of my comment on the review:
https://github.com/python/cpython/pull/15496#pullrequestreview-287311626

I'm not excited by adding a new builtin symbol: IMHO this module already exists too many symbols.

I'm not convinced that it's worth it. There is exactly 0 line of code in CPython code base which expects ENOTEMPTY errno. So it looks like an artifical use case.

I recall that when PEP 3151 was implemented, the implementation replaced a lot of code using "except OSError as exc: if exc.errno == XXX: ... else: raise" with "except : ..." which was neat.

But again, I don't see any usage of ENOTEMPTY in the Python stdlib.

Are you aware of 3rd party code expecting ENOTEMPTY? How many projects?

Adding a builtin symbol is a significant change, it should be well motived.
msg352084 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-09-12 09:50
> Python's code base never specifically handles ENOTEMPTY.

Right, so this use case looks artificial to me.


> On the other hand, in terms of basic operations on files and directories, I think implementing DirectoryNotEmptyError follows naturally from the existing set: FileNotFoundError, FileExistsError, NotADirectoryError, and IsADirectoryError.

Again, I consider that we must not add one exception per errno, since the Python errno module contains 133 error codes on Linux. Many are very rare and don't deserve to add a new symbol to the builtins module.

I suggest to close this issue.

If later someone wants to get DirectoryNotEmptyError, please open a new issue with statistics of how many 3rd party projects use ENOTEMPTY.
History
Date User Action Args
2022-04-11 14:58:57adminsetgithub: 77028
2019-09-12 09:50:46vstinnersetmessages: + msg352084
2019-09-12 09:49:49vstinnersetmessages: + msg352083
2019-08-29 16:37:07nanjekyejoannahsetnosy: + nanjekyejoannah
2019-08-29 14:41:21pitrousetmessages: + msg350785
2019-08-29 14:37:05eryksunsetnosy: + eryksun
messages: + msg350783
2019-08-29 13:35:49vstinnersetnosy: + vstinner
messages: + msg350778
2019-08-26 08:08:07serhiy.storchakasetnosy: + pitrou

versions: + Python 3.9, - Python 3.8
2019-08-26 02:16:05nanjekyejoannahsetkeywords: + patch
stage: patch review
pull_requests: + pull_request15186
2018-02-15 03:11:57barrycreate