Skip to content

Commit 76ad78c

Browse files
authoredJul 8, 2023
Correct Exception Type for SocketLock (#24)
1 parent 8fc32e9 commit 76ad78c

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed
 

‎locking/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""
22
xxx
33
"""
4+
from .custom_exceptions import CouldNotLockException
45
from .baselock import BaseLock
56
from .filelock import FileLock
67
from .socketlock import SocketLock
78
from .utils import get_caller
89

910
outgoing = [
1011
BaseLock,
12+
CouldNotLockException,
1113
FileLock,
1214
get_caller,
1315
SocketLock,

‎locking/baselock.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
import time
44

55
from .utils import get_caller
6+
from .custom_exceptions import CouldNotLockException
67

78
TIMEOUT_MAX = int(sys.maxsize / 10**9)
89

910

10-
class CouldNotLockException(Exception):
11-
pass
12-
13-
1411
class BaseLock:
1512
def __init__(self, lockname=None, block=True):
1613
self.lockname = lockname or get_caller()

‎locking/socketlock/socketlock.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import socket
55
import time
66

7-
from .. import BaseLock
7+
from .. import BaseLock, CouldNotLockException
88

99

1010
class SocketLock(BaseLock):
@@ -45,7 +45,7 @@ def acquire(self, blocking=True, timeout=-1):
4545
return False
4646
time.sleep(random.random() * 0.005)
4747
else:
48-
raise
48+
raise CouldNotLockException()
4949

5050
def release(self):
5151
"""Release the lock."""

‎locking/tests/test_locks.py

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
from _thread import start_new_thread
55
from .. import FileLock, SocketLock
6+
from ..custom_exceptions import CouldNotLockException
67

78
try:
89
# try to import lock_tests as if it has been distributed with cpython
@@ -37,6 +38,11 @@ def test_at_fork_reinit(self):
3738
class SocketLockTests(LockTClass):
3839
locktype = staticmethod(SocketLock)
3940

41+
def test_raises_right_exception(self):
42+
with SocketLock(lockname="uniquename"):
43+
with self.assertRaises(CouldNotLockException):
44+
with SocketLock(lockname="uniquename"):
45+
pass
4046

4147
class FileLockTests(LockTClass):
4248
locktype = staticmethod(FileLock)

0 commit comments

Comments
 (0)
Please sign in to comment.