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

Class SocketIO

Lib/socket.py:683–796  ·  view source on GitHub ↗

Raw I/O implementation for stream sockets. This class supports the makefile() method on sockets. It provides the raw I/O interface on top of a socket object.

Source from the content-addressed store, hash-verified

681_blocking_errnos = { EAGAIN, EWOULDBLOCK }
682
683class SocketIO(io.RawIOBase):
684
685 """Raw I/O implementation for stream sockets.
686
687 This class supports the makefile() method on sockets. It provides
688 the raw I/O interface on top of a socket object.
689 """
690
691 # One might wonder why not let FileIO do the job instead. There are two
692 # main reasons why FileIO is not adapted:
693 # - it wouldn't work under Windows (where you can't used read() and
694 # write() on a socket handle)
695 # - it wouldn't work with socket timeouts (FileIO would ignore the
696 # timeout and consider the socket non-blocking)
697
698 # XXX More docs
699
700 def __init__(self, sock, mode):
701 if mode not in ("r", "w", "rw", "rb", "wb", "rwb"):
702 raise ValueError("invalid mode: %r" % mode)
703 io.RawIOBase.__init__(self)
704 self._sock = sock
705 if "b" not in mode:
706 mode += "b"
707 self._mode = mode
708 self._reading = "r" in mode
709 self._writing = "w" in mode
710 self._timeout_occurred = False
711
712 def readinto(self, b):
713 """Read up to len(b) bytes into the writable buffer *b* and return
714 the number of bytes read. If the socket is non-blocking and no bytes
715 are available, None is returned.
716
717 If *b* is non-empty, a 0 return value indicates that the connection
718 was shutdown at the other end.
719 """
720 self._checkClosed()
721 self._checkReadable()
722 if self._timeout_occurred:
723 raise OSError("cannot read from timed out object")
724 try:
725 return self._sock.recv_into(b)
726 except timeout:
727 self._timeout_occurred = True
728 raise
729 except error as e:
730 if e.errno in _blocking_errnos:
731 return None
732 raise
733
734 def write(self, b):
735 """Write the given bytes or bytearray object *b* to the socket
736 and return the number of bytes written. This can be less than
737 len(b) if not all data could be written. If the socket is
738 non-blocking and no bytes could be written None is returned.
739 """
740 self._checkClosed()

Callers 1

makefileMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected