From d043743e4b8118b7cbc2f905b0c67314ff075190 Mon Sep 17 00:00:00 2001 From: Svein Seldal Date: Sun, 15 Jun 2025 02:35:57 +0200 Subject: [PATCH 1/2] Prevent warnings if SDO setup fails --- canopen/sdo/client.py | 5 +++++ canopen/sdo/server.py | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/canopen/sdo/client.py b/canopen/sdo/client.py index 6d92588e..bfa897e9 100644 --- a/canopen/sdo/client.py +++ b/canopen/sdo/client.py @@ -669,6 +669,8 @@ def __init__(self, sdo_client, index, subindex=0, size=None, request_crc_support self._blksize, = struct.unpack_from("B", response, 4) logger.debug("Server requested a block size of %d", self._blksize) self.crc_supported = bool(res_command & CRC_SUPPORTED) + # Run this last, used later to determine if initialization was successful + self._initialized = True def write(self, b): """ @@ -784,6 +786,9 @@ def close(self): if self.closed: return super(BlockDownloadStream, self).close() + if not hasattr(self, "_initialized"): + # Don't do finalization if initialization was not successful + return if not self._done: logger.error("Block transfer was not finished") command = REQUEST_BLOCK_DOWNLOAD | END_BLOCK_TRANSFER diff --git a/canopen/sdo/server.py b/canopen/sdo/server.py index c6a4c27c..2947afd0 100644 --- a/canopen/sdo/server.py +++ b/canopen/sdo/server.py @@ -120,6 +120,10 @@ def request_aborted(self, data): def block_download(self, data): # We currently don't support BLOCK DOWNLOAD + # Unpack the index and subindex in order to send appropriate abort + command, index, subindex = SDO_STRUCT.unpack_from(data) + self._index = index + self._subindex = subindex logger.error("Block download is not supported") self.abort(0x05040001) From 970f20407315a32424d6750ffdbedc8866701b4c Mon Sep 17 00:00:00 2001 From: Svein Seldal Date: Wed, 18 Jun 2025 17:12:48 +0200 Subject: [PATCH 2/2] Use getattr instead of hasattr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Colomb --- canopen/sdo/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/canopen/sdo/client.py b/canopen/sdo/client.py index bfa897e9..2a82ed12 100644 --- a/canopen/sdo/client.py +++ b/canopen/sdo/client.py @@ -786,7 +786,7 @@ def close(self): if self.closed: return super(BlockDownloadStream, self).close() - if not hasattr(self, "_initialized"): + if not getattr(self, "_initialized", False): # Don't do finalization if initialization was not successful return if not self._done: