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

Class BytesIO

Lib/_pyio.py:872–1012  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

870
871
872class BytesIO(BufferedIOBase):
873
874 """Buffered I/O implementation using an in-memory bytes buffer."""
875
876 # Initialize _buffer as soon as possible since it's used by __del__()
877 # which calls close()
878 _buffer = None
879
880 def __init__(self, initial_bytes=None):
881 buf = bytearray()
882 if initial_bytes is not None:
883 buf += initial_bytes
884 self._buffer = buf
885 self._pos = 0
886
887 def __getstate__(self):
888 if self.closed:
889 raise ValueError("__getstate__ on closed file")
890 return self.__dict__.copy()
891
892 def getvalue(self):
893 """Return the bytes value (contents) of the buffer
894 """
895 if self.closed:
896 raise ValueError("getvalue on closed file")
897 return bytes(self._buffer)
898
899 def getbuffer(self):
900 """Return a readable and writable view of the buffer.
901 """
902 if self.closed:
903 raise ValueError("getbuffer on closed file")
904 return memoryview(self._buffer)
905
906 def close(self):
907 if self._buffer is not None:
908 self._buffer.clear()
909 super().close()
910
911 def read(self, size=-1):
912 if self.closed:
913 raise ValueError("read from closed file")
914 if size is None:
915 size = -1
916 else:
917 try:
918 size_index = size.__index__
919 except AttributeError:
920 raise TypeError(f"{size!r} is not an integer")
921 else:
922 size = size_index()
923 if size < 0:
924 size = len(self._buffer)
925 if len(self._buffer) <= self._pos:
926 return b""
927 newpos = min(len(self._buffer), self._pos + size)
928 b = self._buffer[self._pos : newpos]
929 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 15

test_streamreaderMethod · 0.72
test_streamwriterMethod · 0.72
test_parse_bytesMethod · 0.72
make_byte_streamMethod · 0.72
ioclassMethod · 0.72
test_filter_basicMethod · 0.72