@@ -70,16 +70,16 @@ def getdefaulttimeout() -> Optional[float]:
7070 return _default_socket_timeout
7171
7272
73- def setdefaulttimeout (timeout : Optional [float ]) -> None :
73+ def setdefaulttimeout (_timeout : Optional [float ]) -> None :
7474 """
7575 Set the default timeout in seconds (float) for new socket objects. When the socket
7676 module is first imported, the default is None. See settimeout() for possible values
7777 and their respective meanings.
7878
79- :param Optional[float] timeout : The default timeout in seconds or None.
79+ :param Optional[float] _timeout : The default timeout in seconds or None.
8080 """
8181 global _default_socket_timeout # pylint: disable=global-statement
82- if timeout is None or (isinstance (timeout , (int , float )) and timeout >= 0 ):
82+ if _timeout is None or (isinstance (timeout , (int , float )) and timeout >= 0 ):
8383 _default_socket_timeout = timeout
8484 else :
8585 raise ValueError ("Timeout must be None, 0.0 or a positive numeric value." )
@@ -450,8 +450,8 @@ def send(self, data: Union[bytes, bytearray]) -> int:
450450
451451 :return int: Number of bytes sent.
452452 """
453- timeout = 0 if self ._timeout is None else self ._timeout
454- bytes_sent = _the_interface .socket_write (self ._socknum , data , timeout )
453+ _timeout = 0 if self ._timeout is None else self ._timeout
454+ bytes_sent = _the_interface .socket_write (self ._socknum , data , _timeout )
455455 gc .collect ()
456456 return bytes_sent
457457
@@ -495,21 +495,9 @@ def recv(
495495
496496 :return bytes: Data from the socket.
497497 """
498- stamp = time .monotonic ()
499- while not self ._available ():
500- if self ._timeout and 0 < self ._timeout < time .monotonic () - stamp :
501- break
502- time .sleep (0.05 )
503- bytes_on_socket = self ._available ()
504- if not bytes_on_socket :
505- return b""
506- bytes_to_read = min (bytes_on_socket , bufsize )
507- if self ._sock_type == SOCK_STREAM :
508- bytes_read = _the_interface .socket_read (self ._socknum , bytes_to_read )[1 ]
509- else :
510- bytes_read = _the_interface .read_udp (self ._socknum , bytes_to_read )[1 ]
511- gc .collect ()
512- return bytes (bytes_read )
498+ buf = bytearray (bufsize )
499+ self .recv_into (buf , bufsize )
500+ return bytes (buf )
513501
514502 def _embed_recv (
515503 self , bufsize : int = 0 , flags : int = 0
@@ -568,12 +556,48 @@ def recv_into(self, buffer: bytearray, nbytes: int = 0, flags: int = 0) -> int:
568556
569557 :return int: the number of bytes received
570558 """
571- if nbytes == 0 :
572- nbytes = len (buffer )
573- bytes_received = self .recv (nbytes )
574- nbytes = len (bytes_received )
575- buffer [:nbytes ] = bytes_received
576- return nbytes
559+ if not 0 <= nbytes <= len (buffer ):
560+ raise ValueError ("nbytes must be 0 to len(buffer)" )
561+
562+ last_read_time = time .monotonic ()
563+ num_to_read = len (buffer ) if nbytes == 0 else nbytes
564+ num_read = 0
565+ while num_to_read > 0 :
566+ # we might have read socket data into the self._buffer with:
567+ # _readline
568+ if len (self ._buffer ) > 0 :
569+ bytes_to_read = min (num_to_read , len (self ._buffer ))
570+ buffer [num_read : num_read + bytes_to_read ] = self ._buffer [
571+ :bytes_to_read
572+ ]
573+ num_read += bytes_to_read
574+ num_to_read -= bytes_to_read
575+ self ._buffer = self ._buffer [bytes_to_read :]
576+ # explicitly recheck num_to_read to avoid extra checks
577+ continue
578+
579+ num_avail = self ._available ()
580+ if num_avail > 0 :
581+ last_read_time = time .monotonic ()
582+ bytes_to_read = min (num_to_read , num_avail )
583+ if self ._sock_type == SOCK_STREAM :
584+ bytes_read = _the_interface .socket_read (
585+ self ._socknum , bytes_to_read
586+ )[1 ]
587+ else :
588+ bytes_read = _the_interface .read_udp (self ._socknum , bytes_to_read )[
589+ 1
590+ ]
591+ buffer [num_read : num_read + len (bytes_read )] = bytes_read
592+ num_read += len (bytes_read )
593+ num_to_read -= len (bytes_read )
594+ elif num_read > 0 :
595+ # We got a message, but there are no more bytes to read, so we can stop.
596+ break
597+ # No bytes yet, or more bytes requested.
598+ if self ._timeout > 0 and time .monotonic () - last_read_time > self ._timeout :
599+ raise timeout ("timed out" )
600+ return num_read
577601
578602 @_check_socket_closed
579603 def recvfrom_into (
@@ -730,3 +754,14 @@ def type(self):
730754 def proto (self ):
731755 """Socket protocol (always 0x00 in this implementation)."""
732756 return 0
757+
758+
759+ class timeout (TimeoutError ):
760+ """TimeoutError class. An instance of this error will be raised by recv_into() if
761+ the timeout has elapsed and we haven't received any data yet."""
762+
763+ def __init__ (self , msg ):
764+ super ().__init__ (msg )
765+
766+
767+ # pylint: enable=unused-argument, redefined-builtin, invalid-name
0 commit comments