Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise NotImplementedError if using wrong class #135

Merged
merged 11 commits into from Feb 17, 2022
5 changes: 5 additions & 0 deletions src/filelock/_api.py
Expand Up @@ -14,6 +14,10 @@
_LOGGER = logging.getLogger("filelock")


class PlatformMismatchError(Exception):
vonschultz marked this conversation as resolved.
Show resolved Hide resolved
"""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",
]
11 changes: 8 additions & 3 deletions src/filelock/_unix.py
Expand Up @@ -2,18 +2,23 @@

import os
import sys
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()
vonschultz marked this conversation as resolved.
Show resolved Hide resolved

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

else: # pragma: win32 no cover
try:
import fcntl
Expand Down
11 changes: 8 additions & 3 deletions src/filelock/_windows.py
Expand Up @@ -2,11 +2,10 @@

import os
import sys
from abc import ABC
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 +48,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