From 8404b16378919b97d0157132303fd0ebf5bcb928 Mon Sep 17 00:00:00 2001 From: Zosili Date: Tue, 24 Nov 2026 19:58:34 +0800 Subject: [PATCH] ntptime: Add module-specific exception types. time() raised a bare Exception for an unsupported platform epoch and OSError(-1) for a malformed reply, so callers had no way to tell these apart from unrelated failures. Add NTPError, which inherits from OSError so existing except OSError handlers keep working, and UnsupportedEpochError for the epoch case. Both are covered by the new tests in test_ntptime.py. Signed-off-by: Zosili --- micropython/net/ntptime/manifest.py | 2 +- micropython/net/ntptime/ntptime.py | 14 ++++++++--- micropython/net/ntptime/test_ntptime.py | 32 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/micropython/net/ntptime/manifest.py b/micropython/net/ntptime/manifest.py index 858f75ca3..e69145ee5 100644 --- a/micropython/net/ntptime/manifest.py +++ b/micropython/net/ntptime/manifest.py @@ -1,3 +1,3 @@ -metadata(description="NTP client.", version="0.2.2") +metadata(description="NTP client.", version="0.3.0") module("ntptime.py", opt=3) diff --git a/micropython/net/ntptime/ntptime.py b/micropython/net/ntptime/ntptime.py index de161a2f0..c718b7cf3 100644 --- a/micropython/net/ntptime/ntptime.py +++ b/micropython/net/ntptime/ntptime.py @@ -8,6 +8,14 @@ timeout = 1 +class NTPError(OSError): + """The NTP server returned an error or an invalid response.""" + + +class UnsupportedEpochError(Exception): + """The platform epoch is neither 1970 nor 2000.""" + + def time(): NTP_QUERY = bytearray(48) NTP_QUERY[0] = 0x23 @@ -20,10 +28,10 @@ def time(): finally: s.close() if len(msg) < 48: - raise OSError(-1) + raise NTPError(-1) val = struct.unpack("!I", msg[40:44])[0] if not (msg[1] and val): - raise OSError(-1) + raise NTPError(-1) # 2024-01-01 00:00:00 converted to an NTP timestamp MIN_NTP_TIMESTAMP = 3913056000 @@ -58,7 +66,7 @@ def time(): # (date(1970, 1, 1) - date(1900, 1, 1)).days * 24*60*60 NTP_DELTA = 2208988800 else: - raise Exception("Unsupported epoch: {}".format(EPOCH_YEAR)) + raise UnsupportedEpochError("Unsupported epoch: {}".format(EPOCH_YEAR)) return val - NTP_DELTA diff --git a/micropython/net/ntptime/test_ntptime.py b/micropython/net/ntptime/test_ntptime.py index acc83c06b..33234ff05 100644 --- a/micropython/net/ntptime/test_ntptime.py +++ b/micropython/net/ntptime/test_ntptime.py @@ -96,7 +96,39 @@ def test_time_zero_ts_raises(): ntptime.socket = orig +def test_time_short_raises_ntp_error(): + orig = _patch(b"short") + try: + try: + ntptime.time() + assert False, "expected NTPError" + except ntptime.NTPError as e: + assert isinstance(e, OSError) + assert e.args == (-1,) + finally: + ntptime.socket = orig + + +def test_unsupported_epoch_raises(): + orig_socket = _patch(ntp_msg()) + orig_gmtime = ntptime.gmtime + ntptime.gmtime = lambda t: (2010, 1, 1, 0, 0, 0, 0, 0) + try: + try: + ntptime.time() + assert False, "expected UnsupportedEpochError" + except ntptime.UnsupportedEpochError as e: + assert isinstance(e, Exception) + assert not isinstance(e, OSError) + assert "Unsupported epoch: 2010" in str(e) + finally: + ntptime.gmtime = orig_gmtime + ntptime.socket = orig_socket + + test_time_ok() test_time_short_raises() test_time_kod_stratum_raises() test_time_zero_ts_raises() +test_time_short_raises_ntp_error() +test_unsupported_epoch_raises()