From 1137f35f387a4d225a240001aef0fc5f0cbc4b84 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Thu, 27 Mar 2025 10:24:47 -0500 Subject: [PATCH 1/6] improve types in class definition --- serial_asyncio_fast/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/serial_asyncio_fast/__init__.py b/serial_asyncio_fast/__init__.py index f4c766f..c684e73 100644 --- a/serial_asyncio_fast/__init__.py +++ b/serial_asyncio_fast/__init__.py @@ -53,6 +53,10 @@ class SerialTransport(asyncio.Transport): calling you back when it succeeds. """ + _loop: asyncio.AbstractEventLoop + _serial: serial.Serial + _protocol: asyncio.Protocol + def __init__( self, loop: asyncio.AbstractEventLoop, @@ -612,9 +616,11 @@ async def open_serial_connection( if __name__ == "__main__": class Output(asyncio.Protocol): + + _transport: SerialTransport + def __init__(self): super().__init__() - self._transport = None def connection_made(self, transport): self._transport = transport From 26c0df3ba47d1567b2ca0060259bb889b5d30bc5 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Mon, 31 Mar 2025 18:23:13 -0500 Subject: [PATCH 2/6] fix return type hint --- serial_asyncio_fast/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serial_asyncio_fast/__init__.py b/serial_asyncio_fast/__init__.py index c684e73..352a3f2 100644 --- a/serial_asyncio_fast/__init__.py +++ b/serial_asyncio_fast/__init__.py @@ -102,7 +102,7 @@ def serial(self) -> Optional[serial.Serial]: """ return self._serial - def get_extra_info(self, name: str, default: Optional[str] = None) -> Optional[str]: + def get_extra_info(self, name: str, default: Optional[str] = None) -> Optional[serial.Serial]: """Get optional transport information. Currently only "serial" is available. From cd01cd134228cc1e33d8170710ba5b94e49861d2 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Mon, 31 Mar 2025 18:24:02 -0500 Subject: [PATCH 3/6] BaseException should not be caught --- serial_asyncio_fast/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/serial_asyncio_fast/__init__.py b/serial_asyncio_fast/__init__.py index 352a3f2..07115c4 100644 --- a/serial_asyncio_fast/__init__.py +++ b/serial_asyncio_fast/__init__.py @@ -314,7 +314,7 @@ def _write_data(self, data: Union[bytes, bytearray, memoryview]) -> None: except serial.SerialException as exc: self._fatal_error(exc, "Fatal write error on serial transport") return - except BaseException as exc: + except Exception as exc: self._fatal_error(exc, "Unhandled fatal write error on serial transport") return else: @@ -406,7 +406,7 @@ def _set_write_buffer_limits( self._low_water = low def _fatal_error( - self, exc: BaseException, message="Fatal error on serial transport" + self, exc: Exception, message="Fatal error on serial transport" ) -> None: """Report a fatal error to the event-loop and abort the transport.""" self._loop.call_exception_handler( @@ -441,7 +441,7 @@ def _close(self, exc: Optional[Exception] = None) -> None: self._remove_writer() _create_background_task(self._call_connection_lost(exc, flush=True)) - def _abort(self, exc: Optional[BaseException]) -> None: + def _abort(self, exc: Optional[Exception]) -> None: """Close the transport immediately. Pending operations will not be given opportunity to complete, From cc0d51c9da66170b00216f2334ea82e22c476ef0 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Mon, 31 Mar 2025 18:24:37 -0500 Subject: [PATCH 4/6] don't access private attribute, which has never changed anyways --- serial_asyncio_fast/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serial_asyncio_fast/__init__.py b/serial_asyncio_fast/__init__.py index 07115c4..bf2a49d 100644 --- a/serial_asyncio_fast/__init__.py +++ b/serial_asyncio_fast/__init__.py @@ -601,7 +601,7 @@ async def open_serial_connection( if loop is None: loop = asyncio.get_event_loop() if limit is None: - limit = asyncio.streams._DEFAULT_LIMIT + limit = 2 ** 16 # 64 KiB reader = asyncio.StreamReader(limit=limit, loop=loop) protocol = asyncio.StreamReaderProtocol(reader, loop=loop) transport, _ = await create_serial_connection( From 729bed360e89f7df0dcb9de1845281c780ff72a8 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Tue, 1 Apr 2025 07:17:13 -0500 Subject: [PATCH 5/6] Revert "fix return type hint" This reverts commit 26c0df3ba47d1567b2ca0060259bb889b5d30bc5. --- serial_asyncio_fast/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serial_asyncio_fast/__init__.py b/serial_asyncio_fast/__init__.py index bf2a49d..83ad2dd 100644 --- a/serial_asyncio_fast/__init__.py +++ b/serial_asyncio_fast/__init__.py @@ -102,7 +102,7 @@ def serial(self) -> Optional[serial.Serial]: """ return self._serial - def get_extra_info(self, name: str, default: Optional[str] = None) -> Optional[serial.Serial]: + def get_extra_info(self, name: str, default: Optional[str] = None) -> Optional[str]: """Get optional transport information. Currently only "serial" is available. From 83088320ba8ca97fd8acaacb3a7972d80d338ebf Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Tue, 1 Apr 2025 07:17:31 -0500 Subject: [PATCH 6/6] Revert "BaseException should not be caught" This reverts commit cd01cd134228cc1e33d8170710ba5b94e49861d2. --- serial_asyncio_fast/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/serial_asyncio_fast/__init__.py b/serial_asyncio_fast/__init__.py index 83ad2dd..54149e5 100644 --- a/serial_asyncio_fast/__init__.py +++ b/serial_asyncio_fast/__init__.py @@ -314,7 +314,7 @@ def _write_data(self, data: Union[bytes, bytearray, memoryview]) -> None: except serial.SerialException as exc: self._fatal_error(exc, "Fatal write error on serial transport") return - except Exception as exc: + except BaseException as exc: self._fatal_error(exc, "Unhandled fatal write error on serial transport") return else: @@ -406,7 +406,7 @@ def _set_write_buffer_limits( self._low_water = low def _fatal_error( - self, exc: Exception, message="Fatal error on serial transport" + self, exc: BaseException, message="Fatal error on serial transport" ) -> None: """Report a fatal error to the event-loop and abort the transport.""" self._loop.call_exception_handler( @@ -441,7 +441,7 @@ def _close(self, exc: Optional[Exception] = None) -> None: self._remove_writer() _create_background_task(self._call_connection_lost(exc, flush=True)) - def _abort(self, exc: Optional[Exception]) -> None: + def _abort(self, exc: Optional[BaseException]) -> None: """Close the transport immediately. Pending operations will not be given opportunity to complete,