From 1c44a08219a60654a7f99bc878c41dfcb8d45a8b Mon Sep 17 00:00:00 2001 From: jstenar <> Date: Fri, 9 Nov 2007 22:45:31 +0000 Subject: [PATCH] pyreadline: Fixes to make clipboard work with unicode. --- doc/ChangeLog | 5 +++++ pyreadline/clipboard/win32_clipboard.py | 9 +++------ pyreadline/lineeditor/lineobj.py | 4 ++-- pyreadline/unicode_helper.py | 6 ++++++ 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/doc/ChangeLog b/doc/ChangeLog index 3c2a91f..ed111cf 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,8 @@ +2007-11-09 Jörgen Stenarson + * Fixes to make clipboard play nice with unicode. Quick that treats the clipboard as + being str with pyreadline_encoding data. Thus convert to unicode when pasting and + converting to str when copying. + 2007-11-09 Jörgen Stenarson * More fixes to ensure unicode works when completing on filepaths with non ascii symbols. This fix assumes sys.stdout.encoding is valid for the filesystem. A more correct fix diff --git a/pyreadline/clipboard/win32_clipboard.py b/pyreadline/clipboard/win32_clipboard.py index a626edc..c3ec81b 100644 --- a/pyreadline/clipboard/win32_clipboard.py +++ b/pyreadline/clipboard/win32_clipboard.py @@ -34,6 +34,7 @@ from ctypes import * from pyreadline.keysyms.winconstants import CF_TEXT, GHND +from pyreadline.unicode_helper import ensure_unicode,ensure_text OpenClipboard = windll.user32.OpenClipboard EmptyClipboard = windll.user32.EmptyClipboard @@ -81,10 +82,10 @@ def GetClipboardText(): text = GlobalLock(hClipMem) GlobalUnlock(hClipMem) CloseClipboard() - return text + return ensure_unicode(text) def SetClipboardText(text): - buffer = c_buffer(text) + buffer = c_buffer(ensure_text(text)) bufferSize = sizeof(buffer) hGlobalMem = GlobalAlloc(c_int(GHND), c_int(bufferSize)) GlobalLock.restype = c_void_p @@ -99,7 +100,3 @@ def SetClipboardText(text): if __name__ == '__main__': txt=GetClipboardText() # display last text clipped print txt - - - - \ No newline at end of file diff --git a/pyreadline/lineeditor/lineobj.py b/pyreadline/lineeditor/lineobj.py index 79364ce..f4d02f8 100644 --- a/pyreadline/lineeditor/lineobj.py +++ b/pyreadline/lineeditor/lineobj.py @@ -717,7 +717,7 @@ class ReadLineTextBuffer(TextLine): begin=min(cursor,mark) end=max(cursor,mark) toclipboard="".join(self.line_buffer[begin:end]) - clipboard.SetClipboardText(str(toclipboard)) + clipboard.SetClipboardText(toclipboard) def copy_selection_to_clipboard(self): # () '''Copy the text in the region to the windows clipboard.''' @@ -729,7 +729,7 @@ class ReadLineTextBuffer(TextLine): begin=min(cursor,selection_mark) end=max(cursor,selection_mark) toclipboard="".join(self.line_buffer[begin:end]) - clipboard.SetClipboardText(str(toclipboard)) + clipboard.SetClipboardText(toclipboard) def cut_selection_to_clipboard(self): # () diff --git a/pyreadline/unicode_helper.py b/pyreadline/unicode_helper.py index ab03e09..1c8a9ec 100644 --- a/pyreadline/unicode_helper.py +++ b/pyreadline/unicode_helper.py @@ -19,3 +19,9 @@ def ensure_unicode(text): if isinstance(text, str): return text.decode(pyreadline_codepage, "replace") return text + +def ensure_text(text): + """Convert unicode to str using pyreadline_codepage""" + if isinstance(text, str): + return text.encode(pyreadline_codepage, "replace") + return text