diff options
author | Andrew Clark IV <starwarsfan2099@gmail.com> | 2023-04-07 13:27:21 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2023-05-02 15:06:55 -0400 |
commit | 950d92aa853eb03b284b76c06383cfeb548fce72 (patch) | |
tree | 5131d41f7505e71e7390b1f1d1c9dabba9d9ff6d | |
parent | bdf17dc37d381a26f8372d545b76b1475539ff55 (diff) | |
download | cmd2-git-950d92aa853eb03b284b76c06383cfeb548fce72.tar.gz |
Check if sys.stdout is None
-rw-r--r-- | cmd2/rl_utils.py | 73 |
1 files changed, 37 insertions, 36 deletions
diff --git a/cmd2/rl_utils.py b/cmd2/rl_utils.py index 048302ef..9a77d7a0 100644 --- a/cmd2/rl_utils.py +++ b/cmd2/rl_utils.py @@ -70,42 +70,43 @@ if 'pyreadline3' in sys.modules: ) # Check if we are running in a terminal - if sys.stdout.isatty(): # pragma: no cover - # noinspection PyPep8Naming,PyUnresolvedReferences - def enable_win_vt100(handle: HANDLE) -> bool: - """ - Enables VT100 character sequences in a Windows console - This only works on Windows 10 and up - :param handle: the handle on which to enable vt100 - :return: True if vt100 characters are enabled for the handle - """ - ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 - - # Get the current mode for this handle in the console - cur_mode = DWORD(0) - readline.rl.console.GetConsoleMode(handle, byref(cur_mode)) - - retVal = False - - # Check if ENABLE_VIRTUAL_TERMINAL_PROCESSING is already enabled - if (cur_mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0: - retVal = True - - elif readline.rl.console.SetConsoleMode(handle, cur_mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING): - # Restore the original mode when we exit - atexit.register(readline.rl.console.SetConsoleMode, handle, cur_mode) - retVal = True - - return retVal - - # Enable VT100 sequences for stdout and stderr - STD_OUT_HANDLE = -11 - STD_ERROR_HANDLE = -12 - # noinspection PyUnresolvedReferences - vt100_stdout_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_OUT_HANDLE)) - # noinspection PyUnresolvedReferences - vt100_stderr_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_ERROR_HANDLE)) - vt100_support = vt100_stdout_support and vt100_stderr_support + if sys.stdout: + if sys.stdout.isatty(): # pragma: no cover + # noinspection PyPep8Naming,PyUnresolvedReferences + def enable_win_vt100(handle: HANDLE) -> bool: + """ + Enables VT100 character sequences in a Windows console + This only works on Windows 10 and up + :param handle: the handle on which to enable vt100 + :return: True if vt100 characters are enabled for the handle + """ + ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 + + # Get the current mode for this handle in the console + cur_mode = DWORD(0) + readline.rl.console.GetConsoleMode(handle, byref(cur_mode)) + + retVal = False + + # Check if ENABLE_VIRTUAL_TERMINAL_PROCESSING is already enabled + if (cur_mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0: + retVal = True + + elif readline.rl.console.SetConsoleMode(handle, cur_mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING): + # Restore the original mode when we exit + atexit.register(readline.rl.console.SetConsoleMode, handle, cur_mode) + retVal = True + + return retVal + + # Enable VT100 sequences for stdout and stderr + STD_OUT_HANDLE = -11 + STD_ERROR_HANDLE = -12 + # noinspection PyUnresolvedReferences + vt100_stdout_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_OUT_HANDLE)) + # noinspection PyUnresolvedReferences + vt100_stderr_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_ERROR_HANDLE)) + vt100_support = vt100_stdout_support and vt100_stderr_support ############################################################################################################ # pyreadline3 is incomplete in terms of the Python readline API. Add the missing functions we need. |