|
42 | 42 | ''' |
43 | 43 | from __future__ import with_statement |
44 | 44 | import gdb |
| 45 | +import locale |
45 | 46 |
|
46 | 47 | # Look up the gdb.Type for some standard types: |
47 | 48 | _type_char_ptr = gdb.lookup_type('char').pointer() # char* |
|
69 | 70 |
|
70 | 71 | hexdigits = "0123456789abcdef" |
71 | 72 |
|
| 73 | +ENCODING = locale.getpreferredencoding() |
72 | 74 |
|
73 | 75 | class NullPyObjectPtr(RuntimeError): |
74 | 76 | pass |
@@ -1128,53 +1130,68 @@ def write_repr(self, out, visited): |
1128 | 1130 |
|
1129 | 1131 | # Non-ASCII characters |
1130 | 1132 | else: |
1131 | | - ucs = ch; |
1132 | | - |
1133 | | - if self.char_width == 2: |
1134 | | - ch2 = 0 |
| 1133 | + ucs = ch |
| 1134 | + orig_ucs = None |
| 1135 | + if self.char_width() == 2: |
1135 | 1136 | # Get code point from surrogate pair |
1136 | | - if i < len(proxy): |
| 1137 | + if (i < len(proxy) |
| 1138 | + and 0xD800 <= ord(ch) < 0xDC00 \ |
| 1139 | + and 0xDC00 <= ord(proxy[i]) <= 0xDFFF): |
1137 | 1140 | ch2 = proxy[i] |
1138 | | - if (ord(ch) >= 0xD800 and ord(ch) < 0xDC00 |
1139 | | - and ord(ch2) >= 0xDC00 and ord(ch2) <= 0xDFFF): |
1140 | | - ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
1141 | | - i += 1 |
| 1141 | + code = (ord(ch) & 0x03FF) << 10 |
| 1142 | + code |= ord(ch2) & 0x03FF |
| 1143 | + code += 0x00010000 |
| 1144 | + orig_ucs = ucs |
| 1145 | + ucs = unichr(code) |
| 1146 | + i += 1 |
| 1147 | + else: |
| 1148 | + ch2 = None |
| 1149 | + |
| 1150 | + printable = _unichr_is_printable(ucs) |
| 1151 | + if printable: |
| 1152 | + try: |
| 1153 | + ucs.encode(ENCODING) |
| 1154 | + except UnicodeEncodeError: |
| 1155 | + printable = False |
| 1156 | + if orig_ucs is not None: |
| 1157 | + ucs = orig_ucs |
| 1158 | + i -= 1 |
1142 | 1159 |
|
1143 | 1160 | # Map Unicode whitespace and control characters |
1144 | 1161 | # (categories Z* and C* except ASCII space) |
1145 | | - if not _unichr_is_printable(ucs): |
| 1162 | + if not printable: |
1146 | 1163 | # Unfortuately, Python 2's unicode type doesn't seem |
1147 | 1164 | # to expose the "isprintable" method |
| 1165 | + code = ord(ucs) |
1148 | 1166 |
|
1149 | 1167 | # Map 8-bit characters to '\\xhh' |
1150 | | - if ucs <= 0xff: |
| 1168 | + if code <= 0xff: |
1151 | 1169 | out.write('\\x') |
1152 | | - out.write(hexdigits[(ord(ucs) >> 4) & 0x000F]) |
1153 | | - out.write(hexdigits[ord(ucs) & 0x000F]) |
| 1170 | + out.write(hexdigits[(code >> 4) & 0x000F]) |
| 1171 | + out.write(hexdigits[code & 0x000F]) |
1154 | 1172 | # Map 21-bit characters to '\U00xxxxxx' |
1155 | | - elif ucs >= 0x10000: |
| 1173 | + elif code >= 0x10000: |
1156 | 1174 | out.write('\\U') |
1157 | | - out.write(hexdigits[(ord(ucs) >> 28) & 0x0000000F]) |
1158 | | - out.write(hexdigits[(ord(ucs) >> 24) & 0x0000000F]) |
1159 | | - out.write(hexdigits[(ord(ucs) >> 20) & 0x0000000F]) |
1160 | | - out.write(hexdigits[(ord(ucs) >> 16) & 0x0000000F]) |
1161 | | - out.write(hexdigits[(ord(ucs) >> 12) & 0x0000000F]) |
1162 | | - out.write(hexdigits[(ord(ucs) >> 8) & 0x0000000F]) |
1163 | | - out.write(hexdigits[(ord(ucs) >> 4) & 0x0000000F]) |
1164 | | - out.write(hexdigits[ord(ucs) & 0x0000000F]) |
| 1175 | + out.write(hexdigits[(code >> 28) & 0x0000000F]) |
| 1176 | + out.write(hexdigits[(code >> 24) & 0x0000000F]) |
| 1177 | + out.write(hexdigits[(code >> 20) & 0x0000000F]) |
| 1178 | + out.write(hexdigits[(code >> 16) & 0x0000000F]) |
| 1179 | + out.write(hexdigits[(code >> 12) & 0x0000000F]) |
| 1180 | + out.write(hexdigits[(code >> 8) & 0x0000000F]) |
| 1181 | + out.write(hexdigits[(code >> 4) & 0x0000000F]) |
| 1182 | + out.write(hexdigits[code & 0x0000000F]) |
1165 | 1183 | # Map 16-bit characters to '\uxxxx' |
1166 | 1184 | else: |
1167 | 1185 | out.write('\\u') |
1168 | | - out.write(hexdigits[(ord(ucs) >> 12) & 0x000F]) |
1169 | | - out.write(hexdigits[(ord(ucs) >> 8) & 0x000F]) |
1170 | | - out.write(hexdigits[(ord(ucs) >> 4) & 0x000F]) |
1171 | | - out.write(hexdigits[ord(ucs) & 0x000F]) |
| 1186 | + out.write(hexdigits[(code >> 12) & 0x000F]) |
| 1187 | + out.write(hexdigits[(code >> 8) & 0x000F]) |
| 1188 | + out.write(hexdigits[(code >> 4) & 0x000F]) |
| 1189 | + out.write(hexdigits[code & 0x000F]) |
1172 | 1190 | else: |
1173 | 1191 | # Copy characters as-is |
1174 | 1192 | out.write(ch) |
1175 | | - if self.char_width == 2: |
1176 | | - if ord(ucs) >= 0x10000: |
1177 | | - out.write(ch2) |
| 1193 | + if self.char_width() == 2 and (ch2 is not None): |
| 1194 | + out.write(ch2) |
1178 | 1195 |
|
1179 | 1196 | out.write(quote) |
1180 | 1197 |
|
|
0 commit comments