(code, fp, options)
| 645 | |
| 646 | |
| 647 | def _unpack_string(code, fp, options): |
| 648 | if (ord(code) & 0xe0) == 0xa0: |
| 649 | length = ord(code) & ~0xe0 |
| 650 | elif code == b'\xd9': |
| 651 | length = struct.unpack("B", _read_except(fp, 1))[0] |
| 652 | elif code == b'\xda': |
| 653 | length = struct.unpack(">H", _read_except(fp, 2))[0] |
| 654 | elif code == b'\xdb': |
| 655 | length = struct.unpack(">I", _read_except(fp, 4))[0] |
| 656 | else: |
| 657 | raise Exception("logic error, not string: 0x%02x" % ord(code)) |
| 658 | |
| 659 | # Always return raw bytes in compatibility mode |
| 660 | global compatibility |
| 661 | if compatibility: |
| 662 | return _read_except(fp, length) |
| 663 | |
| 664 | data = _read_except(fp, length) |
| 665 | try: |
| 666 | return bytes.decode(data, 'utf-8') |
| 667 | except UnicodeDecodeError: |
| 668 | if options.get("allow_invalid_utf8"): |
| 669 | return InvalidString(data) |
| 670 | raise InvalidStringException("unpacked string is invalid utf-8") |
| 671 | |
| 672 | |
| 673 | def _unpack_binary(code, fp, options): |
nothing calls this directly
no test coverage detected