Skip to content

Commit

Permalink
Simplify the atomic_writes code
Browse files Browse the repository at this point in the history
Related to pytest-dev#10114
  • Loading branch information
nicoddemus committed Jul 8, 2022
1 parent 57945bd commit 753c9cd
Showing 1 changed file with 12 additions and 46 deletions.
58 changes: 12 additions & 46 deletions src/_pytest/atomic_writes.py
Expand Up @@ -9,38 +9,12 @@
import sys
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


if sys.platform != "win32":
import fcntl

if hasattr(fcntl, "F_FULLFSYNC"):

def _proper_fsync(fd):
Expand Down Expand Up @@ -85,18 +59,14 @@ def _handle_errors(rv):
def _replace_atomic(src, dst):
_handle_errors(
windll.kernel32.MoveFileExW(
_path_to_unicode(src),
_path_to_unicode(dst),
src,
dst,
_windows_default_flags | _MOVEFILE_REPLACE_EXISTING,
)
)

def _move_atomic(src, dst):
_handle_errors(
windll.kernel32.MoveFileExW(
_path_to_unicode(src), _path_to_unicode(dst), _windows_default_flags
)
)
_handle_errors(windll.kernel32.MoveFileExW(src, dst, _windows_default_flags))


def replace_atomic(src, dst):
Expand Down Expand Up @@ -143,7 +113,7 @@ class AtomicWriter:
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:
raise ValueError(
"Appending to an existing file is not supported, because that "
Expand All @@ -156,19 +126,13 @@ def __init__(self, path, mode=DEFAULT_MODE, overwrite=False, **open_kwargs):
if "w" not in mode:
raise ValueError("AtomicWriters can only be written to.")

# Attempt to convert `path` to `str` or `bytes`
if fspath is not None:
path = fspath(path)

self._path = path
self._path = os.fspath(path)
self._mode = mode
self._overwrite = overwrite
self._open_kwargs = open_kwargs

def open(self):
"""
Open the temporary file.
"""
"""Open the temporary file."""
return self._open(self.get_fileobject)

@contextlib.contextmanager
Expand Down Expand Up @@ -204,8 +168,10 @@ def get_fileobject(
return open(**kwargs)

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()
_proper_fsync(f.fileno())

Expand Down

0 comments on commit 753c9cd

Please sign in to comment.