def backslashreplace_errors(space, w_exc): check_exception(space, w_exc) if space.isinstance_w(w_exc, space.w_UnicodeEncodeError): obj = space.realunicode_w(space.getattr(w_exc, space.wrap('object'))) start = space.int_w(space.getattr(w_exc, space.wrap('start'))) w_end = space.getattr(w_exc, space.wrap('end')) end = space.int_w(w_end) builder = UnicodeBuilder() pos = start while pos < end: oc = ord(obj[pos]) num = hex(oc) if (oc >= 0x10000): builder.append(u"\\U") zeros = 8 elif (oc >= 0x100): builder.append(u"\\u") zeros = 4 else: builder.append(u"\\x") zeros = 2 lnum = len(num) nb = zeros + 2 - lnum # num starts with '0x' if nb > 0: builder.append_multiple_char(u'0', nb) builder.append_slice(unicode(num), 2, lnum) pos += 1 return space.newtuple([space.wrap(builder.build()), w_end]) else: typename = space.type(w_exc).getname(space, '?') raise operationerrfmt(space.w_TypeError, "don't know how to handle %s in error callback", typename)
def backslashreplace_errors(space, w_exc): check_exception(space, w_exc) if space.isinstance_w(w_exc, space.w_UnicodeEncodeError): obj = space.realunicode_w(space.getattr(w_exc, space.wrap('object'))) start = space.int_w(space.getattr(w_exc, space.wrap('start'))) w_end = space.getattr(w_exc, space.wrap('end')) end = space.int_w(w_end) builder = UnicodeBuilder() pos = start while pos < end: oc = ord(obj[pos]) num = hex(oc) if (oc >= 0x10000): builder.append(u"\\U") zeros = 8 elif (oc >= 0x100): builder.append(u"\\u") zeros = 4 else: builder.append(u"\\x") zeros = 2 lnum = len(num) nb = zeros + 2 - lnum # num starts with '0x' if nb > 0: builder.append_multiple_char(u'0', nb) builder.append_slice(unicode(num), 2, lnum) pos += 1 return space.newtuple([space.wrap(builder.build()), w_end]) else: typename = space.type(w_exc).getname(space) raise operationerrfmt(space.w_TypeError, "don't know how to handle %s in error callback", typename)
def func(): s = UnicodeBuilder() s.append(u'a') s.append(u'abc') s.append(u'abcdef') s.append_slice(u'abc', 1, 2) s.append_multiple_char(u'u', 4) return s.build()
def test_unicode_builder(): s = UnicodeBuilder() s.append(u'a') s.append(u'abc') s.append_slice(u'abcdef', 1, 2) s.append_multiple_char('d', 4) assert s.build() == 'aabcbdddd' assert isinstance(s.build(), unicode)