@@ -114,29 +114,28 @@ def test_bytes(self):
114114 @support .requires_resource ('cpu' )
115115 def test_strings (self ):
116116 'Verify the pretty-printing of unicode strings'
117- # We cannot simply call locale.getpreferredencoding() here,
118- # as GDB might have been linked against a different version
119- # of Python with a different encoding and coercion policy
120- # with respect to PEP 538 and PEP 540.
117+ # gdb emits its output in the host charset, which is not necessarily the
118+ # getpreferredencoding() of the (possibly differently coerced) embedded
119+ # Python.
121120 stdout , stderr = run_gdb (
122121 '--eval-command' ,
123- 'python import locale ; print(locale.getpreferredencoding ())' )
122+ 'python import gdb ; print(gdb.host_charset ())' )
124123
125- encoding = stdout
124+ encoding = stdout . strip ()
126125 if stderr or not encoding :
127126 raise RuntimeError (
128- f'unable to determine the Python locale preferred encoding '
129- f'of embedded Python in GDB\n '
127+ f'unable to determine the host charset of gdb\n '
130128 f'stdout={ stdout !r} \n '
131129 f'stderr={ stderr !r} ' )
132130
133131 def check_repr (text ):
134132 try :
135133 text .encode (encoding )
136- except UnicodeEncodeError :
134+ # LookupError or ValueError if the host charset is unknown or invalid.
135+ except (UnicodeEncodeError , LookupError , ValueError ):
137136 self .assertGdbRepr (text , ascii (text ))
138137 else :
139- self .assertGdbRepr (text )
138+ self .assertGdbRepr (text , repr ( text ). encode ( encoding ). decode ( 'ascii' , 'surrogateescape' ) )
140139
141140 self .assertGdbRepr ('' )
142141 self .assertGdbRepr ('And now for something hopefully the same' )
0 commit comments