[Python-checkins] r84164 - python/branches/py3k/Tools/gdb/libpython.py

victor.stinner python-checkins at python.org
Wed Aug 18 00:49:25 CEST 2010


Author: victor.stinner
Date: Wed Aug 18 00:49:25 2010
New Revision: 84164

Log:
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).


Modified:
   python/branches/py3k/Tools/gdb/libpython.py

Modified: python/branches/py3k/Tools/gdb/libpython.py
==============================================================================
--- python/branches/py3k/Tools/gdb/libpython.py	(original)
+++ python/branches/py3k/Tools/gdb/libpython.py	Wed Aug 18 00:49:25 2010
@@ -88,6 +88,13 @@
     # 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 @@
         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())


More information about the Python-checkins mailing list