diff --git a/doc/ChangeLog b/doc/ChangeLog index 80c6a5f..f055ad1 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,7 @@ +2007-11-09 Jörgen Stenarson + * make all conversion is done by ensure_unicode or ensure_str to ensure uniform + handling of unicode issues. + 2007-11-09 Jörgen Stenarson * More fixes to unicode handling. diff --git a/pyreadline/clipboard/win32_clipboard.py b/pyreadline/clipboard/win32_clipboard.py index c3ec81b..3dba355 100644 --- a/pyreadline/clipboard/win32_clipboard.py +++ b/pyreadline/clipboard/win32_clipboard.py @@ -34,7 +34,7 @@ from ctypes import * from pyreadline.keysyms.winconstants import CF_TEXT, GHND -from pyreadline.unicode_helper import ensure_unicode,ensure_text +from pyreadline.unicode_helper import ensure_unicode,ensure_str OpenClipboard = windll.user32.OpenClipboard EmptyClipboard = windll.user32.EmptyClipboard @@ -85,7 +85,7 @@ def GetClipboardText(): return ensure_unicode(text) def SetClipboardText(text): - buffer = c_buffer(ensure_text(text)) + buffer = c_buffer(ensure_str(text)) bufferSize = sizeof(buffer) hGlobalMem = GlobalAlloc(c_int(GHND), c_int(bufferSize)) GlobalLock.restype = c_void_p diff --git a/pyreadline/console/console.py b/pyreadline/console/console.py index 0df20cb..e81de11 100644 --- a/pyreadline/console/console.py +++ b/pyreadline/console/console.py @@ -17,7 +17,7 @@ import sys import traceback import re from pyreadline.logger import log,log_sock -from pyreadline.unicode_helper import ensure_unicode +from pyreadline.unicode_helper import ensure_unicode,ensure_str import pyreadline.unicode_helper as unicode_helper try: from ctypes import * @@ -348,7 +348,7 @@ class Console(object): if attr is None: attr = self.attr self.SetConsoleTextAttribute(self.hout, attr) - #self.WriteConsoleW(self.hout, ensure_text(chunk), len(chunk), byref(junk), None) + #self.WriteConsoleW(self.hout, ensure_str(chunk), len(chunk), byref(junk), None) return n def write_color(self, text, attr=None): @@ -675,7 +675,7 @@ def hook_wrapper_23(stdin, stdout, prompt): '''Wrap a Python readline so it behaves like GNU readline.''' try: # call the Python hook - res = readline_hook(prompt).encode(unicode_helper.pyreadline_codepage) + res = ensure_str(readline_hook(prompt)) # make sure it returned the right sort of thing if res and not isinstance(res, str): raise TypeError, 'readline must return a string.' @@ -699,7 +699,7 @@ def hook_wrapper(prompt): '''Wrap a Python readline so it behaves like GNU readline.''' try: # call the Python hook - res = readline_hook(prompt).encode(unicode_helper.pyreadline_codepage) + res = ensure_str(readline_hook(prompt)) # make sure it returned the right sort of thing if res and not isinstance(res, str): raise TypeError, 'readline must return a string.' diff --git a/pyreadline/lineeditor/history.py b/pyreadline/lineeditor/history.py index 1c9b7b9..761a8aa 100644 --- a/pyreadline/lineeditor/history.py +++ b/pyreadline/lineeditor/history.py @@ -9,6 +9,7 @@ import re,operator,string,sys,os #import wordmatcher #import pyreadline.clipboard as clipboard +from pyreadline.unicode_helper import ensure_unicode,ensure_str if "pyreadline" in sys.modules: pyreadline= sys.modules["pyreadline"] else: @@ -66,7 +67,7 @@ class LineHistory(object): filename=self.history_filename try: for line in open(filename, 'r'): - self.add_history(lineobj.ReadLineTextBuffer(line.rstrip().decode("utf8"))) + self.add_history(lineobj.ReadLineTextBuffer(ensure_unicode(line.rstrip()))) except IOError: self.history = [] self.history_cursor = 0 @@ -77,7 +78,7 @@ class LineHistory(object): filename=self.history_filename fp = open(filename, 'wb') for line in self.history[-self.history_length:]: - fp.write(line.get_line_text().encode("utf8")) + fp.write(ensure_str(line.get_line_text())) fp.write('\n') fp.close() diff --git a/pyreadline/logger.py b/pyreadline/logger.py index a3283ea..491d33e 100644 --- a/pyreadline/logger.py +++ b/pyreadline/logger.py @@ -7,7 +7,7 @@ #***************************************************************************** import socket -from pyreadline.unicode_helper import ensure_text +from pyreadline.unicode_helper import ensure_str _logfile=False def start_log(on,filename): @@ -37,9 +37,9 @@ def log_sock(s,event_type=None): pass else: if event_type is None: - logsocket.sendto(ensure_text(s),(host,port)) + logsocket.sendto(ensure_str(s),(host,port)) elif event_type in show_event: - logsocket.sendto(ensure_text(s),(host,port)) + logsocket.sendto(ensure_str(s),(host,port)) else: pass diff --git a/pyreadline/unicode_helper.py b/pyreadline/unicode_helper.py index 75928c6..f33fa09 100644 --- a/pyreadline/unicode_helper.py +++ b/pyreadline/unicode_helper.py @@ -20,7 +20,7 @@ def ensure_unicode(text): return text.decode(pyreadline_codepage, "replace") return text -def ensure_text(text): +def ensure_str(text): """Convert unicode to str using pyreadline_codepage""" if isinstance(text, unicode): return text.encode(pyreadline_codepage, "replace")