diff cutils/util/cm.py @ 115:e15b3d1ff0d9

New subpackage with a "contextlib.nullcontext" for older Python versions
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 29 Dec 2024 15:31:38 +0100
parents
children dfe7bb0579e2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cutils/util/cm.py	Sun Dec 29 15:31:38 2024 +0100
@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+# :-
+# :Copyright: (c) 2020-2024 Franz Glasner
+# :License:   BSD-3-Clause
+# :-
+r"""Context manager extensions and compatibility.
+
+"""
+
+__all__ = ["nullcontext"]
+
+
+try:
+    from contextlib import nullcontext
+except ImportError:
+    class nullcontext(object):
+
+        """Compatibility implementation for systems that are missing yet
+        a standard :class:`contextlib.nullcontext`.
+
+        """
+
+        __slots__ = ("thing", )
+
+        def __init__(self, thing=None):
+            self.thing = thing
+
+        def __enter__(self):
+            return self.thing
+
+        def __exit__(self, *args, **kwds):
+            pass