Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 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
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Changelog
=========
v3.10.5 (2023-03-25)
--------------------
- Add explicit error check as certain UNIX filesystems do not support flock. by :user:`jahrules`.

v3.10.4 (2023-03-24)
--------------------
- Update os.open to preserve mode= for certain edge cases. by :user:`jahrules`.
Expand Down
5 changes: 4 additions & 1 deletion src/filelock/_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
from errno import ENOSYS
from typing import cast

from ._api import BaseFileLock
Expand Down Expand Up @@ -39,8 +40,10 @@ def _acquire(self) -> None:
pass # This locked is not owned by this UID
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError:
except OSError as exception:
os.close(fd)
if exception.errno == ENOSYS: # NotImplemented error
raise NotImplementedError("FileSystem does not appear to support flock; user SoftFileLock instead")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo fix.
Perhaps "Filesystem does not support file locking. Please use SoftFileLock instead."?

else:
self._lock_file_fd = fd

Expand Down
28 changes: 27 additions & 1 deletion tests/test_filelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import sys
import threading
from contextlib import contextmanager
from errno import ENOSYS
from inspect import getframeinfo, stack
from pathlib import Path, PurePath
from stat import S_IWGRP, S_IWOTH, S_IWUSR, filemode
from types import TracebackType
from typing import Callable, Iterator, Tuple, Type, Union
from typing import TYPE_CHECKING, Callable, Iterator, Tuple, Type, Union

import pytest
from _pytest.logging import LogCaptureFixture
Expand All @@ -24,6 +25,9 @@
WindowsFileLock,
)

if TYPE_CHECKING:
from _typeshed import HasFileno


@pytest.mark.parametrize(
("lock_type", "path_type"),
Expand Down Expand Up @@ -494,3 +498,25 @@ def test_wrong_platform(tmp_path: Path) -> None:
lock.acquire()
with pytest.raises(NotImplementedError):
lock._release()


def test_flock_not_implemented_unix(tmp_path: Path) -> None:
if sys.platform == "win32":
pytest.skip("Windows filesystems support flock")

def dummy_flock(fd: int | HasFileno, operation: int) -> None:
raise OSError(ENOSYS, "mock error")
return fd, operation # needed for strict type checker

import fcntl

lock_path = tmp_path / "a.lock"
_fcntl_flock = fcntl.flock
try:
fcntl.flock = dummy_flock
with pytest.raises(NotImplementedError):
with FileLock(str(lock_path)):
pass

finally:
fcntl.flock = _fcntl_flock
3 changes: 3 additions & 0 deletions whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ autodoc
autosectionlabel
caplog
eacces
enosys
extlinks
filelock
filemode
fileno
frameinfo
fspath
getframeinfo
Expand All @@ -29,5 +31,6 @@ skipif
stacklevel
trunc
typehints
typeshed
unlck
win32