From 8d704b424d9e920a080a531c4adb2b37d269b302 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 3 May 2017 18:33:42 -0400 Subject: [PATCH 1/2] Try to import gnureadline and then if that fails import readline --- cmd2.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/cmd2.py b/cmd2.py index e22016325..9ebbbd060 100755 --- a/cmd2.py +++ b/cmd2.py @@ -62,6 +62,12 @@ # noinspection PyUnresolvedReferences from six.moves.urllib.request import urlopen +# Prefer statically linked gnureadline if available (for Mac OS X compatibility due to issues with libedit) +try: + import gnureadline +except ImportError: + import readline + # Python 3 compatibility hack due to no built-in file keyword in Python 3 # Due to one occurrence of isinstance(, file) checking to see if something is of file type try: @@ -1111,9 +1117,8 @@ def pseudo_raw_input(self, prompt): if not len(line): line = 'EOF' else: - if line[-1] == '\n': # this was always true in Cmd - line = line[:-1] - return line + line = line.rstrip('\r\n') + return line.strip() def _cmdloop(self): """Repeatedly issue a prompt, accept input, parse an initial prefix @@ -1127,14 +1132,9 @@ def _cmdloop(self): # An almost perfect copy from Cmd; however, the pseudo_raw_input portion # has been split out so that it can be called separately if self.use_rawinput and self.completekey: - try: - # noinspection PyUnresolvedReferences - import readline - self.old_completer = readline.get_completer() - readline.set_completer(self.complete) - readline.parse_and_bind(self.completekey + ": complete") - except ImportError: - pass + self.old_completer = readline.get_completer() + readline.set_completer(self.complete) + readline.parse_and_bind(self.completekey + ": complete") stop = None try: while not stop: @@ -1148,12 +1148,7 @@ def _cmdloop(self): stop = self.onecmd_plus_hooks(line) finally: if self.use_rawinput and self.completekey: - try: - # noinspection PyUnresolvedReferences - import readline - readline.set_completer(self.old_completer) - except ImportError: - pass + readline.set_completer(self.old_completer) return stop # noinspection PyUnusedLocal From 660bd38a1ecc36a6c7aaa5c27841a3957978802b Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 3 May 2017 18:43:58 -0400 Subject: [PATCH 2/2] Added some checks to deal with unit testing on Windows systems with no readline NOTE: For real use on Windows you should have pyreadline installed. --- cmd2.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/cmd2.py b/cmd2.py index 9ebbbd060..ed481330e 100755 --- a/cmd2.py +++ b/cmd2.py @@ -64,9 +64,12 @@ # Prefer statically linked gnureadline if available (for Mac OS X compatibility due to issues with libedit) try: - import gnureadline + import gnureadline as readline except ImportError: - import readline + try: + import readline + except ImportError: + pass # Python 3 compatibility hack due to no built-in file keyword in Python 3 # Due to one occurrence of isinstance(, file) checking to see if something is of file type @@ -1132,9 +1135,12 @@ def _cmdloop(self): # An almost perfect copy from Cmd; however, the pseudo_raw_input portion # has been split out so that it can be called separately if self.use_rawinput and self.completekey: - self.old_completer = readline.get_completer() - readline.set_completer(self.complete) - readline.parse_and_bind(self.completekey + ": complete") + try: + self.old_completer = readline.get_completer() + readline.set_completer(self.complete) + readline.parse_and_bind(self.completekey + ": complete") + except NameError: + pass stop = None try: while not stop: @@ -1148,7 +1154,10 @@ def _cmdloop(self): stop = self.onecmd_plus_hooks(line) finally: if self.use_rawinput and self.completekey: - readline.set_completer(self.old_completer) + try: + readline.set_completer(self.old_completer) + except NameError: + pass return stop # noinspection PyUnusedLocal