cpython: 9cf89366bbcb (original) (raw)
--- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3155,7 +3155,8 @@ class ASCIITest(unittest.TestCase): ('[\x80\xff\u20ac]', 'ignore', b'[]'), ('[\x80\xff\u20ac]', 'replace', b'[???]'), ('[\x80\xff\u20ac]', 'xmlcharrefreplace', b'[€ÿ€]'),
('[\x80\xff\u20ac]', 'backslashreplace', b'[\\x80\\xff\\u20ac]'),[](#l1.7)
('[\x80\xff\u20ac\U000abcde]', 'backslashreplace',[](#l1.8)
b'[\\x80\\xff\\u20ac\\U000abcde]'),[](#l1.9) ('[\udc80\udcff]', 'surrogateescape', b'[\x80\xff]'),[](#l1.10) ):[](#l1.11) with self.subTest(data=data, error_handler=error_handler,[](#l1.12)
@@ -3197,7 +3198,8 @@ class Latin1Test(unittest.TestCase): for data, error_handler, expected in ( ('[\u20ac\udc80]', 'ignore', b'[]'), ('[\u20ac\udc80]', 'replace', b'[??]'),
('[\u20ac\udc80]', 'backslashreplace', b'[\\u20ac\\udc80]'),[](#l1.17)
('[\u20ac\U000abcde]', 'backslashreplace',[](#l1.18)
b'[\\u20ac\\U000abcde]'),[](#l1.19) ('[\u20ac\udc80]', 'xmlcharrefreplace', b'[€�]'),[](#l1.20) ('[\udc80\udcff]', 'surrogateescape', b'[\x80\xff]'),[](#l1.21) ):[](#l1.22)
--- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -610,14 +610,25 @@ backslashreplace(_PyBytesWriter writer, / generate replacement */ for (i = collstart; i < collend; ++i) { ch = PyUnicode_READ(kind, data, i);
if (ch < 0x100)[](#l2.7)
str += sprintf(str, "\\x%02x", ch);[](#l2.8)
else if (ch < 0x10000)[](#l2.9)
str += sprintf(str, "\\u%04x", ch);[](#l2.10)
else {[](#l2.11)
assert(ch <= MAX_UNICODE);[](#l2.12)
str += sprintf(str, "\\U%08x", ch);[](#l2.13)
}[](#l2.14)
*str++ = '\\';[](#l2.15)
if (ch >= 0x00010000) {[](#l2.16)
*str++ = 'U';[](#l2.17)
*str++ = Py_hexdigits[(ch>>28)&0xf];[](#l2.18)
*str++ = Py_hexdigits[(ch>>24)&0xf];[](#l2.19)
*str++ = Py_hexdigits[(ch>>20)&0xf];[](#l2.20)
*str++ = Py_hexdigits[(ch>>16)&0xf];[](#l2.21)
*str++ = Py_hexdigits[(ch>>12)&0xf];[](#l2.22)
*str++ = Py_hexdigits[(ch>>8)&0xf];[](#l2.23)
}[](#l2.24)
else if (ch >= 0x100) {[](#l2.25)
*str++ = 'u';[](#l2.26)
*str++ = Py_hexdigits[(ch>>12)&0xf];[](#l2.27)
*str++ = Py_hexdigits[(ch>>8)&0xf];[](#l2.28)
}[](#l2.29)
else[](#l2.30)
*str++ = 'x';[](#l2.31)
*str++ = Py_hexdigits[(ch>>4)&0xf];[](#l2.32)
} return str; }*str++ = Py_hexdigits[ch&0xf];[](#l2.33)