From: Perry Gagne <pgagne(a)redhat.com>
When rewriting Neper code to handle tcp_crr server not having stats, I found this made the
code flow simpler to adapt.
contextlib.nullcontext was added in python 3.7, but we need it in 3.6. The code was simple
enough so I just copied it from CPython.
Signed-off-by: Perry Gagne <pgagne(a)redhat.com>
---
lnst/Common/Utils.py | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/lnst/Common/Utils.py b/lnst/Common/Utils.py
index 477ef8db..c276a78f 100644
--- a/lnst/Common/Utils.py
+++ b/lnst/Common/Utils.py
@@ -23,6 +23,7 @@ import collections
import math
import itertools
from collections.abc import Iterable
+from contextlib import AbstractContextManager
from _ast import Call, Attribute
from lnst.Common.ExecCmd import exec_cmd
@@ -329,3 +330,27 @@ def pairwise(iterable: Iterable) -> Iterable:
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
+
+
+class nullcontext(AbstractContextManager):
+ """
+ Context manager that does no additional processing.
+ Used as a stand-in for a normal context manager, when a particular
+ block of code is only sometimes used with a normal context manager:
+ cm = optional_cm if condition else nullcontext()
+ with cm:
+ # Perform operation, using optional_cm if condition is True
+
+ Copied from
+
https://github.com/python/cpython/blob/3.9/Lib/contextlib.py
+ To backport it for python 3.6 (it was added in 3.7).
+ """
+
+ def __init__(self, enter_result=None):
+ self.enter_result = enter_result
+
+ def __enter__(self):
+ return self.enter_result
+
+ def __exit__(self, *excinfo):
+ pass
\ No newline at end of file
--
2.30.2