catchlib 1.0.1__tar.gz → 1.0.3__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.
- {catchlib-1.0.1/src/catchlib.egg-info → catchlib-1.0.3}/PKG-INFO +1 -1
- {catchlib-1.0.1 → catchlib-1.0.3}/pyproject.toml +1 -1
- {catchlib-1.0.1 → catchlib-1.0.3}/src/catchlib/core/__init__.py +5 -4
- catchlib-1.0.3/src/catchlib/tests/__init__.py +16 -0
- {catchlib-1.0.1 → catchlib-1.0.3}/src/catchlib/tests/test_0.py +14 -6
- {catchlib-1.0.1 → catchlib-1.0.3/src/catchlib.egg-info}/PKG-INFO +1 -1
- catchlib-1.0.1/src/catchlib/tests/__init__.py +0 -12
- {catchlib-1.0.1 → catchlib-1.0.3}/LICENSE.txt +0 -0
- {catchlib-1.0.1 → catchlib-1.0.3}/MANIFEST.in +0 -0
- {catchlib-1.0.1 → catchlib-1.0.3}/README.rst +0 -0
- {catchlib-1.0.1 → catchlib-1.0.3}/setup.cfg +0 -0
- {catchlib-1.0.1 → catchlib-1.0.3}/src/catchlib/__init__.py +0 -0
- {catchlib-1.0.1 → catchlib-1.0.3}/src/catchlib.egg-info/SOURCES.txt +0 -0
- {catchlib-1.0.1 → catchlib-1.0.3}/src/catchlib.egg-info/dependency_links.txt +0 -0
- {catchlib-1.0.1 → catchlib-1.0.3}/src/catchlib.egg-info/requires.txt +0 -0
- {catchlib-1.0.1 → catchlib-1.0.3}/src/catchlib.egg-info/top_level.txt +0 -0
|
@@ -7,7 +7,6 @@ __all__ = ["Catcher"]
|
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
class Catcher:
|
|
10
|
-
|
|
11
10
|
"This class catches exceptions."
|
|
12
11
|
|
|
13
12
|
__slots__ = ("_caught",)
|
|
@@ -21,8 +20,8 @@ class Catcher:
|
|
|
21
20
|
@contextmanager
|
|
22
21
|
def catch(self: Self, *args: type[BaseException]) -> Generator[Self, None, None]:
|
|
23
22
|
"This contextmanager catches exceptions."
|
|
24
|
-
self._caught = None
|
|
25
23
|
exc: BaseException
|
|
24
|
+
self._caught = None
|
|
26
25
|
try:
|
|
27
26
|
yield self
|
|
28
27
|
except args as exc:
|
|
@@ -41,7 +40,8 @@ class Catcher:
|
|
|
41
40
|
@release.overload(False)
|
|
42
41
|
def release(self: Self) -> None:
|
|
43
42
|
"This method raises the caught exception."
|
|
44
|
-
exc: BaseException
|
|
43
|
+
exc: BaseException
|
|
44
|
+
exc = self.caught
|
|
45
45
|
self._caught = None
|
|
46
46
|
if exc is not None:
|
|
47
47
|
raise exc
|
|
@@ -49,7 +49,8 @@ class Catcher:
|
|
|
49
49
|
@release.overload(True)
|
|
50
50
|
def release(self: Self, cause: Optional[BaseException]) -> None:
|
|
51
51
|
"This method raises the caught exception."
|
|
52
|
-
exc: BaseException
|
|
52
|
+
exc: BaseException
|
|
53
|
+
exc = self.caught
|
|
53
54
|
self._caught = None
|
|
54
55
|
if exc is not None:
|
|
55
56
|
raise exc from cause
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
|
|
3
|
+
__all__ = ["test"]
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test() -> unittest.TextTestRunner:
|
|
7
|
+
"This function runs all the tests."
|
|
8
|
+
loader: unittest.TestLoader
|
|
9
|
+
tests: unittest.TestSuite
|
|
10
|
+
runner: unittest.TextTestRunner
|
|
11
|
+
result: unittest.TextTestResult
|
|
12
|
+
loader = unittest.TestLoader()
|
|
13
|
+
tests = loader.discover(start_dir="catchlib.tests")
|
|
14
|
+
runner = unittest.TextTestRunner()
|
|
15
|
+
result = runner.run(tests)
|
|
16
|
+
return result
|
|
@@ -3,10 +3,13 @@ from typing import *
|
|
|
3
3
|
|
|
4
4
|
from catchlib.core import Catcher
|
|
5
5
|
|
|
6
|
+
__all__ = ["TestCatcher"]
|
|
7
|
+
|
|
6
8
|
|
|
7
9
|
class TestCatcher(unittest.TestCase):
|
|
8
10
|
def test_captures_matching_exception(self: Self) -> None:
|
|
9
|
-
catcher: Catcher
|
|
11
|
+
catcher: Catcher
|
|
12
|
+
catcher = Catcher()
|
|
10
13
|
with catcher.catch(ValueError):
|
|
11
14
|
raise ValueError("bad value")
|
|
12
15
|
self.assertIsNotNone(catcher.caught)
|
|
@@ -14,13 +17,15 @@ class TestCatcher(unittest.TestCase):
|
|
|
14
17
|
self.assertEqual(str(catcher.caught), "bad value")
|
|
15
18
|
|
|
16
19
|
def test_no_exception_leaves_caught_none(self: Self) -> None:
|
|
17
|
-
catcher: Catcher
|
|
20
|
+
catcher: Catcher
|
|
21
|
+
catcher = Catcher()
|
|
18
22
|
with catcher.catch(ValueError):
|
|
19
23
|
pass
|
|
20
24
|
self.assertIsNone(catcher.caught)
|
|
21
25
|
|
|
22
26
|
def test_captures_one_of_multiple_types(self: Self) -> None:
|
|
23
|
-
catcher: Catcher
|
|
27
|
+
catcher: Catcher
|
|
28
|
+
catcher = Catcher()
|
|
24
29
|
with catcher.catch(ValueError, KeyError):
|
|
25
30
|
raise KeyError("missing")
|
|
26
31
|
self.assertIsNotNone(catcher.caught)
|
|
@@ -32,7 +37,8 @@ class TestCatcher(unittest.TestCase):
|
|
|
32
37
|
def test_non_matching_exception_propagates_and_does_not_set_caught(
|
|
33
38
|
self: Self,
|
|
34
39
|
) -> None:
|
|
35
|
-
catcher: Catcher
|
|
40
|
+
catcher: Catcher
|
|
41
|
+
catcher = Catcher()
|
|
36
42
|
with self.assertRaises(ZeroDivisionError):
|
|
37
43
|
with catcher.catch(ValueError, KeyError):
|
|
38
44
|
1 / 0 # ZeroDivisionError not in capture set
|
|
@@ -40,7 +46,8 @@ class TestCatcher(unittest.TestCase):
|
|
|
40
46
|
self.assertIsNone(catcher.caught)
|
|
41
47
|
|
|
42
48
|
def test_reuse_and_reset_semantics(self: Self) -> None:
|
|
43
|
-
catcher: Catcher
|
|
49
|
+
catcher: Catcher
|
|
50
|
+
catcher = Catcher()
|
|
44
51
|
|
|
45
52
|
# First, capture an exception
|
|
46
53
|
with catcher.catch(RuntimeError):
|
|
@@ -58,7 +65,8 @@ class TestCatcher(unittest.TestCase):
|
|
|
58
65
|
self.assertIsInstance(catcher.caught, TypeError)
|
|
59
66
|
|
|
60
67
|
def test_empty_type_tuple_never_catches(self: Self) -> None:
|
|
61
|
-
catcher: Catcher
|
|
68
|
+
catcher: Catcher
|
|
69
|
+
catcher = Catcher()
|
|
62
70
|
# Passing no types should behave like catching nothing: exception propagates
|
|
63
71
|
with self.assertRaises(ValueError):
|
|
64
72
|
with catcher.catch():
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import unittest
|
|
2
|
-
|
|
3
|
-
__all__ = ["test"]
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def test() -> unittest.TextTestRunner:
|
|
7
|
-
"This function runs all the tests."
|
|
8
|
-
loader: unittest.TestLoader = unittest.TestLoader()
|
|
9
|
-
tests: unittest.TestSuite = loader.discover(start_dir="catchlib.tests")
|
|
10
|
-
runner: unittest.TextTestRunner = unittest.TextTestRunner()
|
|
11
|
-
result: unittest.TextTestResult = runner.run(tests)
|
|
12
|
-
return result
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|