Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/c-api/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ the variables:
single: PyExc_ConnectionError
single: PyExc_ConnectionRefusedError
single: PyExc_ConnectionResetError
single: PyExc_DirectoryNotEmptyError
single: PyExc_EOFError
single: PyExc_FileExistsError
single: PyExc_FileNotFoundError
Expand Down Expand Up @@ -848,6 +849,8 @@ the variables:
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_ConnectionResetError` | :exc:`ConnectionResetError` | |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_DirectoryNotEmptyError` | :exc:`DirectoryNotEmptyError` | |
+--------------------------------------------------------------------------------------+
| :c:data:`PyExc_EOFError` | :exc:`EOFError` | |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_FileExistsError` | :exc:`FileExistsError` | |
Expand Down Expand Up @@ -945,6 +948,9 @@ the variables:
.. versionadded:: 3.6
:c:data:`PyExc_ModuleNotFoundError`.

.. versionadded:: 3.9
:c:data:`PyExc_DirectoryNotEmptyError`.

These are compatibility aliases to :c:data:`PyExc_OSError`:

.. index::
Expand Down
6 changes: 6 additions & 0 deletions Doc/library/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,12 @@ depending on the system error code.
reset by the peer.
Corresponds to :c:data:`errno` ``ECONNRESET``.

.. exception:: DirectoryNotEmptyError

Raised when a directory operation (such as :func:`os.rmdir`) is requested
on a directory which is not empty.
Corresponds to :c:data:`errno` ``ENOTEMPTY``

.. exception:: FileExistsError

Raised when trying to create a file or directory which already exists.
Expand Down
1 change: 1 addition & 0 deletions Include/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ PyAPI_DATA(PyObject *) PyExc_ConnectionError;
PyAPI_DATA(PyObject *) PyExc_ConnectionAbortedError;
PyAPI_DATA(PyObject *) PyExc_ConnectionRefusedError;
PyAPI_DATA(PyObject *) PyExc_ConnectionResetError;
PyAPI_DATA(PyObject *) PyExc_DirectoryNotEmptyError;
PyAPI_DATA(PyObject *) PyExc_FileExistsError;
PyAPI_DATA(PyObject *) PyExc_FileNotFoundError;
PyAPI_DATA(PyObject *) PyExc_InterruptedError;
Expand Down
1 change: 1 addition & 0 deletions Lib/_compat_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
'ConnectionError',
'ConnectionRefusedError',
'ConnectionResetError',
'DirectoryNotEmptyError',
'FileExistsError',
'FileNotFoundError',
'InterruptedError',
Expand Down
1 change: 1 addition & 0 deletions Lib/test/exception_hierarchy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ BaseException
| | +-- ConnectionAbortedError
| | +-- ConnectionRefusedError
| | +-- ConnectionResetError
| +-- DirectoryNotEmptyError
| +-- FileExistsError
| +-- FileNotFoundError
| +-- InterruptedError
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_exception_hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_select_error(self):
+-- ConnectionAbortedError ECONNABORTED
+-- ConnectionRefusedError ECONNREFUSED
+-- ConnectionResetError ECONNRESET
+-- DirectoryNotEmptyError ENOTEMPTY
+-- FileExistsError EEXIST
+-- FileNotFoundError ENOENT
+-- InterruptedError EINTR
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import copy
import os
import tempfile
import sys
import unittest
import pickle
Expand Down Expand Up @@ -1170,6 +1171,18 @@ def inner():
self.fail("RecursionError not raised")
self.assertEqual(wr(), None)

def test_errno_ENOTEMPTY(self):
dirname = tempfile.mkdtemp()

pkgdir = os.path.join(dirname, "pkgname")
os.mkdir(pkgdir)
with open(os.path.join(pkgdir, '__init__.py'), 'w') as fl:
fl.write('from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n')

with self.assertRaises(DirectoryNotEmptyError) as cm:
os.rmdir(dirname)
self.assertEqual(cm.exception.errno, errno.ENOTEMPTY, cm.exception)

def test_errno_ENOTDIR(self):
# Issue #12802: "not a directory" errors are ENOTDIR even on Windows
with self.assertRaises(OSError) as cm:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add `DirectoryNotEmptyError` subclass of `OSError`.
Patch by Joannah Nanjekye .
11 changes: 8 additions & 3 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,8 @@ MiddlingExtendsException(PyExc_OSError, FileNotFoundError, OSError,
"File not found.");
MiddlingExtendsException(PyExc_OSError, IsADirectoryError, OSError,
"Operation doesn't work on directories.");
MiddlingExtendsException(PyExc_OSError, DirectoryNotEmptyError, OSError,
"Operation only works on empty directories.");
MiddlingExtendsException(PyExc_OSError, NotADirectoryError, OSError,
"Operation only works on directories.");
MiddlingExtendsException(PyExc_OSError, InterruptedError, OSError,
Expand Down Expand Up @@ -2571,11 +2573,12 @@ _PyExc_Init(void)
PRE_INIT(ConnectionAbortedError);
PRE_INIT(ConnectionRefusedError);
PRE_INIT(ConnectionResetError);
PRE_INIT(DirectoryNotEmptyError);
PRE_INIT(FileExistsError);
PRE_INIT(FileNotFoundError);
PRE_INIT(InterruptedError);
PRE_INIT(IsADirectoryError);
PRE_INIT(NotADirectoryError);
PRE_INIT(InterruptedError);
PRE_INIT(PermissionError);
PRE_INIT(ProcessLookupError);
PRE_INIT(TimeoutError);
Expand Down Expand Up @@ -2604,11 +2607,12 @@ _PyExc_Init(void)
ADD_ERRNO(ConnectionAbortedError, ECONNABORTED);
ADD_ERRNO(ConnectionRefusedError, ECONNREFUSED);
ADD_ERRNO(ConnectionResetError, ECONNRESET);
ADD_ERRNO(DirectoryNotEmptyError, ENOTEMPTY);
ADD_ERRNO(FileExistsError, EEXIST);
ADD_ERRNO(FileNotFoundError, ENOENT);
ADD_ERRNO(InterruptedError, EINTR);
ADD_ERRNO(IsADirectoryError, EISDIR);
ADD_ERRNO(NotADirectoryError, ENOTDIR);
ADD_ERRNO(InterruptedError, EINTR);
ADD_ERRNO(PermissionError, EACCES);
ADD_ERRNO(PermissionError, EPERM);
ADD_ERRNO(ProcessLookupError, ESRCH);
Expand Down Expand Up @@ -2711,11 +2715,12 @@ _PyBuiltins_AddExceptions(PyObject *bltinmod)
POST_INIT(ConnectionAbortedError);
POST_INIT(ConnectionRefusedError);
POST_INIT(ConnectionResetError);
POST_INIT(DirectoryNotEmptyError);
POST_INIT(FileExistsError);
POST_INIT(FileNotFoundError);
POST_INIT(InterruptedError);
POST_INIT(IsADirectoryError);
POST_INIT(NotADirectoryError);
POST_INIT(InterruptedError);
POST_INIT(PermissionError);
POST_INIT(ProcessLookupError);
POST_INIT(TimeoutError);
Expand Down
1 change: 1 addition & 0 deletions PC/python3.def
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ EXPORTS
PyExc_IndexError=python39.PyExc_IndexError DATA
PyExc_InterruptedError=python39.PyExc_InterruptedError DATA
PyExc_IsADirectoryError=python39.PyExc_IsADirectoryError DATA
PyExc_DirectoryNotEmptyError=python39.PyExc_DirectoryNotEmptyError DATA
PyExc_KeyError=python39.PyExc_KeyError DATA
PyExc_KeyboardInterrupt=python39.PyExc_KeyboardInterrupt DATA
PyExc_LookupError=python39.PyExc_LookupError DATA
Expand Down