MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / BytesIO

Class BytesIO

tools/python-3.11.9-amd64/Lib/_pyio.py:902–1042  ·  view source on GitHub ↗

Buffered I/O implementation using an in-memory bytes buffer.

Source from the content-addressed store, hash-verified

900
901
902class BytesIO(BufferedIOBase):
903
904 """Buffered I/O implementation using an in-memory bytes buffer."""
905
906 # Initialize _buffer as soon as possible since it's used by __del__()
907 # which calls close()
908 _buffer = None
909
910 def __init__(self, initial_bytes=None):
911 buf = bytearray()
912 if initial_bytes is not None:
913 buf += initial_bytes
914 self._buffer = buf
915 self._pos = 0
916
917 def __getstate__(self):
918 if self.closed:
919 raise ValueError("__getstate__ on closed file")
920 return self.__dict__.copy()
921
922 def getvalue(self):
923 """Return the bytes value (contents) of the buffer
924 """
925 if self.closed:
926 raise ValueError("getvalue on closed file")
927 return bytes(self._buffer)
928
929 def getbuffer(self):
930 """Return a readable and writable view of the buffer.
931 """
932 if self.closed:
933 raise ValueError("getbuffer on closed file")
934 return memoryview(self._buffer)
935
936 def close(self):
937 if self._buffer is not None:
938 self._buffer.clear()
939 super().close()
940
941 def read(self, size=-1):
942 if self.closed:
943 raise ValueError("read from closed file")
944 if size is None:
945 size = -1
946 else:
947 try:
948 size_index = size.__index__
949 except AttributeError:
950 raise TypeError(f"{size!r} is not an integer")
951 else:
952 size = size_index()
953 if size < 0:
954 size = len(self._buffer)
955 if len(self._buffer) <= self._pos:
956 return b""
957 newpos = min(len(self._buffer), self._pos + size)
958 b = self._buffer[self._pos : newpos]
959 self._pos = newpos

Callers 15

loadsFunction · 0.90
dumpsFunction · 0.90
encodestringFunction · 0.90
decodestringFunction · 0.90
__getitem__Method · 0.90
__setitem__Method · 0.90
set_locationMethod · 0.90
nextMethod · 0.90
previousMethod · 0.90
firstMethod · 0.90
lastMethod · 0.90
setupMethod · 0.90

Calls

no outgoing calls

Tested by 1

__init__Method · 0.72