Skip to content

Commit

Permalink
Raise PlatformMismatchError if using wrong class
Browse files Browse the repository at this point in the history
Instead of leaving abstract methods without definition when running on
the other platform (Unix vs Windows), define them and raise a
PlatformMismatchError if called on the wrong platform.

This fixes pylint warnings such as
   Abstract class 'WindowsFileLock' with abstract methods instantiated
   Abstract class 'UnixFileLock' with abstract methods instantiated

Fixes tox-dev#102
  • Loading branch information
vonschultz committed Feb 14, 2022
1 parent 541baba commit e5fdb3d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/filelock/_api.py
Expand Up @@ -14,6 +14,10 @@
_LOGGER = logging.getLogger("filelock")


class PlatformMismatchError(Exception):
"""Exception raised when attempting Unix locks on Windows, or vice versa."""


# This is a helper class which is returned by :meth:`BaseFileLock.acquire` and wraps the lock to make sure __enter__
# is not called twice when entering the with statement. If we would simply return *self*, the lock would be acquired
# again in the *__enter__* method of the BaseFileLock, but not released again automatically. issue #37 (memory leak)
Expand Down Expand Up @@ -236,4 +240,5 @@ def __del__(self) -> None:
__all__ = [
"BaseFileLock",
"AcquireReturnProxy",
"PlatformMismatchError",
]
10 changes: 8 additions & 2 deletions src/filelock/_unix.py
Expand Up @@ -5,15 +5,21 @@
from abc import ABC
from typing import cast

from ._api import BaseFileLock
from ._api import BaseFileLock, PlatformMismatchError

#: a flag to indicate if the fcntl API is available
has_fcntl = False
if sys.platform == "win32": # pragma: win32 cover

class UnixFileLock(BaseFileLock, ABC):
class UnixFileLock(BaseFileLock):
"""Uses the :func:`fcntl.flock` to hard lock the lock file on unix systems."""

def _acquire(self) -> None:
raise PlatformMismatchError()

def _release(self) -> None:
raise PlatformMismatchError()

else: # pragma: win32 no cover
try:
import fcntl
Expand Down
10 changes: 8 additions & 2 deletions src/filelock/_windows.py
Expand Up @@ -6,7 +6,7 @@
from errno import ENOENT
from typing import cast

from ._api import BaseFileLock
from ._api import BaseFileLock, PlatformMismatchError
from ._util import raise_on_exist_ro_file

if sys.platform == "win32": # pragma: win32 cover
Expand Down Expand Up @@ -49,9 +49,15 @@ def _release(self) -> None:

else: # pragma: win32 no cover

class WindowsFileLock(BaseFileLock, ABC):
class WindowsFileLock(BaseFileLock):
"""Uses the :func:`msvcrt.locking` function to hard lock the lock file on windows systems."""

def _acquire(self) -> None:
raise PlatformMismatchError()

def _release(self) -> None:
raise PlatformMismatchError()


__all__ = [
"WindowsFileLock",
Expand Down

0 comments on commit e5fdb3d

Please sign in to comment.