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