MCPcopy Index your code
hub / github.com/RustPython/RustPython / read_unicodestring8

Function read_unicodestring8

Lib/pickletools.py:709–735  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

707
708
709def 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
737unicodestring8 = ArgumentDescriptor(
738 name="unicodestring8",

Callers

nothing calls this directly

Calls 4

read_uint8Function · 0.85
lenFunction · 0.85
strFunction · 0.85
readMethod · 0.45

Tested by

no test coverage detected