bytes/str/unicode tweaking for py2/3.

This commit is contained in:
shimizukawa
2011-04-09 18:04:41 +09:00
parent 452c5d831c
commit bf8f52bad9
3 changed files with 12 additions and 6 deletions
+6 -3
View File
@@ -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)
+2 -2
View File
@@ -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:
+4 -1
View File
@@ -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):