Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e5a41b

Browse files
author
Victor Stinner
committed
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).
1 parent 6961bd6 commit 0e5a41b

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

Tools/gdb/libpython.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ def safe_range(val):
8888
# threshold in case the data was corrupted
8989
return xrange(safety_limit(val))
9090

91+
def write_unicode(file, text):
92+
# Write a byte or unicode string to file. Unicode strings are encoded to
93+
# ENCODING encoding with 'backslashreplace' error handler to avoid
94+
# UnicodeEncodeError.
95+
if isinstance(text, unicode):
96+
text = text.encode(ENCODING, 'backslashreplace')
97+
file.write(text)
9198

9299
class StringTruncated(RuntimeError):
93100
pass
@@ -1360,7 +1367,8 @@ def print_summary(self):
13601367
if self.is_evalframeex():
13611368
pyop = self.get_pyop()
13621369
if pyop:
1363-
sys.stdout.write('#%i %s\n' % (self.get_index(), pyop.get_truncated_repr(MAX_OUTPUT_LEN)))
1370+
line = pyop.get_truncated_repr(MAX_OUTPUT_LEN)
1371+
write_unicode(sys.stdout, '#%i %s\n' % (self.get_index(), line))
13641372
sys.stdout.write(pyop.current_line())
13651373
else:
13661374
sys.stdout.write('#%i (unable to read python frame information)\n' % self.get_index())

0 commit comments

Comments
 (0)