diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-11-24 22:15:22 -0500 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-11-24 22:15:22 -0500 |
commit | efa75484c35dc765df425043fb4d2c2672c9c26e (patch) | |
tree | 223dbc37afdec4a70f71fa54df6a312aed88305a | |
parent | aeb517d7249b6f17cbb0d09a1a22f2d689be1d57 (diff) | |
download | cmd2-git-efa75484c35dc765df425043fb4d2c2672c9c26e.tar.gz |
Added exception handling to account for non-standard Python environments in which readline is not loaded dynamically from a shared library file
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | cmd2/cmd2.py | 5 | ||||
-rw-r--r-- | cmd2/rl_utils.py | 33 |
3 files changed, 28 insertions, 13 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 780c9ad5..b0e4dac5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ ## 0.9.21 (TBD, 2019) * Bug Fixes * Fixed bug where pipe processes were not being stopped by Ctrl-C + * Added exception handling to account for non-standard Python environments in which readline is not loaded + dynamically from a shared library file + * Enhancements * Added `read_input()` function that is used to read from stdin. Unlike the Python built-in `input()`, it also has an argument to disable tab completion while input is being entered. diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 1eeb4212..d949a374 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -52,13 +52,10 @@ from .clipboard import can_clip, get_paste_buffer, write_to_paste_buffer from .decorators import with_argparser from .history import History, HistoryItem from .parsing import StatementParser, Statement, Macro, MacroArg, shlex_split -from .rl_utils import rl_type, RlType, rl_get_point, rl_set_prompt, vt100_support, rl_make_safe_prompt +from .rl_utils import rl_type, RlType, rl_get_point, rl_set_prompt, vt100_support, rl_make_safe_prompt, rl_warning # Set up readline if rl_type == RlType.NONE: # pragma: no cover - rl_warning = ("Readline features including tab completion have been disabled since no\n" - "supported version of readline was found. To resolve this, install pyreadline\n" - "on Windows or gnureadline on Mac.\n\n") sys.stderr.write(ansi.style_warning(rl_warning)) else: from .rl_utils import rl_force_redisplay, readline diff --git a/cmd2/rl_utils.py b/cmd2/rl_utils.py index 7f47db79..6278e859 100644 --- a/cmd2/rl_utils.py +++ b/cmd2/rl_utils.py @@ -32,6 +32,9 @@ rl_type = RlType.NONE # Tells if the terminal we are running in supports vt100 control characters vt100_support = False +# Explanation for why readline wasn't loaded +_rl_warn_reason = '' + # The order of this check matters since importing pyreadline will also show readline in the modules list if 'pyreadline' in sys.modules: rl_type = RlType.PYREADLINE @@ -113,15 +116,27 @@ if 'pyreadline' in sys.modules: elif 'gnureadline' in sys.modules or 'readline' in sys.modules: # We don't support libedit if 'libedit' not in readline.__doc__: - rl_type = RlType.GNU - - # Load the readline lib so we can access members of it - import ctypes - readline_lib = ctypes.CDLL(readline.__file__) - - # Check if we are running in a terminal - if sys.stdout.isatty(): - vt100_support = True + try: + # Load the readline lib so we can access members of it + import ctypes + readline_lib = ctypes.CDLL(readline.__file__) + except AttributeError: + _rl_warn_reason = ("this application is running in a non-standard Python environment in\n" + "which readline is not loaded dynamically from a shared library file") + else: + rl_type = RlType.GNU + if sys.stdout.isatty(): + vt100_support = True + +# Check if readline was loaded +if rl_type == RlType.NONE: + if not _rl_warn_reason: + _rl_warn_reason = ("no supported version of readline was found. To resolve this, install\n" + "pyreadline on Windows or gnureadline on Mac.") + rl_warning = ("Readline features including tab completion have been disabled because\n" + + _rl_warn_reason + '\n\n') +else: + rl_warning = '' # noinspection PyProtectedMember,PyUnresolvedReferences |