|
17 | 17 | from __future__ import annotations
|
18 | 18 |
|
19 | 19 | try:
|
20 |
| - from typing import TYPE_CHECKING, Optional, Tuple, List, Union |
| 20 | + from typing import TYPE_CHECKING, List, Optional, Tuple, Union |
21 | 21 |
|
22 | 22 | if TYPE_CHECKING:
|
23 | 23 | from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
|
|
27 | 27 | import gc
|
28 | 28 | import time
|
29 | 29 | from sys import byteorder
|
| 30 | + |
30 | 31 | from micropython import const
|
31 | 32 |
|
32 | 33 | import adafruit_wiznet5k as wiznet5k
|
@@ -79,7 +80,7 @@ def setdefaulttimeout(_timeout: Optional[float]) -> None:
|
79 | 80 | :param Optional[float] _timeout: The default timeout in seconds or None.
|
80 | 81 | """
|
81 | 82 | global _default_socket_timeout # pylint: disable=global-statement
|
82 |
| - if _timeout is None or (isinstance(_timeout, (int, float)) and _timeout >= 0): |
| 83 | + if _timeout is None or _timeout >= 0: |
83 | 84 | _default_socket_timeout = _timeout
|
84 | 85 | else:
|
85 | 86 | raise ValueError("Timeout must be None, 0.0 or a positive numeric value.")
|
@@ -496,7 +497,11 @@ def recv(
|
496 | 497 | :return bytes: Data from the socket.
|
497 | 498 | """
|
498 | 499 | buf = bytearray(bufsize)
|
499 |
| - self.recv_into(buf, bufsize) |
| 500 | + nread = self.recv_into(buf, bufsize) |
| 501 | + if nread == 0: |
| 502 | + return b"" |
| 503 | + if nread < bufsize: |
| 504 | + return bytes(buf[:nread]) |
500 | 505 | return bytes(buf)
|
501 | 506 |
|
502 | 507 | def _embed_recv(
|
@@ -595,7 +600,13 @@ def recv_into(self, buffer: bytearray, nbytes: int = 0, flags: int = 0) -> int:
|
595 | 600 | # We got a message, but there are no more bytes to read, so we can stop.
|
596 | 601 | break
|
597 | 602 | # No bytes yet, or more bytes requested.
|
598 |
| - if self._timeout > 0 and time.monotonic() - last_read_time > self._timeout: |
| 603 | + if self._timeout is None: |
| 604 | + # blocking mode |
| 605 | + continue |
| 606 | + if self._timeout == 0: |
| 607 | + # non-blocking mode |
| 608 | + break |
| 609 | + if time.monotonic() - last_read_time > self._timeout: |
599 | 610 | raise timeout("timed out")
|
600 | 611 | return num_read
|
601 | 612 |
|
@@ -688,7 +699,7 @@ def settimeout(self, value: Optional[float]) -> None:
|
688 | 699 |
|
689 | 700 | :param Optional[float] value: Socket read timeout in seconds.
|
690 | 701 | """
|
691 |
| - if value is None or (isinstance(value, (int, float)) and value >= 0): |
| 702 | + if value is None or value >= 0: |
692 | 703 | self._timeout = value
|
693 | 704 | else:
|
694 | 705 | raise ValueError("Timeout must be None, 0.0 or a positive numeric value.")
|
|
0 commit comments