@support.requires_resource('cpu')
def test_strings(self):
'Verify the pretty-printing of unicode strings'
- # We cannot simply call locale.getpreferredencoding() here,
- # as GDB might have been linked against a different version
- # of Python with a different encoding and coercion policy
- # with respect to PEP 538 and PEP 540.
+ # gdb emits its output in the host charset, which is not necessarily the
+ # getpreferredencoding() of the (possibly differently coerced) embedded
+ # Python.
stdout, stderr = run_gdb(
'--eval-command',
- 'python import locale; print(locale.getpreferredencoding())')
+ 'python import gdb; print(gdb.host_charset())')
- encoding = stdout
+ encoding = stdout.strip()
if stderr or not encoding:
raise RuntimeError(
- f'unable to determine the Python locale preferred encoding '
- f'of embedded Python in GDB\n'
+ f'unable to determine the host charset of gdb\n'
f'stdout={stdout!r}\n'
f'stderr={stderr!r}')
def check_repr(text):
try:
text.encode(encoding)
- except UnicodeEncodeError:
+ # LookupError or ValueError if the host charset is unknown or invalid.
+ except (UnicodeEncodeError, LookupError, ValueError):
self.assertGdbRepr(text, ascii(text))
else:
- self.assertGdbRepr(text)
+ self.assertGdbRepr(text, repr(text).encode(encoding).decode('ascii', 'surrogateescape'))
self.assertGdbRepr('')
self.assertGdbRepr('And now for something hopefully the same')
import gdb
import os
-import locale
import sys
USED_TAGS = 0b11
-ENCODING = locale.getpreferredencoding()
-
FRAME_INFO_OPTIMIZED_OUT = '(frame information optimized out)'
UNABLE_READ_INFO_PYTHON_FRAME = 'Unable to read information on python frame'
EVALFRAME = '_PyEval_EvalFrameDefault'
def write_repr(self, out, visited):
# Write this out as a Python str literal
+ # gdb writes its output in the host charset, so a character is escaped
+ # unless it is printable and encodable in that charset.
+ encoding = gdb.host_charset()
+
# Get a PyUnicodeObject* within the Python gdb process:
proxy = self.proxyval(visited)
printable = ucs.isprintable()
if printable:
try:
- ucs.encode(ENCODING)
- except UnicodeEncodeError:
+ ucs.encode(encoding)
+ # LookupError or ValueError if the host charset is unknown
+ # or invalid.
+ except (UnicodeEncodeError, LookupError, ValueError):
printable = False
# Map Unicode whitespace and control characters