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

Function read_unicodestring4

Lib/pickletools.py:668–694  ·  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), 0, 0, 0]) # little-endian 4-byte length >>> t = read_unicodestring4(io.BytesIO(n + enc + b'junk')) >>> s == t True >>> read_unicodestring

(f)

Source from the content-addressed store, hash-verified

666
667
668def 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
696unicodestring4 = ArgumentDescriptor(
697 name="unicodestring4",

Callers

nothing calls this directly

Calls 4

read_uint4Function · 0.85
lenFunction · 0.85
strFunction · 0.85
readMethod · 0.45

Tested by

no test coverage detected