MCPcopy Create free account
hub / github.com/Bitmessage/PyBitmessage / _unpack_string

Function _unpack_string

src/fallback/umsgpack/umsgpack.py:647–670  ·  view source on GitHub ↗
(code, fp, options)

Source from the content-addressed store, hash-verified

645
646
647def _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
673def _unpack_binary(code, fp, options):

Callers

nothing calls this directly

Calls 5

_read_exceptFunction · 0.85
InvalidStringClass · 0.85
decodeMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected