zstdlib 0.10.7__tar.gz → 0.10.9__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {zstdlib-0.10.7 → zstdlib-0.10.9}/PKG-INFO +3 -4
- {zstdlib-0.10.7 → zstdlib-0.10.9}/pyproject.toml +9 -5
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/log/test_cute.py +12 -3
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/log/test_trace.py +3 -3
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/test_enum.py +3 -3
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/test_exc_capture.py +6 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/test_io.py +8 -8
- zstdlib-0.10.9/tests/test_ip_lock.py +350 -0
- zstdlib-0.10.9/tests/test_keyed_instance.py +306 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/test_prompt.py +8 -2
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/test_singleton.py +7 -4
- zstdlib-0.10.9/tests/test_util.py +32 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/__init__.py +14 -1
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/ansi.py +3 -3
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/enum.py +7 -11
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/exc_capture.py +19 -1
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/frozen.py +30 -19
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/io.py +29 -23
- zstdlib-0.10.9/zstdlib/ip_lock.py +287 -0
- zstdlib-0.10.9/zstdlib/keyed_instance.py +169 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/lazy.py +2 -5
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/macos.py +3 -3
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/not_set.py +1 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/prompt.py +4 -1
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/signal_cm.py +2 -1
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/singleton.py +3 -5
- zstdlib-0.10.9/zstdlib/util.py +8 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib.egg-info/PKG-INFO +3 -4
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib.egg-info/SOURCES.txt +6 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib.egg-info/requires.txt +1 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/LICENSE +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/README.md +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/setup.cfg +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/__init__.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/log/__init__.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/log/base.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/test_ansi.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/test_frozen.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/test_hybridmethod.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/test_lazy.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/tests/test_not_set.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/hybridmethod.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/log/__init__.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/log/cute.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/log/trace.py +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib/py.typed +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib.egg-info/dependency_links.txt +0 -0
- {zstdlib-0.10.7 → zstdlib-0.10.9}/zstdlib.egg-info/top_level.txt +0 -0
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: zstdlib
|
|
3
|
-
Version: 0.10.
|
|
3
|
+
Version: 0.10.9
|
|
4
4
|
Summary: A set of useful python utilities
|
|
5
|
-
License:
|
|
5
|
+
License-Expression: GPL-3.0-only
|
|
6
6
|
Project-URL: Homepage, https://github.com/zwimer/zstdlib
|
|
7
7
|
Classifier: Programming Language :: Python :: 3
|
|
8
8
|
Classifier: Programming Language :: Python :: 3.14
|
|
9
|
-
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
-
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
|
|
11
9
|
Requires-Python: >=3.14
|
|
12
10
|
Description-Content-Type: text/markdown
|
|
13
11
|
License-File: LICENSE
|
|
12
|
+
Requires-Dist: fasteners
|
|
14
13
|
Requires-Dist: osascript2
|
|
15
14
|
Dynamic: license-file
|
|
16
15
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["setuptools>=
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
3
|
build-backend = "setuptools.build_meta"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
@@ -7,15 +7,15 @@ name = "zstdlib"
|
|
|
7
7
|
classifiers = [
|
|
8
8
|
"Programming Language :: Python :: 3",
|
|
9
9
|
"Programming Language :: Python :: 3.14",
|
|
10
|
-
"Development Status :: 5 - Production/Stable",
|
|
11
|
-
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
|
|
12
10
|
]
|
|
13
|
-
license =
|
|
11
|
+
license = "GPL-3.0-only"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
14
13
|
description = "A set of useful python utilities"
|
|
15
14
|
urls = {Homepage = "https://github.com/zwimer/zstdlib"}
|
|
16
15
|
requires-python = ">= 3.14"
|
|
17
16
|
dynamic = ["version"]
|
|
18
17
|
dependencies = [
|
|
18
|
+
"fasteners",
|
|
19
19
|
"osascript2" # For macOS
|
|
20
20
|
]
|
|
21
21
|
|
|
@@ -25,7 +25,6 @@ content-type = "text/markdown"
|
|
|
25
25
|
|
|
26
26
|
[tool.setuptools]
|
|
27
27
|
include-package-data = true
|
|
28
|
-
license-files = ["LICENSE"]
|
|
29
28
|
|
|
30
29
|
[tool.setuptools.packages.find]
|
|
31
30
|
namespaces = false
|
|
@@ -40,6 +39,7 @@ attr = "zstdlib.__version__"
|
|
|
40
39
|
|
|
41
40
|
[tool.pylint.MASTER]
|
|
42
41
|
ignore-paths = '^tests/.*$'
|
|
42
|
+
jobs = 0
|
|
43
43
|
[tool.pylint."MESSAGES CONTROL"]
|
|
44
44
|
disable = [
|
|
45
45
|
"unnecessary-lambda-assignment",
|
|
@@ -50,6 +50,9 @@ disable = [
|
|
|
50
50
|
"invalid-name"
|
|
51
51
|
]
|
|
52
52
|
|
|
53
|
+
[tool.mypy]
|
|
54
|
+
strict = true
|
|
55
|
+
|
|
53
56
|
[tool.black]
|
|
54
57
|
line-length = 110
|
|
55
58
|
target-version = ["py314"]
|
|
@@ -63,6 +66,7 @@ ignore=[
|
|
|
63
66
|
"E731",
|
|
64
67
|
"I001",
|
|
65
68
|
"PLR0913", "PLR2004",
|
|
69
|
+
"PYI025", "PYI041",
|
|
66
70
|
"S101", "S603",
|
|
67
71
|
"TID252",
|
|
68
72
|
]
|
|
@@ -69,7 +69,10 @@ class TestCuteFormatter(LeftBase, unittest.TestCase):
|
|
|
69
69
|
for i in levels:
|
|
70
70
|
log.log(i, msg)
|
|
71
71
|
self.assertEqual(len(levels), len(self.messages[log]))
|
|
72
|
-
|
|
72
|
+
|
|
73
|
+
def l_name(idx: int) -> str:
|
|
74
|
+
return self.messages[log][idx].split("|")[0].strip()
|
|
75
|
+
|
|
73
76
|
self.assertEqual(Color.dim("Level 5".ljust(8)), l_name(0))
|
|
74
77
|
self.assertEqual("DEBUG", l_name(1))
|
|
75
78
|
self.assertEqual(Color.blue("INFO".ljust(8)), l_name(2))
|
|
@@ -85,7 +88,10 @@ class TestCuteFormatter(LeftBase, unittest.TestCase):
|
|
|
85
88
|
log.log(4, msg)
|
|
86
89
|
log.log(5, msg)
|
|
87
90
|
self.assertEqual(2, len(self.messages[log]))
|
|
88
|
-
|
|
91
|
+
|
|
92
|
+
def l_name(idx: int) -> str:
|
|
93
|
+
return self.messages[log][idx].split("|")[0].strip()
|
|
94
|
+
|
|
89
95
|
self.assertEqual(Color.dim("Level 4".ljust(8)), l_name(0))
|
|
90
96
|
self.assertEqual("Level 5", l_name(1))
|
|
91
97
|
|
|
@@ -96,7 +102,10 @@ class TestCuteFormatter(LeftBase, unittest.TestCase):
|
|
|
96
102
|
log.log(14, msg)
|
|
97
103
|
log.log(15, msg)
|
|
98
104
|
self.assertEqual(2, len(self.messages[log]))
|
|
99
|
-
|
|
105
|
+
|
|
106
|
+
def l_name(idx: int) -> str:
|
|
107
|
+
return self.messages[log][idx].split("|")[0].strip()
|
|
108
|
+
|
|
100
109
|
self.assertEqual("Level 14".ljust(8), l_name(0))
|
|
101
110
|
self.assertEqual(Color.bright_red_bg_yellow("Level 15".ljust(8)), l_name(1))
|
|
102
111
|
|
|
@@ -42,15 +42,15 @@ class TestTrace(LeftBase, unittest.TestCase):
|
|
|
42
42
|
self.assertTrue(hasattr(logging, "trace"))
|
|
43
43
|
self.assertTrue(hasattr(logging.getLogger(), "trace"))
|
|
44
44
|
self.assertEqual(logging.TRACE, 5)
|
|
45
|
-
self.assertEqual(logging.getLevelName(logging.TRACE), "TRACE")
|
|
45
|
+
self.assertEqual(logging.getLevelName(logging.TRACE), "TRACE")
|
|
46
46
|
# Root check
|
|
47
47
|
with self.hijack("") as log:
|
|
48
48
|
old = log.getEffectiveLevel()
|
|
49
49
|
# pylint: disable-next=not-callable
|
|
50
|
-
logging.trace("test1")
|
|
50
|
+
logging.trace("test1")
|
|
51
51
|
log.setLevel(logging.DEBUG)
|
|
52
52
|
# pylint: disable-next=not-callable
|
|
53
|
-
logging.trace("test2")
|
|
53
|
+
logging.trace("test2")
|
|
54
54
|
log.setLevel(old)
|
|
55
55
|
self.assertEqual(self.messages[log], ["test1"])
|
|
56
56
|
# Logger check
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring,unused-variable
|
|
2
|
-
from typing import Self
|
|
2
|
+
from typing import Self, Any
|
|
3
3
|
import unittest
|
|
4
4
|
|
|
5
5
|
from zstdlib.enum import EnumType, Enum, entries, values, auto
|
|
@@ -25,7 +25,7 @@ class TestEnumType(unittest.TestCase):
|
|
|
25
25
|
class ET1(metaclass=EnumType, dupes_ok=True):
|
|
26
26
|
a: int = 5
|
|
27
27
|
b: int = 5
|
|
28
|
-
c: dict = {} # noqa: RUF012
|
|
28
|
+
c: dict[Any, Any] = {} # noqa: RUF012
|
|
29
29
|
|
|
30
30
|
with self.assertRaises(ValueError):
|
|
31
31
|
|
|
@@ -36,7 +36,7 @@ class TestEnumType(unittest.TestCase):
|
|
|
36
36
|
with self.assertRaises(TypeError):
|
|
37
37
|
|
|
38
38
|
class ET3(metaclass=EnumType):
|
|
39
|
-
a: dict = {} # noqa: RUF012
|
|
39
|
+
a: dict[Any, Any] = {} # noqa: RUF012
|
|
40
40
|
|
|
41
41
|
def test_type_check(self) -> None:
|
|
42
42
|
class ET1(metaclass=EnumType, type_check=set()):
|
|
@@ -47,6 +47,12 @@ class TestLazy(unittest.TestCase):
|
|
|
47
47
|
with non.capture(RuntimeError):
|
|
48
48
|
raise RuntimeError()
|
|
49
49
|
self.assertEqual(len(non.errors), 1)
|
|
50
|
+
# run_fn
|
|
51
|
+
ke = ExcCapture(KeyError)
|
|
52
|
+
self.assertEqual(ke.run_fn({0: 5}.__getitem__, 0), 5)
|
|
53
|
+
self.assertEqual(len(ke.errors), 0)
|
|
54
|
+
self.assertIs(ke.run_fn({}.__getitem__, 0), ke.NoResult)
|
|
55
|
+
self.assertEqual(len(ke.errors), 1)
|
|
50
56
|
|
|
51
57
|
def test_delay(self) -> None:
|
|
52
58
|
# No raise
|
|
@@ -5,7 +5,7 @@ import unittest
|
|
|
5
5
|
from zstdlib.io import ProtectedFile, BinaryIO, TextIO, io
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
class RamFile(tempfile.SpooledTemporaryFile):
|
|
8
|
+
class RamFile(tempfile.SpooledTemporaryFile[Any]):
|
|
9
9
|
"""A file-like object that stores its contents in memory."""
|
|
10
10
|
|
|
11
11
|
rollover = NotImplemented # Disallow rollover
|
|
@@ -22,7 +22,7 @@ class RamFile(tempfile.SpooledTemporaryFile):
|
|
|
22
22
|
class TestIO(unittest.TestCase):
|
|
23
23
|
|
|
24
24
|
def test_init(self) -> None:
|
|
25
|
-
with RamFile(mode="
|
|
25
|
+
with RamFile(mode="w+b") as fb, RamFile() as fs:
|
|
26
26
|
ts = TextIO(fs)
|
|
27
27
|
tb = BinaryIO(fb)
|
|
28
28
|
with self.assertRaises(TypeError):
|
|
@@ -44,7 +44,7 @@ class TestIO(unittest.TestCase):
|
|
|
44
44
|
def test_unread(self) -> None:
|
|
45
45
|
with RamFile() as raw:
|
|
46
46
|
f = io(raw)
|
|
47
|
-
f.unread("hello")
|
|
47
|
+
f.unread("hello")
|
|
48
48
|
self.assertEqual("hello", f._buffer)
|
|
49
49
|
|
|
50
50
|
def test_read(self) -> None:
|
|
@@ -55,18 +55,18 @@ class TestIO(unittest.TestCase):
|
|
|
55
55
|
self.assertEqual("fo", f.read(2))
|
|
56
56
|
self.assertEqual("o", f.read(1))
|
|
57
57
|
# Test read of buffer
|
|
58
|
-
f.unread("bar")
|
|
58
|
+
f.unread("bar")
|
|
59
59
|
self.assertEqual("b", f.read(1))
|
|
60
60
|
# Test read of file + buffer
|
|
61
|
-
f.unread("foob")
|
|
61
|
+
f.unread("foob")
|
|
62
62
|
self.assertEqual("foobar", f.read(6))
|
|
63
|
-
f.unread("foobar")
|
|
63
|
+
f.unread("foobar")
|
|
64
64
|
self.assertEqual("foobarbaz", f.read())
|
|
65
65
|
|
|
66
66
|
def test_peek(self) -> None:
|
|
67
67
|
with RamFile() as raw:
|
|
68
68
|
f = io(raw)
|
|
69
|
-
f.unread("foo")
|
|
69
|
+
f.unread("foo")
|
|
70
70
|
# Test peek of buffer
|
|
71
71
|
for i in range(3):
|
|
72
72
|
self.assertEqual("fo", f.peek(2))
|
|
@@ -77,7 +77,7 @@ class TestIO(unittest.TestCase):
|
|
|
77
77
|
self.assertEqual("ba", f.peek(2))
|
|
78
78
|
self.assertEqual("bar", f.read())
|
|
79
79
|
# Test peek of both
|
|
80
|
-
f.unread("baz")
|
|
80
|
+
f.unread("baz")
|
|
81
81
|
raw.load("qux")
|
|
82
82
|
for i in range(3):
|
|
83
83
|
for k in range(5):
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring
|
|
2
|
+
from tempfile import TemporaryDirectory, NamedTemporaryFile as NTF
|
|
3
|
+
from collections.abc import Callable, Iterator
|
|
4
|
+
from contextlib import contextmanager
|
|
5
|
+
import multiprocessing as mp
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
import unittest
|
|
9
|
+
import time
|
|
10
|
+
import os
|
|
11
|
+
|
|
12
|
+
from zstdlib import (
|
|
13
|
+
GlobalIPLock as GIPL,
|
|
14
|
+
AbstractIPLock,
|
|
15
|
+
IPLock,
|
|
16
|
+
RIPLock,
|
|
17
|
+
LockHung,
|
|
18
|
+
LockPathInUse,
|
|
19
|
+
NotAcquired,
|
|
20
|
+
normpath,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
DELAY = 0.25 # Lower is faster but less accurate
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@contextmanager
|
|
27
|
+
def mk_gipl() -> Iterator[GIPL]:
|
|
28
|
+
with NTF(delete_on_close=False) as f:
|
|
29
|
+
f.close()
|
|
30
|
+
yield GIPL.new(Path(f.name))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@contextmanager
|
|
34
|
+
def mk_lock[T: (IPLock, RIPLock)](lock_t: type[T]) -> Iterator[T]:
|
|
35
|
+
assert lock_t in (IPLock, RIPLock)
|
|
36
|
+
with NTF(delete_on_close=False) as f:
|
|
37
|
+
f.close()
|
|
38
|
+
yield lock_t(Path(f.name))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@contextmanager
|
|
42
|
+
def enter(lock: AbstractIPLock, timeout: int | float = 10) -> Iterator[None]:
|
|
43
|
+
assert 0 <= timeout <= 10
|
|
44
|
+
old = lock.acquired
|
|
45
|
+
if not lock.acquire(timeout_per=timeout):
|
|
46
|
+
raise RuntimeError("Failed to acquire")
|
|
47
|
+
try:
|
|
48
|
+
yield
|
|
49
|
+
finally:
|
|
50
|
+
lock.release()
|
|
51
|
+
if old != lock.acquired:
|
|
52
|
+
raise RuntimeError("Different state")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class IPLog: # Not thread-safe
|
|
56
|
+
|
|
57
|
+
def __init__(self) -> None:
|
|
58
|
+
self._q: mp.Queue[tuple[float, int, str]] = mp.Queue()
|
|
59
|
+
self._q.put((0, 0, ""))
|
|
60
|
+
self._main: int = os.getpid()
|
|
61
|
+
self._done: bool = False
|
|
62
|
+
|
|
63
|
+
def __call__(self, s: str, pid: int | None = None) -> None:
|
|
64
|
+
main = os.getpid() == self._main
|
|
65
|
+
assert main == (pid is None)
|
|
66
|
+
if main:
|
|
67
|
+
pid = 1
|
|
68
|
+
assert not self._done and isinstance(pid, int) and (main or pid > 1)
|
|
69
|
+
self._q.put((time.monotonic(), pid, s))
|
|
70
|
+
|
|
71
|
+
def read(self) -> list[str]:
|
|
72
|
+
assert not self._done
|
|
73
|
+
self._done = True
|
|
74
|
+
self._q.put((-1, 0, ""))
|
|
75
|
+
contents: list[tuple[float, int, str]] = []
|
|
76
|
+
while not contents or contents[-1][0] != -1:
|
|
77
|
+
contents.append(self._q.get())
|
|
78
|
+
contents = contents[1:-1]
|
|
79
|
+
if not contents:
|
|
80
|
+
return []
|
|
81
|
+
pids = list({i[1] for i in contents})
|
|
82
|
+
rv = sorted(f"{i}|PID-{pids.index(j)+1}: {k}" for i, j, k in contents)
|
|
83
|
+
return [i.split("|", 1)[1] for i in rv]
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
# For remote testing
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class RemoteProc:
|
|
90
|
+
|
|
91
|
+
type Logger = Callable[[str], None]
|
|
92
|
+
|
|
93
|
+
def __init__(self, fn: Callable[[Logger, Path], None], log: IPLog, path: Path, *, pid: int) -> None:
|
|
94
|
+
if pid < 2:
|
|
95
|
+
raise ValueError("Bad pid")
|
|
96
|
+
self._p = mp.get_context("spawn").Process(target=self._wrapper)
|
|
97
|
+
self._func = fn
|
|
98
|
+
self._pid = pid
|
|
99
|
+
self._done = False
|
|
100
|
+
self._path = path
|
|
101
|
+
self._log = log
|
|
102
|
+
|
|
103
|
+
def start(self) -> None:
|
|
104
|
+
self._p.start()
|
|
105
|
+
|
|
106
|
+
def die(self, timeout: int) -> None:
|
|
107
|
+
assert isinstance(timeout, int) and 0 <= timeout <= 10
|
|
108
|
+
self._p.join(timeout)
|
|
109
|
+
if self._p.is_alive():
|
|
110
|
+
self._log("Alive")
|
|
111
|
+
self._p.kill()
|
|
112
|
+
|
|
113
|
+
def _wrapper(self) -> None:
|
|
114
|
+
|
|
115
|
+
def log(s: str) -> None:
|
|
116
|
+
self._log(s, pid=self._pid)
|
|
117
|
+
|
|
118
|
+
log("START")
|
|
119
|
+
try:
|
|
120
|
+
self._func(log, self._path)
|
|
121
|
+
log("FIN")
|
|
122
|
+
except Exception as e: # noqa: BLE001
|
|
123
|
+
log(f"{e.__class__.__name__}: {e.args}")
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def gipl_p1(log: RemoteProc.Logger, path: Path) -> None:
|
|
127
|
+
with GIPL.new(path) as lock:
|
|
128
|
+
log("Locked")
|
|
129
|
+
time.sleep(DELAY)
|
|
130
|
+
with lock:
|
|
131
|
+
log("Double locked; sleeping")
|
|
132
|
+
time.sleep(DELAY)
|
|
133
|
+
log("Awake")
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
# Unit tests
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class TestGlobalProcessLock(unittest.TestCase):
|
|
140
|
+
|
|
141
|
+
def test_bad_usage(self) -> None:
|
|
142
|
+
# Path
|
|
143
|
+
bad = Path("a/b/../c")
|
|
144
|
+
norm = normpath(bad)
|
|
145
|
+
fixed = norm.absolute()
|
|
146
|
+
assert bad != norm != fixed and fixed == Path.cwd() / norm
|
|
147
|
+
with self.assertRaises(ValueError):
|
|
148
|
+
GIPL(bad)
|
|
149
|
+
self.assertEqual(GIPL.new(bad).path, fixed)
|
|
150
|
+
self.assertEqual(GIPL.new(str(bad)).path, fixed)
|
|
151
|
+
with self.assertRaises(TypeError):
|
|
152
|
+
GIPL(str(fixed)) # type: ignore[arg-type]
|
|
153
|
+
# File
|
|
154
|
+
with TemporaryDirectory() as _td:
|
|
155
|
+
td = GIPL.normalize(_td, check=False)
|
|
156
|
+
with self.assertRaises(FileExistsError):
|
|
157
|
+
_ = GIPL(td)
|
|
158
|
+
no_exist = td / "no_exist"
|
|
159
|
+
self.assertIs(GIPL(no_exist), GIPL.new(no_exist))
|
|
160
|
+
norm = td / "normal"
|
|
161
|
+
norm.touch()
|
|
162
|
+
self.assertIs(GIPL(norm), GIPL.new(norm))
|
|
163
|
+
symlink = td / "symlink"
|
|
164
|
+
symlink.symlink_to(norm)
|
|
165
|
+
assert symlink.resolve() == norm
|
|
166
|
+
with self.assertRaises(FileExistsError): # test for Path, easier here b/c temp dir
|
|
167
|
+
_ = GIPL(symlink)
|
|
168
|
+
with self.assertRaises(FileExistsError):
|
|
169
|
+
GIPL.new(symlink)
|
|
170
|
+
hardlink = td / "hardlink"
|
|
171
|
+
hardlink.hardlink_to(norm)
|
|
172
|
+
with self.assertRaises(FileExistsError):
|
|
173
|
+
GIPL(hardlink)
|
|
174
|
+
with self.assertRaises(FileExistsError):
|
|
175
|
+
GIPL(hardlink)
|
|
176
|
+
# Timeout
|
|
177
|
+
with mk_gipl() as lock:
|
|
178
|
+
self.assertFalse(lock.acquired)
|
|
179
|
+
with self.assertRaises(TypeError):
|
|
180
|
+
lock.acquire(timeout_per="q") # type: ignore[arg-type]
|
|
181
|
+
with self.assertRaises(ValueError):
|
|
182
|
+
lock.acquire(timeout_per=-1)
|
|
183
|
+
with self.assertRaises(ValueError):
|
|
184
|
+
lock.acquire(blocking=False, timeout_per=1)
|
|
185
|
+
self.assertFalse(lock.acquired)
|
|
186
|
+
self.assertTrue(lock.acquire(timeout_per=1))
|
|
187
|
+
lock.release()
|
|
188
|
+
self.assertFalse(lock.acquired)
|
|
189
|
+
self.assertTrue(lock.acquire(timeout_per=1.0))
|
|
190
|
+
lock.release()
|
|
191
|
+
self.assertFalse(lock.acquired)
|
|
192
|
+
|
|
193
|
+
def test_sole_owner(self) -> None:
|
|
194
|
+
with mk_gipl() as lock:
|
|
195
|
+
self.assertFalse(lock.acquired)
|
|
196
|
+
# Can acquire existing file
|
|
197
|
+
self.assertTrue(lock.acquire(timeout_per=0))
|
|
198
|
+
self.assertTrue(lock.acquired)
|
|
199
|
+
# Re-acquisition, count == 2
|
|
200
|
+
self.assertTrue(lock.acquire(timeout_per=0))
|
|
201
|
+
self.assertTrue(lock.acquired)
|
|
202
|
+
# Release 1/2
|
|
203
|
+
lock.release()
|
|
204
|
+
self.assertTrue(lock.acquired)
|
|
205
|
+
# Release 2/2
|
|
206
|
+
lock.release()
|
|
207
|
+
self.assertFalse(lock.acquired)
|
|
208
|
+
# Release 3/2
|
|
209
|
+
with self.assertRaises(NotAcquired):
|
|
210
|
+
lock.release()
|
|
211
|
+
self.assertFalse(lock.acquired)
|
|
212
|
+
# CM exit, lock file removed by NTF
|
|
213
|
+
# Fresh acquire on non-existing file
|
|
214
|
+
lock.acquire(timeout_per=0)
|
|
215
|
+
lock.release()
|
|
216
|
+
|
|
217
|
+
def test_global(self) -> None:
|
|
218
|
+
with mk_gipl() as lock:
|
|
219
|
+
other = GIPL(lock.path)
|
|
220
|
+
self.assertFalse(lock.acquired or other.acquired)
|
|
221
|
+
self.assertTrue(lock.acquire(timeout_per=1))
|
|
222
|
+
self.assertTrue(lock.acquired and other.acquired)
|
|
223
|
+
self.assertTrue(lock.acquire(timeout_per=1))
|
|
224
|
+
other.release()
|
|
225
|
+
self.assertTrue(lock.acquired and other.acquired)
|
|
226
|
+
other.release()
|
|
227
|
+
self.assertFalse(lock.acquired or other.acquired)
|
|
228
|
+
|
|
229
|
+
def test_ipc(self) -> None:
|
|
230
|
+
log = IPLog()
|
|
231
|
+
with mk_gipl() as lock:
|
|
232
|
+
p = RemoteProc(gipl_p1, log, lock.path, pid=2)
|
|
233
|
+
with enter(lock):
|
|
234
|
+
log("Starting child")
|
|
235
|
+
p.start()
|
|
236
|
+
time.sleep(DELAY)
|
|
237
|
+
log("Releasing")
|
|
238
|
+
lock.release()
|
|
239
|
+
time.sleep(DELAY)
|
|
240
|
+
log("Reclaiming")
|
|
241
|
+
lock.acquire(timeout_per=1)
|
|
242
|
+
log("Got")
|
|
243
|
+
expected: list[str] = [
|
|
244
|
+
"PID-1: Starting child",
|
|
245
|
+
"PID-2: START",
|
|
246
|
+
"PID-1: Releasing",
|
|
247
|
+
"PID-2: Locked",
|
|
248
|
+
"PID-1: Reclaiming",
|
|
249
|
+
"PID-2: Double locked; sleeping",
|
|
250
|
+
"PID-2: Awake",
|
|
251
|
+
"PID-2: FIN",
|
|
252
|
+
"PID-1: Got",
|
|
253
|
+
]
|
|
254
|
+
self.assertEqual(log.read(), expected)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
class TestIPLocks(unittest.TestCase):
|
|
258
|
+
|
|
259
|
+
def test_locks(self) -> None:
|
|
260
|
+
# Test using a basic IPLock b/c more likely to err if we do something dumb
|
|
261
|
+
# We already trust GlobalProcessLock, threading.Lock, and threading.RLock
|
|
262
|
+
# so instead of spinning up threads and processes, just verify their states
|
|
263
|
+
with mk_lock(IPLock) as lock:
|
|
264
|
+
lock2 = IPLock(lock.path)
|
|
265
|
+
self.assertIsNot(lock, lock2)
|
|
266
|
+
self.assertEqual(lock.path, lock2.path)
|
|
267
|
+
# pylint: disable=protected-access
|
|
268
|
+
self.assertIs(lock._gipl, lock2._gipl)
|
|
269
|
+
self.assertFalse(lock._gipl.acquired)
|
|
270
|
+
with enter(lock):
|
|
271
|
+
|
|
272
|
+
def chk() -> None:
|
|
273
|
+
self.assertTrue(lock._gipl.acquired)
|
|
274
|
+
self.assertTrue(lock.acquired)
|
|
275
|
+
self.assertTrue(lock._sync_lock.locked())
|
|
276
|
+
self.assertFalse(lock2.acquired)
|
|
277
|
+
self.assertTrue(lock2._sync_lock.locked())
|
|
278
|
+
|
|
279
|
+
chk()
|
|
280
|
+
self.assertFalse(lock2.acquire(timeout_per=2))
|
|
281
|
+
chk()
|
|
282
|
+
self.assertFalse(lock._gipl.acquired or lock2._gipl.acquired)
|
|
283
|
+
self.assertFalse(lock._sync_lock.locked())
|
|
284
|
+
self.assertFalse(lock2._sync_lock.locked())
|
|
285
|
+
# pylint: enable=protected-access
|
|
286
|
+
# Test releasing when we don't have
|
|
287
|
+
self.assertFalse(lock.acquired)
|
|
288
|
+
with self.assertRaises(NotAcquired):
|
|
289
|
+
lock.release()
|
|
290
|
+
# pylint: disable=protected-access
|
|
291
|
+
lock._rc += 1
|
|
292
|
+
with self.assertRaises(RuntimeError):
|
|
293
|
+
lock.release()
|
|
294
|
+
lock._rc += 1
|
|
295
|
+
lock._sync_lock.acquire()
|
|
296
|
+
self.assertFalse(lock._gipl.acquired)
|
|
297
|
+
# pylint: enable=protected-access
|
|
298
|
+
with self.assertRaises(OSError):
|
|
299
|
+
lock.release()
|
|
300
|
+
|
|
301
|
+
# Fake GIPL cannot acquire test
|
|
302
|
+
class FakeGIPL:
|
|
303
|
+
def acquire(self, *_: Any, **__: Any) -> bool:
|
|
304
|
+
return False
|
|
305
|
+
|
|
306
|
+
with mk_lock(IPLock) as lock:
|
|
307
|
+
# pylint: disable=protected-access
|
|
308
|
+
old = lock._gipl
|
|
309
|
+
lock._gipl = FakeGIPL() # type: ignore[assignment ,misc]
|
|
310
|
+
self.assertFalse(lock.acquire(timeout_per=3))
|
|
311
|
+
lock._gipl = old # type: ignore[misc]
|
|
312
|
+
# pylint: enable=protected-access
|
|
313
|
+
|
|
314
|
+
def test_lock_types(self) -> None:
|
|
315
|
+
# pylint: disable=protected-access
|
|
316
|
+
self.assertFalse(IPLock._cls_reentrant)
|
|
317
|
+
self.assertTrue(RIPLock._cls_reentrant)
|
|
318
|
+
with mk_lock(IPLock) as lock:
|
|
319
|
+
self.assertFalse(lock._instance_reentrant)
|
|
320
|
+
del lock
|
|
321
|
+
with mk_lock(RIPLock) as rlock:
|
|
322
|
+
self.assertTrue(rlock._instance_reentrant)
|
|
323
|
+
del rlock
|
|
324
|
+
|
|
325
|
+
# pylint: enable=protected-access
|
|
326
|
+
with mk_lock(IPLock) as lock, enter(lock):
|
|
327
|
+
self.assertTrue(lock.acquired)
|
|
328
|
+
self.assertFalse(lock.acquire(timeout_per=2))
|
|
329
|
+
self.assertTrue(lock.acquired)
|
|
330
|
+
self.assertFalse(lock.acquired)
|
|
331
|
+
del lock
|
|
332
|
+
with mk_lock(RIPLock) as rlock, enter(rlock), enter(rlock):
|
|
333
|
+
self.assertTrue(rlock.acquired)
|
|
334
|
+
self.assertFalse(rlock.acquired)
|
|
335
|
+
del rlock
|
|
336
|
+
|
|
337
|
+
with mk_lock(IPLock) as lock, self.assertRaises(LockPathInUse):
|
|
338
|
+
_ = RIPLock(lock.path)
|
|
339
|
+
del lock
|
|
340
|
+
with mk_lock(RIPLock) as rlock, self.assertRaises(LockPathInUse):
|
|
341
|
+
_ = IPLock(rlock.path)
|
|
342
|
+
del rlock
|
|
343
|
+
|
|
344
|
+
def test_lock_hung(self) -> None:
|
|
345
|
+
with mk_lock(IPLock) as lock, enter(lock), self.assertRaises(LockHung):
|
|
346
|
+
_ = lock.acquire()
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
if __name__ == "__main__":
|
|
350
|
+
unittest.main()
|