r""" >>> import io >>> s = 'abcd\uabcd' >>> enc = s.encode('utf-8') >>> enc b'abcd\xea\xaf\x8d' >>> n = bytes([len(enc), 0, 0, 0]) # little-endian 4-byte length >>> t = read_unicodestring4(io.BytesIO(n + enc + b'junk')) >>> s == t True >>> read_unicodestring
(f)
| 666 | |
| 667 | |
| 668 | def read_unicodestring4(f): |
| 669 | r""" |
| 670 | >>> import io |
| 671 | >>> s = 'abcd\uabcd' |
| 672 | >>> enc = s.encode('utf-8') |
| 673 | >>> enc |
| 674 | b'abcd\xea\xaf\x8d' |
| 675 | >>> n = bytes([len(enc), 0, 0, 0]) # little-endian 4-byte length |
| 676 | >>> t = read_unicodestring4(io.BytesIO(n + enc + b'junk')) |
| 677 | >>> s == t |
| 678 | True |
| 679 | |
| 680 | >>> read_unicodestring4(io.BytesIO(n + enc[:-1])) |
| 681 | Traceback (most recent call last): |
| 682 | ... |
| 683 | ValueError: expected 7 bytes in a unicodestring4, but only 6 remain |
| 684 | """ |
| 685 | |
| 686 | n = read_uint4(f) |
| 687 | assert n >= 0 |
| 688 | if n > sys.maxsize: |
| 689 | raise ValueError("unicodestring4 byte count > sys.maxsize: %d" % n) |
| 690 | data = f.read(n) |
| 691 | if len(data) == n: |
| 692 | return str(data, 'utf-8', 'surrogatepass') |
| 693 | raise ValueError("expected %d bytes in a unicodestring4, but only %d " |
| 694 | "remain" % (n, len(data))) |
| 695 | |
| 696 | unicodestring4 = ArgumentDescriptor( |
| 697 | name="unicodestring4", |
nothing calls this directly
no test coverage detected