Skip to content

Commit a7276ee

Browse files
Update FileLock constructor to accept pathlib.Path (#110)
Co-authored-by: Bernát Gábor <[email protected]>
1 parent 851f8bb commit a7276ee

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
=========
33

4+
v3.3.2 (2021-10-29)
5+
-------------------
6+
- Accept path types (like ``pathlib.Path`` and ``pathlib.PurePath``) in the constructor for ``FileLock`` objects.
7+
48
v3.3.1 (2021-10-15)
59
-------------------
610
- Add changelog to the documentation :pr:`108` - by :user:`gaborbernat`

src/filelock/_api.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import logging
2+
import os
23
import time
34
from abc import ABC, abstractmethod
45
from threading import Lock
56
from types import TracebackType
6-
from typing import Optional, Type, Union
7+
from typing import Any, Optional, Type, Union
78

89
from ._error import Timeout
910

@@ -34,7 +35,7 @@ def __exit__(
3435
class BaseFileLock(ABC):
3536
"""Abstract base class for a file lock object."""
3637

37-
def __init__(self, lock_file: str, timeout: float = -1) -> None:
38+
def __init__(self, lock_file: Union[str, "os.PathLike[Any]"], timeout: float = -1) -> None:
3839
"""
3940
Create a new lock object.
4041
@@ -44,7 +45,7 @@ def __init__(self, lock_file: str, timeout: float = -1) -> None:
4445
A timeout of 0 means, that there is exactly one attempt to acquire the file lock.
4546
"""
4647
# The path to the lock file.
47-
self._lock_file: str = lock_file
48+
self._lock_file: str = os.fspath(lock_file)
4849

4950
# The file descriptor for the *_lock_file* as it is returned by the os.open() function.
5051
# This file lock is only NOT None, if the object currently holds the lock.

tests/test_filelock.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
import threading
44
from contextlib import contextmanager
5-
from pathlib import Path
5+
from pathlib import Path, PurePath
66
from stat import S_IWGRP, S_IWOTH, S_IWUSR
77
from types import TracebackType
88
from typing import Callable, Iterator, Optional, Tuple, Type, Union
@@ -13,12 +13,25 @@
1313
from filelock import BaseFileLock, FileLock, SoftFileLock, Timeout
1414

1515

16-
@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
17-
def test_simple(lock_type: Type[BaseFileLock], tmp_path: Path, caplog: LogCaptureFixture) -> None:
16+
@pytest.mark.parametrize(
17+
("lock_type", "path_type"),
18+
[
19+
(FileLock, str),
20+
(FileLock, PurePath),
21+
(FileLock, Path),
22+
(SoftFileLock, str),
23+
(SoftFileLock, PurePath),
24+
(SoftFileLock, Path),
25+
],
26+
)
27+
def test_simple(
28+
lock_type: Type[BaseFileLock], path_type: Union[Type[str], Type[Path]], tmp_path: Path, caplog: LogCaptureFixture
29+
) -> None:
1830
caplog.set_level(logging.DEBUG)
19-
lock_path = tmp_path / "a"
20-
lock = lock_type(str(lock_path))
2131

32+
# test lock creation by passing a `str`
33+
lock_path = tmp_path / "a"
34+
lock = lock_type(path_type(lock_path))
2235
with lock as locked:
2336
assert lock.is_locked
2437
assert lock is locked

whitelist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ favicon
1212
fcntl
1313
filelock
1414
fmt
15+
fspath
1516
intersphinx
1617
intervall
1718
iwgrp

0 commit comments

Comments
 (0)