parent
57945bd8b1
commit
753c9cd0ed
|
@ -9,38 +9,12 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
try:
|
|
||||||
import fcntl
|
|
||||||
except ImportError:
|
|
||||||
fcntl = None
|
|
||||||
|
|
||||||
# `fspath` was added in Python 3.6
|
|
||||||
try:
|
|
||||||
from os import fspath
|
|
||||||
except ImportError:
|
|
||||||
fspath = None
|
|
||||||
|
|
||||||
__version__ = "1.4.1"
|
|
||||||
|
|
||||||
|
|
||||||
PY2 = sys.version_info[0] == 2
|
|
||||||
|
|
||||||
text_type = unicode if PY2 else str # noqa
|
|
||||||
|
|
||||||
|
|
||||||
def _path_to_unicode(x):
|
|
||||||
if not isinstance(x, text_type):
|
|
||||||
return x.decode(sys.getfilesystemencoding())
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_MODE = "wb" if PY2 else "w"
|
|
||||||
|
|
||||||
|
|
||||||
_proper_fsync = os.fsync
|
_proper_fsync = os.fsync
|
||||||
|
|
||||||
|
|
||||||
if sys.platform != "win32":
|
if sys.platform != "win32":
|
||||||
|
import fcntl
|
||||||
|
|
||||||
if hasattr(fcntl, "F_FULLFSYNC"):
|
if hasattr(fcntl, "F_FULLFSYNC"):
|
||||||
|
|
||||||
def _proper_fsync(fd):
|
def _proper_fsync(fd):
|
||||||
|
@ -85,18 +59,14 @@ else:
|
||||||
def _replace_atomic(src, dst):
|
def _replace_atomic(src, dst):
|
||||||
_handle_errors(
|
_handle_errors(
|
||||||
windll.kernel32.MoveFileExW(
|
windll.kernel32.MoveFileExW(
|
||||||
_path_to_unicode(src),
|
src,
|
||||||
_path_to_unicode(dst),
|
dst,
|
||||||
_windows_default_flags | _MOVEFILE_REPLACE_EXISTING,
|
_windows_default_flags | _MOVEFILE_REPLACE_EXISTING,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _move_atomic(src, dst):
|
def _move_atomic(src, dst):
|
||||||
_handle_errors(
|
_handle_errors(windll.kernel32.MoveFileExW(src, dst, _windows_default_flags))
|
||||||
windll.kernel32.MoveFileExW(
|
|
||||||
_path_to_unicode(src), _path_to_unicode(dst), _windows_default_flags
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def replace_atomic(src, dst):
|
def replace_atomic(src, dst):
|
||||||
|
@ -143,7 +113,7 @@ class AtomicWriter:
|
||||||
subclass.
|
subclass.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, path, mode=DEFAULT_MODE, overwrite=False, **open_kwargs):
|
def __init__(self, path, mode="w", overwrite=False, **open_kwargs):
|
||||||
if "a" in mode:
|
if "a" in mode:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Appending to an existing file is not supported, because that "
|
"Appending to an existing file is not supported, because that "
|
||||||
|
@ -156,19 +126,13 @@ class AtomicWriter:
|
||||||
if "w" not in mode:
|
if "w" not in mode:
|
||||||
raise ValueError("AtomicWriters can only be written to.")
|
raise ValueError("AtomicWriters can only be written to.")
|
||||||
|
|
||||||
# Attempt to convert `path` to `str` or `bytes`
|
self._path = os.fspath(path)
|
||||||
if fspath is not None:
|
|
||||||
path = fspath(path)
|
|
||||||
|
|
||||||
self._path = path
|
|
||||||
self._mode = mode
|
self._mode = mode
|
||||||
self._overwrite = overwrite
|
self._overwrite = overwrite
|
||||||
self._open_kwargs = open_kwargs
|
self._open_kwargs = open_kwargs
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
"""
|
"""Open the temporary file."""
|
||||||
Open the temporary file.
|
|
||||||
"""
|
|
||||||
return self._open(self.get_fileobject)
|
return self._open(self.get_fileobject)
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
|
@ -204,8 +168,10 @@ class AtomicWriter:
|
||||||
return open(**kwargs)
|
return open(**kwargs)
|
||||||
|
|
||||||
def sync(self, f):
|
def sync(self, f):
|
||||||
"""responsible for clearing as many file caches as possible before
|
"""
|
||||||
commit"""
|
Responsible for clearing as many file caches as possible before
|
||||||
|
commit.
|
||||||
|
"""
|
||||||
f.flush()
|
f.flush()
|
||||||
_proper_fsync(f.fileno())
|
_proper_fsync(f.fileno())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue