r""" >>> import io >>> s = 'abcd\uabcd' >>> enc = s.encode('utf-8') >>> enc b'abcd\xea\xaf\x8d' >>> n = bytes([len(enc)]) + b'\0' * 7 # little-endian 8-byte length >>> t = read_unicodestring8(io.BytesIO(n + enc + b'junk')) >>> s == t True >>> read_unicodestr
(f)
| 707 | |
| 708 | |
| 709 | def read_unicodestring8(f): |
| 710 | r""" |
| 711 | >>> import io |
| 712 | >>> s = 'abcd\uabcd' |
| 713 | >>> enc = s.encode('utf-8') |
| 714 | >>> enc |
| 715 | b'abcd\xea\xaf\x8d' |
| 716 | >>> n = bytes([len(enc)]) + b'\0' * 7 # little-endian 8-byte length |
| 717 | >>> t = read_unicodestring8(io.BytesIO(n + enc + b'junk')) |
| 718 | >>> s == t |
| 719 | True |
| 720 | |
| 721 | >>> read_unicodestring8(io.BytesIO(n + enc[:-1])) |
| 722 | Traceback (most recent call last): |
| 723 | ... |
| 724 | ValueError: expected 7 bytes in a unicodestring8, but only 6 remain |
| 725 | """ |
| 726 | |
| 727 | n = read_uint8(f) |
| 728 | assert n >= 0 |
| 729 | if n > sys.maxsize: |
| 730 | raise ValueError("unicodestring8 byte count > sys.maxsize: %d" % n) |
| 731 | data = f.read(n) |
| 732 | if len(data) == n: |
| 733 | return str(data, 'utf-8', 'surrogatepass') |
| 734 | raise ValueError("expected %d bytes in a unicodestring8, but only %d " |
| 735 | "remain" % (n, len(data))) |
| 736 | |
| 737 | unicodestring8 = ArgumentDescriptor( |
| 738 | name="unicodestring8", |
nothing calls this directly
no test coverage detected