catchlib 0.0.0.dev0__tar.gz → 0.0.1__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.
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: catchlib
3
- Version: 0.0.0.dev0
4
- Summary: catchlib
3
+ Version: 0.0.1
4
+ Summary: This project allows to catch exceptions easily.
5
5
  Author-email: Johannes <johannes.programming@gmail.com>
6
6
  License: The MIT License (MIT)
7
7
 
@@ -27,7 +27,8 @@ License: The MIT License (MIT)
27
27
  Project-URL: Download, https://pypi.org/project/catchlib/#files
28
28
  Project-URL: Index, https://pypi.org/project/catchlib/
29
29
  Project-URL: Source, https://github.com/johannes-programming/catchlib/
30
- Classifier: Development Status :: 1 - Planning
30
+ Project-URL: Website, https://catchlib.johannes-programming.online/
31
+ Classifier: Development Status :: 2 - Pre-Alpha
31
32
  Classifier: License :: OSI Approved :: MIT License
32
33
  Classifier: Natural Language :: English
33
34
  Classifier: Operating System :: OS Independent
@@ -44,36 +45,4 @@ Dynamic: license-file
44
45
  catchlib
45
46
  ========
46
47
 
47
- Overview
48
- --------
49
-
50
- catchlib
51
-
52
- Installation
53
- ------------
54
-
55
- To install ``catchlib``, you can use ``pip``. Open your terminal and run:
56
-
57
- .. code-block:: bash
58
-
59
- pip install catchlib
60
-
61
- License
62
- -------
63
-
64
- This project is licensed under the MIT License.
65
-
66
- Links
67
- -----
68
-
69
- * `Download <https://pypi.org/project/catchlib/#files>`_
70
- * `Index <https://pypi.org/project/catchlib/>`_
71
- * `Source <https://github.com/johannes-programming/catchlib/>`_
72
-
73
- Credits
74
- -------
75
-
76
- * Author: Johannes
77
- * Email: `johannes.programming@gmail.com <mailto:johannes.programming@gmail.com>`_
78
-
79
- Thank you for using ``catchlib``!
48
+ Visit the website `https://catchlib.johannes-programming.online/ <https://catchlib.johannes-programming.online/>`_ for more information.
@@ -0,0 +1,5 @@
1
+ ========
2
+ catchlib
3
+ ========
4
+
5
+ Visit the website `https://catchlib.johannes-programming.online/ <https://catchlib.johannes-programming.online/>`_ for more information.
@@ -9,7 +9,7 @@ authors = [
9
9
  { email = "johannes.programming@gmail.com", name = "Johannes" },
10
10
  ]
11
11
  classifiers = [
12
- "Development Status :: 1 - Planning",
12
+ "Development Status :: 2 - Pre-Alpha",
13
13
  "License :: OSI Approved :: MIT License",
14
14
  "Natural Language :: English",
15
15
  "Operating System :: OS Independent",
@@ -19,12 +19,12 @@ classifiers = [
19
19
  "Typing :: Typed",
20
20
  ]
21
21
  dependencies = []
22
- description = "catchlib"
22
+ description = "This project allows to catch exceptions easily."
23
23
  keywords = []
24
24
  name = "catchlib"
25
25
  readme = "README.rst"
26
26
  requires-python = ">=3.11"
27
- version = "0.0.0.dev0"
27
+ version = "0.0.1"
28
28
 
29
29
  [project.license]
30
30
  file = "LICENSE.txt"
@@ -33,3 +33,4 @@ file = "LICENSE.txt"
33
33
  Download = "https://pypi.org/project/catchlib/#files"
34
34
  Index = "https://pypi.org/project/catchlib/"
35
35
  Source = "https://github.com/johannes-programming/catchlib/"
36
+ Website = "https://catchlib.johannes-programming.online/"
@@ -1,5 +1,2 @@
1
1
  from catchlib.core import *
2
2
  from catchlib.tests import *
3
-
4
- if __name__ == "__main__":
5
- main()
@@ -0,0 +1,32 @@
1
+ from contextlib import contextmanager
2
+ from typing import *
3
+
4
+ __all__ = ["Catcher"]
5
+
6
+
7
+ class Catcher:
8
+
9
+ "This class catches exceptions."
10
+
11
+ __slots__ = ("_caught",)
12
+
13
+ caught: Optional[BaseException]
14
+
15
+ def __init__(self: Self) -> None:
16
+ "This magic method initializes the current instance."
17
+ self._caught = None
18
+
19
+ @contextmanager
20
+ def catch(self: Self, *args: type[BaseException]) -> Generator[Self, None, None]:
21
+ "This contextmanager catches exceptions."
22
+ self._caught = None
23
+ exc: BaseException
24
+ try:
25
+ yield self
26
+ except args as exc:
27
+ self._caught = exc
28
+
29
+ @property
30
+ def caught(self: Self) -> Optional[BaseException]:
31
+ "This property stores the caught exception."
32
+ return self._caught
@@ -0,0 +1,70 @@
1
+ import unittest
2
+ from typing import *
3
+
4
+ from catchlib.core import Catcher
5
+
6
+
7
+ class TestCatcher(unittest.TestCase):
8
+ def test_captures_matching_exception(self: Self) -> None:
9
+ catcher: Catcher = Catcher()
10
+ with catcher.catch(ValueError):
11
+ raise ValueError("bad value")
12
+ self.assertIsNotNone(catcher.caught)
13
+ self.assertIsInstance(catcher.caught, ValueError)
14
+ self.assertEqual(str(catcher.caught), "bad value")
15
+
16
+ def test_no_exception_leaves_caught_none(self: Self) -> None:
17
+ catcher: Catcher = Catcher()
18
+ with catcher.catch(ValueError):
19
+ pass
20
+ self.assertIsNone(catcher.caught)
21
+
22
+ def test_captures_one_of_multiple_types(self: Self) -> None:
23
+ catcher: Catcher = Catcher()
24
+ with catcher.catch(ValueError, KeyError):
25
+ raise KeyError("missing")
26
+ self.assertIsNotNone(catcher.caught)
27
+ self.assertIsInstance(catcher.caught, KeyError)
28
+ self.assertEqual(
29
+ str(catcher.caught), "'missing'"
30
+ ) # KeyError stringifies with quotes
31
+
32
+ def test_non_matching_exception_propagates_and_does_not_set_caught(
33
+ self: Self,
34
+ ) -> None:
35
+ catcher: Catcher = Catcher()
36
+ with self.assertRaises(ZeroDivisionError):
37
+ with catcher.catch(ValueError, KeyError):
38
+ 1 / 0 # ZeroDivisionError not in capture set
39
+ # Since the exception propagated out, Catcher should not have recorded anything
40
+ self.assertIsNone(catcher.caught)
41
+
42
+ def test_reuse_and_reset_semantics(self: Self) -> None:
43
+ catcher: Catcher = Catcher()
44
+
45
+ # First, capture an exception
46
+ with catcher.catch(RuntimeError):
47
+ raise RuntimeError("first")
48
+ self.assertIsInstance(catcher.caught, RuntimeError)
49
+
50
+ # Next, a clean block should reset caught to None
51
+ with catcher.catch(RuntimeError):
52
+ pass
53
+ self.assertIsNone(catcher.caught)
54
+
55
+ # Finally, capture another (different) exception type by passing multiple
56
+ with catcher.catch(RuntimeError, TypeError):
57
+ raise TypeError("second")
58
+ self.assertIsInstance(catcher.caught, TypeError)
59
+
60
+ def test_empty_type_tuple_never_catches(self: Self) -> None:
61
+ catcher: Catcher = Catcher()
62
+ # Passing no types should behave like catching nothing: exception propagates
63
+ with self.assertRaises(ValueError):
64
+ with catcher.catch():
65
+ raise ValueError("won't be caught")
66
+ self.assertIsNone(catcher.caught)
67
+
68
+
69
+ if __name__ == "__main__":
70
+ unittest.main()
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: catchlib
3
- Version: 0.0.0.dev0
4
- Summary: catchlib
3
+ Version: 0.0.1
4
+ Summary: This project allows to catch exceptions easily.
5
5
  Author-email: Johannes <johannes.programming@gmail.com>
6
6
  License: The MIT License (MIT)
7
7
 
@@ -27,7 +27,8 @@ License: The MIT License (MIT)
27
27
  Project-URL: Download, https://pypi.org/project/catchlib/#files
28
28
  Project-URL: Index, https://pypi.org/project/catchlib/
29
29
  Project-URL: Source, https://github.com/johannes-programming/catchlib/
30
- Classifier: Development Status :: 1 - Planning
30
+ Project-URL: Website, https://catchlib.johannes-programming.online/
31
+ Classifier: Development Status :: 2 - Pre-Alpha
31
32
  Classifier: License :: OSI Approved :: MIT License
32
33
  Classifier: Natural Language :: English
33
34
  Classifier: Operating System :: OS Independent
@@ -44,36 +45,4 @@ Dynamic: license-file
44
45
  catchlib
45
46
  ========
46
47
 
47
- Overview
48
- --------
49
-
50
- catchlib
51
-
52
- Installation
53
- ------------
54
-
55
- To install ``catchlib``, you can use ``pip``. Open your terminal and run:
56
-
57
- .. code-block:: bash
58
-
59
- pip install catchlib
60
-
61
- License
62
- -------
63
-
64
- This project is licensed under the MIT License.
65
-
66
- Links
67
- -----
68
-
69
- * `Download <https://pypi.org/project/catchlib/#files>`_
70
- * `Index <https://pypi.org/project/catchlib/>`_
71
- * `Source <https://github.com/johannes-programming/catchlib/>`_
72
-
73
- Credits
74
- -------
75
-
76
- * Author: Johannes
77
- * Email: `johannes.programming@gmail.com <mailto:johannes.programming@gmail.com>`_
78
-
79
- Thank you for using ``catchlib``!
48
+ Visit the website `https://catchlib.johannes-programming.online/ <https://catchlib.johannes-programming.online/>`_ for more information.
@@ -4,11 +4,10 @@ README.rst
4
4
  pyproject.toml
5
5
  setup.cfg
6
6
  src/catchlib/__init__.py
7
- src/catchlib/__main__.py
8
7
  src/catchlib.egg-info/PKG-INFO
9
8
  src/catchlib.egg-info/SOURCES.txt
10
9
  src/catchlib.egg-info/dependency_links.txt
11
10
  src/catchlib.egg-info/top_level.txt
12
11
  src/catchlib/core/__init__.py
13
12
  src/catchlib/tests/__init__.py
14
- src/catchlib/tests/test_1984.py
13
+ src/catchlib/tests/test_0.py
@@ -1,37 +0,0 @@
1
- ========
2
- catchlib
3
- ========
4
-
5
- Overview
6
- --------
7
-
8
- catchlib
9
-
10
- Installation
11
- ------------
12
-
13
- To install ``catchlib``, you can use ``pip``. Open your terminal and run:
14
-
15
- .. code-block:: bash
16
-
17
- pip install catchlib
18
-
19
- License
20
- -------
21
-
22
- This project is licensed under the MIT License.
23
-
24
- Links
25
- -----
26
-
27
- * `Download <https://pypi.org/project/catchlib/#files>`_
28
- * `Index <https://pypi.org/project/catchlib/>`_
29
- * `Source <https://github.com/johannes-programming/catchlib/>`_
30
-
31
- Credits
32
- -------
33
-
34
- * Author: Johannes
35
- * Email: `johannes.programming@gmail.com <mailto:johannes.programming@gmail.com>`_
36
-
37
- Thank you for using ``catchlib``!
@@ -1,4 +0,0 @@
1
- from catchlib import main
2
-
3
- if __name__ == "__main__":
4
- main()
@@ -1,8 +0,0 @@
1
- from typing import *
2
-
3
- __all__ = ["main"]
4
-
5
-
6
- def main(args: Optional[Iterable] = None) -> None:
7
- "This function prints 'Hello World!'."
8
- print("Hello World!")
@@ -1,11 +0,0 @@
1
- import unittest
2
- from typing import *
3
-
4
-
5
- class Test1984(unittest.TestCase):
6
- def test_1984(self: Self) -> None:
7
- self.assertEqual(2 + 2, 4, "Ignorance is Strength")
8
-
9
-
10
- if __name__ == "__main__":
11
- unittest.main()
File without changes
File without changes
File without changes