summaryrefslogtreecommitdiff
path: root/cmd2/utils.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-24 22:26:20 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-24 22:26:20 -0400
commitc9a92359de593f00ec936dcd48aa0931a77d3ef6 (patch)
tree5b7f4c669f5548857646f0681af0f43137c987d5 /cmd2/utils.py
parent93506e82a37b29911774607560cb1d6d3ea1cae9 (diff)
downloadcmd2-git-c9a92359de593f00ec936dcd48aa0931a77d3ef6.tar.gz
Made ContextFlag reentrant
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r--cmd2/utils.py21
1 files changed, 12 insertions, 9 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py
index e4da938d..5bb08edd 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -142,7 +142,6 @@ def which(editor: str) -> Optional[str]:
:param editor: filename of the editor to check, ie 'notepad.exe' or 'vi'
:return: a full path or None
"""
- import subprocess
try:
editor_path = subprocess.check_output(['which', editor], stderr=subprocess.STDOUT).strip()
editor_path = editor_path.decode()
@@ -476,19 +475,23 @@ class ProcReader(object):
class ContextFlag(object):
"""
- A flag value that can be used in a with statement.
+ A flag value that is used in a with statement.
Its main use is as a flag to prevent the SIGINT handler in cmd2 from raising a KeyboardInterrupt
- while another code section has set the flag to True. Because signal handling is always done on the
- main thread, this class is not thread since there is no need.
+ while a critical code section has set the flag to True. Because signal handling is always done on the
+ main thread, this class is not thread-safe since there is no need.
"""
- def __init__(self, value):
- self.value = value
+ def __init__(self):
+ # When this flag has a positive value, it is considered set.
+ # When it is 0, it is not set. It should never go below 0.
+ self.__count = 0
def __bool__(self):
- return self.value
+ return self.__count > 0
def __enter__(self):
- self.value = True
+ self.__count += 1
def __exit__(self, *args):
- self.value = False
+ self.__count -= 1
+ if self.__count < 0:
+ raise ValueError("count has gone below 0")