Skip to content

Commit

Permalink
Remove pointless try/except (#2091)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrudd2 authored Mar 7, 2024
1 parent d6704a2 commit bb888c6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
20 changes: 7 additions & 13 deletions pymodbus/transport/serialtransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/transport/test_serial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test transport."""
import asyncio
import contextlib
import os
from functools import partial
from unittest import mock
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit bb888c6

Please sign in to comment.