Skip to content
This repository was archived by the owner on Jul 16, 2022. It is now read-only.

Commit b41a4fd

Browse files
committed
add support for FileNotFoundError
1 parent 2347537 commit b41a4fd

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

atomicwrites/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
if PY2:
1313
class FileExistsError(OSError):
1414
errno = errno.EEXIST
15+
16+
class FileNotFoundError(OSError):
17+
errno = errno.ENOENT
1518
else:
1619
# For some reason we have to redefine this, or users can't import it.
1720
FileExistsError = FileExistsError
21+
FileNotFoundError = FileNotFoundError
1822

1923

2024
if sys.platform != 'win32':
@@ -42,14 +46,19 @@ def _move_atomic(src, dst):
4246
import pywintypes
4347

4448
_windows_default_flags = win32file.MOVEFILE_WRITE_THROUGH
49+
_windows_error_table = {
50+
183: FileExistsError,
51+
3: FileNotFoundError
52+
}
4553

4654
@contextlib.contextmanager
4755
def handle_errors():
4856
try:
4957
yield
5058
except pywintypes.error as e:
51-
if e.winerror == 183:
52-
raise FileExistsError(str(e))
59+
native_cls = _windows_error_table.get(e.winerror, None)
60+
if native_cls is not None:
61+
raise native_cls(str(e))
5362
else:
5463
raise
5564

docs/index.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@ API
77

88
.. autofunction:: atomic_write
99

10+
11+
Exceptions
12+
----------
13+
1014
.. exception:: FileExistsError
1115

12-
This is Python 3's builtin ``FileExistsError``, under Python 2 it's a
13-
subclass of ``OSError`` with similar semantics.
16+
This is Python 3's builtin ``FileExistsError``, under Python 2 it's a
17+
subclass of ``OSError`` with similar semantics.
18+
19+
.. exception:: FileNotFoundError
20+
21+
This is Python 3's builtin ``FileNotFoundError``, under Python 2 it's a
22+
subclass of ``OSError`` with similar semantics.
1423

1524
Low-level API
1625
-------------

0 commit comments

Comments
 (0)