MCPcopy Index your code
hub / github.com/python/cpython / BytesIO

Class BytesIO

Lib/_pyio.py:872–1031  ·  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 # Use to keep self._buffer and self._pos consistent.
882 self._lock = Lock()
883
884 buf = bytearray()
885 if initial_bytes is not None:
886 buf += initial_bytes
887
888 with self._lock:
889 self._buffer = buf
890 self._pos = 0
891
892 def __getstate__(self):
893 if self.closed:
894 raise ValueError("__getstate__ on closed file")
895 with self._lock:
896 state = self.__dict__.copy()
897 del state['_lock']
898 return state
899
900 def __setstate__(self, state):
901 self.__dict__.update(state)
902 self._lock = Lock()
903
904 def getvalue(self):
905 """Return the bytes value (contents) of the buffer
906 """
907 if self.closed:
908 raise ValueError("getvalue on closed file")
909 return bytes(self._buffer)
910
911 def getbuffer(self):
912 """Return a readable and writable view of the buffer.
913 """
914 if self.closed:
915 raise ValueError("getbuffer on closed file")
916 return memoryview(self._buffer)
917
918 def close(self):
919 if self._buffer is not None:
920 self._buffer.clear()
921 super().close()
922
923 def read(self, size=-1):
924 if self.closed:
925 raise ValueError("read from closed file")
926 if size is None:
927 size = -1
928 else:
929 try:

Callers 15

loadsFunction · 0.90
dumpsFunction · 0.90
_load_testfileFunction · 0.90
encodestringFunction · 0.90
decodestringFunction · 0.90
setupMethod · 0.90
uu_encodeFunction · 0.90
uu_decodeFunction · 0.90
quopri_encodeFunction · 0.90
quopri_decodeFunction · 0.90
as_bytesMethod · 0.90
_new_bufferMethod · 0.90

Calls

no outgoing calls

Tested by 15

_load_testfileFunction · 0.72
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

Used in the wild real call sites across dependent graphs

searching dependent graphs…