From ecd14db7928c28604ac6459e93dba9f3dbb45490 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Thu, 7 Mar 2024 11:13:28 -0600 Subject: [PATCH] Remove pointless try/except --- pymodbus/transport/serialtransport.py | 20 +++++++------------- test/transport/test_serial.py | 4 +++- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/pymodbus/transport/serialtransport.py b/pymodbus/transport/serialtransport.py index 310e40066..c783187ad 100644 --- a/pymodbus/transport/serialtransport.py +++ b/pymodbus/transport/serialtransport.py @@ -26,9 +26,8 @@ def __init__(self, loop, protocol, *args, **kwargs) -> None: self._poll_wait_time = 0.0005 self.sync_serial.timeout = 0 self.sync_serial.write_timeout = 0 - self.future: asyncio.Task | None = None - def setup(self): + def setup(self) -> None: """Prepare to read/write.""" if os.name == "nt" or self.force_poll: self.poll_task = asyncio.create_task(self.polling_task()) @@ -44,7 +43,6 @@ def close(self, exc: Exception | None = None) -> None: self.flush() if self.poll_task: self.poll_task.cancel() - self.future = asyncio.ensure_future(self.poll_task) self.poll_task = None else: self.async_loop.remove_reader(self.sync_serial.fileno()) @@ -147,16 +145,12 @@ def intern_write_ready(self) -> None: async def polling_task(self): """Poll and try to read/write.""" - try: - while self.sync_serial: - await asyncio.sleep(self._poll_wait_time) - while self.intern_write_buffer: - self.intern_write_ready() - if self.sync_serial.in_waiting: - self.intern_read_ready() - except asyncio.CancelledError: - self.close(None) - + while self.sync_serial: + await asyncio.sleep(self._poll_wait_time) + while self.intern_write_buffer: + self.intern_write_ready() + if self.sync_serial.in_waiting: + self.intern_read_ready() async def create_serial_connection( loop, protocol_factory, *args, **kwargs diff --git a/test/transport/test_serial.py b/test/transport/test_serial.py index 77a6c154c..62f54e8b4 100644 --- a/test/transport/test_serial.py +++ b/test/transport/test_serial.py @@ -1,5 +1,6 @@ """Test transport.""" import asyncio +import contextlib import os from functools import partial from unittest import mock @@ -114,7 +115,8 @@ async def test_polling(self): comm = SerialTransport(asyncio.get_running_loop(), mock.Mock(), "dummy") comm.sync_serial = mock.MagicMock() comm.sync_serial.read.side_effect = asyncio.CancelledError("test") - await comm.polling_task() + with contextlib.suppress(asyncio.CancelledError): + await comm.polling_task() @pytest.mark.skipif(os.name == "nt", reason="Windows not supported") async def test_poll_task(self):