From 0e5a41b8f5b87bf07ed6368df5a84137d3e4e4da Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 17 Aug 2010 22:49:25 +0000 Subject: libpython.py: py-bt commands escape unencodable characters Encode unicode strings to the terminal encoding with backslashreplace error (as Python does for sys.stderr) before writing them to sys.stdout. It fixes UnicodeEncodeError on writing non-ascii characters in an ascii terminal (C locale: ASCII encoding). --- Tools/gdb/libpython.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Tools/gdb/libpython.py b/Tools/gdb/libpython.py index ac4aff5..b23a22e 100644 --- a/Tools/gdb/libpython.py +++ b/Tools/gdb/libpython.py @@ -88,6 +88,13 @@ def safe_range(val): # threshold in case the data was corrupted return xrange(safety_limit(val)) +def write_unicode(file, text): + # Write a byte or unicode string to file. Unicode strings are encoded to + # ENCODING encoding with 'backslashreplace' error handler to avoid + # UnicodeEncodeError. + if isinstance(text, unicode): + text = text.encode(ENCODING, 'backslashreplace') + file.write(text) class StringTruncated(RuntimeError): pass @@ -1360,7 +1367,8 @@ class Frame(object): if self.is_evalframeex(): pyop = self.get_pyop() if pyop: - sys.stdout.write('#%i %s\n' % (self.get_index(), pyop.get_truncated_repr(MAX_OUTPUT_LEN))) + line = pyop.get_truncated_repr(MAX_OUTPUT_LEN) + write_unicode(sys.stdout, '#%i %s\n' % (self.get_index(), line)) sys.stdout.write(pyop.current_line()) else: sys.stdout.write('#%i (unable to read python frame information)\n' % self.get_index()) -- cgit v0.12