Skip to content

Commit

Permalink
Fixed several bugs with the testing suite.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Nov 18, 2024
1 parent d42529f commit cada500
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion portalocker/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
__author__ = 'Rick van Hattem'
__email__ = '[email protected]'
__version__ = '2.10.1'
__description__ = """Wraps the portalocker recipe for easy usage"""
__description__ = '''Wraps the portalocker recipe for easy usage'''
__url__ = 'https://github.com/WoLpH/portalocker'
6 changes: 6 additions & 0 deletions portalocker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def _read_file(
paren = False
from_ = None
for line in path.open():
if '__future__' in line:
continue

if paren:
if ')' in line:
line = line.split(')', 1)[1]
Expand Down Expand Up @@ -101,6 +104,9 @@ def combine(args: argparse.Namespace):
output_file = args.output_file
pathlib.Path(output_file.name).parent.mkdir(parents=True, exist_ok=True)

# We're handling this separately because it has to be the first import.
output_file.write('from __future__ import annotations\n')

output_file.write(
_TEXT_TEMPLATE.format((base_path / 'README.rst').read_text()),
)
Expand Down
4 changes: 2 additions & 2 deletions portalocker/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
'''
Locking constants
Lock types:
Expand All @@ -13,7 +13,7 @@
Manually unlock, only needed internally
- `UNBLOCK` unlock
"""
'''

import enum
import os
Expand Down
8 changes: 5 additions & 3 deletions portalocker/redis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# pyright: reportUnknownMemberType=false
from __future__ import annotations

import _thread
import json
import logging
Expand Down Expand Up @@ -26,7 +28,7 @@ def run(self) -> None:


class RedisLock(utils.LockBase):
"""
'''
An extremely reliable Redis lock based on pubsub with a keep-alive thread
As opposed to most Redis locking systems based on key/value pairs,
Expand Down Expand Up @@ -60,7 +62,7 @@ class RedisLock(utils.LockBase):
to override these you need to explicitly specify a value (e.g.
`health_check_interval=0`)
"""
'''

redis_kwargs: typing.Dict[str, typing.Any]

Check failure on line 67 in portalocker/redis.py

View workflow job for this annotation

GitHub Actions / lint (3.8)

Ruff (UP006)

portalocker/redis.py:67:19: UP006 Use `dict` instead of `typing.Dict` for type annotation

Check failure on line 67 in portalocker/redis.py

View workflow job for this annotation

GitHub Actions / lint (3.10)

Ruff (UP006)

portalocker/redis.py:67:19: UP006 Use `dict` instead of `typing.Dict` for type annotation

Check failure on line 67 in portalocker/redis.py

View workflow job for this annotation

GitHub Actions / lint (3.11)

Ruff (UP006)

portalocker/redis.py:67:19: UP006 Use `dict` instead of `typing.Dict` for type annotation

Check failure on line 67 in portalocker/redis.py

View workflow job for this annotation

GitHub Actions / lint (3.8)

Ruff (UP006)

portalocker/redis.py:67:19: UP006 Use `dict` instead of `typing.Dict` for type annotation
thread: typing.Optional[PubSubWorkerThread]

Check failure on line 68 in portalocker/redis.py

View workflow job for this annotation

GitHub Actions / lint (3.8)

Ruff (UP007)

portalocker/redis.py:68:13: UP007 Use `X | Y` for type annotations

Check failure on line 68 in portalocker/redis.py

View workflow job for this annotation

GitHub Actions / lint (3.10)

Ruff (UP007)

portalocker/redis.py:68:13: UP007 Use `X | Y` for type annotations

Check failure on line 68 in portalocker/redis.py

View workflow job for this annotation

GitHub Actions / lint (3.11)

Ruff (UP007)

portalocker/redis.py:68:13: UP007 Use `X | Y` for type annotations

Check failure on line 68 in portalocker/redis.py

View workflow job for this annotation

GitHub Actions / lint (3.8)

Ruff (UP007)

portalocker/redis.py:68:13: UP007 Use `X | Y` for type annotations
Expand Down Expand Up @@ -137,7 +139,7 @@ def acquire( # type: ignore[override]
timeout: typing.Optional[float] = None,
check_interval: typing.Optional[float] = None,
fail_when_locked: typing.Optional[bool] = None,
) -> 'RedisLock':
) -> RedisLock:
timeout = utils.coalesce(timeout, self.timeout, 0.0)
check_interval = utils.coalesce(
check_interval,
Expand Down
18 changes: 9 additions & 9 deletions portalocker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def acquire(
check_interval: typing.Optional[float] = None,
fail_when_locked: typing.Optional[bool] = None,
) -> typing.IO[typing.AnyStr]:
"""Acquire the locked filehandle"""
'''Acquire the locked filehandle'''

fail_when_locked = coalesce(fail_when_locked, self.fail_when_locked)

Expand Down Expand Up @@ -316,14 +316,14 @@ def __enter__(self) -> typing.IO[typing.AnyStr]:
return self.acquire()

def release(self):
"""Releases the currently locked file handle"""
'''Releases the currently locked file handle'''
if self.fh:
portalocker.unlock(self.fh)
self.fh.close()
self.fh = None

def _get_fh(self) -> types.IO:
"""Get a new filehandle"""
'''Get a new filehandle'''
return typing.cast(
types.IO,
open( # noqa: SIM115
Expand All @@ -334,20 +334,20 @@ def _get_fh(self) -> types.IO:
)

def _get_lock(self, fh: types.IO) -> types.IO:
"""
'''
Try to lock the given filehandle
returns LockException if it fails"""
returns LockException if it fails'''
portalocker.lock(fh, self.flags)
return fh

def _prepare_fh(self, fh: types.IO) -> types.IO:
"""
'''
Prepare the filehandle for usage
If truncate is a number, the file will be truncated to that amount of
bytes
"""
'''
if self.truncate:
fh.seek(0)
fh.truncate(0)
Expand All @@ -356,11 +356,11 @@ def _prepare_fh(self, fh: types.IO) -> types.IO:


class RLock(Lock):
"""
'''
A reentrant lock, functions in a similar way to threading.RLock in that it
can be acquired multiple times. When the corresponding number of release()
calls are made the lock will finally release the underlying file lock.
"""
'''

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion portalocker_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ def pytest_sessionstart(session):

@pytest.fixture(autouse=True)
def reduce_timeouts(monkeypatch):
"For faster testing we reduce the timeouts."
'For faster testing we reduce the timeouts.'
monkeypatch.setattr(utils, 'DEFAULT_TIMEOUT', 0.1)
monkeypatch.setattr(utils, 'DEFAULT_CHECK_INTERVAL', 0.05)
4 changes: 2 additions & 2 deletions portalocker_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def test_lock_fileno(tmpfile, locker):
)
@pytest.mark.parametrize('locker', LOCKERS, indirect=True)
def test_locker_mechanism(tmpfile, locker):
"""Can we switch the locking mechanism?"""
'''Can we switch the locking mechanism?'''
# We can test for flock vs lockf based on their different behaviour re.
# locking the same file.
with portalocker.Lock(tmpfile, 'a+', flags=LockFlags.EXCLUSIVE):
Expand All @@ -434,7 +434,7 @@ def test_locker_mechanism(tmpfile, locker):


def test_exception(monkeypatch, tmpfile):
"""Do we stop immediately if the locking fails, even with a timeout?"""
'''Do we stop immediately if the locking fails, even with a timeout?'''

def patched_lock(*args, **kwargs):
raise ValueError('Test exception')
Expand Down

0 comments on commit cada500

Please sign in to comment.