diff options
author | Ian Ward <ian@excess.org> | 2011-02-15 16:55:33 -0500 |
---|---|---|
committer | Ian Ward <ian@excess.org> | 2011-02-15 16:55:33 -0500 |
commit | e6ebebe917f5798721e439b1f3234ea9c46ddfaf (patch) | |
tree | 3b5f641c142da253ccb23073d35da456033e3f6e /urwid/widget.py | |
parent | c6c432ae68dc3a9a9a8572fd6889b7365ddc2c06 (diff) | |
download | urwid-e6ebebe917f5798721e439b1f3234ea9c46ddfaf.tar.gz |
normalize edit text to type of edit caption
--HG--
branch : python3
Diffstat (limited to 'urwid/widget.py')
-rw-r--r-- | urwid/widget.py | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/urwid/widget.py b/urwid/widget.py index 464049a..eaaf077 100644 --- a/urwid/widget.py +++ b/urwid/widget.py @@ -21,7 +21,7 @@ from urwid.util import MetaSuper, decompose_tagmarkup, calc_width, \ is_wide_char, move_prev_char, move_next_char -from urwid.compat import bytes, PYTHON3 +from urwid.compat import bytes from urwid.text_layout import calc_pos, calc_coords, shift_line from urwid import signals from urwid import text_layout @@ -881,7 +881,7 @@ class Edit(Text): Set the cursor position with a self.edit_text offset. Clips pos to [0, len(edit_text)]. - >>> e = Edit("", "word") + >>> e = Edit(u"", u"word") >>> e.edit_pos 4 >>> e.set_edit_pos(2) @@ -967,14 +967,25 @@ class Edit(Text): >>> print e.edit_text 42a.5 """ - if not PYTHON3: - text = unicode(text) - assert isinstance(text, unicode) + text = self._normalize_to_caption(text) result_text, result_pos = self.insert_text_result(text) self.set_edit_text(result_text) self.set_edit_pos(result_pos) self.highlight = None + def _normalize_to_caption(self, text): + """ + Return text converted to the same type as self.caption + (bytes or unicode) + """ + tu = isinstance(text, unicode) + cu = isinstance(self._caption, unicode) + if tu == cu: + return text + if tu: + return text.encode('ascii') # follow python2's implicit conversion + return text.decode('ascii') + def insert_text_result(self, text): """ Return result of insert_text(text) without actually performing the @@ -982,9 +993,7 @@ class Edit(Text): """ # if there's highlighted text, it'll get replaced by the new text - if not PYTHON3: - text = unicode(text) - assert isinstance(text, unicode) + text = self._normalize_to_caption(text) if self.highlight: start, stop = self.highlight btext, etext = self.edit_text[:start], self.edit_text[stop:] |