[Python-checkins] r84638 - in python/branches/py3k: Lib/test/test_gdb.py Tools/gdb/libpython.py

antoine.pitrou python-checkins at python.org
Wed Sep 8 23:57:37 CEST 2010


Author: antoine.pitrou
Date: Wed Sep  8 23:57:37 2010
New Revision: 84638

Log:
gdb: fix representation of non-printable surrogate pairs, and workaround
a bug in ascii().



Modified:
   python/branches/py3k/Lib/test/test_gdb.py
   python/branches/py3k/Tools/gdb/libpython.py

Modified: python/branches/py3k/Lib/test/test_gdb.py
==============================================================================
--- python/branches/py3k/Lib/test/test_gdb.py	(original)
+++ python/branches/py3k/Lib/test/test_gdb.py	Wed Sep  8 23:57:37 2010
@@ -234,7 +234,9 @@
                 text.encode(encoding)
                 printable = True
             except UnicodeEncodeError:
-                self.assertGdbRepr(text, ascii(text))
+                # Workaround ascii() bug on UCS-2 builds: issue #9804
+                asc = "'" + text.encode('unicode-escape').decode('ascii') + "'"
+                self.assertGdbRepr(text, asc)
             else:
                 self.assertGdbRepr(text)
 

Modified: python/branches/py3k/Tools/gdb/libpython.py
==============================================================================
--- python/branches/py3k/Tools/gdb/libpython.py	(original)
+++ python/branches/py3k/Tools/gdb/libpython.py	Wed Sep  8 23:57:37 2010
@@ -1171,9 +1171,8 @@
             # Non-ASCII characters
             else:
                 ucs = ch
-                orig_ucs = None
                 ch2 = None
-                if self.char_width() == 2:
+                if sys.maxunicode < 0x10000:
                     # If sizeof(Py_UNICODE) is 2 here (in gdb), join
                     # surrogate pairs before calling _unichr_is_printable.
                     if (i < len(proxy)
@@ -1183,22 +1182,26 @@
                         ucs = ch + ch2
                         i += 1
 
+                # Unfortuately, Python 2's unicode type doesn't seem
+                # to expose the "isprintable" method
                 printable = _unichr_is_printable(ucs)
                 if printable:
                     try:
                         ucs.encode(ENCODING)
                     except UnicodeEncodeError:
                         printable = False
-                        if orig_ucs is not None:
-                            ucs = orig_ucs
-                            i -= 1
 
                 # Map Unicode whitespace and control characters
                 # (categories Z* and C* except ASCII space)
                 if not printable:
-                    # Unfortuately, Python 2's unicode type doesn't seem
-                    # to expose the "isprintable" method
-                    code = ord(ucs)
+                    if ch2 is not None:
+                        # Match Python 3's representation of non-printable
+                        # wide characters.
+                        code = (ord(ch) & 0x03FF) << 10
+                        code |= ord(ch2) & 0x03FF
+                        code += 0x00010000
+                    else:
+                        code = ord(ucs)
 
                     # Map 8-bit characters to '\\xhh'
                     if code <= 0xff:


More information about the Python-checkins mailing list