view cutils/util/cm.py @ 123:4a0c3c9eead7

Use the output of "scandir()" directly if it has a "close" method. Use a "nullcontext" otherwise.
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 02 Jan 2025 12:43:40 +0100
parents e15b3d1ff0d9
children dfe7bb0579e2
line wrap: on
line source

# -*- 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