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.
| 661 | _blocking_errnos = { EAGAIN, EWOULDBLOCK } |
| 662 | |
| 663 | class SocketIO(io.RawIOBase): |
| 664 | |
| 665 | """Raw I/O implementation for stream sockets. |
| 666 | |
| 667 | This class supports the makefile() method on sockets. It provides |
| 668 | the raw I/O interface on top of a socket object. |
| 669 | """ |
| 670 | |
| 671 | # One might wonder why not let FileIO do the job instead. There are two |
| 672 | # main reasons why FileIO is not adapted: |
| 673 | # - it wouldn't work under Windows (where you can't used read() and |
| 674 | # write() on a socket handle) |
| 675 | # - it wouldn't work with socket timeouts (FileIO would ignore the |
| 676 | # timeout and consider the socket non-blocking) |
| 677 | |
| 678 | # XXX More docs |
| 679 | |
| 680 | def __init__(self, sock, mode): |
| 681 | if mode not in ("r", "w", "rw", "rb", "wb", "rwb"): |
| 682 | raise ValueError("invalid mode: %r" % mode) |
| 683 | io.RawIOBase.__init__(self) |
| 684 | self._sock = sock |
| 685 | if "b" not in mode: |
| 686 | mode += "b" |
| 687 | self._mode = mode |
| 688 | self._reading = "r" in mode |
| 689 | self._writing = "w" in mode |
| 690 | self._timeout_occurred = False |
| 691 | |
| 692 | def readinto(self, b): |
| 693 | """Read up to len(b) bytes into the writable buffer *b* and return |
| 694 | the number of bytes read. If the socket is non-blocking and no bytes |
| 695 | are available, None is returned. |
| 696 | |
| 697 | If *b* is non-empty, a 0 return value indicates that the connection |
| 698 | was shutdown at the other end. |
| 699 | """ |
| 700 | self._checkClosed() |
| 701 | self._checkReadable() |
| 702 | if self._timeout_occurred: |
| 703 | raise OSError("cannot read from timed out object") |
| 704 | while True: |
| 705 | try: |
| 706 | return self._sock.recv_into(b) |
| 707 | except timeout: |
| 708 | self._timeout_occurred = True |
| 709 | raise |
| 710 | except error as e: |
| 711 | if e.errno in _blocking_errnos: |
| 712 | return None |
| 713 | raise |
| 714 | |
| 715 | def write(self, b): |
| 716 | """Write the given bytes or bytearray object *b* to the socket |
| 717 | and return the number of bytes written. This can be less than |
| 718 | len(b) if not all data could be written. If the socket is |
| 719 | non-blocking and no bytes could be written None is returned. |
| 720 | """ |
no outgoing calls
no test coverage detected