diff --git a/pyreadline/console/console.py b/pyreadline/console/console.py index 2204003..9487b88 100644 --- a/pyreadline/console/console.py +++ b/pyreadline/console/console.py @@ -30,6 +30,9 @@ try: except ImportError: raise ImportError(u"You need ctypes to run this code") +if sys.version_info < (2, 6): + bytes = str + def nolog(string): pass @@ -690,7 +693,7 @@ def hook_wrapper_23(stdin, stdout, prompt): # call the Python hook res = ensure_str(readline_hook(prompt)) # make sure it returned the right sort of thing - if res and not isinstance(res, str): + if res and not isinstance(res, bytes): raise TypeError, u'readline must return a string.' except KeyboardInterrupt: # GNU readline returns 0 on keyboard interrupt @@ -714,7 +717,7 @@ def hook_wrapper(prompt): # call the Python hook res = ensure_str(readline_hook(prompt)) # make sure it returned the right sort of thing - if res and not isinstance(res, str): + if res and not isinstance(res, bytes): raise TypeError, u'readline must return a string.' except KeyboardInterrupt: # GNU readline returns 0 on keyboard interrupt @@ -738,7 +741,7 @@ def install_readline(hook): readline_hook = hook # get the address of PyOS_ReadlineFunctionPointer so we can update it PyOS_RFP = c_int.from_address(Console.GetProcAddress(sys.dllhandle, - "PyOS_ReadlineFunctionPointer")) + "PyOS_ReadlineFunctionPointer".encode('ascii'))) # save a reference to the generated C-callable so it doesn't go away if sys.version < '2.3': readline_ref = HOOKFUNC22(hook_wrapper) diff --git a/pyreadline/modes/basemode.py b/pyreadline/modes/basemode.py index df1147d..35e2e79 100644 --- a/pyreadline/modes/basemode.py +++ b/pyreadline/modes/basemode.py @@ -195,7 +195,7 @@ class BaseMode(object): i = 0 while 1: try: - r = ensure_unicode(self.completer(text, i)) + r = self.completer(ensure_unicode(text), i) except IndexError: break i += 1 @@ -215,7 +215,7 @@ class BaseMode(object): break text = ensure_str(u''.join(buf[self.begidx:self.endidx])) log(u'file complete text="%s"' % ensure_unicode(text)) - completions = map(ensure_unicode, glob.glob(os.path.expanduser(text) + '*')) + completions = map(ensure_unicode, glob.glob(os.path.expanduser(text) + '*'.encode('ascii'))) if self.mark_directories == u'on': mc = [] for f in completions: diff --git a/pyreadline/unicode_helper.py b/pyreadline/unicode_helper.py index bf171ab..7fffd9f 100644 --- a/pyreadline/unicode_helper.py +++ b/pyreadline/unicode_helper.py @@ -17,9 +17,12 @@ except AttributeError: if pyreadline_codepage is None: pyreadline_codepage = u"ascii" +if sys.version_info < (2, 6): + bytes = str + def ensure_unicode(text): u"""helper to ensure that text passed to WriteConsoleW is unicode""" - if isinstance(text, str): + if isinstance(text, bytes): try: return text.decode(pyreadline_codepage, u"replace") except (LookupError, TypeError):